/jEdit/branches/4.3.x/org/gjt/sp/jedit/EditAction.java

# · Java · 224 lines · 78 code · 18 blank · 128 comment · 4 complexity · b4cb9f796b508284fa3332c227e1fa1e MD5 · raw file

  1. /*
  2. * EditAction.java - jEdit action listener
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1998, 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 org.gjt.sp.util.Log;
  25. import java.awt.Component;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. //}}}
  29. /**
  30. * An action that can be bound to a menu item, tool bar button or keystroke.
  31. *
  32. * @see jEdit#getAction(String)
  33. * @see jEdit#getActionNames()
  34. * @see ActionSet
  35. *
  36. * @author Slava Pestov
  37. * @version $Id: EditAction.java 16334 2009-10-14 09:31:11Z kpouer $
  38. */
  39. public abstract class EditAction extends JEditAbstractEditAction<View>
  40. {
  41. //{{{ EditAction constructors
  42. /**
  43. * Creates a new edit action with the specified name.
  44. * @param name The action name
  45. */
  46. public EditAction(String name)
  47. {
  48. super(name);
  49. }
  50. public EditAction(String name, Object[] newArgs)
  51. {
  52. super(name, newArgs);
  53. } //}}}
  54. //{{{ getLabel() method
  55. /**
  56. * Returns the action's label. This returns the
  57. * value of the property named by {@link #getName()} suffixed
  58. * with <code>.label</code>.
  59. *
  60. */
  61. public String getLabel()
  62. {
  63. if (args != null)
  64. {
  65. return jEdit.getProperty(name + ".label", args);
  66. }
  67. return jEdit.getProperty(name + ".label");
  68. } //}}}
  69. //{{{ getMouseOverText() method
  70. /**
  71. * Returns the action's mouse over message. This returns the
  72. * value of the property named by {@link #getName()} suffixed
  73. * with <code>.mouse-over</code>.
  74. */
  75. public final String getMouseOverText()
  76. {
  77. return jEdit.getProperty(name + ".mouse-over");
  78. } //}}}
  79. //{{{ invoke() method
  80. /**
  81. * Invokes the action. This is an implementation of the Command pattern,
  82. * and concrete actions should override this.
  83. *
  84. * @param view The view
  85. * @since jEdit 2.7pre2
  86. * abstract since jEdit 4.3pre7
  87. */
  88. abstract public void invoke(View view);
  89. //{{{ getView() method
  90. /**
  91. * @deprecated Call <code>GUIUtilities.getView()</code> instead.
  92. */
  93. public static View getView(Component comp)
  94. {
  95. // moved to GUIUtilities as it makes no sense being here.
  96. return GUIUtilities.getView(comp);
  97. } //}}}
  98. //{{{ isToggle() method
  99. /**
  100. * Returns if this edit action should be displayed as a check box
  101. * in menus. This returns the
  102. * value of the property named by {@link #getName()} suffixed
  103. * with <code>.toggle</code>.
  104. *
  105. * @since jEdit 2.2pre4
  106. */
  107. public final boolean isToggle()
  108. {
  109. return jEdit.getBooleanProperty(name + ".toggle");
  110. } //}}}
  111. //{{{ isSelected() method
  112. /**
  113. * If this edit action is a toggle, returns if it is selected or not.
  114. * @param comp The component
  115. * @since jEdit 4.2pre1
  116. */
  117. public boolean isSelected(Component comp)
  118. {
  119. return false;
  120. } //}}}
  121. //{{{ noRepeat() method
  122. /**
  123. * Returns if this edit action should not be repeated. Returns false
  124. * by default.
  125. * @since jEdit 2.7pre2
  126. */
  127. public boolean noRepeat()
  128. {
  129. return false;
  130. } //}}}
  131. //{{{ noRecord() method
  132. /**
  133. * Returns if this edit action should not be recorded. Returns false
  134. * by default.
  135. * @since jEdit 2.7pre2
  136. */
  137. public boolean noRecord()
  138. {
  139. return false;
  140. } //}}}
  141. //{{{ noRememberLast() method
  142. /**
  143. * Returns if this edit action should not be remembered as the most
  144. * recently invoked action.
  145. * @since jEdit 4.2pre1
  146. */
  147. public boolean noRememberLast()
  148. {
  149. return false;
  150. } //}}}
  151. //{{{ getCode() method
  152. /**
  153. * Returns the BeanShell code that will replay this action.
  154. * BeanShellAction.getCode() returns something more interesting for Actions that were loaded
  155. * from the actions.xml file.
  156. * You do not need to override this method if your action name is unique,
  157. * this EditAction was added to an ActionSet and that to an ActionContext of jEdit.
  158. *
  159. * concrete since jEdit 4.3pre7
  160. * @since jEdit 2.7pre2
  161. *
  162. */
  163. public String getCode()
  164. {
  165. return "jEdit.getAction(" + name + ").invoke(view); ";
  166. }
  167. //}}}
  168. //{{{ Wrapper class
  169. /**
  170. * 'Wrap' EditActions in this class to turn them into AWT
  171. * ActionListeners, that can be attached to buttons, menu items, etc.
  172. */
  173. public static class Wrapper implements ActionListener
  174. {
  175. private final ActionContext context;
  176. private final String actionName;
  177. /**
  178. * Creates a new action listener wrapper.
  179. * @since jEdit 4.2pre1
  180. */
  181. public Wrapper(ActionContext context, String actionName)
  182. {
  183. this.context = context;
  184. this.actionName = actionName;
  185. }
  186. /**
  187. * Called when the user selects this action from a menu.
  188. * It passes the action through the
  189. * {@link org.gjt.sp.jedit.gui.InputHandler#invokeAction(EditAction)}
  190. * method, which performs any recording or repeating.
  191. *
  192. * @param evt The action event
  193. */
  194. public void actionPerformed(ActionEvent evt)
  195. {
  196. EditAction action = context.getAction(actionName);
  197. if(action == null)
  198. {
  199. Log.log(Log.WARNING,this,"Unknown action: "
  200. + actionName);
  201. }
  202. else
  203. context.invokeAction(evt,action);
  204. }
  205. } //}}}
  206. }