alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/* 
 * :tabSize=8:indentSize=8:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * Delegate.java - A delegate for NSApplication
 * Copyright (C) 2003 Kris Kopicki
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package macos;

//{{{ Imports
import com.apple.eawt.*;
import com.apple.eio.*;
import com.apple.cocoa.application.*;
import com.apple.cocoa.foundation.*;
import java.util.*;
import java.io.File;
import javax.swing.*;
import org.gjt.sp.jedit.*;
import org.gjt.sp.jedit.browser.*;
import org.gjt.sp.jedit.gui.*;
import org.gjt.sp.jedit.msg.*;
import org.gjt.sp.jedit.options.GlobalOptions;
import org.gjt.sp.util.Log;
//}}}

public class Delegate extends ApplicationAdapter
{
	//{{{ Variables
	private final NSSelector actionSel = new NSSelector("doAction", new Class[] {});
	
	private List filenames = new LinkedList();
	//}}}
	
	//{{{ Constructor
	public Delegate()
	{
		if (jEdit.getBooleanProperty("MacOSPlugin.useScreenMenuBar",
			jEdit.getBooleanProperty("MacOSPlugin.default.useScreenMenuBar"))
		)
			System.setProperty("apple.laf.useScreenMenuBar","true");
		else
			System.setProperty("apple.laf.useScreenMenuBar","false");
	} //}}}
	
	//{{{ Handlers
	
	//{{{ handleAbout() method
	public void handleAbout(ApplicationEvent event)
	{
		event.setHandled(true);
		new AboutDialog(jEdit.getActiveView());
	} //}}}

	//{{{ handleFileCodes() method
	public void handleFileCodes(BufferUpdate msg)
	{
		Buffer buffer = msg.getBuffer();
		
		// Set type/creator on save
		if (!buffer.isDirty() && msg.getWhat() == BufferUpdate.DIRTY_CHANGED)
		{
			try {
				FileManager.setFileTypeAndCreator(buffer.getPath(),
					buffer.getIntegerProperty("MacOSPlugin.type",
						jEdit.getIntegerProperty("MacOSPlugin.default.type",0)),
					buffer.getIntegerProperty("MacOSPlugin.creator",
						jEdit.getIntegerProperty("MacOSPlugin.default.creator",0)));
			} catch (Exception e) {
				// Fail silently, since we may be using UFS
			}
		}
		// Add type/creator to local buffer property list on open
		else if (msg.getWhat() == BufferUpdate.CREATED)
		{			
			if ("true".equals(
				jEdit.getProperty("MacOSPlugin.preserveCodes")))
			{
				try {
					int type = FileManager.getFileType(buffer.getPath());
					int creator = FileManager.getFileCreator(buffer.getPath());
					
					if (type != 0)
						buffer.setIntegerProperty("MacOSPlugin.type",type);
					if (creator != 0)
						buffer.setIntegerProperty("MacOSPlugin.creator",creator);
				} catch (Exception e) {
					// This will happen when a new file is created
				}
			}
		}
	} //}}}
	
	//{{{ handleOpenFile() method
	public void handleOpenFile(ApplicationEvent event)
	{
		filenames.add(event.getFilename());
		event.setHandled(true);
	} //}}}

	//{{{ handleOpenFile() method
	public void handleOpenFile(ViewUpdate msg)
	{
		if(msg.getWhat() == ViewUpdate.CREATED)
		{
			Iterator i = filenames.iterator();
			while (i.hasNext())
				jEdit.openFile(msg.getView(),(String)i.next());
			MacOSPlugin.started = true;
			NSApplication app = NSApplication.sharedApplication();
			app.setServicesProvider(new Delegate());
		}
	} //}}}
	
	//{{{ handlePreferences() method
	public void handlePreferences(ApplicationEvent event)
	{
		event.setHandled(true);
		new GlobalOptions(jEdit.getActiveView());
	} //}}}
	
	//{{{ handleQuit() method
	/**
	 * This never seems to be called when used with a delegate
	 */
	//public void handleQuit(ApplicationEvent event)
	//{
	//	event.setHandled(false);
	//	jEdit.exit(jEdit.getActiveView(),true);
	//} //}}}

	//}}}
	
	//{{{ Delegate methods
	
	//{{{ applicationDockMenu() method
	public NSMenu applicationDockMenu(NSApplication sender)
	{
		NSMenu dockMenu;
		BufferMenu bufMenu;
		MacrosMenu macMenu;
		RecentMenu recMenu;
		RecentDirMenu dirMenu;
		NSMenuItem showCurrItem;
		NSMenuItem showCurrDirItem;
		NSMenuItem newViewItem;
		
		// Buffers
		NSMenuItem miBuff = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.buffers.label"),null,"");
		miBuff.setSubmenu(bufMenu = new BufferMenu());
		
		// Recent Buffers
		NSMenuItem miRec = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recent.label"),null,"");
		miRec.setSubmenu(recMenu = new RecentMenu());
		
		// Recent Directories
		NSMenuItem miDir = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recentDir.label"),null,"");
		miDir.setSubmenu(dirMenu = new RecentDirMenu());
		
		// Macros
		NSMenuItem miMac = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.macros.label"),null,"");
		miMac.setSubmenu(macMenu = new MacrosMenu());
		
		dockMenu = new NSMenu();
		newViewItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.newView"),actionSel,"");
		newViewItem.setTarget(new NewViewAction());
		dockMenu.addItem(newViewItem);
		dockMenu.addItem(new NSMenuItem().separatorItem());
		showCurrItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.showCurrent"),actionSel,"");
		dockMenu.addItem(showCurrItem);
		showCurrDirItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.showCurrentDir"),actionSel,"");
		dockMenu.addItem(showCurrDirItem);
		dockMenu.addItem(new NSMenuItem().separatorItem());
		dockMenu.addItem(miBuff);
		dockMenu.addItem(miRec);
		dockMenu.addItem(miDir);
		//dockMenu.addItem(new NSMenuItem().separatorItem());
		//dockMenu.addItem(miMac);
		if (jEdit.getViewCount() == 0)
			miMac.setEnabled(false);
		
		bufMenu.updateMenu();
		recMenu.updateMenu();
		dirMenu.updateMenu();
		macMenu.updateMenu();
		
		View view = jEdit.getActiveView();
		if (view != null)
		{
			File buff = new File(view.getBuffer().getPath());
			if (buff.exists())
			{
				showCurrItem.setTarget(new ShowFileAction(buff.getPath()));
				showCurrDirItem.setTarget(new ShowFileAction(buff.getParent()));
			}
		}
		else
		{
			showCurrItem.setEnabled(false);
			showCurrDirItem.setEnabled(false);
		}
		
		return dockMenu;
	} //}}}
	
	//{{{ applicationOpenFiles() method
	public void applicationOpenFiles(NSApplication sender, NSArray filenames)
	{
		int count = filenames.count();
		for (int i=0; i 0)
		{
			View view = jEdit.getActiveView();
			view.getBuffer().insert(view.getTextArea().getCaretPosition(),string);
		}
		return null;
	} //}}}
	
	//{{{ openSelection() method
	public String openSelection(NSPasteboard pboard, String userData)
	{
		String string = pboard.stringForType("NSStringPboardType");
		if (jEdit.getViewCount() == 0)
			new NewViewAction().doAction();
		jEdit.newFile(jEdit.getActiveView()).insert(0,pboard.stringForType("NSStringPboardType"));
		return null;
	} //}}}
	
	//}}}
	
	//{{{ Dock Menu
	
	//{{{ BufferMenu class
	class BufferMenu extends NSMenu
	{
		public BufferMenu()
		{
			super();
		}
		
		public void updateMenu()
		{
			NSMenuItem item;
			for (int i=0; i 0)
					{
						NSMenuItem submenuitem = new NSMenuItem(name,null,"");
						submenuitem.setSubmenu(submenu);
						menu.addItem(submenuitem);
					}
				}
			}
		}
	} //}}}
	
	//{{{ RecentMenu class
	class RecentMenu extends NSMenu
	{
		public RecentMenu()
		{
			super();
		}
		
		public void updateMenu()
		{
			List recent = BufferHistory.getHistory();
			NSMenuItem item;
			File file;
			int max = recent.size();
			int min = max - 20;
			
			int length = numberOfItems();
			for (int i=0; i= min ; i--)
			{
				file = new File(((BufferHistory.Entry)recent.get(i)).path);
				item = new NSMenuItem(file.getName(),actionSel,"");
				item.setTarget(new ShowFileAction(file.getPath()));
				if (!file.exists())
					item.setEnabled(false);
				addItem(item);
			}
		}
	} //}}}
	
	//{{{ RecentDirMenu class
	class RecentDirMenu extends NSMenu
	{
		public RecentDirMenu()
		{
			super();
		}
		
		public void updateMenu()
		{
			HistoryModel model = HistoryModel.getModel("vfs.browser.path");
			NSMenuItem item;
			File file;
			int max = model.getSize();
			
			int length = numberOfItems();
			for (int i=0; i
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.