PageRenderTime 58ms CodeModel.GetById 40ms app.highlight 11ms RepoModel.GetById 3ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/EditBus.java

#
Java | 190 lines | 106 code | 18 blank | 66 comment | 13 complexity | 4504081c4834caedc5b28f0e09b4cbb5 MD5 | raw file
  1/*
  2 * EditBus.java - The EditBus
  3 * Copyright (C) 1999 Slava Pestov
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20package org.gjt.sp.jedit;
 21
 22import java.util.*;
 23import org.gjt.sp.util.Log;
 24
 25/**
 26 * The EditBus is jEdit's global event notification mechanism. A number of
 27 * messages are sent by jEdit; they are all instances of the classes found
 28 * in the <code>org.gjt.sp.jedit.msg</code> package. Plugins can also send
 29 * their own messages.
 30 *
 31 * @author Slava Pestov
 32 * @version $Id: EditBus.java 3825 2001-10-02 13:54:14Z spestov $
 33 *
 34 * @since jEdit 2.2pre6
 35 */
 36public class EditBus
 37{
 38	/**
 39	 * Adds a component to the bus. It will receive all messages sent
 40	 * on the bus.
 41	 * @param comp The component to add
 42	 */
 43	public static void addToBus(EBComponent comp)
 44	{
 45		synchronized(components)
 46		{
 47			components.addElement(comp);
 48			copyComponents = null;
 49		}
 50	}
 51
 52	/**
 53	 * Removes a component from the bus.
 54	 * @param comp The component to remove
 55	 */
 56	public static void removeFromBus(EBComponent comp)
 57	{
 58		synchronized(components)
 59		{
 60			components.removeElement(comp);
 61			copyComponents = null;
 62		}
 63	}
 64
 65	/**
 66	 * Returns an array of all components connected to the bus.
 67	 */
 68	public static EBComponent[] getComponents()
 69	{
 70		synchronized(components)
 71		{
 72			if (copyComponents == null)
 73			{
 74				copyComponents = new EBComponent[components.size()];
 75				components.copyInto(copyComponents);
 76			}
 77			return copyComponents;
 78		}
 79	}
 80
 81	/**
 82	 * Sends a message to all components on the bus.
 83	 * The message will be sent to all components in turn, with the
 84	 * original sender receiving it last.
 85	 * @param message The message
 86	 */
 87	public static void send(EBMessage message)
 88	{
 89		Log.log(Log.DEBUG,EditBus.class,message.toString());
 90
 91		// To avoid any problems if components are added or removed
 92		// while the message is being sent
 93		EBComponent[] comps = getComponents();
 94
 95		for(int i = 0; i < comps.length; i++)
 96		{
 97			try
 98			{
 99				comps[i].handleMessage(message);
100				if(message.isVetoed())
101					break;
102			}
103			catch(Throwable t)
104			{
105				Log.log(Log.ERROR,EditBus.class,"Exception"
106					+ " while sending message on EditBus:");
107				Log.log(Log.ERROR,EditBus.class,t);
108			}
109		}
110	}
111
112	/**
113	 * @deprecated Do not use
114	 */
115	public static Object[] getNamedList(Object tag)
116	{
117		Object[] list = (Object[])listArrays.get(tag);
118		if(list != null)
119			return list;
120
121		Vector listVector = (Vector)listVectors.get(tag);
122		if(listVector != null)
123		{
124			list = new Object[listVector.size()];
125			listVector.copyInto(list);
126			listArrays.put(tag,list);
127			return list;
128		}
129
130		return null;
131	}
132
133	/**
134	 * @deprecated Do not use
135	 */
136	public static Enumeration getNamedLists()
137	{
138		return listVectors.keys();
139	}
140
141	/**
142	 * @deprecated For dockable windows, write a <code>dockables.xml</code>
143	 * file instead. For ErrorList error sources, use the appropriate
144	 * ErrorList APIs.
145	 */
146	public static void addToNamedList(Object tag, Object entry)
147	{
148		if(tag.equals(org.gjt.sp.jedit.gui.DockableWindow.DOCKABLE_WINDOW_LIST))
149		{
150			// clumsy backwards compatibility hack
151			org.gjt.sp.jedit.gui.DockableWindowManager
152				.registerDockableWindow((String)entry,
153				null,false,jEdit.getActionSets()[0]);
154		}
155		else
156		{
157			Vector listVector = (Vector)listVectors.get(tag);
158			if(listVector == null)
159			{
160				listVector = new Vector();
161				listVectors.put(tag,listVector);
162			}
163
164			listVector.addElement(entry);
165			listArrays.remove(tag);
166		}
167	}
168
169	/**
170	 * @deprecated Do not use.
171	 */
172	public static void removeFromNamedList(Object tag, Object entry)
173	{
174		Vector listVector = (Vector)listVectors.get(tag);
175		if(listVector == null)
176			return;
177
178		listVector.removeElement(entry);
179		listArrays.remove(tag);
180	}
181
182	// private members
183	private static Vector components = new Vector();
184	private static EBComponent[] copyComponents;
185	private static Hashtable listVectors = new Hashtable();
186	private static Hashtable listArrays = new Hashtable();
187
188	// can't create new instances
189	private EditBus() {}
190}