KMBatchOperateAddHeaderFooterViewController.swift 30 KB

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