001: /*
002: * BeanShellAction.java - BeanShell action
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit;
024:
025: import org.gjt.sp.jedit.bsh.*;
026: import java.awt.Component;
027: import org.gjt.sp.jedit.gui.BeanShellErrorDialog;
028: import org.gjt.sp.util.Log;
029:
030: /**
031: * An action that evaluates BeanShell code when invoked. BeanShell actions are
032: * usually loaded from <code>actions.xml</code> and
033: * <code>browser.actions.xml</code> files; see {@link ActionSet} for syntax
034: * information.
035: *
036: * @see jEdit#getAction(String)
037: * @see jEdit#getActionNames()
038: * @see ActionSet
039: *
040: * @author Slava Pestov
041: * @version $Id: BeanShellAction.java 10803 2007-10-04 20:45:31Z kpouer $
042: */
043: public class BeanShellAction extends EditAction {
044: //{{{ BeanShellAction constructor
045: public BeanShellAction(String name, String code, String isSelected,
046: boolean noRepeat, boolean noRecord, boolean noRememberLast) {
047: super (name);
048:
049: this .code = code;
050: this .isSelected = isSelected;
051: this .noRepeat = noRepeat;
052: this .noRecord = noRecord;
053: this .noRememberLast = noRememberLast;
054:
055: /* Some characters that we like to use in action names
056: * ('.', '-') are not allowed in BeanShell identifiers. */
057: sanitizedName = name.replace('.', '_').replace('-', '_');
058:
059: jEdit.setTemporaryProperty(name + ".toggle",
060: isSelected != null ? "true" : "false");
061: } //}}}
062:
063: //{{{ invoke() method
064: public void invoke(View view) {
065: try {
066: if (cachedCode == null) {
067: String cachedCodeName = "action_" + sanitizedName;
068: cachedCode = BeanShell.cacheBlock(cachedCodeName, code,
069: true);
070: }
071:
072: BeanShell.runCachedBlock(cachedCode, view, new NameSpace(
073: BeanShell.getNameSpace(),
074: "BeanShellAction.invoke()"));
075: } catch (Throwable e) {
076: Log.log(Log.ERROR, this , e);
077:
078: new BeanShellErrorDialog(view, e);
079: }
080: } //}}}
081:
082: //{{{ isSelected() method
083: public boolean isSelected(Component comp) {
084: if (isSelected == null)
085: return false;
086:
087: NameSpace global = BeanShell.getNameSpace();
088:
089: try {
090: if (cachedIsSelected == null) {
091: String cachedIsSelectedName = "selected_"
092: + sanitizedName;
093: cachedIsSelected = BeanShell.cacheBlock(
094: cachedIsSelectedName, isSelected, true);
095: }
096:
097: View view = GUIUtilities.getView(comp);
098:
099: // undocumented hack to allow browser actions to work.
100: // XXX - clean up in 4.3
101: global.setVariable("_comp", comp);
102:
103: return Boolean.TRUE.equals(BeanShell.runCachedBlock(
104: cachedIsSelected, view, new NameSpace(BeanShell
105: .getNameSpace(),
106: "BeanShellAction.isSelected()")));
107: } catch (Throwable e) {
108: Log.log(Log.ERROR, this , e);
109:
110: // dialogs fuck things up if a menu is visible, etc!
111: //new BeanShellErrorDialog(view,e);
112:
113: // so that in the future we don't see streams of
114: // exceptions
115: isSelected = null;
116:
117: return false;
118: } finally {
119: try {
120: global.setVariable("_comp", null);
121: } catch (UtilEvalError err) {
122: Log.log(Log.ERROR, this , err);
123: }
124: }
125: } //}}}
126:
127: //{{{ noRepeat() method
128: public boolean noRepeat() {
129: return noRepeat;
130: } //}}}
131:
132: //{{{ noRecord() method
133: public boolean noRecord() {
134: return noRecord;
135: } //}}}
136:
137: //{{{ noRememberLast() method
138: /**
139: * Returns if this edit action should not be remembered as the most
140: * recently invoked action.
141: * @since jEdit 4.2pre1
142: */
143: public boolean noRememberLast() {
144: return noRememberLast;
145: } //}}}
146:
147: //{{{ getCode() method
148: public String getCode() {
149: return code.trim();
150: } //}}}
151:
152: //{{{ Private members
153: private boolean noRepeat;
154: private boolean noRecord;
155: private boolean noRememberLast;
156: private String code;
157: private String isSelected;
158: private BshMethod cachedCode;
159: private BshMethod cachedIsSelected;
160: private String sanitizedName;
161: //}}}
162: }
|