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

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/JEditActionContext.java

#
Java | 181 lines | 78 code | 15 blank | 88 comment | 10 complexity | 9adae4e2e716b5895c5ba967d9299994 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. * JEditActionContext.java - For code sharing between jEdit and VFSBrowser
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1998, 2003 Slava Pestov
  7. * Portions copyright (C) 2007 Matthieu Casanova
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. package org.gjt.sp.jedit;
  24. import org.gjt.sp.util.StandardUtilities;
  25. import java.lang.reflect.Array;
  26. import java.util.*;
  27. /**
  28. * Manages a collection of action sets. There are two instances of this class
  29. * in jEdit:
  30. * <ul>
  31. * <li>{@link org.gjt.sp.jedit.jEdit#getActionContext()} - editor actions
  32. * <li>{@link org.gjt.sp.jedit.browser.VFSBrowser#getActionContext()} - browser
  33. * actions
  34. * </ul>
  35. *
  36. * @since jEdit 4.3pre13
  37. * @author Slava Pestov
  38. * @version $Id: ActionContext.java 6884 2006-09-06 02:38:55Z ezust $
  39. */
  40. public abstract class JEditActionContext<F extends JEditAbstractEditAction, E extends JEditActionSet<F>>
  41. {
  42. //{{{ invokeAction() method
  43. /**
  44. * Invokes the given action in response to a user-generated event.
  45. * @param evt The event
  46. * @param action The action
  47. * @since jEdit 4.3pre13
  48. */
  49. public abstract void invokeAction(EventObject evt, F action);
  50. //}}}
  51. //{{{ addActionSet() method
  52. /**
  53. * Adds a new action set to the context.
  54. * @since jEdit 4.3pre13
  55. */
  56. public void addActionSet(E actionSet)
  57. {
  58. actionNames = null;
  59. actionSets.addElement(actionSet);
  60. actionSet.context = this;
  61. String[] actions = actionSet.getActionNames();
  62. for(int i = 0; i < actions.length; i++)
  63. {
  64. /* Is it already there? */
  65. if (actionHash.containsKey(actions[i]))
  66. {
  67. /* Save it for plugin unloading time */
  68. E oldAction = actionHash.get(actions[i]);
  69. overriddenActions.put(actions[i], oldAction);
  70. }
  71. actionHash.put(actions[i],actionSet);
  72. }
  73. } //}}}
  74. //{{{ removeActionSet() method
  75. /**
  76. * Removes an action set from the context.
  77. * @since jEdit 4.23pre13
  78. */
  79. public void removeActionSet(E actionSet)
  80. {
  81. actionNames = null;
  82. actionSets.removeElement(actionSet);
  83. actionSet.context = null;
  84. String[] actions = actionSet.getActionNames();
  85. for(int i = 0; i < actions.length; i++)
  86. {
  87. actionHash.remove(actions[i]);
  88. if (overriddenActions.containsKey(actions[i]))
  89. {
  90. E oldAction = overriddenActions.remove(actions[i]);
  91. actionHash.put(actions[i], oldAction);
  92. }
  93. }
  94. } //}}}
  95. //{{{ getActionSets() method
  96. /**
  97. * Returns all registered action sets.
  98. * @since jEdit 4.3pre13
  99. */
  100. public E[] getActionSets()
  101. {
  102. if (actionSets.isEmpty())
  103. return null;
  104. Class clazz = actionSets.get(0).getClass();
  105. E[] retVal =(E[]) Array.newInstance(clazz, actionSets.size());
  106. actionSets.copyInto(retVal);
  107. return retVal;
  108. } //}}}
  109. //{{{ getAction() method
  110. /**
  111. * Returns the specified action.
  112. * @param name The action name
  113. * @return a JEditAbstractEditAction or null if it doesn't exist
  114. * @since jEdit 4.3pre13
  115. */
  116. public F getAction(String name)
  117. {
  118. E set = actionHash.get(name);
  119. if(set == null)
  120. return null;
  121. else
  122. return set.getAction(name);
  123. } //}}}
  124. //{{{ getActionSetForAction() method
  125. /**
  126. * Returns the action set that contains the specified action.
  127. *
  128. * @param action The action
  129. * @return the actionSet that contains the given action
  130. * @since jEdit 4.3pre13
  131. */
  132. public E getActionSetForAction(String action)
  133. {
  134. return actionHash.get(action);
  135. } //}}}
  136. //{{{ getActionNames() method
  137. /**
  138. * Returns all registered action names.
  139. */
  140. public String[] getActionNames()
  141. {
  142. if(actionNames == null)
  143. {
  144. List<String> vec = new LinkedList<String>();
  145. for(int i = 0; i < actionSets.size(); i++)
  146. (actionSets.elementAt(i)).getActionNames(vec);
  147. actionNames = vec.toArray(new String[vec.size()]);
  148. Arrays.sort(actionNames,
  149. new StandardUtilities.StringCompare<String>(true));
  150. }
  151. return actionNames;
  152. } //}}}
  153. //{{{ Package-private members
  154. String[] actionNames;
  155. /**
  156. * This map contains as key an action name,
  157. * and as value the JEditActionSet that contains this action
  158. */
  159. Hashtable<String, E> actionHash = new Hashtable<String, E>();
  160. /** A map of built-in actions that were overridden by plugins. */
  161. Hashtable<String, E> overriddenActions = new Hashtable<String, E>();
  162. //}}}
  163. //{{{ Private members
  164. private final Vector<E> actionSets = new Vector<E>();
  165. //}}}
  166. }