PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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