KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > BeanShellAction


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
23 package org.gjt.sp.jedit;
24
25 import bsh.*;
26 import java.awt.Component JavaDoc;
27 import org.gjt.sp.jedit.gui.BeanShellErrorDialog;
28 import 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  */

43 public class BeanShellAction extends EditAction
44 {
45     //{{{ BeanShellAction constructor
46
public BeanShellAction(String JavaDoc name, String JavaDoc code, String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc code;
176     private String JavaDoc isSelected;
177     private BshMethod cachedCode;
178     private BshMethod cachedIsSelected;
179     private String JavaDoc sanitizedName;
180     //}}}
181
}
182
Popular Tags