PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/jars/MacOS/macos/Delegate.java

#
Java | 559 lines | 414 code | 70 blank | 75 comment | 62 complexity | f528b4eee8fe6de0e43b09008a80cf99 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * :tabSize=8:indentSize=8:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Delegate.java - A delegate for NSApplication
  6. * Copyright (C) 2003 Kris Kopicki
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package macos;
  23. //{{{ Imports
  24. import com.apple.eawt.*;
  25. import com.apple.eio.*;
  26. import com.apple.cocoa.application.*;
  27. import com.apple.cocoa.foundation.*;
  28. import java.util.*;
  29. import java.io.File;
  30. import javax.swing.*;
  31. import org.gjt.sp.jedit.*;
  32. import org.gjt.sp.jedit.browser.*;
  33. import org.gjt.sp.jedit.gui.*;
  34. import org.gjt.sp.jedit.msg.*;
  35. import org.gjt.sp.jedit.options.GlobalOptions;
  36. import org.gjt.sp.util.Log;
  37. //}}}
  38. public class Delegate extends ApplicationAdapter
  39. {
  40. //{{{ Variables
  41. private final NSSelector actionSel = new NSSelector("doAction", new Class[] {});
  42. private List filenames = new LinkedList();
  43. //}}}
  44. //{{{ Constructor
  45. public Delegate()
  46. {
  47. if (jEdit.getBooleanProperty("MacOSPlugin.useScreenMenuBar",
  48. jEdit.getBooleanProperty("MacOSPlugin.default.useScreenMenuBar"))
  49. )
  50. System.setProperty("apple.laf.useScreenMenuBar","true");
  51. else
  52. System.setProperty("apple.laf.useScreenMenuBar","false");
  53. } //}}}
  54. //{{{ Handlers
  55. //{{{ handleAbout() method
  56. public void handleAbout(ApplicationEvent event)
  57. {
  58. event.setHandled(true);
  59. new AboutDialog(jEdit.getActiveView());
  60. } //}}}
  61. //{{{ handleFileCodes() method
  62. public void handleFileCodes(BufferUpdate msg)
  63. {
  64. Buffer buffer = msg.getBuffer();
  65. // Set type/creator on save
  66. if (!buffer.isDirty() && msg.getWhat() == BufferUpdate.DIRTY_CHANGED)
  67. {
  68. try {
  69. FileManager.setFileTypeAndCreator(buffer.getPath(),
  70. buffer.getIntegerProperty("MacOSPlugin.type",
  71. jEdit.getIntegerProperty("MacOSPlugin.default.type",0)),
  72. buffer.getIntegerProperty("MacOSPlugin.creator",
  73. jEdit.getIntegerProperty("MacOSPlugin.default.creator",0)));
  74. } catch (Exception e) {
  75. // Fail silently, since we may be using UFS
  76. }
  77. }
  78. // Add type/creator to local buffer property list on open
  79. else if (msg.getWhat() == BufferUpdate.CREATED)
  80. {
  81. if ("true".equals(
  82. jEdit.getProperty("MacOSPlugin.preserveCodes")))
  83. {
  84. try {
  85. int type = FileManager.getFileType(buffer.getPath());
  86. int creator = FileManager.getFileCreator(buffer.getPath());
  87. if (type != 0)
  88. buffer.setIntegerProperty("MacOSPlugin.type",type);
  89. if (creator != 0)
  90. buffer.setIntegerProperty("MacOSPlugin.creator",creator);
  91. } catch (Exception e) {
  92. // This will happen when a new file is created
  93. }
  94. }
  95. }
  96. } //}}}
  97. //{{{ handleOpenFile() method
  98. public void handleOpenFile(ApplicationEvent event)
  99. {
  100. filenames.add(event.getFilename());
  101. event.setHandled(true);
  102. } //}}}
  103. //{{{ handleOpenFile() method
  104. public void handleOpenFile(ViewUpdate msg)
  105. {
  106. if(msg.getWhat() == ViewUpdate.CREATED)
  107. {
  108. Iterator i = filenames.iterator();
  109. while (i.hasNext())
  110. jEdit.openFile(msg.getView(),(String)i.next());
  111. MacOSPlugin.started = true;
  112. NSApplication app = NSApplication.sharedApplication();
  113. app.setServicesProvider(new Delegate());
  114. }
  115. } //}}}
  116. //{{{ handlePreferences() method
  117. public void handlePreferences(ApplicationEvent event)
  118. {
  119. event.setHandled(true);
  120. new GlobalOptions(jEdit.getActiveView());
  121. } //}}}
  122. //{{{ handleQuit() method
  123. /**
  124. * This never seems to be called when used with a delegate
  125. */
  126. //public void handleQuit(ApplicationEvent event)
  127. //{
  128. // event.setHandled(false);
  129. // jEdit.exit(jEdit.getActiveView(),true);
  130. //} //}}}
  131. //}}}
  132. //{{{ Delegate methods
  133. //{{{ applicationDockMenu() method
  134. public NSMenu applicationDockMenu(NSApplication sender)
  135. {
  136. NSMenu dockMenu;
  137. BufferMenu bufMenu;
  138. MacrosMenu macMenu;
  139. RecentMenu recMenu;
  140. RecentDirMenu dirMenu;
  141. NSMenuItem showCurrItem;
  142. NSMenuItem showCurrDirItem;
  143. NSMenuItem newViewItem;
  144. // Buffers
  145. NSMenuItem miBuff = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.buffers.label"),null,"");
  146. miBuff.setSubmenu(bufMenu = new BufferMenu());
  147. // Recent Buffers
  148. NSMenuItem miRec = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recent.label"),null,"");
  149. miRec.setSubmenu(recMenu = new RecentMenu());
  150. // Recent Directories
  151. NSMenuItem miDir = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recentDir.label"),null,"");
  152. miDir.setSubmenu(dirMenu = new RecentDirMenu());
  153. // Macros
  154. NSMenuItem miMac = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.macros.label"),null,"");
  155. miMac.setSubmenu(macMenu = new MacrosMenu());
  156. dockMenu = new NSMenu();
  157. newViewItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.newView"),actionSel,"");
  158. newViewItem.setTarget(new NewViewAction());
  159. dockMenu.addItem(newViewItem);
  160. dockMenu.addItem(new NSMenuItem().separatorItem());
  161. showCurrItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.showCurrent"),actionSel,"");
  162. dockMenu.addItem(showCurrItem);
  163. showCurrDirItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.showCurrentDir"),actionSel,"");
  164. dockMenu.addItem(showCurrDirItem);
  165. dockMenu.addItem(new NSMenuItem().separatorItem());
  166. dockMenu.addItem(miBuff);
  167. dockMenu.addItem(miRec);
  168. dockMenu.addItem(miDir);
  169. //dockMenu.addItem(new NSMenuItem().separatorItem());
  170. //dockMenu.addItem(miMac);
  171. if (jEdit.getViewCount() == 0)
  172. miMac.setEnabled(false);
  173. bufMenu.updateMenu();
  174. recMenu.updateMenu();
  175. dirMenu.updateMenu();
  176. macMenu.updateMenu();
  177. View view = jEdit.getActiveView();
  178. if (view != null)
  179. {
  180. File buff = new File(view.getBuffer().getPath());
  181. if (buff.exists())
  182. {
  183. showCurrItem.setTarget(new ShowFileAction(buff.getPath()));
  184. showCurrDirItem.setTarget(new ShowFileAction(buff.getParent()));
  185. }
  186. }
  187. else
  188. {
  189. showCurrItem.setEnabled(false);
  190. showCurrDirItem.setEnabled(false);
  191. }
  192. return dockMenu;
  193. } //}}}
  194. //{{{ applicationOpenFiles() method
  195. public void applicationOpenFiles(NSApplication sender, NSArray filenames)
  196. {
  197. int count = filenames.count();
  198. for (int i=0; i<count; i++)
  199. {
  200. File file = new File((String)filenames.objectAtIndex(i));
  201. Buffer buffer;
  202. View view = jEdit.getActiveView();
  203. if(view == null)
  204. view = PerspectiveManager.loadPerspective(true);
  205. if (file.isDirectory())
  206. {
  207. VFSBrowser.browseDirectory(jEdit.getActiveView(),file.getPath());
  208. return;
  209. }
  210. if (jEdit.openFile(view,file.getPath()) == null)
  211. Log.log(Log.ERROR,this,"Error opening file.");
  212. }
  213. } //}}}
  214. //{{{ applicationShouldHandleReopen() method
  215. public boolean applicationShouldHandleReopen(NSApplication theApplication, boolean flag)
  216. {
  217. SwingUtilities.invokeLater(new Runnable()
  218. {
  219. public void run()
  220. {
  221. if (jEdit.getViewCount() == 0)
  222. new NewViewAction().doAction();
  223. }
  224. });
  225. return false;
  226. } //}}}
  227. //{{{ applicationShouldTerminate() method
  228. public boolean applicationShouldTerminate(NSApplication sender)
  229. {
  230. SwingUtilities.invokeLater(new Runnable()
  231. {
  232. public void run()
  233. {
  234. jEdit.exit(jEdit.getActiveView(),true);
  235. }
  236. });
  237. return false;
  238. }
  239. //}}}
  240. //}}}
  241. //{{{ Services
  242. //{{{ openFile() method
  243. public String openFile(NSPasteboard pboard, String userData)
  244. {
  245. if (jEdit.getViewCount() == 0)
  246. return null;
  247. NSData data = pboard.dataForType("NSFilenamesPboardType");
  248. String[] error = new String[1];
  249. int[] format = new int[1];
  250. NSArray filenames = (NSArray)NSPropertyListSerialization.propertyListFromData(data,
  251. NSPropertyListSerialization.PropertyListImmutable,
  252. format,
  253. error);
  254. int count = filenames.count();
  255. for (int i=0; i<count; i++)
  256. {
  257. File file = new File((String)filenames.objectAtIndex(i));
  258. if (file.isDirectory())
  259. VFSBrowser.browseDirectory(jEdit.getActiveView(),file.getPath());
  260. else
  261. jEdit.openFile(jEdit.getActiveView(),file.getPath());
  262. }
  263. return null;
  264. } //}}}
  265. //{{{ insertSelection() method
  266. public String insertSelection(NSPasteboard pboard, String userData)
  267. {
  268. String string = pboard.stringForType("NSStringPboardType");
  269. if (jEdit.getViewCount() > 0)
  270. {
  271. View view = jEdit.getActiveView();
  272. view.getBuffer().insert(view.getTextArea().getCaretPosition(),string);
  273. }
  274. return null;
  275. } //}}}
  276. //{{{ openSelection() method
  277. public String openSelection(NSPasteboard pboard, String userData)
  278. {
  279. String string = pboard.stringForType("NSStringPboardType");
  280. if (jEdit.getViewCount() == 0)
  281. new NewViewAction().doAction();
  282. jEdit.newFile(jEdit.getActiveView()).insert(0,pboard.stringForType("NSStringPboardType"));
  283. return null;
  284. } //}}}
  285. //}}}
  286. //{{{ Dock Menu
  287. //{{{ BufferMenu class
  288. class BufferMenu extends NSMenu
  289. {
  290. public BufferMenu()
  291. {
  292. super();
  293. }
  294. public void updateMenu()
  295. {
  296. NSMenuItem item;
  297. for (int i=0; i<numberOfItems(); i++)
  298. removeItemAtIndex(0);
  299. Buffer[] buffs = jEdit.getBuffers();
  300. for (int i=0; i < buffs.length; i++)
  301. {
  302. if (!buffs[i].isUntitled())
  303. {
  304. item = new NSMenuItem(buffs[i].getName(),actionSel,"");
  305. item.setTarget(new ShowFileAction(buffs[i].getPath()));
  306. //item.setImage(NSWorkspace.sharedWorkspace().iconForFile(
  307. // buffs[i].getPath()));
  308. if (!new File(buffs[i].getPath()).exists())
  309. item.setEnabled(false);
  310. addItem(item);
  311. }
  312. }
  313. if (numberOfItems() == 0)
  314. {
  315. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.buffers.none"),null,"");
  316. item.setEnabled(false);
  317. addItem(item);
  318. }
  319. }
  320. } //}}}
  321. //{{{ MacrosMenu class
  322. class MacrosMenu extends NSMenu
  323. {
  324. public MacrosMenu()
  325. {
  326. super();
  327. }
  328. public void updateMenu()
  329. {
  330. Vector macroVector = Macros.getMacroHierarchy();
  331. NSMenuItem item;
  332. File file;
  333. int max = macroVector.size();
  334. int length = numberOfItems();
  335. for (int i=0; i<length; i++)
  336. removeItemAtIndex(0);
  337. if (max == 0)
  338. {
  339. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.macros.none"),null,"");
  340. item.setEnabled(false);
  341. addItem(item);
  342. return;
  343. }
  344. createMenu(this,macroVector);
  345. }
  346. public void createMenu(NSMenu menu, Vector vector)
  347. {
  348. for(int i=0; i < vector.size(); i++)
  349. {
  350. Object obj = vector.elementAt(i);
  351. if(obj instanceof Macros.Macro)
  352. {
  353. Macros.Macro macro = (Macros.Macro)obj;
  354. NSMenuItem item = new NSMenuItem(macro.getLabel(),actionSel,"");
  355. item.setTarget(new MacroAction(macro));
  356. menu.addItem(item);
  357. }
  358. else if(obj instanceof Vector)
  359. {
  360. Vector subvector = (Vector)obj;
  361. String name = (String)subvector.elementAt(0);
  362. NSMenu submenu = new NSMenu();
  363. createMenu(submenu,subvector);
  364. if(submenu.numberOfItems() > 0)
  365. {
  366. NSMenuItem submenuitem = new NSMenuItem(name,null,"");
  367. submenuitem.setSubmenu(submenu);
  368. menu.addItem(submenuitem);
  369. }
  370. }
  371. }
  372. }
  373. } //}}}
  374. //{{{ RecentMenu class
  375. class RecentMenu extends NSMenu
  376. {
  377. public RecentMenu()
  378. {
  379. super();
  380. }
  381. public void updateMenu()
  382. {
  383. List recent = BufferHistory.getHistory();
  384. NSMenuItem item;
  385. File file;
  386. int max = recent.size();
  387. int min = max - 20;
  388. int length = numberOfItems();
  389. for (int i=0; i<length; i++)
  390. removeItemAtIndex(0);
  391. if (max == 0)
  392. {
  393. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recent.none"),null,"");
  394. item.setEnabled(false);
  395. addItem(item);
  396. return;
  397. }
  398. if (min < 0)
  399. min = 0;
  400. for (int i=max-1; i >= min ; i--)
  401. {
  402. file = new File(((BufferHistory.Entry)recent.get(i)).path);
  403. item = new NSMenuItem(file.getName(),actionSel,"");
  404. item.setTarget(new ShowFileAction(file.getPath()));
  405. if (!file.exists())
  406. item.setEnabled(false);
  407. addItem(item);
  408. }
  409. }
  410. } //}}}
  411. //{{{ RecentDirMenu class
  412. class RecentDirMenu extends NSMenu
  413. {
  414. public RecentDirMenu()
  415. {
  416. super();
  417. }
  418. public void updateMenu()
  419. {
  420. HistoryModel model = HistoryModel.getModel("vfs.browser.path");
  421. NSMenuItem item;
  422. File file;
  423. int max = model.getSize();
  424. int length = numberOfItems();
  425. for (int i=0; i<length; i++)
  426. removeItemAtIndex(0);
  427. if (max == 0)
  428. {
  429. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recentDir.none"),null,"");
  430. item.setEnabled(false);
  431. addItem(item);
  432. return;
  433. }
  434. for (int i=0; i < max ; i++)
  435. {
  436. file = new File(model.getItem(i));
  437. item = new NSMenuItem(file.getName(),actionSel,"");
  438. item.setTarget(new ShowFileAction(file.getPath()));
  439. if (!file.exists())
  440. item.setEnabled(false);
  441. addItem(item);
  442. }
  443. }
  444. } //}}}
  445. //{{{ MacroAction class
  446. class MacroAction
  447. {
  448. private Macros.Macro macro;
  449. public MacroAction(Macros.Macro macro)
  450. {
  451. this.macro = macro;
  452. }
  453. public void doAction()
  454. {
  455. macro.invoke(jEdit.getActiveView());
  456. }
  457. } //}}}
  458. //{{{ NewViewAction class
  459. class NewViewAction
  460. {
  461. public void doAction()
  462. {
  463. SwingUtilities.invokeLater(new Runnable()
  464. {
  465. public void run()
  466. {
  467. if (jEdit.getViewCount() == 0)
  468. PerspectiveManager.loadPerspective(true);
  469. else
  470. jEdit.newView(jEdit.getActiveView());
  471. }
  472. });
  473. }
  474. } //}}}
  475. //{{{ ShowFileAction class
  476. class ShowFileAction
  477. {
  478. private String path;
  479. public ShowFileAction(String path)
  480. {
  481. this.path = path;
  482. }
  483. public void doAction()
  484. {
  485. MacOSActions.showInFinder(path);
  486. }
  487. } //}}}
  488. //}}}
  489. }