KMBatchOperateAddHeaderFooterViewController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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")
  45. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  46. } else {
  47. self.actionButton.tag = 0
  48. self.actionButton.title = KMLocalizedString("Cancel")
  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")
  70. self.actionButton.title = KMLocalizedString("Apply")
  71. self.titleLabel.stringValue = KMLocalizedString("Apply")
  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")
  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. if let data = files?.first {
  200. if data.currentOperateType != self.operateType {
  201. return
  202. }
  203. }
  204. let viewC = notification.userInfo?[kObjectKey] as? NSViewController
  205. if let data = self.view.window?.isEqual(to: viewC?.view.window), data == false {
  206. return
  207. }
  208. self.files? = files ?? []
  209. if files?.count ?? 0 > 0 {
  210. haveFiles = true
  211. } else {
  212. haveFiles = false
  213. }
  214. updateActionButtonbackgroundColor()
  215. }
  216. func loadData() {
  217. self.tableView.reloadData()
  218. }
  219. func updateViewColor() {
  220. if KMAppearance.isDarkMode() {
  221. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  222. self.addButton.layer?.backgroundColor = NSColor(red: 0.337, green: 0.345, blue: 0.353, alpha: 1).cgColor
  223. self.addButton.setTitleColor(NSColor.white)
  224. self.actionButton.layer?.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 1).cgColor
  225. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  226. } else {
  227. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
  228. self.addButton.layer?.backgroundColor = NSColor(red: 0.855, green: 0.859, blue: 0.871, alpha: 1).cgColor
  229. self.addButton.setTitleColor(NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1))
  230. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 1).cgColor
  231. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  232. }
  233. }
  234. func deleteAll() {
  235. if self.isBates {
  236. // for waterMark in KMHeaderFooterManager.defaultManager.onlyBatesObjects {
  237. // KMHeaderFooterManager.defaultManager.removeHeaderFooter(waterMark)
  238. // }
  239. } else {
  240. // for waterMark in KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects {
  241. // KMHeaderFooterManager.defaultManager.removeHeaderFooter(waterMark)
  242. // }
  243. }
  244. reloadTable()
  245. loadData()
  246. postNotification()
  247. }
  248. func reloadTable() {
  249. self.tableView.noteNumberOfRowsChanged()
  250. var count = 0
  251. var array: [KMHeaderFooterObject] = []
  252. // if self.isBates {
  253. // count = KMHeaderFooterManager.defaultManager.onlyBatesObjects.count
  254. // array = KMHeaderFooterManager.defaultManager.onlyBatesObjects
  255. // } else {
  256. // count = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count
  257. // array = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects
  258. // }
  259. if array.contains(self.currentObject ?? KMHeaderFooterObject()) {
  260. let row = array.firstIndex(of: self.currentObject!)!
  261. let indexSet = IndexSet(integer: row)
  262. self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  263. }
  264. updateActionButtonbackgroundColor()
  265. }
  266. func postNotification() {
  267. NotificationCenter.default.post(name: NSNotification.Name("KMBatchOperateHeaderFootersNotification"), object: self)
  268. }
  269. @IBAction func buttonClicked_AddHeaderFooter(_ sender: Any) {
  270. let filePath: String = Bundle.main.path(forResource: NSLocalizedString("Quick Start Guide.pdf", comment: ""), ofType: "") ?? ""
  271. var cdocument = self.pdfView?.document
  272. if isBatchOperation {
  273. cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  274. }
  275. let windowController = KMHeaderFooterWindowController(windowNibName: "KMHeaderFooterWindowController")
  276. windowController.isBatch = isBatchOperation
  277. windowController.type = .Add
  278. windowController.isBates = isBates
  279. windowController.pdfDocument = cdocument
  280. windowController.cancelAction = { [weak self] controller in
  281. self?.km_endSheet()
  282. }
  283. windowController.operateCallBack = { [weak self] wController, model in
  284. self?.currentObject = model
  285. self?.loadData()
  286. self?.postNotification()
  287. let set = NSIndexSet(index: 0)
  288. self?.tableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  289. }
  290. self.km_beginSheet(windowC: windowController)
  291. }
  292. @IBAction func buttonClicked_Action(_ sender: NSButton) {
  293. // if (!self.onlyManagerTemplate) {
  294. // if (!self.haveFiles) {
  295. // return
  296. // }
  297. // }
  298. // if (self.onlyManagerTemplate) {
  299. // if (sender.tag == 1) {
  300. // var headFooter: KMHeaderFooterObject?
  301. // let row = self.tableView.selectedRow
  302. // if (row == -1) {
  303. // NSSound.beep()
  304. // return
  305. // } else {
  306. // if (self.isBates) {
  307. // headFooter = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  308. // } else {
  309. // headFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  310. // }
  311. // }
  312. //
  313. // let openPanel = NSOpenPanel()
  314. // openPanel.canChooseFiles = false
  315. // openPanel.canChooseDirectories = true
  316. // openPanel.canCreateDirectories = true
  317. // openPanel.beginSheetModal(for: self.view.window!) { result in
  318. // if (result == .OK) {
  319. // for fileURL in openPanel.urls {
  320. // self.hiddenWindowCloseButtonIfNeeded()
  321. // self.successFilePathURLArray?.removeAll()
  322. // if (self.isBates) {
  323. // let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddBates)
  324. // if file.fileType == .PDF {
  325. // file.addBatesInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headFooter?.pageRangeType.rawValue ?? 0) ?? .All
  326. // file.addBatesInfo.pageRangeString = headFooter?.pageRangeString
  327. // file.addBatesInfo.savePath = fileURL.path
  328. // if file.status == .Waiting {
  329. // let op = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headFooter!)
  330. // op.delegate = self
  331. // self.queue?.addOperation(op)
  332. // }
  333. // }
  334. // } else {
  335. // self.successFilePathURLArray?.removeAll()
  336. // let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddHeaderFooter)
  337. // if file.fileType == .PDF {
  338. // file.addHeaderFooterInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headFooter?.pageRangeType.rawValue ?? 0) ?? .All
  339. // file.addHeaderFooterInfo.pageRangeString = headFooter?.pageRangeString
  340. // file.addHeaderFooterInfo.savePath = fileURL.path
  341. // if file.status == .Waiting {
  342. // let op = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headFooter!)
  343. // op.delegate = self
  344. // self.queue?.addOperation(op)
  345. // }
  346. // }
  347. // }
  348. // if self.queue?.operations.count ?? 0 > 0 {
  349. // self.interfaceStatus = .Processing
  350. // }
  351. // }
  352. // }
  353. // }
  354. // } else if (sender.tag == 0) {
  355. // if let cnt = self.queue?.operations.count, cnt > 0 {
  356. // self.queue?.cancelAllOperations()
  357. // }
  358. // self.interfaceStatus = .PrepareProcess
  359. // }
  360. // } else {
  361. // //点击开始
  362. // if (sender.tag == 1) {
  363. // if (!self.checkAndResetTask()) {
  364. // NSSound.beep()
  365. // return;
  366. // }
  367. // var headFooter: KMHeaderFooterObject?
  368. // let row = self.tableView.selectedRow
  369. // if (row == -1 || self.files?.count ?? 0 < 1) {
  370. // NSSound.beep()
  371. // return;
  372. // } else {
  373. // if (self.isBates) {
  374. // headFooter = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  375. // } else {
  376. // headFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  377. // }
  378. // }
  379. //
  380. // self.choosePathAndBeginOperation(headFooter!)
  381. // } else if (sender.tag == 0) {
  382. // if let cnt = self.queue?.operations.count, cnt > 0 {
  383. // self.queue?.cancelAllOperations()
  384. // }
  385. // self.interfaceStatus = .PrepareProcess
  386. // }
  387. // }
  388. }
  389. func checkAndResetTask() -> Bool {
  390. if files?.count ?? 0 < 1 {
  391. return false
  392. }
  393. for i in 0..<(files?.count ?? 0) {
  394. if let file = files?[i] as? KMBatchOperateFile {
  395. if isBates {
  396. file.addBatesInfo.resetState()
  397. } else {
  398. file.addHeaderFooterInfo.resetState()
  399. }
  400. }
  401. }
  402. return true
  403. }
  404. func choosePathAndBeginOperation(_ obj: Any) {
  405. let openPanel = NSOpenPanel()
  406. openPanel.canChooseFiles = false
  407. openPanel.canChooseDirectories = true
  408. openPanel.canCreateDirectories = true
  409. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  410. if result == NSApplication.ModalResponse.OK {
  411. for fileURL in openPanel.urls {
  412. self.choosePath = fileURL.path
  413. if self.isBates {
  414. self.beginAddBates(obj as! KMHeaderFooterObject)
  415. } else {
  416. self.beginAddHeaderFooter(obj as! KMHeaderFooterObject)
  417. }
  418. }
  419. }
  420. }
  421. }
  422. func beginAddBates(_ bates: KMHeaderFooterObject) {
  423. hiddenWindowCloseButtonIfNeeded()
  424. successFilePathURLArray?.removeAll()
  425. for i in 0..<(files?.count ?? 0) {
  426. if let file = files?[i] as? KMBatchOperateFile {
  427. if file.fileType == .PDF {
  428. file.addBatesInfo.savePath = choosePath
  429. if file.status == .Waiting {
  430. let operation = KMBatchAddHeaderFooterOperation(file: file, headerFooter: bates)
  431. operation.delegate = self
  432. queue?.addOperation(operation)
  433. }
  434. }
  435. }
  436. }
  437. if queue?.operationCount ?? 0 > 0 {
  438. interfaceStatus = .Processing
  439. }
  440. }
  441. func beginAddHeaderFooter(_ headerFooter: KMHeaderFooterObject) {
  442. hiddenWindowCloseButtonIfNeeded()
  443. successFilePathURLArray?.removeAll()
  444. for i in 0..<(files?.count ?? 0) {
  445. if let file = files?[i] as? KMBatchOperateFile {
  446. if file.fileType == .PDF {
  447. file.addHeaderFooterInfo.savePath = choosePath
  448. if file.status == .Waiting {
  449. let operation = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headerFooter)
  450. operation.delegate = self
  451. queue?.addOperation(operation)
  452. }
  453. }
  454. }
  455. }
  456. if queue?.operationCount ?? 0 > 0 {
  457. interfaceStatus = .Processing
  458. }
  459. }
  460. func modify(obj: KMHeaderFooterObject) {
  461. var filePath: String = Bundle.main.path(forResource: NSLocalizedString("Quick Start Guide.pdf", comment: ""), ofType: "") ?? ""
  462. if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
  463. filePath = self.pdfView?.document.documentURL.path ?? ""
  464. }
  465. let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  466. let windowController = KMHeaderFooterWindowController(windowNibName: "KMHeaderFooterWindowController")
  467. windowController.headerFooterObj = obj
  468. windowController.isBatch = isBatchOperation
  469. windowController.type = .Edit
  470. windowController.isBates = isBates
  471. windowController.pdfDocument = cdocument
  472. windowController.cancelAction = { [weak self] controller in
  473. self?.km_endSheet()
  474. }
  475. windowController.operateCallBack = { wController, model in
  476. self.currentObject = model
  477. self.loadData()
  478. self.postNotification()
  479. let set = NSIndexSet(index: 0)
  480. self.tableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  481. }
  482. self.km_beginSheet(windowC: windowController)
  483. }
  484. func delete(obj: KMHeaderFooterObject) {
  485. KMHeaderFooterManager.defaultManager.removeHeaderFooter(obj)
  486. loadData()
  487. reloadTable()
  488. postNotification()
  489. }
  490. func headerFooterInterfaceSelectHeaderFooter(headerFooter: KMHeaderFooterObject) {
  491. self.tableView.reloadData()
  492. // if let index = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.firstIndex(of: headerFooter) {
  493. // let indexSet = IndexSet(integer: index)
  494. // self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  495. // }
  496. }
  497. func batesInterfaceSelectBates(headerFooter: KMHeaderFooterObject) {
  498. self.tableView.reloadData()
  499. // if let index = KMHeaderFooterManager.defaultManager.onlyBatesObjects.firstIndex(of: headerFooter) {
  500. // let indexSet = IndexSet(integer: index)
  501. // self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  502. // }
  503. }
  504. func numberOfRows(in tableView: NSTableView) -> Int {
  505. var count = 0
  506. // if self.isBates {
  507. // count = KMHeaderFooterManager.defaultManager.onlyBatesObjects.count
  508. // } else {
  509. // count = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count
  510. // }
  511. self.blankView.isHidden = count != 0
  512. return count
  513. }
  514. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  515. var obj: KMHeaderFooterObject? = nil
  516. // if self.isBates {
  517. // obj = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  518. // } else {
  519. // obj = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  520. // }
  521. guard let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "mainCell"), owner: self) as? KMHeaderFooterTableCellView else {
  522. return nil
  523. }
  524. cellView.updateInterface(obj!)
  525. cellView.headerFooterTableCellViewCallback = { type in
  526. if type == .Edit {
  527. let indexSet = IndexSet(integer: row)
  528. self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  529. self.currentObject = obj
  530. self.modify(obj: obj!)
  531. } else {
  532. self.delete(obj: obj!)
  533. }
  534. }
  535. return cellView
  536. }
  537. func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
  538. let rowView = KMTableRowView()
  539. return rowView
  540. }
  541. // func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  542. // if self.isBates {
  543. // return KMHeaderFooterManager.defaultManager.onlyBatesObjects[row].cellHeight
  544. // } else {
  545. // return KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row].cellHeight
  546. // }
  547. // }
  548. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  549. return true
  550. }
  551. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  552. return true
  553. }
  554. func tableViewSelectionDidChange(_ notification: Notification) {
  555. // let row = self.tableView.selectedRow
  556. // updateActionButtonbackgroundColor()
  557. // if row == -1 { return }
  558. // if self.isBates {
  559. // let bates = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  560. // self.currentObject = bates
  561. // for i in 0..<(self.files?.count ?? 0) {
  562. // let file = self.files?[i]
  563. // file?.addBatesInfo.pageChoice = KMBatchOperatePageChoice(rawValue: bates.pageRangeType.rawValue) ?? .All
  564. // if file?.addBatesInfo.pageChoice == .Input {
  565. // let arr = allPageNumbers(bates.pageRangeString)
  566. // let sortedArray: NSArray = file!.pagesArrayIntersect(with: arr) as NSArray
  567. // if sortedArray.count < 1 {
  568. // file?.addBatesInfo.pageChoice = .All
  569. // } else {
  570. // file!.addBatesInfo.pageRangeString = sortedArray.componentsJoined(by: ",")
  571. // }
  572. // }
  573. // }
  574. // } else {
  575. // let headerFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  576. // self.currentObject = headerFooter
  577. // for i in 0..<(self.files?.count ?? 0) {
  578. // let file = self.files?[i]
  579. // file?.addHeaderFooterInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headerFooter.pageRangeType.rawValue) ?? .All
  580. // if file?.addHeaderFooterInfo.pageChoice == .Input {
  581. // let arr = allPageNumbers(headerFooter.pageRangeString)
  582. // let sortedArray: NSArray = file!.pagesArrayIntersect(with: arr) as NSArray
  583. // if sortedArray.count < 1 {
  584. // file?.addHeaderFooterInfo.pageChoice = .All
  585. // } else {
  586. // file!.addHeaderFooterInfo.pageRangeString = sortedArray.componentsJoined(by: ",")
  587. // }
  588. // }
  589. // }
  590. // }
  591. reloadTable()
  592. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNeedChangePageRangeNotification), object: nil)
  593. }
  594. }
  595. extension KMBatchOperateAddHeaderFooterViewController: NSMenuDelegate, NSMenuItemValidation{
  596. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  597. let action = menuItem.action
  598. if action == #selector(buttonItemClick_CleanAll(_:)) {
  599. // if (self.isBates && KMHeaderFooterManager.defaultManager.onlyBatesObjects.count < 1) || (!self.isBates && KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count < 1){
  600. // return false
  601. // }
  602. return true
  603. }
  604. return true
  605. }
  606. }