/jEdit/tags/jedit-4-4-2/org/gjt/sp/jedit/ActionSet.java

# · Java · 335 lines · 109 code · 20 blank · 206 comment · 9 complexity · eec70c9f2824e14dacfc7405e0e42041 MD5 · raw file

  1. /*
  2. * ActionSet.java - A set of actions
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001, 2003 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. //{{{ imports
  24. import java.net.URL;
  25. import java.util.*;
  26. import org.gjt.sp.jedit.input.AbstractInputHandler;
  27. import org.gjt.sp.util.Log;
  28. //}}}
  29. /**
  30. * A set of actions, either loaded from an XML file, or constructed at runtime
  31. * by a plugin. <p>
  32. *
  33. * <h3>Action sets loaded from XML files</h3>
  34. *
  35. * Action sets are read from these files inside the plugin JAR:
  36. * <ul>
  37. * <li><code>actions.xml</code> - actions made available for use in jEdit views,
  38. * including the view's <b>Plugins</b> menu, the tool bar, etc.</li>
  39. * <li><code>browser.actions.xml</code> - actions for the file system browser's
  40. * <b>Plugins</b> menu.</li>
  41. * </ul>
  42. *
  43. * An action definition file has the following form:
  44. *
  45. * <pre>&lt;?xml version="1.0"?&gt;
  46. *&lt;!DOCTYPE ACTIONS SYSTEM "actions.dtd"&gt;
  47. *&lt;ACTIONS&gt;
  48. * &lt;ACTION NAME="some-action"&gt;
  49. * &lt;CODE&gt;
  50. * // BeanShell code evaluated when the action is invoked
  51. * &lt;/CODE&gt;
  52. * &lt;/ACTION&gt;
  53. * &lt;ACTION NAME="some-toggle-action"&gt;
  54. * &lt;CODE&gt;
  55. * // BeanShell code evaluated when the action is invoked
  56. * &lt;/CODE&gt;
  57. * &lt;IS_SELECTED&gt;
  58. * // BeanShell code that should evaluate to true or false
  59. * &lt;/IS_SELECTED&gt;
  60. * &lt;/ACTION&gt;
  61. *&lt;/ACTIONS&gt;</pre>
  62. *
  63. * The following elements are valid:
  64. *
  65. * <ul>
  66. * <li>
  67. * <code>ACTIONS</code> is the top-level element and refers
  68. * to the set of actions used by the plugin.
  69. * </li>
  70. * <li>
  71. * An <code>ACTION</code> contains the data for a particular action.
  72. * It has three attributes: a required <code>NAME</code>;
  73. * an optional <code>NO_REPEAT</code>, which is a flag
  74. * indicating whether the action should not be repeated with the
  75. * <b>C+ENTER</b> command; and an optional
  76. * <code>NO_RECORD</code> which is a a flag indicating whether the
  77. * action should be recorded if it is invoked while the user is recording a
  78. * macro. The two flag attributes
  79. * can have two possible values, "TRUE" or
  80. * "FALSE". In both cases, "FALSE" is the
  81. * default if the attribute is not specified.
  82. * </li>
  83. * <li>
  84. * An <code>ACTION</code> can have two child elements
  85. * within it: a required <code>CODE</code> element which
  86. * specifies the
  87. * BeanShell code that will be executed when the action is invoked,
  88. * and an optional <code>IS_SELECTED</code> element, used for
  89. * checkbox
  90. * menu items. The <code>IS_SELECTED</code> element contains
  91. * BeanShell code that returns a boolean flag that will
  92. * determine the state of the checkbox.
  93. * </li>
  94. * </ul>
  95. *
  96. * Each action must have a property <code><i>name</i>.label</code> containing
  97. * the action's menu item label.
  98. *
  99. * <h3>View actions</h3>
  100. *
  101. * Actions defined in <code>actions.xml</code> can be added to the view's
  102. * <b>Plugins</b> menu; see {@link EditPlugin}.
  103. * The action code may use any standard predefined
  104. * BeanShell variable; see {@link BeanShell}.
  105. *
  106. * <h3>File system browser actions</h3>
  107. *
  108. * Actions defined in <code>actions.xml</code> can be added to the file
  109. * system browser's <b>Plugins</b> menu; see {@link EditPlugin}.
  110. * The action code may use any standard predefined
  111. * BeanShell variable, in addition to a variable <code>browser</code> which
  112. * contains a reference to the current
  113. * {@link org.gjt.sp.jedit.browser.VFSBrowser} instance.<p>
  114. *
  115. * File system browser actions should not define
  116. * <code>&lt;IS_SELECTED&gt;</code> blocks.
  117. *
  118. * <h3>Custom action sets</h3>
  119. *
  120. * Call {@link jEdit#addActionSet(ActionSet)} to add a custom action set to
  121. * jEdit's action context. You must also call {@link #initKeyBindings()} for new
  122. * action sets. Don't forget to call {@link jEdit#removeActionSet(ActionSet)}
  123. * before your plugin is unloaded, too.
  124. *
  125. * @see jEdit#getActionContext()
  126. * @see org.gjt.sp.jedit.browser.VFSBrowser#getActionContext()
  127. * @see ActionContext#getActionNames()
  128. * @see ActionContext#getAction(String)
  129. * @see jEdit#addActionSet(ActionSet)
  130. * @see jEdit#removeActionSet(ActionSet)
  131. * @see PluginJAR#getActionSet()
  132. * @see BeanShell
  133. * @see View
  134. *
  135. * @author Slava Pestov
  136. * @author John Gellene (API documentation)
  137. * @version $Id: ActionSet.java 16333 2009-10-14 09:30:00Z kpouer $
  138. * @since jEdit 4.0pre1
  139. */
  140. public class ActionSet extends JEditActionSet<EditAction> implements Comparable
  141. {
  142. //{{{ ActionSet constructor
  143. /**
  144. * Creates a new action set.
  145. * @since jEdit 4.0pre1
  146. */
  147. public ActionSet()
  148. {
  149. label = "<no label set; plugin bug>";
  150. } //}}}
  151. //{{{ ActionSet constructor
  152. /**
  153. * Creates a new action set.
  154. * @param plugin The plugin
  155. * @param cachedActionNames The list of cached action names
  156. * @param cachedActionToggleFlags The list of cached action toggle flags
  157. * @param uri The actions.xml URI
  158. * @since jEdit 4.2pre2
  159. */
  160. public ActionSet(PluginJAR plugin, String[] cachedActionNames,
  161. boolean[] cachedActionToggleFlags, URL uri)
  162. {
  163. this();
  164. this.plugin = plugin;
  165. this.uri = uri;
  166. if(cachedActionNames != null)
  167. {
  168. for(int i = 0; i < cachedActionNames.length; i++)
  169. {
  170. actions.put(cachedActionNames[i],placeholder);
  171. jEdit.setTemporaryProperty(cachedActionNames[i]
  172. + ".toggle",cachedActionToggleFlags[i]
  173. ? "true" : "false");
  174. }
  175. }
  176. loaded = false;
  177. } //}}}
  178. //{{{ addAction() method
  179. /**
  180. * Adds an action to the action set.
  181. * It exists for binary compatibility issues
  182. * @param action The action
  183. * @since jEdit 4.0pre1
  184. */
  185. @Override
  186. public void addAction(EditAction action)
  187. {
  188. super.addAction(action);
  189. } //}}}
  190. //{{{ getArray() method
  191. protected EditAction[] getArray(int size)
  192. {
  193. return new EditAction[size];
  194. } //}}}
  195. //{{{ getActions() method
  196. /**
  197. * Returns an array of all actions in this action set.<p>
  198. *
  199. * <b>Deferred loading:</b> this will load the action set if necessary.
  200. *
  201. * @since jEdit 4.0pre1
  202. */
  203. @Override
  204. public EditAction[] getActions()
  205. {
  206. return super.getActions();
  207. } //}}}
  208. //{{{ ActionSet constructor
  209. /**
  210. * Creates a new action set.
  211. * @param label The label, shown in the shortcuts option pane
  212. * @since jEdit 4.0pre1
  213. */
  214. public ActionSet(String label)
  215. {
  216. this();
  217. setLabel(label);
  218. } //}}}
  219. //{{{ getLabel() method
  220. /**
  221. * Return the action source label.
  222. * @since jEdit 4.0pre1
  223. */
  224. public String getLabel()
  225. {
  226. return label;
  227. } //}}}
  228. //{{{ setLabel() method
  229. /**
  230. * Sets the action source label.
  231. * @param label The label
  232. * @since jEdit 4.0pre1
  233. */
  234. public void setLabel(String label)
  235. {
  236. if(label == null)
  237. throw new NullPointerException();
  238. this.label = label;
  239. } //}}}
  240. //{{{ getPluginJAR() method
  241. /**
  242. * Return the plugin this action set was loaded from, or null.
  243. * @since jEdit 4.2pre13
  244. */
  245. public PluginJAR getPluginJAR()
  246. {
  247. return plugin;
  248. } //}}}
  249. //{{{ getCacheableActionNames() method
  250. /**
  251. * Returns an array of all action names in this action set that should
  252. * be cached; namely, <code>BeanShellAction</code>s.
  253. * @since jEdit 4.2pre1
  254. */
  255. @Override
  256. public String[] getCacheableActionNames()
  257. {
  258. LinkedList<String> retVal = new LinkedList<String>();
  259. Enumeration e = actions.elements();
  260. while(e.hasMoreElements())
  261. {
  262. Object obj = e.nextElement();
  263. if(obj == placeholder)
  264. {
  265. // ??? this should only be called with
  266. // fully loaded action set
  267. Log.log(Log.WARNING,this,"Action set not up "
  268. + "to date");
  269. }
  270. else if(obj instanceof BeanShellAction)
  271. retVal.add(((BeanShellAction)obj).getName());
  272. }
  273. return retVal.toArray(new String[retVal.size()]);
  274. } //}}}
  275. //{{{ getProperty() method
  276. protected String getProperty(String name)
  277. {
  278. return jEdit.getProperty(name);
  279. } //}}}
  280. //{{{ getInputHandler() method
  281. public AbstractInputHandler getInputHandler()
  282. {
  283. return jEdit.getInputHandler();
  284. } //}}}
  285. //{{{ compareTo() method
  286. public int compareTo(Object o)
  287. {
  288. return label.compareTo(((ActionSet)o).label);
  289. }//}}}
  290. //{{{ toString() method
  291. @Override
  292. public String toString()
  293. {
  294. return label;
  295. } //}}}
  296. //{{{ createBeanShellAction() method
  297. /**
  298. * Creates a BeanShellAction.
  299. * @since 4.3pre13
  300. */
  301. protected EditAction createBeanShellAction(String actionName,
  302. String code,
  303. String selected,
  304. boolean noRepeat,
  305. boolean noRecord,
  306. boolean noRememberLast)
  307. {
  308. return new BeanShellAction(actionName,code,selected,noRepeat,noRecord,noRememberLast);
  309. }
  310. //}}}
  311. //{{{ Private members
  312. private String label;
  313. private PluginJAR plugin;
  314. //}}}
  315. }