PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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
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. * 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 4329 2002-08-28 20:51:29Z spestov $
  33. *
  34. * @since jEdit 2.2pre6
  35. */
  36. public class EditBus
  37. {
  38. //{{{ addToBus() method
  39. /**
  40. * Adds a component to the bus. It will receive all messages sent
  41. * on the bus.
  42. * @param comp The component to add
  43. */
  44. public static void addToBus(EBComponent comp)
  45. {
  46. synchronized(components)
  47. {
  48. components.add(comp);
  49. copyComponents = null;
  50. }
  51. } //}}}
  52. //{{{ removeFromBus() method
  53. /**
  54. * Removes a component from the bus.
  55. * @param comp The component to remove
  56. */
  57. public static void removeFromBus(EBComponent comp)
  58. {
  59. synchronized(components)
  60. {
  61. components.remove(comp);
  62. copyComponents = null;
  63. }
  64. } //}}}
  65. //{{{ getComponents() method
  66. /**
  67. * Returns an array of all components connected to the bus.
  68. */
  69. public static EBComponent[] getComponents()
  70. {
  71. synchronized(components)
  72. {
  73. if (copyComponents == null)
  74. {
  75. copyComponents = (EBComponent[])components.toArray(
  76. new EBComponent[components.size()]);
  77. }
  78. return copyComponents;
  79. }
  80. } //}}}
  81. //{{{ timeTest() method
  82. /*static long timeTest(int msgCount)
  83. {
  84. EBMessage msg = new EBMessage(null) {};
  85. // To avoid any problems if components are added or removed
  86. // while the message is being sent
  87. EBComponent[] comps = getComponents();
  88. long start = System.currentTimeMillis();
  89. for(int i = 0; i < msgCount; i++)
  90. {
  91. for(int j = 0; j < comps.length; j++)
  92. {
  93. try
  94. {
  95. comps[j].handleMessage(msg);
  96. }
  97. catch(Throwable t)
  98. {
  99. Log.log(Log.ERROR,EditBus.class,"Exception"
  100. + " while sending message on EditBus:");
  101. Log.log(Log.ERROR,EditBus.class,t);
  102. }
  103. }
  104. }
  105. return System.currentTimeMillis() - start;
  106. }*/ //}}}
  107. //{{{ send() method
  108. /**
  109. * Sends a message to all components on the bus.
  110. * The message will be sent to all components in turn, with the
  111. * original sender receiving it last.
  112. * @param message The message
  113. */
  114. public static void send(EBMessage message)
  115. {
  116. Log.log(Log.DEBUG,EditBus.class,message.toString());
  117. // To avoid any problems if components are added or removed
  118. // while the message is being sent
  119. EBComponent[] comps = getComponents();
  120. for(int i = 0; i < comps.length; i++)
  121. {
  122. try
  123. {
  124. comps[i].handleMessage(message);
  125. }
  126. catch(Throwable t)
  127. {
  128. Log.log(Log.ERROR,EditBus.class,"Exception"
  129. + " while sending message on EditBus:");
  130. Log.log(Log.ERROR,EditBus.class,t);
  131. }
  132. }
  133. } //}}}
  134. //{{{ Private members
  135. private static ArrayList components = new ArrayList();
  136. private static EBComponent[] copyComponents;
  137. // can't create new instances
  138. private EditBus() {}
  139. //}}}
  140. }