PageRenderTime 63ms CodeModel.GetById 54ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/BeanShellAction.java

#
Java | 98 lines | 61 code | 14 blank | 23 comment | 7 complexity | 827f13a2339f523b82ac32163a067265 MD5 | raw file
 1/*
 2 * BeanShellAction.java - BeanShell action
 3 * Copyright (C) 2000, 2001 Slava Pestov
 4 *
 5 * This program is free software; you can redistribute it and/or
 6 * modify it under the terms of the GNU General Public License
 7 * as published by the Free Software Foundation; either version 2
 8 * of the License, or any later version.
 9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20package org.gjt.sp.jedit;
21
22import java.awt.event.ActionEvent;
23import java.awt.*;
24
25public class BeanShellAction extends EditAction
26{
27	public BeanShellAction(String name, String code, String isSelected,
28		boolean noRepeat, boolean noRecord)
29	{
30		super(name);
31
32		this.code = code;
33		this.isSelected = isSelected;
34		this.noRepeat = noRepeat;
35		this.noRecord = noRecord;
36
37		/* Some characters that we like to use in action names
38		 * ('.', '-') are not allowed in BeanShell identifiers. */
39		sanitizedName = name.replace('.','_').replace('-','_');
40
41		// evaluate isSelected now so that menus don't take a long
42		// time to first come up
43		if(isSelected != null)
44		{
45			String cachedIsSelectedName = "selected_" + sanitizedName;
46			cachedIsSelected = BeanShell.cacheBlock(cachedIsSelectedName,
47				isSelected,true);
48		}
49	}
50
51	public void invoke(View view)
52	{
53		if(cachedCode == null)
54		{
55			String cachedCodeName = "action_" + sanitizedName;
56			cachedCode = BeanShell.cacheBlock(cachedCodeName,code,true);
57		}
58		BeanShell.runCachedBlock(cachedCode,view,null);
59	}
60
61	public boolean isToggle()
62	{
63		return isSelected != null;
64	}
65
66	public boolean isSelected(View view)
67	{
68		if(isSelected == null)
69			return false;
70
71		return Boolean.TRUE.equals(BeanShell.runCachedBlock(cachedIsSelected,
72			view,null));
73	}
74
75	public boolean noRepeat()
76	{
77		return noRepeat;
78	}
79
80	public boolean noRecord()
81	{
82		return noRecord;
83	}
84
85	public String getCode()
86	{
87		return code.trim();
88	}
89
90	// private members
91	private boolean noRepeat;
92	private boolean noRecord;
93	private String code;
94	private String isSelected;
95	private String cachedCode;
96	private String cachedIsSelected;
97	private String sanitizedName;
98}