/plugins/AntFarm/tags/antfarm-0_5_1/plugin/integration/IntegrationManager.java

# · Java · 284 lines · 135 code · 32 blank · 117 comment · 22 complexity · 810c70995f7f27abbe560e5910a6d024 MD5 · raw file

  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package plugin.integration;
  55. import jEditException;
  56. import java.util.*;
  57. import org.gjt.sp.jedit.*;
  58. import org.gjt.sp.jedit.msg.*;
  59. import org.gjt.sp.util.Log;
  60. /**
  61. * A manager for checking possible plugin to plugin integration.
  62. *
  63. *@author steinbeck
  64. *@created 27. August 2001
  65. */
  66. public class IntegrationManager
  67. implements EBComponent
  68. {
  69. private EditPlugin srcPlugin;
  70. private Map bridges;
  71. private List loadedBridges;
  72. private boolean listening;
  73. private boolean editorStarted;
  74. /**
  75. * Create a new <code>IntegrationManager</code>.
  76. *
  77. *@param aSrcPlugin The plugin which wants to communicate with another plugin
  78. */
  79. public IntegrationManager(EditPlugin aSrcPlugin)
  80. {
  81. srcPlugin = aSrcPlugin;
  82. bridges = new HashMap(2);
  83. loadedBridges = new ArrayList(2);
  84. listening = false;
  85. editorStarted = false;
  86. }
  87. /**
  88. * Handles a message sent on the EditBus.
  89. *
  90. *@param message The message
  91. */
  92. public void handleMessage(EBMessage message)
  93. {
  94. if (message instanceof EditorStarted)
  95. {
  96. checkBridges();
  97. editorStarted = true;
  98. }
  99. if (!editorStarted)
  100. {
  101. return;
  102. }
  103. if (message instanceof ViewUpdate)
  104. {
  105. ViewUpdate update = (ViewUpdate) message;
  106. if (update.getWhat() == ViewUpdate.CREATED)
  107. {
  108. enableBridges(update.getView());
  109. }
  110. }
  111. if (message instanceof CreateDockableWindow)
  112. {
  113. enableBridges(((CreateDockableWindow) message).getView());
  114. }
  115. }
  116. /**
  117. * Add a bridge. A bridge is an object that bridges the APIs of the source
  118. * plugin to the target plugin. The specified bridge will be ignored if the
  119. * target plugin does not exist.
  120. *
  121. *@param targetPlugin The feature to be added to the Bridge attribute
  122. *@param bridgeClass The feature to be added to the Bridge attribute
  123. */
  124. public void addBridge(String targetPlugin, String bridgeClass)
  125. {
  126. if (editorStarted && jEdit.getPlugin(targetPlugin) == null)
  127. {
  128. return;
  129. }
  130. bridges.put(targetPlugin, bridgeClass);
  131. listen();
  132. }
  133. /**
  134. * Returns <code>true</code> if the given key identifies a loaded bridge.
  135. *
  136. *@param pluginKey The key identifying the bridge
  137. *@return True, if the bridge is loaded
  138. */
  139. private boolean isLoadedBridge(String pluginKey)
  140. {
  141. return loadedBridges.contains(pluginKey);
  142. }
  143. /**
  144. * Check bridges to see if the required plugin exists. If not, throw away the
  145. * bridge.
  146. */
  147. private void checkBridges()
  148. {
  149. for (Iterator i = bridges.keySet().iterator(); i.hasNext(); )
  150. {
  151. if (jEdit.getPlugin((String) i.next()) == null)
  152. {
  153. i.remove();
  154. }
  155. }
  156. if (loadedBridges.size() == bridges.size())
  157. {
  158. ignore();
  159. }
  160. }
  161. /**
  162. * Check for possible bridge points, enabling the bridges if the target
  163. * plugins are present.
  164. *
  165. *@param view A view ?!
  166. */
  167. private void enableBridges(View view)
  168. {
  169. for (Iterator i = bridges.keySet().iterator(); i.hasNext(); )
  170. {
  171. String each = (String) i.next();
  172. if (isLoadedBridge(each))
  173. {
  174. continue;
  175. }
  176. EditPlugin plugin = jEdit.getPlugin(each);
  177. try
  178. {
  179. if (plugin != null)
  180. {
  181. enableBridge(each, plugin, view);
  182. }
  183. }
  184. catch (jEditException e)
  185. {
  186. Log.log(Log.WARNING, this, e);
  187. }
  188. }
  189. }
  190. /**
  191. * Listen to the bus.
  192. */
  193. private void listen()
  194. {
  195. if (!listening)
  196. {
  197. EditBus.addToBus(this);
  198. }
  199. listening = true;
  200. //Log.log( Log.DEBUG, this, "Listening to the bus..." );
  201. }
  202. /**
  203. * Ignore the bus.
  204. */
  205. private void ignore()
  206. {
  207. EditBus.removeFromBus(this);
  208. //Log.log( Log.DEBUG, this, "Ignoring to the bus..." );
  209. }
  210. /**
  211. * Enable a bridge.
  212. *
  213. *@param pluginKey A key for the plugin
  214. *@param tgtPlugin The target plugin
  215. *@param view The view ?!
  216. *@exception jEditException An exception if something goes wrong.
  217. */
  218. private void enableBridge(String pluginKey, EditPlugin tgtPlugin, View view)
  219. throws jEditException
  220. {
  221. PluginBridge bridge = loadBridge(pluginKey);
  222. if (bridge.enable(srcPlugin, tgtPlugin, view))
  223. {
  224. loadedBridges.add(pluginKey);
  225. //Log.log( Log.DEBUG, this, srcPlugin.getClass() + " to " + tgtPlugin.getClass() + " enabled" );
  226. if (loadedBridges.size() == bridges.size())
  227. {
  228. ignore();
  229. }
  230. }
  231. }
  232. /**
  233. * Load a bridge.
  234. *
  235. *@param pluginKey A plugin key
  236. *@return The plugin bridge for the given key
  237. *@exception jEditException An Exception if something goes wrong
  238. */
  239. private PluginBridge loadBridge(String pluginKey)
  240. throws jEditException
  241. {
  242. try
  243. {
  244. String bridgeClass = (String) bridges.get(pluginKey);
  245. return (PluginBridge) Class.forName(bridgeClass).newInstance();
  246. }
  247. catch (Throwable t)
  248. {
  249. throw new jEditException("Unable to load plugin bridge", t);
  250. }
  251. }
  252. }