PageRenderTime 88ms CodeModel.GetById 62ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 559 lines | 414 code | 70 blank | 75 comment | 62 complexity | f528b4eee8fe6de0e43b09008a80cf99 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
 23package macos;
 24
 25//{{{ Imports
 26import com.apple.eawt.*;
 27import com.apple.eio.*;
 28import com.apple.cocoa.application.*;
 29import com.apple.cocoa.foundation.*;
 30import java.util.*;
 31import java.io.File;
 32import javax.swing.*;
 33import org.gjt.sp.jedit.*;
 34import org.gjt.sp.jedit.browser.*;
 35import org.gjt.sp.jedit.gui.*;
 36import org.gjt.sp.jedit.msg.*;
 37import org.gjt.sp.jedit.options.GlobalOptions;
 38import org.gjt.sp.util.Log;
 39//}}}
 40
 41public class Delegate extends ApplicationAdapter
 42{
 43	//{{{ Variables
 44	private final NSSelector actionSel = new NSSelector("doAction", new Class[] {});
 45	
 46	private List filenames = new LinkedList();
 47	//}}}
 48	
 49	//{{{ Constructor
 50	public Delegate()
 51	{
 52		if (jEdit.getBooleanProperty("MacOSPlugin.useScreenMenuBar",
 53			jEdit.getBooleanProperty("MacOSPlugin.default.useScreenMenuBar"))
 54		)
 55			System.setProperty("apple.laf.useScreenMenuBar","true");
 56		else
 57			System.setProperty("apple.laf.useScreenMenuBar","false");
 58	} //}}}
 59	
 60	//{{{ Handlers
 61	
 62	//{{{ handleAbout() method
 63	public void handleAbout(ApplicationEvent event)
 64	{
 65		event.setHandled(true);
 66		new AboutDialog(jEdit.getActiveView());
 67	} //}}}
 68
 69	//{{{ handleFileCodes() method
 70	public void handleFileCodes(BufferUpdate msg)
 71	{
 72		Buffer buffer = msg.getBuffer();
 73		
 74		// Set type/creator on save
 75		if (!buffer.isDirty() && msg.getWhat() == BufferUpdate.DIRTY_CHANGED)
 76		{
 77			try {
 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				// Fail silently, since we may be using UFS
 85			}
 86		}
 87		// Add type/creator to local buffer property list on open
 88		else if (msg.getWhat() == BufferUpdate.CREATED)
 89		{			
 90			if ("true".equals(
 91				jEdit.getProperty("MacOSPlugin.preserveCodes")))
 92			{
 93				try {
 94					int type = FileManager.getFileType(buffer.getPath());
 95					int creator = FileManager.getFileCreator(buffer.getPath());
 96					
 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
115	//{{{ handleOpenFile() method
116	public void handleOpenFile(ViewUpdate msg)
117	{
118		if(msg.getWhat() == ViewUpdate.CREATED)
119		{
120			Iterator i = filenames.iterator();
121			while (i.hasNext())
122				jEdit.openFile(msg.getView(),(String)i.next());
123			MacOSPlugin.started = true;
124			NSApplication app = NSApplication.sharedApplication();
125			app.setServicesProvider(new Delegate());
126		}
127	} //}}}
128	
129	//{{{ handlePreferences() method
130	public void handlePreferences(ApplicationEvent event)
131	{
132		event.setHandled(true);
133		new GlobalOptions(jEdit.getActiveView());
134	} //}}}
135	
136	//{{{ handleQuit() method
137	/**
138	 * This never seems to be called when used with a delegate
139	 */
140	//public void handleQuit(ApplicationEvent event)
141	//{
142	//	event.setHandled(false);
143	//	jEdit.exit(jEdit.getActiveView(),true);
144	//} //}}}
145
146	//}}}
147	
148	//{{{ Delegate methods
149	
150	//{{{ applicationDockMenu() method
151	public NSMenu applicationDockMenu(NSApplication sender)
152	{
153		NSMenu dockMenu;
154		BufferMenu bufMenu;
155		MacrosMenu macMenu;
156		RecentMenu recMenu;
157		RecentDirMenu dirMenu;
158		NSMenuItem showCurrItem;
159		NSMenuItem showCurrDirItem;
160		NSMenuItem newViewItem;
161		
162		// Buffers
163		NSMenuItem miBuff = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.buffers.label"),null,"");
164		miBuff.setSubmenu(bufMenu = new BufferMenu());
165		
166		// Recent Buffers
167		NSMenuItem miRec = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recent.label"),null,"");
168		miRec.setSubmenu(recMenu = new RecentMenu());
169		
170		// Recent Directories
171		NSMenuItem miDir = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recentDir.label"),null,"");
172		miDir.setSubmenu(dirMenu = new RecentDirMenu());
173		
174		// Macros
175		NSMenuItem miMac = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.macros.label"),null,"");
176		miMac.setSubmenu(macMenu = new MacrosMenu());
177		
178		dockMenu = new NSMenu();
179		newViewItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.newView"),actionSel,"");
180		newViewItem.setTarget(new NewViewAction());
181		dockMenu.addItem(newViewItem);
182		dockMenu.addItem(new NSMenuItem().separatorItem());
183		showCurrItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.showCurrent"),actionSel,"");
184		dockMenu.addItem(showCurrItem);
185		showCurrDirItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.showCurrentDir"),actionSel,"");
186		dockMenu.addItem(showCurrDirItem);
187		dockMenu.addItem(new NSMenuItem().separatorItem());
188		dockMenu.addItem(miBuff);
189		dockMenu.addItem(miRec);
190		dockMenu.addItem(miDir);
191		//dockMenu.addItem(new NSMenuItem().separatorItem());
192		//dockMenu.addItem(miMac);
193		if (jEdit.getViewCount() == 0)
194			miMac.setEnabled(false);
195		
196		bufMenu.updateMenu();
197		recMenu.updateMenu();
198		dirMenu.updateMenu();
199		macMenu.updateMenu();
200		
201		View view = jEdit.getActiveView();
202		if (view != null)
203		{
204			File buff = new File(view.getBuffer().getPath());
205			if (buff.exists())
206			{
207				showCurrItem.setTarget(new ShowFileAction(buff.getPath()));
208				showCurrDirItem.setTarget(new ShowFileAction(buff.getParent()));
209			}
210		}
211		else
212		{
213			showCurrItem.setEnabled(false);
214			showCurrDirItem.setEnabled(false);
215		}
216		
217		return dockMenu;
218	} //}}}
219	
220	//{{{ applicationOpenFiles() method
221	public void applicationOpenFiles(NSApplication sender, NSArray filenames)
222	{
223		int count = filenames.count();
224		for (int i=0; i<count; i++)
225		{
226			File file = new File((String)filenames.objectAtIndex(i));
227			Buffer buffer;
228			
229			View view = jEdit.getActiveView();
230			if(view == null)
231				view = PerspectiveManager.loadPerspective(true);
232			
233			if (file.isDirectory())
234			{
235				VFSBrowser.browseDirectory(jEdit.getActiveView(),file.getPath());
236				return;
237			}
238			
239			if (jEdit.openFile(view,file.getPath()) == null)
240				Log.log(Log.ERROR,this,"Error opening file.");
241		}
242	} //}}}
243	
244	//{{{ applicationShouldHandleReopen() method
245	public boolean applicationShouldHandleReopen(NSApplication theApplication, boolean flag)
246	{
247		SwingUtilities.invokeLater(new Runnable()
248		{
249			public void run()
250			{
251				if (jEdit.getViewCount() == 0)
252					new NewViewAction().doAction();
253			}
254		});
255		
256		return false;
257	} //}}}
258	
259	//{{{ applicationShouldTerminate() method
260	public boolean applicationShouldTerminate(NSApplication sender)
261	{
262		SwingUtilities.invokeLater(new Runnable()
263		{
264			public void run()
265			{
266				jEdit.exit(jEdit.getActiveView(),true);
267			}
268		});
269		return false;
270	}
271	//}}}
272	
273	//}}}
274	
275	//{{{ Services
276	
277	//{{{ openFile() method
278	public String openFile(NSPasteboard pboard, String userData)
279	{
280		if (jEdit.getViewCount() == 0)
281			return null;
282		
283		NSData data = pboard.dataForType("NSFilenamesPboardType");
284		String[] error = new String[1];
285		int[] format = new int[1];
286		NSArray filenames = (NSArray)NSPropertyListSerialization.propertyListFromData(data,
287			NSPropertyListSerialization.PropertyListImmutable,
288			format,
289			error);
290		int count = filenames.count();
291		for (int i=0; i<count; i++)
292		{
293			File file = new File((String)filenames.objectAtIndex(i));
294			if (file.isDirectory())
295				VFSBrowser.browseDirectory(jEdit.getActiveView(),file.getPath());
296			else
297				jEdit.openFile(jEdit.getActiveView(),file.getPath());
298		}
299		
300		return null;
301	} //}}}
302	
303	//{{{ insertSelection() method
304	public String insertSelection(NSPasteboard pboard, String userData)
305	{
306		String string = pboard.stringForType("NSStringPboardType");
307		if (jEdit.getViewCount() > 0)
308		{
309			View view = jEdit.getActiveView();
310			view.getBuffer().insert(view.getTextArea().getCaretPosition(),string);
311		}
312		return null;
313	} //}}}
314	
315	//{{{ openSelection() method
316	public String openSelection(NSPasteboard pboard, String userData)
317	{
318		String string = pboard.stringForType("NSStringPboardType");
319		if (jEdit.getViewCount() == 0)
320			new NewViewAction().doAction();
321		jEdit.newFile(jEdit.getActiveView()).insert(0,pboard.stringForType("NSStringPboardType"));
322		return null;
323	} //}}}
324	
325	//}}}
326	
327	//{{{ Dock Menu
328	
329	//{{{ BufferMenu class
330	class BufferMenu extends NSMenu
331	{
332		public BufferMenu()
333		{
334			super();
335		}
336		
337		public void updateMenu()
338		{
339			NSMenuItem item;
340			for (int i=0; i<numberOfItems(); i++)
341				removeItemAtIndex(0);
342			
343			Buffer[] buffs = jEdit.getBuffers();
344			for (int i=0; i < buffs.length; i++)
345			{
346				if (!buffs[i].isUntitled())
347				{
348					item = new NSMenuItem(buffs[i].getName(),actionSel,"");
349					item.setTarget(new ShowFileAction(buffs[i].getPath()));
350					//item.setImage(NSWorkspace.sharedWorkspace().iconForFile(
351					//	buffs[i].getPath()));
352					if (!new File(buffs[i].getPath()).exists())
353						item.setEnabled(false);
354					addItem(item);
355				}
356			}
357			
358			if (numberOfItems() == 0)
359			{
360				item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.buffers.none"),null,"");
361				item.setEnabled(false);
362				addItem(item);
363			}
364		}
365	} //}}}
366	
367	//{{{ MacrosMenu class
368	class MacrosMenu extends NSMenu
369	{
370		public MacrosMenu()
371		{
372			super();
373		}
374		
375		public void updateMenu()
376		{
377			Vector macroVector = Macros.getMacroHierarchy();
378			NSMenuItem item;
379			File file;
380			int max = macroVector.size();
381			
382			int length = numberOfItems();
383			for (int i=0; i<length; i++)
384				removeItemAtIndex(0);
385			
386			if (max == 0)
387			{
388				item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.macros.none"),null,"");
389				item.setEnabled(false);
390				addItem(item);
391				return;
392			}
393			
394			createMenu(this,macroVector);
395		}
396		
397		public void createMenu(NSMenu menu, Vector vector)
398		{
399			for(int i=0; i < vector.size(); i++)
400			{
401				Object obj = vector.elementAt(i);
402				if(obj instanceof Macros.Macro)
403				{
404					Macros.Macro macro = (Macros.Macro)obj;
405					NSMenuItem item = new NSMenuItem(macro.getLabel(),actionSel,"");
406					item.setTarget(new MacroAction(macro));
407					menu.addItem(item);
408				}
409				else if(obj instanceof Vector)
410				{
411					Vector subvector = (Vector)obj;
412					String name = (String)subvector.elementAt(0);
413					NSMenu submenu = new NSMenu();
414					createMenu(submenu,subvector);
415					if(submenu.numberOfItems() > 0)
416					{
417						NSMenuItem submenuitem = new NSMenuItem(name,null,"");
418						submenuitem.setSubmenu(submenu);
419						menu.addItem(submenuitem);
420					}
421				}
422			}
423		}
424	} //}}}
425	
426	//{{{ RecentMenu class
427	class RecentMenu extends NSMenu
428	{
429		public RecentMenu()
430		{
431			super();
432		}
433		
434		public void updateMenu()
435		{
436			List recent = BufferHistory.getHistory();
437			NSMenuItem item;
438			File file;
439			int max = recent.size();
440			int min = max - 20;
441			
442			int length = numberOfItems();
443			for (int i=0; i<length; i++)
444				removeItemAtIndex(0);
445			
446			if (max == 0)
447			{
448				item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recent.none"),null,"");
449				item.setEnabled(false);
450				addItem(item);
451				return;
452			}
453			
454			if (min < 0)
455				min = 0;
456			
457			for (int i=max-1; i >= min ; i--)
458			{
459				file = new File(((BufferHistory.Entry)recent.get(i)).path);
460				item = new NSMenuItem(file.getName(),actionSel,"");
461				item.setTarget(new ShowFileAction(file.getPath()));
462				if (!file.exists())
463					item.setEnabled(false);
464				addItem(item);
465			}
466		}
467	} //}}}
468	
469	//{{{ RecentDirMenu class
470	class RecentDirMenu extends NSMenu
471	{
472		public RecentDirMenu()
473		{
474			super();
475		}
476		
477		public void updateMenu()
478		{
479			HistoryModel model = HistoryModel.getModel("vfs.browser.path");
480			NSMenuItem item;
481			File file;
482			int max = model.getSize();
483			
484			int length = numberOfItems();
485			for (int i=0; i<length; i++)
486				removeItemAtIndex(0);
487			
488			if (max == 0)
489			{
490				item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recentDir.none"),null,"");
491				item.setEnabled(false);
492				addItem(item);
493				return;
494			}
495			
496			for (int i=0; i < max ; i++)
497			{
498				file = new File(model.getItem(i));
499				item = new NSMenuItem(file.getName(),actionSel,"");
500				item.setTarget(new ShowFileAction(file.getPath()));
501				if (!file.exists())
502					item.setEnabled(false);
503				addItem(item);
504			}
505		}
506	} //}}}
507	
508	//{{{ MacroAction class
509	class MacroAction
510	{
511		private Macros.Macro macro;
512		
513		public MacroAction(Macros.Macro macro)
514		{
515			this.macro = macro;
516		}
517		
518		public void doAction()
519		{
520			macro.invoke(jEdit.getActiveView());
521		}
522	} //}}}
523	
524	//{{{ NewViewAction class
525	class NewViewAction
526	{
527		public void doAction()
528		{
529			SwingUtilities.invokeLater(new Runnable()
530			{
531				public void run()
532				{
533					if (jEdit.getViewCount() == 0)
534						PerspectiveManager.loadPerspective(true);
535					else
536						jEdit.newView(jEdit.getActiveView());
537				}
538			});
539		}
540	} //}}}
541	
542	//{{{ ShowFileAction class
543	class ShowFileAction
544	{
545		private String path;
546		
547		public ShowFileAction(String path)
548		{
549			this.path = path;
550		}
551		
552		public void doAction()
553		{
554			MacOSActions.showInFinder(path);
555		}
556	} //}}}
557	
558	//}}}
559}