1 22 23 package org.gjt.sp.jedit; 24 25 import bsh.*; 26 import java.awt.Component ; 27 import org.gjt.sp.jedit.gui.BeanShellErrorDialog; 28 import org.gjt.sp.util.Log; 29 30 43 public class BeanShellAction extends EditAction 44 { 45 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 59 sanitizedName = name.replace('.','_').replace('-','_'); 60 61 jEdit.setTemporaryProperty(name + ".toggle", 62 isSelected != null ? "true" : "false"); 63 } 65 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 } 88 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 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 123 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 } 142 public boolean noRepeat() 144 { 145 return noRepeat; 146 } 148 public boolean noRecord() 150 { 151 return noRecord; 152 } 154 160 public boolean noRememberLast() 161 { 162 return noRememberLast; 163 } 165 public String getCode() 167 { 168 return code.trim(); 169 } 171 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 } 182 | Popular Tags |