PageRenderTime 90ms CodeModel.GetById 85ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/EditBus.java

#
Java | 154 lines | 55 code | 11 blank | 88 comment | 3 complexity | aeb9b624b713bd5ba37d244187d9217b MD5 | raw file
  1/*
  2 * EditBus.java - The EditBus
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999 Slava Pestov
  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 org.gjt.sp.jedit;
 24
 25import java.util.*;
 26import org.gjt.sp.util.Log;
 27
 28/**
 29 * The EditBus is jEdit's global event notification mechanism. A number of
 30 * messages are sent by jEdit; they are all instances of the classes found
 31 * in the <code>org.gjt.sp.jedit.msg</code> package. Plugins can also send
 32 * their own messages.
 33 *
 34 * @author Slava Pestov
 35 * @version $Id: EditBus.java 4329 2002-08-28 20:51:29Z spestov $
 36 *
 37 * @since jEdit 2.2pre6
 38 */
 39public class EditBus
 40{
 41	//{{{ addToBus() method
 42	/**
 43	 * Adds a component to the bus. It will receive all messages sent
 44	 * on the bus.
 45	 * @param comp The component to add
 46	 */
 47	public static void addToBus(EBComponent comp)
 48	{
 49		synchronized(components)
 50		{
 51			components.add(comp);
 52			copyComponents = null;
 53		}
 54	} //}}}
 55
 56	//{{{ removeFromBus() method
 57	/**
 58	 * Removes a component from the bus.
 59	 * @param comp The component to remove
 60	 */
 61	public static void removeFromBus(EBComponent comp)
 62	{
 63		synchronized(components)
 64		{
 65			components.remove(comp);
 66			copyComponents = null;
 67		}
 68	} //}}}
 69
 70	//{{{ getComponents() method
 71	/**
 72	 * Returns an array of all components connected to the bus.
 73	 */
 74	public static EBComponent[] getComponents()
 75	{
 76		synchronized(components)
 77		{
 78			if (copyComponents == null)
 79			{
 80				copyComponents = (EBComponent[])components.toArray(
 81					new EBComponent[components.size()]);
 82			}
 83			return copyComponents;
 84		}
 85	} //}}}
 86
 87	//{{{ timeTest() method
 88	/*static long timeTest(int msgCount)
 89	{
 90		EBMessage msg = new EBMessage(null) {};
 91
 92		// To avoid any problems if components are added or removed
 93		// while the message is being sent
 94		EBComponent[] comps = getComponents();
 95
 96		long start = System.currentTimeMillis();
 97		for(int i = 0; i < msgCount; i++)
 98		{
 99			for(int j = 0; j < comps.length; j++)
100			{
101				try
102				{
103					comps[j].handleMessage(msg);
104				}
105				catch(Throwable t)
106				{
107					Log.log(Log.ERROR,EditBus.class,"Exception"
108						+ " while sending message on EditBus:");
109					Log.log(Log.ERROR,EditBus.class,t);
110				}
111			}
112		}
113
114		return System.currentTimeMillis() - start;
115	}*/ //}}}
116
117	//{{{ send() method
118	/**
119	 * Sends a message to all components on the bus.
120	 * The message will be sent to all components in turn, with the
121	 * original sender receiving it last.
122	 * @param message The message
123	 */
124	public static void send(EBMessage message)
125	{
126		Log.log(Log.DEBUG,EditBus.class,message.toString());
127
128		// To avoid any problems if components are added or removed
129		// while the message is being sent
130		EBComponent[] comps = getComponents();
131
132		for(int i = 0; i < comps.length; i++)
133		{
134			try
135			{
136				comps[i].handleMessage(message);
137			}
138			catch(Throwable t)
139			{
140				Log.log(Log.ERROR,EditBus.class,"Exception"
141					+ " while sending message on EditBus:");
142				Log.log(Log.ERROR,EditBus.class,t);
143			}
144		}
145	} //}}}
146
147	//{{{ Private members
148	private static ArrayList components = new ArrayList();
149	private static EBComponent[] copyComponents;
150
151	// can't create new instances
152	private EditBus() {}
153	//}}}
154}