/plugins/Templates/trunk/templates/TemplateDockable.java

#
Java | 232 lines | 139 code | 27 blank | 66 comment | 22 complexity | 6c2d602c50f7019065d5bffe7a4b8410 MD5 | raw file

✨ Summary
  1. /*
  2. * TemplateDockable.java
  3. * :tabSize=3:indentSize=3:noTabs=true:
  4. *
  5. * Copyright (c) 2002 Calvin Yu
  6. * Copyright (c) 2008 Steve Jakob
  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 templates;
  23. import java.awt.BorderLayout;
  24. import java.awt.event.*;
  25. import javax.swing.*;
  26. import javax.swing.tree.TreePath;
  27. import org.gjt.sp.jedit.*;
  28. import org.gjt.sp.util.Log;
  29. /**
  30. * A dockable template tree..
  31. */
  32. public class TemplateDockable extends JPanel
  33. implements MouseListener, ActionListener, EBComponent
  34. {
  35. public final static String RELOAD = "reload";
  36. public final static String EDIT = "edit";
  37. public final static String SET_ACCELERATOR = "setAccelerator";
  38. private View view;
  39. private TemplateTree templates;
  40. /**
  41. * Create a new <code>TemplateDockable</code>.
  42. */
  43. public TemplateDockable(View aView)
  44. {
  45. super(new BorderLayout());
  46. view = aView;
  47. add(new JScrollPane(templates = new TemplateTree()));
  48. // Add ENTER key binding for TemplateTree component
  49. templates.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
  50. "templates-tree-select-node");
  51. templates.getActionMap().put("templates-tree-select-node",
  52. new NodeSelectAction());
  53. templates.addMouseListener(this);
  54. }
  55. public boolean requestDefaultFocus() {
  56. templates.requestFocus();
  57. return true;
  58. }
  59. /**
  60. * Register this object as a receiver for EditBus messages.
  61. */
  62. public void addNotify() {
  63. super.addNotify();
  64. EditBus.addToBus(this);
  65. }
  66. /**
  67. * Remove this object as a receiver for EditBus messages.
  68. */
  69. public void removeNotify() {
  70. super.removeNotify();
  71. EditBus.removeFromBus(this);
  72. }
  73. /**
  74. * Handle messages received by the jEdit EditBus.
  75. * At this time, TemplatesDockable objects will respond only to
  76. * TemplatesChanged messages.
  77. * @param msg An EBMessage object sent by the jEdit EditBus, to which
  78. * the TemplatesMenu object may wish to respond.
  79. */
  80. public void handleMessage(EBMessage msg) {
  81. if (msg instanceof TemplatesChanged) {
  82. Log.log(Log.DEBUG,this,"... TemplateDockable.handleMessage()");
  83. templates.reload();
  84. }
  85. }
  86. /**
  87. * Process the selected template.
  88. */
  89. public void processSelectedTemplate()
  90. {
  91. TemplatesPlugin plugin = (TemplatesPlugin) jEdit.getPlugin("templates.TemplatesPlugin");
  92. plugin.processTemplate(templates.getSelectedTemplate(), view, view.getTextArea());
  93. view.getEditPane().getTextArea().requestFocus();
  94. }
  95. //{{{ MouseListener Method
  96. /**
  97. * Handle a mouse entered.
  98. */
  99. public void mouseEntered(MouseEvent evt)
  100. {
  101. }
  102. /**
  103. * Handle a mouse exited.
  104. */
  105. public void mouseExited(MouseEvent evt)
  106. {
  107. }
  108. /**
  109. * Handle a mouse press.
  110. */
  111. public void mousePressed(MouseEvent evt)
  112. {
  113. if (GUIUtilities.isPopupTrigger(evt)) {
  114. templates.setSelectionPath(templates.getPathForLocation(evt.getX(), evt.getY()));
  115. JPopupMenu popup = new JPopupMenu();
  116. JMenuItem edit = new JMenuItem(jEdit.getProperty("Templates.edit-template.label"));
  117. edit.setActionCommand(EDIT);
  118. edit.addActionListener(this);
  119. popup.add(edit);
  120. JMenuItem setAccelerator = new JMenuItem(jEdit.getProperty("Templates.set-accelerator.label"));
  121. setAccelerator.setActionCommand(SET_ACCELERATOR);
  122. setAccelerator.addActionListener(this);
  123. popup.add(setAccelerator);
  124. popup.addSeparator();
  125. JMenuItem reload = new JMenuItem(jEdit.getProperty("Templates.refresh-templates.label"));
  126. reload.setActionCommand(RELOAD);
  127. reload.addActionListener(this);
  128. popup.add(reload);
  129. popup.show(templates, evt.getX(), evt.getY());
  130. }
  131. }
  132. /**
  133. * Handle a mouse release.
  134. */
  135. public void mouseReleased(MouseEvent evt)
  136. {
  137. }
  138. /**
  139. * Handle a mouse click.
  140. */
  141. public void mouseClicked(MouseEvent evt)
  142. {
  143. if ((evt.getModifiers() & MouseEvent.BUTTON1_MASK) != 0 && evt.getClickCount() == 1) {
  144. TreePath path = templates.getPathForLocation(evt.getX(), evt.getY());
  145. if (templates.isLastPathComponentATemplate(path) && path.equals(templates.getSelectionPath())) {
  146. processSelectedTemplate();
  147. }
  148. }
  149. }
  150. //}}}
  151. //{{{ ActionListener Method
  152. /**
  153. * Handle a action.
  154. */
  155. public void actionPerformed(ActionEvent evt)
  156. {
  157. if (RELOAD.equals(evt.getActionCommand())) {
  158. TemplatesPlugin.refreshTemplates();
  159. templates.reload();
  160. } else if (EDIT.equals(evt.getActionCommand())) {
  161. jEdit.openFile(view, MiscUtilities.concatPath(TemplatesPlugin.getTemplateDir(),
  162. templates.getSelectedTemplate()));
  163. } else if (SET_ACCELERATOR.equals(evt.getActionCommand())) {
  164. String mode =
  165. GUIUtilities.input(view, "plugin.TemplatesPlugin.set-accelerator.input-mode",
  166. jEdit.getModes(), view.getEditPane().getBuffer().getMode().getName());
  167. if (mode == null) {
  168. return;
  169. }
  170. String accelerator =
  171. GUIUtilities.input(view, "plugin.TemplatesPlugin.set-accelerator.input-accelerator", null);
  172. if (accelerator == null) {
  173. return;
  174. }
  175. if (AcceleratorManager.getInstance().findTemplatePath(mode,
  176. accelerator) != null)
  177. {
  178. int result =
  179. GUIUtilities.confirm(view, "plugin.TemplatesPlugin.set-accelerator.confirm-overwrite",
  180. new String[] {"accelerator"}, JOptionPane.YES_NO_OPTION,
  181. JOptionPane.WARNING_MESSAGE);
  182. if (result != JOptionPane.YES_OPTION) {
  183. return;
  184. }
  185. }
  186. AcceleratorManager.getInstance().addAccelerator(mode, accelerator,
  187. templates.getSelectedTemplate());
  188. }
  189. }
  190. //}}}
  191. public class NodeSelectAction extends AbstractAction
  192. {
  193. public void actionPerformed(ActionEvent e)
  194. {
  195. TreePath path = templates.getSelectionPath();
  196. if (templates.isLastPathComponentATemplate(path))
  197. {
  198. processSelectedTemplate();
  199. } else
  200. {
  201. templates.expandPath(path);
  202. }
  203. }
  204. }
  205. }