PageRenderTime 33ms CodeModel.GetById 26ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 222 lines | 79 code | 19 blank | 124 comment | 2 complexity | 4550d9627ff1e8e7001b5bfe28cec56a 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
 23package org.gjt.sp.jedit;
 24
 25//{{{ Imports
 26import java.awt.event.ActionEvent;
 27import java.awt.event.ActionListener;
 28import java.awt.Component;
 29import org.gjt.sp.jedit.gui.InputHandler;
 30import org.gjt.sp.util.Log;
 31//}}}
 32
 33/**
 34 * An action that can be bound to a menu item, tool bar button or keystroke.
 35 *
 36 * @see jEdit#getAction(String)
 37 * @see jEdit#getActionNames()
 38 * @see ActionSet
 39 *
 40 * @author Slava Pestov
 41 * @version $Id: EditAction.java 4670 2003-05-01 04:17:13Z spestov $
 42 */
 43public abstract class EditAction
 44{
 45	//{{{ EditAction constructor
 46	/**
 47	 * Creates a new edit action with the specified name.
 48	 * @param name The action name
 49	 */
 50	public EditAction(String name)
 51	{
 52		this.name = name;
 53	} //}}}
 54
 55	//{{{ getName() method
 56	/**
 57	 * Returns the internal name of this action.
 58	 */
 59	public String getName()
 60	{
 61		return name;
 62	} //}}}
 63
 64	//{{{ getLabel() method
 65	/**
 66	 * Returns the action's label. This returns the
 67	 * value of the property named by {@link #getName()} suffixed
 68	 * with <code>.label</code>.
 69	 */
 70	public final String getLabel()
 71	{
 72		return jEdit.getProperty(name + ".label");
 73	} //}}}
 74
 75	//{{{ getMouseOverText() method
 76	/**
 77	 * Returns the action's mouse over message. This returns the
 78	 * value of the property named by {@link #getName()} suffixed
 79	 * with <code>.mouse-over</code>.
 80	 */
 81	public final String getMouseOverText()
 82	{
 83		return jEdit.getProperty(name + ".mouse-over");
 84	} //}}}
 85
 86	//{{{ invoke() method
 87	/**
 88	 * Invokes the action.
 89	 * @param view The view
 90	 * @since jEdit 2.7pre2
 91	 */
 92	public void invoke(View view)
 93	{
 94	} //}}}
 95
 96	//{{{ getView() method
 97	/**
 98	 * @deprecated Call <code>GUIUtilities.getView()</code> instead.
 99	 */
100	public static View getView(Component comp)
101	{
102		// moved to GUIUtilities as it makes no sense being here.
103		return GUIUtilities.getView(comp);
104	} //}}}
105
106	//{{{ isToggle() method
107	/**
108	 * Returns if this edit action should be displayed as a check box
109	 * in menus. This returns the
110	 * value of the property named by {@link #getName()} suffixed
111	 * with <code>.toggle</code>.
112	 *
113	 * @since jEdit 2.2pre4
114	 */
115	public final boolean isToggle()
116	{
117		return jEdit.getBooleanProperty(name + ".toggle");
118	} //}}}
119
120	//{{{ isSelected() method
121	/**
122	 * If this edit action is a toggle, returns if it is selected or not.
123	 * @param comp The component
124	 * @since jEdit 4.2pre1
125	 */
126	public boolean isSelected(Component comp)
127	{
128		return false;
129	} //}}}
130
131	//{{{ noRepeat() method
132	/**
133	 * Returns if this edit action should not be repeated. Returns false
134	 * by default.
135	 * @since jEdit 2.7pre2
136	 */
137	public boolean noRepeat()
138	{
139		return false;
140	} //}}}
141
142	//{{{ noRecord() method
143	/**
144	 * Returns if this edit action should not be recorded. Returns false
145	 * by default.
146	 * @since jEdit 2.7pre2
147	 */
148	public boolean noRecord()
149	{
150		return false;
151	} //}}}
152
153	//{{{ noRememberLast() method
154	/**
155	 * Returns if this edit action should not be remembered as the most
156	 * recently invoked action.
157	 * @since jEdit 4.2pre1
158	 */
159	public boolean noRememberLast()
160	{
161		return false;
162	} //}}}
163
164	//{{{ getCode() method
165	/**
166	 * Returns the BeanShell code that will replay this action.
167	 * @since jEdit 2.7pre2
168	 */
169	public abstract String getCode();
170	//}}}
171
172	//{{{ toString() method
173	public String toString()
174	{
175		return name;
176	} //}}}
177
178	//{{{ Private members
179	private String name;
180	//}}}
181
182	//{{{ Wrapper class
183	/**
184	 * 'Wrap' EditActions in this class to turn them into AWT
185	 * ActionListeners, that can be attached to buttons, menu items, etc.
186	 */
187	public static class Wrapper implements ActionListener
188	{
189		/**
190		 * Creates a new action listener wrapper.
191		 * @since jEdit 4.2pre1
192		 */
193		public Wrapper(ActionContext context, String actionName)
194		{
195			this.context = context;
196			this.actionName = actionName;
197		}
198
199		/**
200		 * Called when the user selects this action from a menu.
201		 * It passes the action through the
202		 * {@link org.gjt.sp.jedit.gui.InputHandler#invokeAction(EditAction)}
203		 * method, which performs any recording or repeating.
204		 *
205		 * @param evt The action event
206		 */
207		public void actionPerformed(ActionEvent evt)
208		{
209			EditAction action = context.getAction(actionName);
210			if(action == null)
211			{
212				Log.log(Log.WARNING,this,"Unknown action: "
213					+ actionName);
214			}
215			else
216				context.invokeAction(evt,action);
217		}
218
219		private ActionContext context;
220		private String actionName;
221	} //}}}
222}