PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 166 lines | 67 code | 10 blank | 89 comment | 6 complexity | 4f960c230c1ec6deab710dabf3d0f9ba 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. * :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. package org.gjt.sp.jedit;
  23. import java.util.*;
  24. import org.gjt.sp.util.Log;
  25. /**
  26. * jEdit's global event notification mechanism.<p>
  27. *
  28. * Plugins register with the EditBus to receive messages reflecting
  29. * changes in the application's state, including changes in buffers,
  30. * views and edit panes, changes in the set of properties maintained
  31. * by the application, and the closing of the application.<p>
  32. *
  33. * The EditBus maintains a list of objects that have requested to receive
  34. * messages. When a message is sent using this class, all registered
  35. * components receive it in turn. Classes for objects that subscribe to
  36. * the EditBus must implement the {@link EBComponent} interface, which
  37. * defines the single method {@link EBComponent#handleMessage(EBMessage)}.<p>
  38. *
  39. * A plugin core class that extends the
  40. * {@link EBPlugin} abstract class (and whose name ends with
  41. * <code>Plugin</code> for identification purposes) will automatically be
  42. * added to the EditBus during jEdit's startup routine. Any other
  43. * class - for example, a dockable window that needs to receive
  44. * notification of buffer changes - must perform its own registration by calling
  45. * {@link #addToBus(EBComponent)} during its initialization.
  46. * A convenient place to register in a class derived from <code>JComponent</code>
  47. * would be in an implementation of the <code>JComponent</code> method
  48. * <code>addNotify()</code>.<p>
  49. *
  50. * Message types sent by jEdit can be found in the
  51. * {@link org.gjt.sp.jedit.msg} package.<p>
  52. *
  53. * Plugins can also send their own messages - any object can send a message to
  54. * the EditBus by calling the static method {@link #send(EBMessage)}.
  55. * Most plugins, however, only concern themselves with receiving, not
  56. * sending, messages.
  57. *
  58. * @see org.gjt.sp.jedit.EBComponent
  59. * @see org.gjt.sp.jedit.EBMessage
  60. *
  61. * @author Slava Pestov
  62. * @author John Gellene (API documentation)
  63. * @version $Id: EditBus.java 4669 2003-05-01 02:21:27Z spestov $
  64. *
  65. * @since jEdit 2.2pre6
  66. */
  67. public class EditBus
  68. {
  69. //{{{ addToBus() method
  70. /**
  71. * Adds a component to the bus. It will receive all messages sent
  72. * on the bus.
  73. *
  74. * @param comp The component to add
  75. */
  76. public static void addToBus(EBComponent comp)
  77. {
  78. synchronized(components)
  79. {
  80. components.add(comp);
  81. copyComponents = null;
  82. }
  83. } //}}}
  84. //{{{ removeFromBus() method
  85. /**
  86. * Removes a component from the bus.
  87. * @param comp The component to remove
  88. */
  89. public static void removeFromBus(EBComponent comp)
  90. {
  91. synchronized(components)
  92. {
  93. components.remove(comp);
  94. copyComponents = null;
  95. }
  96. } //}}}
  97. //{{{ getComponents() method
  98. /**
  99. * Returns an array of all components connected to the bus.
  100. */
  101. public static EBComponent[] getComponents()
  102. {
  103. synchronized(components)
  104. {
  105. if (copyComponents == null)
  106. {
  107. copyComponents = (EBComponent[])components.toArray(
  108. new EBComponent[components.size()]);
  109. }
  110. return copyComponents;
  111. }
  112. } //}}}
  113. //{{{ send() method
  114. /**
  115. * Sends a message to all components on the bus in turn.
  116. * @param message The message
  117. */
  118. public static void send(EBMessage message)
  119. {
  120. Log.log(Log.DEBUG,EditBus.class,message.toString());
  121. // To avoid any problems if components are added or removed
  122. // while the message is being sent
  123. EBComponent[] comps = getComponents();
  124. for(int i = 0; i < comps.length; i++)
  125. {
  126. try
  127. {
  128. EBComponent comp = comps[i];
  129. if(Debug.EB_TIMER)
  130. {
  131. long start = System.currentTimeMillis();
  132. comp.handleMessage(message);
  133. long time = (System.currentTimeMillis() - start);
  134. if(time != 0)
  135. {
  136. Log.log(Log.DEBUG,EditBus.class,comp + ": " + time + " ms");
  137. }
  138. }
  139. else
  140. comps[i].handleMessage(message);
  141. }
  142. catch(Throwable t)
  143. {
  144. Log.log(Log.ERROR,EditBus.class,"Exception"
  145. + " while sending message on EditBus:");
  146. Log.log(Log.ERROR,EditBus.class,t);
  147. }
  148. }
  149. } //}}}
  150. //{{{ Private members
  151. private static ArrayList components = new ArrayList();
  152. private static EBComponent[] copyComponents;
  153. // can't create new instances
  154. private EditBus() {}
  155. //}}}
  156. }