PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit;
  20. import java.util.*;
  21. import org.gjt.sp.util.Log;
  22. /**
  23. * The EditBus is jEdit's global event notification mechanism. A number of
  24. * messages are sent by jEdit; they are all instances of the classes found
  25. * in the <code>org.gjt.sp.jedit.msg</code> package. Plugins can also send
  26. * their own messages.
  27. *
  28. * @author Slava Pestov
  29. * @version $Id: EditBus.java 3825 2001-10-02 13:54:14Z spestov $
  30. *
  31. * @since jEdit 2.2pre6
  32. */
  33. public class EditBus
  34. {
  35. /**
  36. * Adds a component to the bus. It will receive all messages sent
  37. * on the bus.
  38. * @param comp The component to add
  39. */
  40. public static void addToBus(EBComponent comp)
  41. {
  42. synchronized(components)
  43. {
  44. components.addElement(comp);
  45. copyComponents = null;
  46. }
  47. }
  48. /**
  49. * Removes a component from the bus.
  50. * @param comp The component to remove
  51. */
  52. public static void removeFromBus(EBComponent comp)
  53. {
  54. synchronized(components)
  55. {
  56. components.removeElement(comp);
  57. copyComponents = null;
  58. }
  59. }
  60. /**
  61. * Returns an array of all components connected to the bus.
  62. */
  63. public static EBComponent[] getComponents()
  64. {
  65. synchronized(components)
  66. {
  67. if (copyComponents == null)
  68. {
  69. copyComponents = new EBComponent[components.size()];
  70. components.copyInto(copyComponents);
  71. }
  72. return copyComponents;
  73. }
  74. }
  75. /**
  76. * Sends a message to all components on the bus.
  77. * The message will be sent to all components in turn, with the
  78. * original sender receiving it last.
  79. * @param message The message
  80. */
  81. public static void send(EBMessage message)
  82. {
  83. Log.log(Log.DEBUG,EditBus.class,message.toString());
  84. // To avoid any problems if components are added or removed
  85. // while the message is being sent
  86. EBComponent[] comps = getComponents();
  87. for(int i = 0; i < comps.length; i++)
  88. {
  89. try
  90. {
  91. comps[i].handleMessage(message);
  92. if(message.isVetoed())
  93. break;
  94. }
  95. catch(Throwable t)
  96. {
  97. Log.log(Log.ERROR,EditBus.class,"Exception"
  98. + " while sending message on EditBus:");
  99. Log.log(Log.ERROR,EditBus.class,t);
  100. }
  101. }
  102. }
  103. /**
  104. * @deprecated Do not use
  105. */
  106. public static Object[] getNamedList(Object tag)
  107. {
  108. Object[] list = (Object[])listArrays.get(tag);
  109. if(list != null)
  110. return list;
  111. Vector listVector = (Vector)listVectors.get(tag);
  112. if(listVector != null)
  113. {
  114. list = new Object[listVector.size()];
  115. listVector.copyInto(list);
  116. listArrays.put(tag,list);
  117. return list;
  118. }
  119. return null;
  120. }
  121. /**
  122. * @deprecated Do not use
  123. */
  124. public static Enumeration getNamedLists()
  125. {
  126. return listVectors.keys();
  127. }
  128. /**
  129. * @deprecated For dockable windows, write a <code>dockables.xml</code>
  130. * file instead. For ErrorList error sources, use the appropriate
  131. * ErrorList APIs.
  132. */
  133. public static void addToNamedList(Object tag, Object entry)
  134. {
  135. if(tag.equals(org.gjt.sp.jedit.gui.DockableWindow.DOCKABLE_WINDOW_LIST))
  136. {
  137. // clumsy backwards compatibility hack
  138. org.gjt.sp.jedit.gui.DockableWindowManager
  139. .registerDockableWindow((String)entry,
  140. null,false,jEdit.getActionSets()[0]);
  141. }
  142. else
  143. {
  144. Vector listVector = (Vector)listVectors.get(tag);
  145. if(listVector == null)
  146. {
  147. listVector = new Vector();
  148. listVectors.put(tag,listVector);
  149. }
  150. listVector.addElement(entry);
  151. listArrays.remove(tag);
  152. }
  153. }
  154. /**
  155. * @deprecated Do not use.
  156. */
  157. public static void removeFromNamedList(Object tag, Object entry)
  158. {
  159. Vector listVector = (Vector)listVectors.get(tag);
  160. if(listVector == null)
  161. return;
  162. listVector.removeElement(entry);
  163. listArrays.remove(tag);
  164. }
  165. // private members
  166. private static Vector components = new Vector();
  167. private static EBComponent[] copyComponents;
  168. private static Hashtable listVectors = new Hashtable();
  169. private static Hashtable listArrays = new Hashtable();
  170. // can't create new instances
  171. private EditBus() {}
  172. }