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

# · Java · 581 lines · 432 code · 72 blank · 77 comment · 68 complexity · d214047b5740c11570b9ae7fee5913b5 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. // Must be declared final to be used by inner class
  209. final Collection files = new ArrayList();
  210. int count = filenames.count();
  211. for (int i=0; i<count; i++)
  212. files.add(new File((String)filenames.objectAtIndex(i)));
  213. SwingUtilities.invokeLater(new Runnable()
  214. {
  215. public void run()
  216. {
  217. for(Iterator iter = files.iterator(); iter.hasNext(); )
  218. {
  219. File file = (File) iter.next();
  220. View view = jEdit.getActiveView();
  221. if(view == null)
  222. view = PerspectiveManager.loadPerspective(true);
  223. if (file.isDirectory())
  224. {
  225. VFSBrowser.browseDirectory(jEdit.getActiveView(),file.getPath());
  226. return;
  227. }
  228. if (jEdit.openFile(view,file.getPath()) == null)
  229. Log.log(Log.ERROR,this,"Error opening file.");
  230. }
  231. }
  232. });
  233. } //}}}
  234. //{{{ applicationShouldHandleReopen() method
  235. public boolean applicationShouldHandleReopen(NSApplication theApplication, boolean flag)
  236. {
  237. SwingUtilities.invokeLater(new Runnable()
  238. {
  239. public void run()
  240. {
  241. if (jEdit.getViewCount() == 0)
  242. new NewViewAction().doAction();
  243. }
  244. });
  245. return false;
  246. } //}}}
  247. //{{{ applicationShouldTerminate() method
  248. public boolean applicationShouldTerminate(NSApplication sender)
  249. {
  250. SwingUtilities.invokeLater(new Runnable()
  251. {
  252. public void run()
  253. {
  254. jEdit.exit(jEdit.getActiveView(),true);
  255. }
  256. });
  257. return false;
  258. }
  259. //}}}
  260. //}}}
  261. //{{{ Services
  262. //{{{ openFile() method
  263. public String openFile(NSPasteboard pboard, String userData)
  264. {
  265. if (jEdit.getViewCount() == 0)
  266. return null;
  267. NSData data = pboard.dataForType("NSFilenamesPboardType");
  268. String[] error = new String[1];
  269. int[] format = new int[1];
  270. NSArray filenames = (NSArray)NSPropertyListSerialization.propertyListFromData(data,
  271. NSPropertyListSerialization.PropertyListImmutable,
  272. format,
  273. error);
  274. int count = filenames.count();
  275. for (int i=0; i<count; i++)
  276. {
  277. File file = new File((String)filenames.objectAtIndex(i));
  278. if (file.isDirectory())
  279. VFSBrowser.browseDirectory(jEdit.getActiveView(),file.getPath());
  280. else
  281. jEdit.openFile(jEdit.getActiveView(),file.getPath());
  282. }
  283. return null;
  284. } //}}}
  285. //{{{ insertSelection() method
  286. public String insertSelection(NSPasteboard pboard, String userData)
  287. {
  288. String string = pboard.stringForType("NSStringPboardType");
  289. if (jEdit.getViewCount() > 0)
  290. {
  291. View view = jEdit.getActiveView();
  292. view.getBuffer().insert(view.getTextArea().getCaretPosition(),string);
  293. }
  294. return null;
  295. } //}}}
  296. //{{{ openSelection() method
  297. public String openSelection(NSPasteboard pboard, String userData)
  298. {
  299. String string = pboard.stringForType("NSStringPboardType");
  300. if (jEdit.getViewCount() == 0)
  301. new NewViewAction().doAction();
  302. jEdit.newFile(jEdit.getActiveView()).insert(0,pboard.stringForType("NSStringPboardType"));
  303. return null;
  304. } //}}}
  305. //}}}
  306. //{{{ Dock Menu
  307. //{{{ BufferMenu class
  308. class BufferMenu extends NSMenu
  309. {
  310. public BufferMenu()
  311. {
  312. super();
  313. }
  314. public void updateMenu()
  315. {
  316. NSMenuItem item;
  317. for (int i=0; i<numberOfItems(); i++)
  318. removeItemAtIndex(0);
  319. Buffer[] buffs = jEdit.getBuffers();
  320. for (int i=0; i < buffs.length; i++)
  321. {
  322. if (!buffs[i].isUntitled())
  323. {
  324. item = new NSMenuItem(buffs[i].getName(),actionSel,"");
  325. item.setTarget(new ShowFileAction(buffs[i].getPath()));
  326. //item.setImage(NSWorkspace.sharedWorkspace().iconForFile(
  327. // buffs[i].getPath()));
  328. if (!new File(buffs[i].getPath()).exists())
  329. item.setEnabled(false);
  330. addItem(item);
  331. }
  332. }
  333. if (numberOfItems() == 0)
  334. {
  335. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.buffers.none"),null,"");
  336. item.setEnabled(false);
  337. addItem(item);
  338. }
  339. }
  340. } //}}}
  341. //{{{ MacrosMenu class
  342. class MacrosMenu extends NSMenu
  343. {
  344. public MacrosMenu()
  345. {
  346. super();
  347. }
  348. public void updateMenu()
  349. {
  350. Vector macroVector = Macros.getMacroHierarchy();
  351. NSMenuItem item;
  352. File file;
  353. int max = macroVector.size();
  354. int length = numberOfItems();
  355. for (int i=0; i<length; i++)
  356. removeItemAtIndex(0);
  357. if (max == 0)
  358. {
  359. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.macros.none"),null,"");
  360. item.setEnabled(false);
  361. addItem(item);
  362. return;
  363. }
  364. createMenu(this,macroVector);
  365. }
  366. public void createMenu(NSMenu menu, Vector vector)
  367. {
  368. for(int i=0; i < vector.size(); i++)
  369. {
  370. Object obj = vector.elementAt(i);
  371. if(obj instanceof Macros.Macro)
  372. {
  373. Macros.Macro macro = (Macros.Macro)obj;
  374. NSMenuItem item = new NSMenuItem(macro.getLabel(),actionSel,"");
  375. item.setTarget(new MacroAction(macro));
  376. menu.addItem(item);
  377. }
  378. else if(obj instanceof Vector)
  379. {
  380. Vector subvector = (Vector)obj;
  381. String name = (String)subvector.elementAt(0);
  382. NSMenu submenu = new NSMenu();
  383. createMenu(submenu,subvector);
  384. if(submenu.numberOfItems() > 0)
  385. {
  386. NSMenuItem submenuitem = new NSMenuItem(name,null,"");
  387. submenuitem.setSubmenu(submenu);
  388. menu.addItem(submenuitem);
  389. }
  390. }
  391. }
  392. }
  393. } //}}}
  394. //{{{ RecentMenu class
  395. class RecentMenu extends NSMenu
  396. {
  397. public RecentMenu()
  398. {
  399. super();
  400. }
  401. public void updateMenu()
  402. {
  403. List recent = BufferHistory.getHistory();
  404. NSMenuItem item;
  405. File file;
  406. int max = recent.size();
  407. int min = max - 20;
  408. int length = numberOfItems();
  409. for (int i=0; i<length; i++)
  410. removeItemAtIndex(0);
  411. if (max == 0)
  412. {
  413. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recent.none"),null,"");
  414. item.setEnabled(false);
  415. addItem(item);
  416. return;
  417. }
  418. if (min < 0)
  419. min = 0;
  420. for (int i=max-1; i >= min ; i--)
  421. {
  422. file = new File(((BufferHistory.Entry)recent.get(i)).path);
  423. item = new NSMenuItem(file.getName(),actionSel,"");
  424. item.setTarget(new ShowFileAction(file.getPath()));
  425. if (!file.exists())
  426. item.setEnabled(false);
  427. addItem(item);
  428. }
  429. }
  430. } //}}}
  431. //{{{ RecentDirMenu class
  432. class RecentDirMenu extends NSMenu
  433. {
  434. public RecentDirMenu()
  435. {
  436. super();
  437. }
  438. public void updateMenu()
  439. {
  440. HistoryModel model = HistoryModel.getModel("vfs.browser.path");
  441. NSMenuItem item;
  442. File file;
  443. int max = model.getSize();
  444. int length = numberOfItems();
  445. for (int i=0; i<length; i++)
  446. removeItemAtIndex(0);
  447. if (max == 0)
  448. {
  449. item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recentDir.none"),null,"");
  450. item.setEnabled(false);
  451. addItem(item);
  452. return;
  453. }
  454. for (int i=0; i < max ; i++)
  455. {
  456. file = new File(model.getItem(i));
  457. item = new NSMenuItem(file.getName(),actionSel,"");
  458. item.setTarget(new ShowFileAction(file.getPath()));
  459. if (!file.exists())
  460. item.setEnabled(false);
  461. addItem(item);
  462. }
  463. }
  464. } //}}}
  465. //{{{ MacroAction class
  466. class MacroAction
  467. {
  468. private Macros.Macro macro;
  469. public MacroAction(Macros.Macro macro)
  470. {
  471. this.macro = macro;
  472. }
  473. public void doAction()
  474. {
  475. macro.invoke(jEdit.getActiveView());
  476. }
  477. } //}}}
  478. //{{{ NewViewAction class
  479. class NewViewAction
  480. {
  481. public void doAction()
  482. {
  483. SwingUtilities.invokeLater(new Runnable()
  484. {
  485. public void run()
  486. {
  487. if (jEdit.getViewCount() == 0)
  488. PerspectiveManager.loadPerspective(true);
  489. else
  490. jEdit.newView(jEdit.getActiveView());
  491. }
  492. });
  493. }
  494. } //}}}
  495. //{{{ ShowFileAction class
  496. class ShowFileAction
  497. {
  498. private String path;
  499. public ShowFileAction(String path)
  500. {
  501. this.path = path;
  502. }
  503. public void doAction()
  504. {
  505. MacOSActions.showInFinder(path);
  506. }
  507. } //}}}
  508. //}}}
  509. }