KMBatchOperateAddHeaderFooterViewController.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. //
  2. // KMBatchOperateAddHeaderFooterViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/11/7.
  6. //
  7. import Cocoa
  8. class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewController, NSTableViewDelegate,NSTableViewDataSource{
  9. var isBates = false
  10. var onlyManagerTemplate = false
  11. var isBatchOperation = false
  12. var pdfView: CPDFView?
  13. @IBOutlet var topBaseView: NSView!
  14. @IBOutlet var bottomBaseView: NSView!
  15. @IBOutlet var tableView: NSTableView!
  16. @IBOutlet var titleLabel: NSTextField!
  17. @IBOutlet var actionButton: NSButton!
  18. @IBOutlet var managerTemplateTitleLabel: NSTextField!
  19. @IBOutlet var addButton: NSButton!
  20. @IBOutlet var blankView: KMBlankView!
  21. @IBOutlet var managerTemplateTopConstraint: NSLayoutConstraint!
  22. @IBOutlet var managerTemplateButtonHeightConstraint: NSButton!
  23. @IBOutlet var topHeightConstraint: NSLayoutConstraint!
  24. @IBOutlet var lineView: NSView!
  25. var haveFiles = false
  26. var currentObject: KMHeaderFooterObject?
  27. override var interfaceStatus: KMBatchOperateInterfaceStatus?{
  28. set{
  29. super.interfaceStatus = newValue
  30. if newValue == .PrepareProcess {
  31. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  32. let files = NSMutableArray()
  33. for url in self.successFilePathURLArray! {
  34. if FileManager.default.fileExists(atPath: url.path) {
  35. files.add(url)
  36. }
  37. }
  38. if files.count > 0 {
  39. let workspace = NSWorkspace.shared
  40. workspace.activateFileViewerSelecting(files as! [URL])
  41. }
  42. }
  43. self.actionButton.tag = 1
  44. self.actionButton.title = KMLocalizedString("Apply", nil)
  45. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  46. } else {
  47. self.actionButton.tag = 0
  48. self.actionButton.title = KMLocalizedString("Cancel", nil)
  49. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  50. }
  51. }
  52. get{
  53. return super.interfaceStatus
  54. }
  55. }
  56. deinit {
  57. NotificationCenter.default.removeObserver(self)
  58. }
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. localizedLanguage()
  62. configuInterface()
  63. NotificationCenter.default.addObserver(self, selector: #selector(headerFootersNotification(notification:)), name: Notification.Name(rawValue: "KMBatchOperateHeaderFootersNotification"), object: nil)
  64. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: Notification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  65. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  66. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: NSNotification.Name("KMBatchFilesCountNotification"), object: nil)
  67. }
  68. func localizedLanguage() {
  69. self.addButton.title = " " + KMLocalizedString("Add Template", nil)
  70. self.actionButton.title = KMLocalizedString("Apply", nil)
  71. self.titleLabel.stringValue = KMLocalizedString("Apply", nil)
  72. }
  73. func configuInterface() {
  74. self.titleLabel.font = NSFont.systemFont(ofSize: 14)
  75. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  76. self.tableView.enclosingScrollView?.borderType = .noBorder
  77. self.actionButton.wantsLayer = true
  78. self.addButton.wantsLayer = true
  79. self.topHeightConstraint.constant = 0
  80. var color = NSColor(red: 227.0/255.0, green: 228.0/255.0, blue: 230.0/255.0, alpha: 1)
  81. if KMAppearance.isSupportNewColor() {
  82. if KMAppearance.isDarkMode() {
  83. color = NSColor(red: 59.0/255.0, green: 61/255.0, blue: 64.0/255.0, alpha: 1)
  84. }
  85. }
  86. self.lineView.wantsLayer = true
  87. self.lineView.layer?.backgroundColor = color.cgColor
  88. self.addButton.imagePosition = .imageLeft
  89. self.addButton.image = NSImage(named: "KMImageNameHeaderFooterAddBtn")
  90. self.addButton.layer?.backgroundColor = KMAppearance.Interactive.s0Color().cgColor
  91. self.addButton.setTitleColor(KMAppearance.Layout.h0Color())
  92. if (onlyManagerTemplate){
  93. managerTemplateTitleLabel.isHidden = false
  94. titleLabel.isHidden = true
  95. self.topBaseView.isHidden = false
  96. topHeightConstraint.constant = 40
  97. }else{
  98. managerTemplateTitleLabel.isHidden = true
  99. titleLabel.isHidden = true
  100. self.topBaseView.isHidden = true
  101. topHeightConstraint.constant = 0
  102. }
  103. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  104. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  105. self.actionButton.imagePosition = .noImage
  106. if !self.onlyManagerTemplate {
  107. if self.files?.count ?? 0 > 0 {
  108. self.haveFiles = true
  109. } else {
  110. self.haveFiles = false
  111. }
  112. } else {
  113. self.haveFiles = true
  114. }
  115. updateViewColor()
  116. updateActionButtonbackgroundColor()
  117. self.addButton.layer?.cornerRadius = 1.0
  118. self.actionButton.layer?.cornerRadius = 1.0
  119. self.topBaseView.wantsLayer = true
  120. self.topBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  121. self.bottomBaseView.wantsLayer = true
  122. self.bottomBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  123. self.managerTemplateTitleLabel.font = NSFont.systemFont(ofSize: 14)
  124. self.managerTemplateTitleLabel.textColor = KMAppearance.Layout.h0Color()
  125. self.addButton.isHidden = false
  126. self.managerTemplateTitleLabel.stringValue = NSLocalizedString("Manage Templates", comment: "")
  127. self.view.addSubview(self.blankView)
  128. self.blankView.mas_makeConstraints { make in
  129. make?.top.equalTo()(self.topBaseView.mas_bottom)
  130. make?.left.equalTo()(self.view)
  131. make?.right.equalTo()(self.view)
  132. make?.bottom.equalTo()(self.bottomBaseView.mas_top)
  133. make?.height.greaterThanOrEqualTo()(200)
  134. }
  135. self.blankView.titleLabel.stringValue = KMLocalizedString("No Templates", nil)
  136. self.tableView.backgroundColor = KMAppearance.Layout.l0Color()
  137. let menu = NSMenu(title: "")
  138. if !self.isBatchOperation && !self.isBates {
  139. menu.addItem(withTitle: NSLocalizedString("Batch Add Header & Footer", comment: ""), action: #selector(buttonItemClick_addBatch(_:)), keyEquivalent: "")
  140. } else if !self.isBatchOperation && self.isBates {
  141. menu.addItem(withTitle: NSLocalizedString("Batch Add Bates Numbers", comment: ""), action: #selector(buttonItemClick_addBatch(_:)), keyEquivalent: "")
  142. }
  143. if !self.isBates {
  144. menu.addItem(withTitle: NSLocalizedString("Remove All Header & Footer Templates", comment: ""), action: #selector(buttonItemClick_CleanAll(_:)), keyEquivalent: "")
  145. } else {
  146. menu.addItem(withTitle: NSLocalizedString("Remove All Bates Numbers Templates", comment: ""), action: #selector(buttonItemClick_CleanAll(_:)), keyEquivalent: "")
  147. }
  148. self.view.menu = menu
  149. }
  150. func updateActionButtonbackgroundColor() {
  151. let row = self.tableView.selectedRow
  152. if (self.files?.count ?? 0 > 0 || self.pdfView != nil) && row > -1 {
  153. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  154. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  155. } else {
  156. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  157. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  158. }
  159. }
  160. @objc func buttonItemClick_addBatch(_ sender: Any) {
  161. let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
  162. if #available(macOS 10.13, *) {
  163. baseWindowController.window?.makeKeyAndOrderFront(nil)
  164. } else {
  165. baseWindowController.showWindow(nil)
  166. }
  167. let arr = NSMutableArray()
  168. let file = KMBatchOperateFile(filePath: self.pdfView!.document!.documentURL!.path, type: self.isBates ? .AddBates : .AddHeaderFooter)
  169. arr.add(file)
  170. baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: self.isBates ? .AddBates : .AddHeaderFooter, files: arr as! [KMBatchOperateFile])
  171. }
  172. @objc func buttonItemClick_CleanAll(_ sender: Any) {
  173. let alert = NSAlert()
  174. alert.alertStyle = .warning
  175. alert.messageText = ""
  176. alert.informativeText = NSLocalizedString("Are you sure to delete all templates?", comment: "")
  177. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  178. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  179. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  180. if response == .alertFirstButtonReturn {
  181. self.deleteAll()
  182. }
  183. }
  184. }
  185. @objc func headerFootersNotification(notification: Notification) {
  186. if let addHeaderFooter = notification.object as? KMBatchOperateAddHeaderFooterViewController {
  187. if self != addHeaderFooter {
  188. loadData()
  189. }
  190. }
  191. }
  192. @objc func themeChanged(notification: Notification) {
  193. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  194. self.updateViewColor()
  195. }
  196. }
  197. @objc func batchFilesCountNotification(notification: Notification) {
  198. let files: Array? = notification.object as? [KMBatchOperateFile]
  199. self.files? = files ?? []
  200. if files?.count ?? 0 > 0 {
  201. haveFiles = true
  202. } else {
  203. haveFiles = false
  204. }
  205. updateActionButtonbackgroundColor()
  206. }
  207. func loadData() {
  208. self.tableView.reloadData()
  209. }
  210. func updateViewColor() {
  211. if KMAppearance.isDarkMode() {
  212. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  213. self.addButton.layer?.backgroundColor = NSColor(red: 0.337, green: 0.345, blue: 0.353, alpha: 1).cgColor
  214. self.addButton.setTitleColor(NSColor.white)
  215. self.actionButton.layer?.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 1).cgColor
  216. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  217. } else {
  218. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
  219. self.addButton.layer?.backgroundColor = NSColor(red: 0.855, green: 0.859, blue: 0.871, alpha: 1).cgColor
  220. self.addButton.setTitleColor(NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1))
  221. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 1).cgColor
  222. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  223. }
  224. }
  225. func deleteAll() {
  226. if self.isBates {
  227. for waterMark in KMHeaderFooterManager.defaultManager.onlyBatesObjects {
  228. KMHeaderFooterManager.defaultManager.removeHeaderFooter(waterMark)
  229. }
  230. } else {
  231. for waterMark in KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects {
  232. KMHeaderFooterManager.defaultManager.removeHeaderFooter(waterMark)
  233. }
  234. }
  235. reloadTable()
  236. loadData()
  237. postNotification()
  238. }
  239. func reloadTable() {
  240. self.tableView.noteNumberOfRowsChanged()
  241. var count = 0
  242. var array: [KMHeaderFooterObject] = []
  243. if self.isBates {
  244. count = KMHeaderFooterManager.defaultManager.onlyBatesObjects.count
  245. array = KMHeaderFooterManager.defaultManager.onlyBatesObjects
  246. } else {
  247. count = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count
  248. array = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects
  249. }
  250. if array.contains(self.currentObject ?? KMHeaderFooterObject()) {
  251. let row = array.firstIndex(of: self.currentObject!)!
  252. let indexSet = IndexSet(integer: row)
  253. self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  254. }
  255. updateActionButtonbackgroundColor()
  256. }
  257. func postNotification() {
  258. NotificationCenter.default.post(name: NSNotification.Name("KMBatchOperateHeaderFootersNotification"), object: self)
  259. }
  260. @IBAction func buttonClicked_AddHeaderFooter(_ sender: Any) {
  261. // guard let windowController = self.view.window?.windowController else { return }
  262. // var filePath: String?
  263. // var password: String?
  264. // if let mainWC = windowController as? MainWindowController {
  265. // // filePath = mainWC.pdfView?.document?.documentURL?.path
  266. // // password = (mainWC.pdfView?.document as? CPDFDocument)?.password
  267. // }
  268. // let wc = KMHeaderFooterManagerWindowController(baseFile: filePath, headerFooter: nil, password: password, type: .KMBatchModifyTemplateType_Add)
  269. // wc.isBates = self.isBates
  270. // wc.operateCallBack = { [weak self] obj in
  271. // self?.currentObject = obj
  272. // self?.loadData()
  273. // self?.postNotification()
  274. // DispatchQueue.main.async {
  275. // guard let strongSelf = self else {
  276. // return
  277. // }
  278. // strongSelf.tableView.selectRowIndexes(IndexSet(integer: 0), byExtendingSelection: false)
  279. // }
  280. // }
  281. // wc.beginSheetModal(for: NSApp.keyWindow!, completionHandler: nil)
  282. let filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  283. var cdocument = self.pdfView?.document
  284. if isBatchOperation {
  285. cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  286. }
  287. let windowController = KMHeaderFooterWindowController(windowNibName: "KMHeaderFooterWindowController")
  288. windowController.isBatch = isBatchOperation
  289. windowController.type = .Add
  290. windowController.isBates = isBates
  291. windowController.pdfDocument = cdocument
  292. windowController.cancelAction = { [weak self] controller in
  293. self?.km_endSheet()
  294. }
  295. windowController.operateCallBack = { [weak self] wController, model in
  296. self?.currentObject = model
  297. self?.loadData()
  298. self?.postNotification()
  299. let set = NSIndexSet(index: 0)
  300. self?.tableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  301. }
  302. self.km_beginSheet(windowC: windowController)
  303. }
  304. @IBAction func buttonClicked_Action(_ sender: NSButton) {
  305. if (!self.onlyManagerTemplate) {
  306. if (!self.haveFiles) {
  307. return
  308. }
  309. }
  310. if (self.onlyManagerTemplate) {
  311. if (sender.tag == 1) {
  312. var headFooter: KMHeaderFooterObject?
  313. let row = self.tableView.selectedRow
  314. if (row == -1) {
  315. NSSound.beep()
  316. return
  317. } else {
  318. if (self.isBates) {
  319. headFooter = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  320. } else {
  321. headFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  322. }
  323. }
  324. let openPanel = NSOpenPanel()
  325. openPanel.canChooseFiles = false
  326. openPanel.canChooseDirectories = true
  327. openPanel.canCreateDirectories = true
  328. openPanel.beginSheetModal(for: self.view.window!) { result in
  329. if (result == .OK) {
  330. for fileURL in openPanel.urls {
  331. self.hiddenWindowCloseButtonIfNeeded()
  332. self.successFilePathURLArray?.removeAll()
  333. if (self.isBates) {
  334. let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddBates)
  335. if file.fileType == .PDF {
  336. file.addBatesInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headFooter?.pageRangeType.rawValue ?? 0) ?? .All
  337. file.addBatesInfo.pageRangeString = headFooter?.pageRangeString
  338. file.addBatesInfo.savePath = fileURL.path
  339. if file.status == .Waiting {
  340. let op = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headFooter!)
  341. op.delegate = self
  342. self.queue?.addOperation(op)
  343. }
  344. }
  345. } else {
  346. self.successFilePathURLArray?.removeAll()
  347. let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddHeaderFooter)
  348. if file.fileType == .PDF {
  349. file.addHeaderFooterInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headFooter?.pageRangeType.rawValue ?? 0) ?? .All
  350. file.addHeaderFooterInfo.pageRangeString = headFooter?.pageRangeString
  351. file.addHeaderFooterInfo.savePath = fileURL.path
  352. if file.status == .Waiting {
  353. let op = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headFooter!)
  354. op.delegate = self
  355. self.queue?.addOperation(op)
  356. }
  357. }
  358. }
  359. if self.queue?.operations.count ?? 0 > 0 {
  360. self.interfaceStatus = .Processing
  361. }
  362. }
  363. }
  364. }
  365. } else if (sender.tag == 0) {
  366. if let cnt = self.queue?.operations.count, cnt > 0 {
  367. self.queue?.cancelAllOperations()
  368. }
  369. self.interfaceStatus = .PrepareProcess
  370. }
  371. } else {
  372. //点击开始
  373. if (sender.tag == 1) {
  374. if (!self.checkAndResetTask()) {
  375. NSSound.beep()
  376. return;
  377. }
  378. var headFooter: KMHeaderFooterObject?
  379. let row = self.tableView.selectedRow
  380. if (row == -1 || self.files?.count ?? 0 < 1) {
  381. NSSound.beep()
  382. return;
  383. } else {
  384. if (self.isBates) {
  385. headFooter = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  386. } else {
  387. headFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  388. }
  389. }
  390. self.choosePathAndBeginOperation(headFooter!)
  391. } else if (sender.tag == 0) {
  392. if let cnt = self.queue?.operations.count, cnt > 0 {
  393. self.queue?.cancelAllOperations()
  394. }
  395. self.interfaceStatus = .PrepareProcess
  396. }
  397. }
  398. }
  399. func checkAndResetTask() -> Bool {
  400. if files?.count ?? 0 < 1 {
  401. return false
  402. }
  403. for i in 0..<(files?.count ?? 0) {
  404. if let file = files?[i] as? KMBatchOperateFile {
  405. if isBates {
  406. file.addBatesInfo.resetState()
  407. } else {
  408. file.addHeaderFooterInfo.resetState()
  409. }
  410. }
  411. }
  412. return true
  413. }
  414. func choosePathAndBeginOperation(_ obj: Any) {
  415. let openPanel = NSOpenPanel()
  416. openPanel.canChooseFiles = false
  417. openPanel.canChooseDirectories = true
  418. openPanel.canCreateDirectories = true
  419. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  420. if result == NSApplication.ModalResponse.OK {
  421. for fileURL in openPanel.urls {
  422. self.choosePath = fileURL.path
  423. if self.isBates {
  424. self.beginAddBates(obj as! KMHeaderFooterObject)
  425. } else {
  426. self.beginAddHeaderFooter(obj as! KMHeaderFooterObject)
  427. }
  428. }
  429. }
  430. }
  431. }
  432. func beginAddBates(_ bates: KMHeaderFooterObject) {
  433. hiddenWindowCloseButtonIfNeeded()
  434. successFilePathURLArray?.removeAll()
  435. for i in 0..<(files?.count ?? 0) {
  436. if let file = files?[i] as? KMBatchOperateFile {
  437. if file.fileType == .PDF {
  438. file.addBatesInfo.savePath = choosePath
  439. if file.status == .Waiting {
  440. let operation = KMBatchAddHeaderFooterOperation(file: file, headerFooter: bates)
  441. operation.delegate = self
  442. queue?.addOperation(operation)
  443. }
  444. }
  445. }
  446. }
  447. if queue?.operationCount ?? 0 > 0 {
  448. interfaceStatus = .Processing
  449. }
  450. }
  451. func beginAddHeaderFooter(_ headerFooter: KMHeaderFooterObject) {
  452. hiddenWindowCloseButtonIfNeeded()
  453. successFilePathURLArray?.removeAll()
  454. for i in 0..<(files?.count ?? 0) {
  455. if let file = files?[i] as? KMBatchOperateFile {
  456. if file.fileType == .PDF {
  457. file.addHeaderFooterInfo.savePath = choosePath
  458. if file.status == .Waiting {
  459. let operation = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headerFooter)
  460. operation.delegate = self
  461. queue?.addOperation(operation)
  462. }
  463. }
  464. }
  465. }
  466. if queue?.operationCount ?? 0 > 0 {
  467. interfaceStatus = .Processing
  468. }
  469. }
  470. func modify(obj: KMHeaderFooterObject) {
  471. // let windowController = self.view.window?.windowController
  472. // var filePath: String? = nil
  473. // var password: String? = nil
  474. // if let mainWindowController = windowController as? MainWindowController {
  475. // filePath = mainWindowController.mainViewController.listView?.document?.documentURL?.path
  476. // let document = mainWindowController.mainViewController.listView?.document as? CPDFDocument
  477. // password = document?.password
  478. // }
  479. // let wc = KMHeaderFooterManagerWindowController(baseFile: filePath, headerFooter: obj, password: password, type: .KMBatchModifyTemplateType_Edit)
  480. // wc?.isBates = self.isBates
  481. // let blockSelf = self
  482. // wc?.operateCallBack = { obj in
  483. // blockSelf.currentObject = obj
  484. // blockSelf.loadData()
  485. // blockSelf.reloadTable()
  486. // blockSelf.postNotification()
  487. // }
  488. // wc?.beginSheetModalForWindow(NSApp.keyWindow, completionHandler: nil)
  489. var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  490. if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
  491. filePath = self.pdfView?.document.documentURL.path ?? ""
  492. }
  493. let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  494. let windowController = KMHeaderFooterWindowController(windowNibName: "KMHeaderFooterWindowController")
  495. windowController.headerFooterObj = obj
  496. windowController.isBatch = isBatchOperation
  497. windowController.type = .Edit
  498. windowController.isBates = isBates
  499. windowController.pdfDocument = cdocument
  500. windowController.cancelAction = { [weak self] controller in
  501. self?.km_endSheet()
  502. }
  503. windowController.operateCallBack = { wController, model in
  504. self.currentObject = model
  505. self.loadData()
  506. self.postNotification()
  507. let set = NSIndexSet(index: 0)
  508. self.tableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  509. }
  510. self.km_beginSheet(windowC: windowController)
  511. }
  512. func delete(obj: KMHeaderFooterObject) {
  513. KMHeaderFooterManager.defaultManager.removeHeaderFooter(obj)
  514. loadData()
  515. reloadTable()
  516. postNotification()
  517. }
  518. func headerFooterInterfaceSelectHeaderFooter(headerFooter: KMHeaderFooterObject) {
  519. self.tableView.reloadData()
  520. if let index = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.firstIndex(of: headerFooter) {
  521. let indexSet = IndexSet(integer: index)
  522. self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  523. }
  524. }
  525. func batesInterfaceSelectBates(headerFooter: KMHeaderFooterObject) {
  526. self.tableView.reloadData()
  527. if let index = KMHeaderFooterManager.defaultManager.onlyBatesObjects.firstIndex(of: headerFooter) {
  528. let indexSet = IndexSet(integer: index)
  529. self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  530. }
  531. }
  532. func numberOfRows(in tableView: NSTableView) -> Int {
  533. var count = 0
  534. if self.isBates {
  535. count = KMHeaderFooterManager.defaultManager.onlyBatesObjects.count
  536. } else {
  537. count = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count
  538. }
  539. self.blankView.isHidden = count != 0
  540. return count
  541. }
  542. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  543. var obj: KMHeaderFooterObject? = nil
  544. if self.isBates {
  545. obj = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  546. } else {
  547. obj = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  548. }
  549. guard let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "mainCell"), owner: self) as? KMHeaderFooterTableCellView else {
  550. return nil
  551. }
  552. cellView.updateInterface(obj!)
  553. cellView.headerFooterTableCellViewCallback = { type in
  554. if type == .Edit {
  555. let indexSet = IndexSet(integer: row)
  556. self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  557. self.currentObject = obj
  558. self.modify(obj: obj!)
  559. } else {
  560. self.delete(obj: obj!)
  561. }
  562. }
  563. return cellView
  564. }
  565. func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
  566. let rowView = KMTableRowView()
  567. return rowView
  568. }
  569. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  570. if self.isBates {
  571. return KMHeaderFooterManager.defaultManager.onlyBatesObjects[row].cellHeight
  572. } else {
  573. return KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row].cellHeight
  574. }
  575. }
  576. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  577. return true
  578. }
  579. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  580. return true
  581. }
  582. func tableViewSelectionDidChange(_ notification: Notification) {
  583. let row = self.tableView.selectedRow
  584. updateActionButtonbackgroundColor()
  585. if row == -1 { return }
  586. if self.isBates {
  587. let bates = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  588. self.currentObject = bates
  589. for i in 0..<(self.files?.count ?? 0) {
  590. let file = self.files?[i]
  591. file?.addBatesInfo.pageChoice = KMBatchOperatePageChoice(rawValue: bates.pageRangeType.rawValue) ?? .All
  592. if file?.addBatesInfo.pageChoice == .Input {
  593. let arr = allPageNumbers(bates.pageRangeString)
  594. let sortedArray: NSArray = file!.pagesArrayIntersect(with: arr) as NSArray
  595. if sortedArray.count < 1 {
  596. file?.addBatesInfo.pageChoice = .All
  597. } else {
  598. file!.addBatesInfo.pageRangeString = sortedArray.componentsJoined(by: ",")
  599. }
  600. }
  601. }
  602. } else {
  603. let headerFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  604. self.currentObject = headerFooter
  605. for i in 0..<(self.files?.count ?? 0) {
  606. let file = self.files?[i]
  607. file?.addHeaderFooterInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headerFooter.pageRangeType.rawValue) ?? .All
  608. if file?.addHeaderFooterInfo.pageChoice == .Input {
  609. let arr = allPageNumbers(headerFooter.pageRangeString)
  610. let sortedArray: NSArray = file!.pagesArrayIntersect(with: arr) as NSArray
  611. if sortedArray.count < 1 {
  612. file?.addHeaderFooterInfo.pageChoice = .All
  613. } else {
  614. file!.addHeaderFooterInfo.pageRangeString = sortedArray.componentsJoined(by: ",")
  615. }
  616. }
  617. }
  618. }
  619. reloadTable()
  620. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNeedChangePageRangeNotification), object: nil)
  621. }
  622. }
  623. extension KMBatchOperateAddHeaderFooterViewController: NSMenuDelegate, NSMenuItemValidation{
  624. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  625. let action = menuItem.action
  626. if action == #selector(buttonItemClick_CleanAll(_:)) {
  627. if (self.isBates && KMHeaderFooterManager.defaultManager.onlyBatesObjects.count < 1) || (!self.isBates && KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count < 1){
  628. return false
  629. }
  630. return true
  631. }
  632. return true
  633. }
  634. }