/plugins/Console/tags/start/ConsoleFramePluginPart.java

# · Java · 127 lines · 87 code · 17 blank · 23 comment · 21 complexity · 7a117807e6f28f291c08b9799a302b9e MD5 · raw file

  1. /*
  2. * ConsoleFramePluginPart.java - Manages console frame
  3. * Copyright (C) 2000 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. import org.gjt.sp.jedit.*;
  20. import org.gjt.sp.jedit.msg.*;
  21. import java.awt.Component;
  22. import java.awt.event.ActionEvent;
  23. import java.util.Hashtable;
  24. import java.util.Vector;
  25. public class ConsoleFramePluginPart extends EBPlugin
  26. {
  27. public void start()
  28. {
  29. jEdit.addAction(new OpenAction());
  30. }
  31. public void createMenuItems(View view, Vector menus, Vector menuItems)
  32. {
  33. menuItems.addElement(GUIUtilities.loadMenuItem(view,"console"));
  34. }
  35. public void handleMessage(EBMessage msg)
  36. {
  37. if(msg instanceof ViewUpdate)
  38. {
  39. ViewUpdate viewUpdate = (ViewUpdate)msg;
  40. View view = viewUpdate.getView();
  41. if(viewUpdate.getWhat() == ViewUpdate.CREATED)
  42. {
  43. if("on".equals(jEdit.getProperty("console.toolbar.enabled")))
  44. {
  45. view.addToolBar(new ConsoleToolBar(view));
  46. }
  47. }
  48. else if(viewUpdate.getWhat() == ViewUpdate.CLOSED)
  49. {
  50. consoles.remove(view);
  51. }
  52. }
  53. }
  54. // package-private members
  55. static boolean isConsoleShowing(View view)
  56. {
  57. if(consoles == null)
  58. return false;
  59. ConsoleFrame console = (ConsoleFrame)consoles.get(view);
  60. if(console == null)
  61. return false;
  62. return console.isShowing();
  63. }
  64. static ConsoleFrame getConsole(View view)
  65. {
  66. if(consoles == null)
  67. consoles = new Hashtable();
  68. ConsoleFrame console = (ConsoleFrame)consoles.get(view);
  69. if(console == null)
  70. {
  71. console = new ConsoleFrame(view);
  72. consoles.put(view,console);
  73. }
  74. console.setVisible(true);
  75. console.toFront();
  76. console.requestFocus();
  77. return console;
  78. }
  79. // private members
  80. private static Hashtable consoles;
  81. class OpenAction extends EditAction
  82. {
  83. OpenAction()
  84. {
  85. super("console");
  86. }
  87. public void actionPerformed(ActionEvent evt)
  88. {
  89. // If action command is null, just toggle visibility,
  90. // otherwise if an action command is set (eg if run from
  91. // a macro) we run that command
  92. View view = getView(evt);
  93. String actionCommand = evt.getActionCommand();
  94. boolean showing = isConsoleShowing(view);
  95. ConsoleFrame console = getConsole(view);
  96. if(showing && evt.getActionCommand() == null)
  97. console.close();
  98. else if(evt.getActionCommand() != null)
  99. console.run(null,evt.getActionCommand());
  100. }
  101. public boolean isToggle()
  102. {
  103. return true;
  104. }
  105. public boolean isSelected(Component comp)
  106. {
  107. return isConsoleShowing(getView(comp));
  108. }
  109. }
  110. }