/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/BeanShellAction.java
Java | 181 lines | 102 code | 23 blank | 56 comment | 7 complexity | 3ba697bead352b949ce85b730749d4bd 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 * BeanShellAction.java - BeanShell action
3 * :tabSize=8:indentSize=8:noTabs=false:
4 * :folding=explicit:collapseFolds=1:
5 *
6 * Copyright (C) 2000, 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
25import bsh.*;
26import java.awt.Component;
27import org.gjt.sp.jedit.gui.BeanShellErrorDialog;
28import org.gjt.sp.util.Log;
29
30/**
31 * An action that evaluates BeanShell code when invoked. BeanShell actions are
32 * usually loaded from <code>actions.xml</code> and
33 * <code>browser.actions.xml</code> files; see {@link ActionSet} for syntax
34 * information.
35 *
36 * @see jEdit#getAction(String)
37 * @see jEdit#getActionNames()
38 * @see ActionSet
39 *
40 * @author Slava Pestov
41 * @version $Id: BeanShellAction.java 4831 2003-07-17 23:49:44Z spestov $
42 */
43public class BeanShellAction extends EditAction
44{
45 //{{{ BeanShellAction constructor
46 public BeanShellAction(String name, String code, String isSelected,
47 boolean noRepeat, boolean noRecord, boolean noRememberLast)
48 {
49 super(name);
50
51 this.code = code;
52 this.isSelected = isSelected;
53 this.noRepeat = noRepeat;
54 this.noRecord = noRecord;
55 this.noRememberLast = noRememberLast;
56
57 /* Some characters that we like to use in action names
58 * ('.', '-') are not allowed in BeanShell identifiers. */
59 sanitizedName = name.replace('.','_').replace('-','_');
60
61 jEdit.setTemporaryProperty(name + ".toggle",
62 isSelected != null ? "true" : "false");
63 } //}}}
64
65 //{{{ invoke() method
66 public void invoke(View view)
67 {
68 try
69 {
70 if(cachedCode == null)
71 {
72 String cachedCodeName = "action_" + sanitizedName;
73 cachedCode = BeanShell.cacheBlock(cachedCodeName,code,true);
74 }
75
76 BeanShell.runCachedBlock(cachedCode,view,
77 new NameSpace(BeanShell.getNameSpace(),
78 "BeanShellAction.invoke()"));
79 }
80 catch(Throwable e)
81 {
82 Log.log(Log.ERROR,this,e);
83
84 new BeanShellErrorDialog(view,e);
85 }
86 } //}}}
87
88 //{{{ isSelected() method
89 public boolean isSelected(Component comp)
90 {
91 if(isSelected == null)
92 return false;
93
94 NameSpace global = BeanShell.getNameSpace();
95
96 try
97 {
98 if(cachedIsSelected == null)
99 {
100 String cachedIsSelectedName = "selected_" + sanitizedName;
101 cachedIsSelected = BeanShell.cacheBlock(cachedIsSelectedName,
102 isSelected,true);
103 }
104
105 View view = GUIUtilities.getView(comp);
106
107 // undocumented hack to allow browser actions to work.
108 // XXX - clean up in 4.3
109 global.setVariable("_comp",comp);
110
111 return Boolean.TRUE.equals(BeanShell.runCachedBlock(
112 cachedIsSelected,view,
113 new NameSpace(BeanShell.getNameSpace(),
114 "BeanShellAction.isSelected()")));
115 }
116 catch(Throwable e)
117 {
118 Log.log(Log.ERROR,this,e);
119
120 // dialogs fuck things up if a menu is visible, etc!
121 //new BeanShellErrorDialog(view,e);
122
123 // so that in the future we don't see streams of
124 // exceptions
125 isSelected = null;
126
127 return false;
128 }
129 finally
130 {
131 try
132 {
133 global.setVariable("_comp",null);
134 }
135 catch(UtilEvalError err)
136 {
137 Log.log(Log.ERROR,this,err);
138 }
139 }
140 } //}}}
141
142 //{{{ noRepeat() method
143 public boolean noRepeat()
144 {
145 return noRepeat;
146 } //}}}
147
148 //{{{ noRecord() method
149 public boolean noRecord()
150 {
151 return noRecord;
152 } //}}}
153
154 //{{{ noRememberLast() method
155 /**
156 * Returns if this edit action should not be remembered as the most
157 * recently invoked action.
158 * @since jEdit 4.2pre1
159 */
160 public boolean noRememberLast()
161 {
162 return noRememberLast;
163 } //}}}
164
165 //{{{ getCode() method
166 public String getCode()
167 {
168 return code.trim();
169 } //}}}
170
171 //{{{ Private members
172 private boolean noRepeat;
173 private boolean noRecord;
174 private boolean noRememberLast;
175 private String code;
176 private String isSelected;
177 private BshMethod cachedCode;
178 private BshMethod cachedIsSelected;
179 private String sanitizedName;
180 //}}}
181}