PageRenderTime 53ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 187 lines | 129 code | 20 blank | 38 comment | 15 complexity | e1a0cb980500afb3e212ccefbd9a658f 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. * CommandoButton.java - A button for a command.
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2005 Alan Ezust
  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.awt.Color;
  25. import java.awt.Component;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.io.File;
  29. import java.io.FileWriter;
  30. import java.io.IOException;
  31. import java.io.Reader;
  32. import javax.swing.JButton;
  33. import javax.swing.JMenuItem;
  34. import javax.swing.JPopupMenu;
  35. import javax.swing.SwingUtilities;
  36. import org.gjt.sp.jedit.View;
  37. import org.gjt.sp.jedit.jEdit;
  38. import org.gjt.sp.jedit.browser.VFSBrowser;
  39. import org.gjt.sp.jedit.gui.DockableWindowManager;
  40. import org.gjt.sp.jedit.io.VFSFile;
  41. import console.ConsolePlugin;
  42. // }}}
  43. /**
  44. * A view for a CommandoCommand
  45. *
  46. * @author ezust
  47. *
  48. */
  49. public class CommandoButton extends JButton implements ActionListener
  50. {
  51. // {{{ Private data members
  52. private boolean visible;
  53. private CommandoCommand command;
  54. private JPopupMenu contextMenu;
  55. private JMenuItem hide;
  56. private JMenuItem customize;
  57. private JMenuItem unCustomize;
  58. // }}}
  59. // {{{ ctor
  60. public CommandoButton(CommandoCommand command)
  61. {
  62. this.command = command;
  63. init();
  64. }
  65. void init() {
  66. setToolTipText(jEdit.getProperty("console.commando.button.tooltiptext"));
  67. String name = command.getLabel();
  68. setText(name);
  69. contextMenu = new JPopupMenu();
  70. visible = jEdit.getBooleanProperty("commando.visible." + name);
  71. setVisible(visible);
  72. if (!command.isUser()) {
  73. hide = new JMenuItem(jEdit.getProperty("commando.hide"));
  74. hide.addActionListener(this);
  75. contextMenu.add(hide);
  76. }
  77. customize = new JMenuItem(jEdit.getProperty("commando.customize"));
  78. customize.addActionListener(this);
  79. contextMenu.add(customize);
  80. if (command.isUser()) {
  81. setBackground(Color.GREEN);
  82. setToolTipText(jEdit.getProperty("console.commando.button.customized.tooltiptext"));
  83. if (command.isOverriding()) {
  84. unCustomize = new JMenuItem(jEdit.getProperty("commando.uncustomize"));
  85. unCustomize.setToolTipText(jEdit.getProperty("commando.uncustomize.tooltiptext", new Object[] {name}));
  86. unCustomize.addActionListener(this);
  87. contextMenu.add(unCustomize);
  88. }
  89. }
  90. setComponentPopupMenu(contextMenu);
  91. }
  92. // }}}
  93. // {{{ unCustomize
  94. public void unCustomizeButton()
  95. {
  96. String userDir = ConsolePlugin.getUserCommandDirectory();
  97. String name = command.getShortLabel() + ".xml";
  98. File f = new File(userDir, name);
  99. String path = f.getPath();
  100. VFSFile vf = new VFSFile(path, path, path, VFSFile.FILE, 0, false);
  101. VFSFile[] vfl = new VFSFile[1];
  102. vfl[0]=vf;
  103. DockableWindowManager dwm = jEdit.getActiveView().getDockableWindowManager();
  104. Component comp = dwm.getDockable("vfs.browser");
  105. VFSBrowser browser = (VFSBrowser) comp;
  106. if (browser == null) {
  107. dwm.showDockableWindow("vfs.browser");
  108. comp = dwm.getDockable("vfs.browser");
  109. browser = (VFSBrowser) comp;
  110. }
  111. if (browser == null) {
  112. return;
  113. }
  114. browser.delete(vfl);
  115. SwingUtilities.invokeLater(new Runnable() {
  116. public void run() { ConsolePlugin.rescanCommands(); }});
  117. }
  118. // {{{ Customize
  119. public void customize()
  120. {
  121. String userDir = ConsolePlugin.getUserCommandDirectory();
  122. try
  123. {
  124. String name = command.getShortLabel() + ".xml";
  125. File f = new File(userDir, name);
  126. if (!f.exists())
  127. {
  128. Reader reader = command.openStream();
  129. FileWriter writer = new FileWriter(f);
  130. int bytes;
  131. char[] buf = new char[200];
  132. while ((bytes = reader.read(buf)) > 0)
  133. {
  134. writer.write(buf, 0, bytes);
  135. }
  136. writer.close();
  137. reader.close();
  138. ConsolePlugin.rescanCommands();
  139. }
  140. View v = jEdit.getActiveView();
  141. jEdit.openFile(v, f.getAbsolutePath());
  142. }
  143. catch (IOException ioe)
  144. {
  145. throw new RuntimeException(ioe);
  146. }
  147. }
  148. // }}}
  149. // {{{ actionPerformed
  150. public void actionPerformed(ActionEvent e)
  151. {
  152. if (e.getSource() == hide)
  153. {
  154. visible = false;
  155. jEdit.setBooleanProperty("commando.visible." + getText(), visible);
  156. setVisible(visible);
  157. }
  158. if (e.getSource() == customize)
  159. {
  160. customize();
  161. }
  162. if (e.getSource() == unCustomize) {
  163. unCustomizeButton();
  164. }
  165. }
  166. // }}}
  167. }