PageRenderTime 50ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 182 lines | 106 code | 28 blank | 48 comment | 11 complexity | 6dc705107397d0b98eb6ddc6108d23c0 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. * CommandoToolBar.java - Commando tool bar
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001 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.awt.Insets;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import java.util.HashMap;
  28. import java.util.Iterator;
  29. import javax.swing.Box;
  30. import javax.swing.BoxLayout;
  31. import javax.swing.JToolBar;
  32. import org.gjt.sp.jedit.EditAction;
  33. import org.gjt.sp.jedit.EditBus;
  34. import org.gjt.sp.jedit.View;
  35. import org.gjt.sp.jedit.jEdit;
  36. import org.gjt.sp.jedit.EditBus.EBHandler;
  37. import org.gjt.sp.jedit.msg.DynamicMenuChanged;
  38. import console.ConsolePlugin;
  39. // }}}
  40. // {{{ CommandoToolBar class
  41. public class CommandoToolBar extends JToolBar
  42. {
  43. // {{{ init()
  44. public static void init() {
  45. remove();
  46. View views[] = jEdit.getViews();
  47. for (int i=0; i<views.length; ++i) {
  48. create(views[i]);
  49. }
  50. }
  51. public static CommandoToolBar create(View view)
  52. {
  53. if (!jEdit.getBooleanProperty("commando.toolbar.enabled"))
  54. return null;
  55. CommandoToolBar tb = new CommandoToolBar(view);
  56. view.addToolBar(tb);
  57. smToolBarMap.put(view, tb);
  58. return tb;
  59. }
  60. // }}}
  61. // {{{ remove()
  62. /** Remove the instance from the all views */
  63. public static void remove()
  64. {
  65. Iterator<View> itr = smToolBarMap.keySet().iterator();
  66. while (itr.hasNext())
  67. {
  68. View v = itr.next();
  69. if (v == null) continue;
  70. CommandoToolBar tb = smToolBarMap.get(v);
  71. if (tb != null) {
  72. v.removeToolBar(tb);
  73. }
  74. }
  75. smToolBarMap.clear();
  76. }
  77. // }}}
  78. /** Remove the instance from the all views */
  79. public static void remove(View v)
  80. {
  81. CommandoToolBar tb = smToolBarMap.get(v);
  82. if (tb != null) {
  83. v.removeToolBar(tb);
  84. smToolBarMap.remove(v);
  85. }
  86. }
  87. // }}}
  88. // {{{ CommandoToolBar constructor
  89. private CommandoToolBar(View dockable)
  90. {
  91. view = dockable;
  92. setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  93. setFloatable(true);
  94. updateButtons();
  95. }
  96. // }}}
  97. // {{{ addNotify() method
  98. public void addNotify()
  99. {
  100. super.addNotify();
  101. EditBus.addToBus(this);
  102. }
  103. // }}}
  104. // {{{ removeNotify() method
  105. public void removeNotify()
  106. {
  107. super.removeNotify();
  108. EditBus.removeFromBus(this);
  109. }
  110. // }}}
  111. // {{{ handleMessage() method
  112. @EBHandler
  113. public void handleMessage(DynamicMenuChanged msg)
  114. {
  115. if (ConsolePlugin.MENU.equals(msg.getMenuName()))
  116. updateButtons();
  117. } // }}}
  118. // {{{ updateButtons() method
  119. private void updateButtons()
  120. {
  121. removeAll();
  122. ActionListener actionHandler = new ActionListener()
  123. {
  124. public void actionPerformed(ActionEvent evt)
  125. {
  126. new CommandoDialog(view, evt.getActionCommand());
  127. }
  128. };
  129. // ConsolePlugin.rescanCommands();
  130. EditAction[] commands = ConsolePlugin.getCommandoCommands();
  131. for (int i = 0; i < commands.length; i++)
  132. {
  133. CommandoCommand command = (CommandoCommand) commands[i];
  134. CommandoButton button = new CommandoButton(command);
  135. button.setActionCommand(command.getName());
  136. button.addActionListener(actionHandler);
  137. button.setRequestFocusEnabled(false);
  138. button.setMargin(new Insets(1, 2, 1, 2));
  139. add(button);
  140. }
  141. add(Box.createGlue());
  142. }
  143. // }}}
  144. // {{{ Data members
  145. private View view;
  146. /**
  147. * For each view, we might add a toolbar.
  148. * This map keeps track of what
  149. * views had toolbars added to them.
  150. */
  151. static HashMap<View, CommandoToolBar> smToolBarMap =
  152. new HashMap<View, CommandoToolBar>();
  153. // }}}
  154. } // }}}