/jEdit/tags/jedit-4-3-pre10/jars/MacOS/macos/Delegate.java

# · Java · 572 lines · 424 code · 72 blank · 76 comment · 67 complexity · c6c2a114c88423cc1c9b957e014954cc MD5 · raw file

  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. if (jEdit.getBooleanProperty("MacOSPlugin.setTypeCreator",
  66. jEdit.getBooleanProperty("MacOSPlugin.default.setTypeCreator")))
  67. {
  68. // Set type/creator on save
  69. if (!buffer.isDirty() && msg.getWhat() == BufferUpdate.DIRTY_CHANGED)
  70. {
  71. try {
  72. int type = FileManager.getFileType(buffer.getPath());
  73. int creator = FileManager.getFileCreator(buffer.getPath());
  74. if (type != 0)
  75. buffer.setIntegerProperty("MacOSPlugin.type",type);
  76. if (creator != 0)
  77. buffer.setIntegerProperty("MacOSPlugin.creator",creator);
  78. FileManager.setFileTypeAndCreator(buffer.getPath(),
  79. buffer.getIntegerProperty("MacOSPlugin.type",
  80. jEdit.getIntegerProperty("MacOSPlugin.default.type",0)),
  81. buffer.getIntegerProperty("MacOSPlugin.creator",
  82. jEdit.getIntegerProperty("MacOSPlugin.default.creator",0)));
  83. } catch (Exception e) {
  84. // This will happen when a new file is created
  85. // Fail silently, since we may be using UFS
  86. }
  87. }
  88. // Add type/creator to local buffer property list on open
  89. else if (msg.getWhat() == BufferUpdate.CREATED)
  90. {
  91. if (jEdit.getBooleanProperty("MacOSPlugin.preserveCodes",
  92. jEdit.getBooleanProperty("MacOSPlugin.default.preserveCodes")))
  93. {
  94. try {
  95. int type = FileManager.getFileType(buffer.getPath());
  96. int creator = FileManager.getFileCreator(buffer.getPath());
  97. if (type != 0)
  98. buffer.setIntegerProperty("MacOSPlugin.type",type);
  99. if (creator != 0)
  100. buffer.setIntegerProperty("MacOSPlugin.creator",creator);
  101. } catch (Exception e) {
  102. // This will happen when a new file is created
  103. }
  104. }
  105. }
  106. }
  107. } //}}}
  108. //{{{ handleOpenFile() method
  109. public void handleOpenFile(ApplicationEvent event)
  110. {
  111. filenames.add(event.getFilename());
  112. event.setHandled(true);
  113. } //}}}
  114. //{{{ handleOpenFile() method
  115. public void handleOpenFile(ViewUpdate msg)
  116. {
  117. if(msg.getWhat() == ViewUpdate.CREATED)
  118. {
  119. Iterator i = filenames.iterator();
  120. while (i.hasNext())
  121. jEdit.openFile(msg.getView(),(String)i.next());
  122. MacOSPlugin.started = true;
  123. NSApplication app = NSApplication.sharedApplication();
  124. app.setServicesProvider(new Delegate());
  125. }
  126. } //}}}
  127. //{{{ handlePreferences() method
  128. public void handlePreferences(ApplicationEvent event)
  129. {
  130. event.setHandled(true);
  131. new GlobalOptions(jEdit.getActiveView());
  132. } //}}}
  133. //{{{ handleQuit() method
  134. /**
  135. * This never seems to be called when used with a delegate
  136. */
  137. //public void handleQuit(ApplicationEvent event)
  138. //{
  139. // event.setHandled(false);
  140. // jEdit.exit(jEdit.getActiveView(),true);
  141. //} //}}}
  142. //}}}
  143. //{{{ Delegate methods
  144. //{{{ applicationDockMenu() method
  145. public NSMenu applicationDockMenu(NSApplication sender)
  146. {
  147. NSMenu dockMenu;
  148. BufferMenu bufMenu;
  149. MacrosMenu macMenu;
  150. RecentMenu recMenu;
  151. RecentDirMenu dirMenu;
  152. NSMenuItem showCurrItem;
  153. NSMenuItem showCurrDirItem;
  154. NSMenuItem newViewItem;
  155. // Buffers
  156. NSMenuItem miBuff = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.buffers.label"),null,"");
  157. miBuff.setSubmenu(bufMenu = new BufferMenu());
  158. // Recent Buffers
  159. NSMenuItem miRec = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recent.label"),null,"");
  160. miRec.setSubmenu(recMenu = new RecentMenu());
  161. // Recent Directories
  162. NSMenuItem miDir = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recentDir.label"),null,"");
  163. miDir.setSubmenu(dirMenu = new RecentDirMenu());
  164. // Macros
  165. NSMenuItem miMac = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.macros.label"),null,"");
  166. miMac.setSubmenu(macMenu = new MacrosMenu());
  167. dockMenu = new NSMenu();
  168. newViewItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.newView"),actionSel,"");
  169. newViewItem.setTarget(new NewViewAction());
  170. dockMenu.addItem(newViewItem);
  171. dockMenu.addItem(new NSMenuItem().separatorItem());
  172. showCurrItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.showCurrent"),actionSel,"");
  173. dockMenu.addItem(showCurrItem);
  174. showCurrDirItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.showCurrentDir"),actionSel,"");
  175. dockMenu.addItem(showCurrDirItem);
  176. dockMenu.addItem(new NSMenuItem().separatorItem());
  177. dockMenu.addItem(miBuff);
  178. dockMenu.addItem(miRec);
  179. dockMenu.addItem(miDir);
  180. //dockMenu.addItem(new NSMenuItem().separatorItem());
  181. //dockMenu.addItem(miMac);
  182. if (jEdit.getViewCount() == 0)
  183. miMac.setEnabled(false);
  184. bufMenu.updateMenu();
  185. recMenu.updateMenu();
  186. dirMenu.updateMenu();
  187. macMenu.updateMenu();
  188. View view = jEdit.getActiveView();
  189. if (view != null)
  190. {
  191. File buff = new File(view.getBuffer().getPath());
  192. if (buff.exists())
  193. {
  194. showCurrItem.setTarget(new ShowFileAction(buff.getPath()));
  195. showCurrDirItem.setTarget(new ShowFileAction(buff.getParent()));
  196. }
  197. }
  198. else
  199. {
  200. showCurrItem.setEnabled(false);
  201. showCurrDirItem.setEnabled(false);
  202. }
  203. return dockMenu;
  204. } //}}}
  205. //{{{ applicationOpenFiles() method
  206. public void applicationOpenFiles(NSApplication sender, NSArray filenames)
  207. {
  208. int count = filenames.count();
  209. for (int i=0; i<count; i++)
  210. {
  211. File file = new File((String)filenames.objectAtIndex(i));
  212. Buffer buffer;
  213. View view = jEdit.getActiveView();
  214. if(view == null)
  215. view = PerspectiveManager.loadPerspective(true);
  216. if (file.isDirectory())
  217. {
  218. VFSBrowser.browseDirectory(jEdit.getActiveView(),file.getPath());
  219. return;
  220. }
  221. if (jEdit.openFile(view,file.getPath()) == null)
  222. Log.log(Log.ERROR,this,"Error opening file.");
  223. }
  224. } //}}}
  225. //{{{ applicationShouldHandleReopen() method
  226. public boolean applicationShouldHandleReopen(NSApplication theApplication, boolean flag)
  227. {
  228. SwingUtilities.invokeLater(new Runnable()
  229. {
  230. public void run()
  231. {
  232. if (jEdit.getViewCount() == 0)
  233. new NewViewAction().doAction();
  234. }
  235. });
  236. return false;
  237. } //}}}
  238. //{{{ applicationShouldTerminate() method
  239. public boolean applicationShouldTerminate(NSApplication sender)
  240. {
  241. SwingUtilities.invokeLater(new Runnable()
  242. {
  243. public void run()
  244. {
  245. jEdit.exit(jEdit.getActiveView(),true);
  246. }
  247. });
  248. return false;
  249. }
  250. //}}}
  251. //}}}
  252. //{{{ Services
  253. //{{{ openFile() method
  254. public String openFile(NSPasteboard pboard, String userData)
  255. {
  256. if (jEdit.getViewCount() == 0)
  257. return null;
  258. NSData data = pboard.dataForType("NSFilenamesPboardType");
  259. String[] error = new String[1];
  260. int[] format = new int[1];
  261. NSArray filenames = (NSArray)NSPropertyListSerialization.propertyListFromData(data,
  262. NSPropertyListSerialization.PropertyListImmutable,
  263. format,
  264. error);
  265. int count = filenames.count();
  266. for (int i=0; i<count; i++)
  267. {
  268. File file = new File((String)filenames.objectAtIndex(i));
  269. if (file.isDirectory())
  270. VFSBrowser.browseDirectory(jEdit.getActiveView(),file.getPath());
  271. else
  272. jEdit.openFile(jEdit.getActiveView(),file.getPath());
  273. }
  274. return null;
  275. } //}}}
  276. //{{{ insertSelection() method
  277. public String insertSelection(NSPasteboard pboard, String userData)
  278. {
  279. String string = pboard.stringForType("NSStringPboardType");
  280. if (jEdit.getViewCount() > 0)
  281. {
  282. View view = jEdit.getActiveView();
  283. view.getBuffer().insert(view.getTextArea().getCaretPosition(),string);
  284. }
  285. return null;
  286. } //}}}
  287. //{{{ openSelection() method
  288. public String openSelection(NSPasteboard pboard, String userData)
  289. {
  290. String string = pboard.stringForType("NSStringPboardType");
  291. if (jEdit.getViewCount() == 0)
  292. new NewViewAction().doAction();
  293. jEdit.newFile(jEdit.getActiveView()).insert(0,pboard.stringForType("NSStringPboardType"));
  294. return null;
  295. } //}}}
  296. //}}}
  297. //{{{ Dock Menu
  298. //{{{ BufferMenu class
  299. class BufferMenu extends NSMenu
  300. {
  301. public BufferMenu()
  302. {
  303. super();
  304. }
  305. public void updateMenu()
  306. {
  307. NSMenuItem item;
  308. for (int i=0; i<numberOfItems(); i++)
  309. removeItemAtIndex(0);
  310. Buffer[] buffs = jEdit.getBuffers();
  311. for (int i=0; i < buffs.length; i++)
  312. {
  313. if (!buffs[i].isUntitled())
  314. {
  315. item = new NSMenuItem(buffs[i].getName(),actionSel,"");
  316. item.setTarget(new ShowFileAction(buffs[i].getPath()));
  317. //item.setImage(NSWorkspace.sharedWorkspace().iconForFile(
  318. // buffs[i].getPath()));
  319. if (!new File(buffs[i].getPath()).exists())
  320. item.setEnabled(false);
  321. addItem(item);
  322. }
  323. }
  324. if (numberOfItems() == 0)
  325. {
  326. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.buffers.none"),null,"");
  327. item.setEnabled(false);
  328. addItem(item);
  329. }
  330. }
  331. } //}}}
  332. //{{{ MacrosMenu class
  333. class MacrosMenu extends NSMenu
  334. {
  335. public MacrosMenu()
  336. {
  337. super();
  338. }
  339. public void updateMenu()
  340. {
  341. Vector macroVector = Macros.getMacroHierarchy();
  342. NSMenuItem item;
  343. File file;
  344. int max = macroVector.size();
  345. int length = numberOfItems();
  346. for (int i=0; i<length; i++)
  347. removeItemAtIndex(0);
  348. if (max == 0)
  349. {
  350. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.macros.none"),null,"");
  351. item.setEnabled(false);
  352. addItem(item);
  353. return;
  354. }
  355. createMenu(this,macroVector);
  356. }
  357. public void createMenu(NSMenu menu, Vector vector)
  358. {
  359. for(int i=0; i < vector.size(); i++)
  360. {
  361. Object obj = vector.elementAt(i);
  362. if(obj instanceof Macros.Macro)
  363. {
  364. Macros.Macro macro = (Macros.Macro)obj;
  365. NSMenuItem item = new NSMenuItem(macro.getLabel(),actionSel,"");
  366. item.setTarget(new MacroAction(macro));
  367. menu.addItem(item);
  368. }
  369. else if(obj instanceof Vector)
  370. {
  371. Vector subvector = (Vector)obj;
  372. String name = (String)subvector.elementAt(0);
  373. NSMenu submenu = new NSMenu();
  374. createMenu(submenu,subvector);
  375. if(submenu.numberOfItems() > 0)
  376. {
  377. NSMenuItem submenuitem = new NSMenuItem(name,null,"");
  378. submenuitem.setSubmenu(submenu);
  379. menu.addItem(submenuitem);
  380. }
  381. }
  382. }
  383. }
  384. } //}}}
  385. //{{{ RecentMenu class
  386. class RecentMenu extends NSMenu
  387. {
  388. public RecentMenu()
  389. {
  390. super();
  391. }
  392. public void updateMenu()
  393. {
  394. List recent = BufferHistory.getHistory();
  395. NSMenuItem item;
  396. File file;
  397. int max = recent.size();
  398. int min = max - 20;
  399. int length = numberOfItems();
  400. for (int i=0; i<length; i++)
  401. removeItemAtIndex(0);
  402. if (max == 0)
  403. {
  404. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recent.none"),null,"");
  405. item.setEnabled(false);
  406. addItem(item);
  407. return;
  408. }
  409. if (min < 0)
  410. min = 0;
  411. for (int i=max-1; i >= min ; i--)
  412. {
  413. file = new File(((BufferHistory.Entry)recent.get(i)).path);
  414. item = new NSMenuItem(file.getName(),actionSel,"");
  415. item.setTarget(new ShowFileAction(file.getPath()));
  416. if (!file.exists())
  417. item.setEnabled(false);
  418. addItem(item);
  419. }
  420. }
  421. } //}}}
  422. //{{{ RecentDirMenu class
  423. class RecentDirMenu extends NSMenu
  424. {
  425. public RecentDirMenu()
  426. {
  427. super();
  428. }
  429. public void updateMenu()
  430. {
  431. HistoryModel model = HistoryModel.getModel("vfs.browser.path");
  432. NSMenuItem item;
  433. File file;
  434. int max = model.getSize();
  435. int length = numberOfItems();
  436. for (int i=0; i<length; i++)
  437. removeItemAtIndex(0);
  438. if (max == 0)
  439. {
  440. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recentDir.none"),null,"");
  441. item.setEnabled(false);
  442. addItem(item);
  443. return;
  444. }
  445. for (int i=0; i < max ; i++)
  446. {
  447. file = new File(model.getItem(i));
  448. item = new NSMenuItem(file.getName(),actionSel,"");
  449. item.setTarget(new ShowFileAction(file.getPath()));
  450. if (!file.exists())
  451. item.setEnabled(false);
  452. addItem(item);
  453. }
  454. }
  455. } //}}}
  456. //{{{ MacroAction class
  457. class MacroAction
  458. {
  459. private Macros.Macro macro;
  460. public MacroAction(Macros.Macro macro)
  461. {
  462. this.macro = macro;
  463. }
  464. public void doAction()
  465. {
  466. macro.invoke(jEdit.getActiveView());
  467. }
  468. } //}}}
  469. //{{{ NewViewAction class
  470. class NewViewAction
  471. {
  472. public void doAction()
  473. {
  474. SwingUtilities.invokeLater(new Runnable()
  475. {
  476. public void run()
  477. {
  478. if (jEdit.getViewCount() == 0)
  479. PerspectiveManager.loadPerspective(true);
  480. else
  481. jEdit.newView(jEdit.getActiveView());
  482. }
  483. });
  484. }
  485. } //}}}
  486. //{{{ ShowFileAction class
  487. class ShowFileAction
  488. {
  489. private String path;
  490. public ShowFileAction(String path)
  491. {
  492. this.path = path;
  493. }
  494. public void doAction()
  495. {
  496. MacOSActions.showInFinder(path);
  497. }
  498. } //}}}
  499. //}}}
  500. }