PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/Console/console/commando/CommandoCommand.java

#
Java | 192 lines | 96 code | 32 blank | 64 comment | 5 complexity | c42d58dbd1783faff786cd82260de6b2 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. * CommandoCommand.java - Commando command wrapper
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000, 2001, 2002 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 console.commando;
  23. // {{{ Imports
  24. import java.io.BufferedReader;
  25. import java.io.File;
  26. import java.io.FileReader;
  27. import java.io.IOException;
  28. import java.io.InputStreamReader;
  29. import java.io.Reader;
  30. import java.net.URL;
  31. import java.util.regex.Matcher;
  32. import java.util.regex.Pattern;
  33. import org.gjt.sp.jedit.EditAction;
  34. import org.gjt.sp.jedit.View;
  35. import org.gjt.sp.jedit.jEdit;
  36. import org.gjt.sp.util.StringList;
  37. // }}}
  38. /**
  39. * An EditAction which is intended to be used in the Console Commando.
  40. * Associated with an .xml file which may be inside a jar, or may be in the
  41. * user dir.
  42. *
  43. */
  44. public class CommandoCommand extends EditAction
  45. {
  46. // {{{ isUser()
  47. /**
  48. *
  49. * @return true if userdefined
  50. */
  51. public boolean isUser()
  52. {
  53. return (url == null);
  54. }
  55. /**
  56. *
  57. * @return true for user commands that override a command
  58. * with the same name in the jar.
  59. */
  60. public boolean isOverriding()
  61. {
  62. if (!isUser()) return false;
  63. String defaultCommands = jEdit.getProperty("commando.default");
  64. StringList sl = StringList.split(defaultCommands, " ");
  65. String cmdName = name.replace("commando.", "");
  66. return sl.contains(cmdName);
  67. }
  68. // }}}
  69. // {{{ create()
  70. public static CommandoCommand create(URL url)
  71. {
  72. String l = shortLabel(url.getPath());
  73. CommandoCommand retval = new CommandoCommand(l, url.getPath());
  74. retval.url = url;
  75. return retval;
  76. }
  77. public static CommandoCommand create(String path)
  78. {
  79. String l = shortLabel(path);
  80. File f = new File(path);
  81. if (f.canRead())
  82. {
  83. return new CommandoCommand(l, path);
  84. }
  85. else
  86. throw new RuntimeException("path: " + path + " abs: " + f.getAbsolutePath());
  87. }
  88. // }}}
  89. // {{{ shortLabel
  90. /**
  91. * @return the short label - for button text
  92. */
  93. public String getShortLabel()
  94. {
  95. return label;
  96. }
  97. /**
  98. * @param path
  99. * an absolute path to a resource
  100. * @return the short label on for a button text
  101. */
  102. static String shortLabel(String path)
  103. {
  104. Matcher m = p.matcher(path);
  105. m.find();
  106. String name = m.group(1);
  107. name = name.replace('_', ' ');
  108. return name;
  109. }
  110. // }}}
  111. // {{{ Constructor (private )
  112. private CommandoCommand(String shortLabel, String path)
  113. {
  114. super("commando." + shortLabel);
  115. label = shortLabel;
  116. // Log.log(Log.WARNING, this, "New command: " + label + " path:
  117. // " + path);
  118. this.path = path;
  119. this.propertyPrefix = getName() + '.';
  120. jEdit.setTemporaryProperty(getName() + ".label", label);
  121. }
  122. // }}}
  123. // {{{ getPropertyPrefix() method
  124. public String getPropertyPrefix()
  125. {
  126. return propertyPrefix;
  127. } // }}}
  128. // {{{ invoke() method
  129. public void invoke(View view)
  130. {
  131. new CommandoDialog(view, getName());
  132. } // }}}
  133. // {{{ getCode() method
  134. public String getCode()
  135. {
  136. return "new console.commando.CommandoDialog(view,\"" + getName() + "\");";
  137. } // }}}
  138. // {{{ openStream() method
  139. protected Reader openStream() throws IOException
  140. {
  141. if (url != null)
  142. {
  143. return new BufferedReader(new InputStreamReader(url.openStream()));
  144. }
  145. else
  146. {
  147. return new BufferedReader(new FileReader(path));
  148. }
  149. } // }}}
  150. // {{{ Private members
  151. private URL url = null;
  152. private String label;
  153. private String path;
  154. private String propertyPrefix;
  155. // }}}
  156. // {{{ static private members
  157. private static final String pattern = "([^\\\\\\./]+)\\.xml$";
  158. private static final Pattern p = Pattern.compile(pattern);
  159. // }}}
  160. }