PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/AbstractContextOptionPane.java

#
Java | 339 lines | 229 code | 35 blank | 75 comment | 38 complexity | 3660d276526c3eb154223e6bffd204e5 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. * Copyright (C) 2000, 2001 Slava Pestov
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. package org.gjt.sp.jedit.gui;
  19. import javax.swing.border.*;
  20. import javax.swing.event.*;
  21. import javax.swing.*;
  22. import java.awt.event.*;
  23. import java.awt.*;
  24. import java.util.*;
  25. import org.gjt.sp.jedit.*;
  26. import org.gjt.sp.jedit.browser.VFSBrowser;
  27. import org.gjt.sp.util.StandardUtilities;
  28. /**
  29. * An abstract base class for context menu editors. Provides the base
  30. * UI and functionality for creating a context menu populated with
  31. * jEdit actions.
  32. *
  33. * @author Slava Pestov
  34. * @author Marcelo Vanzin
  35. * @version $Id: AbstractContextOptionPane.java 19655 2011-07-07 10:02:06Z kpouer $
  36. * @since jEdit 4.3pre13
  37. */
  38. public abstract class AbstractContextOptionPane extends AbstractOptionPane
  39. {
  40. private final ActionContext actionContext;
  41. /**
  42. * Constructor that takes a name as an argument, for use by
  43. * subclasses.
  44. *
  45. * @param name Name of the option pane.
  46. * @param caption String to use as the caption of the context menu
  47. * configuration list.
  48. * @since jEdit 4.3pre13
  49. */
  50. protected AbstractContextOptionPane(String name, String caption)
  51. {
  52. this(name, caption, jEdit.getActionContext());
  53. }
  54. /**
  55. * Constructor that takes a name as an argument, for use by
  56. * subclasses.
  57. *
  58. * @param name Name of the option pane.
  59. * @param caption String to use as the caption of the context menu
  60. * configuration list.
  61. * @param actionContext the actionContext
  62. * @since jEdit 4.5pre1
  63. */
  64. protected AbstractContextOptionPane(String name, String caption, ActionContext actionContext)
  65. {
  66. super(name);
  67. this.actionContext = actionContext;
  68. this.caption = new JLabel(caption);
  69. }
  70. /**
  71. * Initializes the pane's UI.
  72. */
  73. @Override
  74. protected void _init()
  75. {
  76. setLayout(new BorderLayout());
  77. add(BorderLayout.NORTH, caption);
  78. listModel = new DefaultListModel();
  79. reloadContextList(getContextMenu());
  80. list = new JList(listModel);
  81. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  82. list.addListSelectionListener(new ListHandler());
  83. add(BorderLayout.CENTER, new JScrollPane(list));
  84. buttons = new JPanel();
  85. buttons.setBorder(new EmptyBorder(3, 0, 0, 0));
  86. buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
  87. ActionHandler actionHandler = new ActionHandler();
  88. add = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.context.add.icon")));
  89. add.setToolTipText(jEdit.getProperty("common.add"));
  90. add.addActionListener(actionHandler);
  91. buttons.add(add);
  92. buttons.add(Box.createHorizontalStrut(6));
  93. remove = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.context.remove.icon")));
  94. remove.setToolTipText(jEdit.getProperty("common.remove"));
  95. remove.addActionListener(actionHandler);
  96. buttons.add(remove);
  97. buttons.add(Box.createHorizontalStrut(6));
  98. moveUp = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.context.moveUp.icon")));
  99. moveUp.setToolTipText(jEdit.getProperty("common.moveUp"));
  100. moveUp.addActionListener(actionHandler);
  101. buttons.add(moveUp);
  102. buttons.add(Box.createHorizontalStrut(6));
  103. moveDown = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.context.moveDown.icon")));
  104. moveDown.setToolTipText(jEdit.getProperty("common.moveDown"));
  105. moveDown.addActionListener(actionHandler);
  106. buttons.add(moveDown);
  107. buttons.add(Box.createGlue());
  108. // add "reset to defaults" button
  109. reset = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.context.reset.icon")));
  110. reset.setToolTipText(jEdit.getProperty("options.context.reset"));
  111. reset.addActionListener(actionHandler);
  112. buttons.add(reset);
  113. updateButtons();
  114. add(BorderLayout.SOUTH, buttons);
  115. }
  116. /**
  117. * Returns the context menu to be edited. The default implementation
  118. * returns jEdit's context menu. Subclasses overriding this method
  119. * should also override {@link #saveContextMenu(String menu) saveContextMenu}.
  120. *
  121. * @since jEdit 4.3pre13
  122. */
  123. protected abstract String getContextMenu();
  124. /**
  125. * Saves the context menu configuration.
  126. *
  127. * @since jEdit 4.3pre13
  128. */
  129. protected abstract void saveContextMenu(String menu);
  130. /**
  131. * Adds a widget to the "buttons" panel at the bottom. The component
  132. * will be added at the very right of the button row (separated from
  133. * the normal buttons).
  134. *
  135. * @since jEdit 4.3pre13
  136. */
  137. protected void addButton(JComponent c)
  138. {
  139. buttons.add(c);
  140. }
  141. static class MenuItemCompare implements Comparator<MenuItem>
  142. {
  143. @Override
  144. public int compare(MenuItem obj1, MenuItem obj2)
  145. {
  146. return StandardUtilities.compareStrings(obj1.label, obj2.label, false);
  147. }
  148. }
  149. @Override
  150. protected void _save()
  151. {
  152. StringBuilder buf = new StringBuilder();
  153. for (int i = 0; i < listModel.getSize(); i++)
  154. {
  155. if (i != 0)
  156. buf.append(' ');
  157. buf.append(((MenuItem) listModel.elementAt(i)).actionName);
  158. }
  159. saveContextMenu(buf.toString());
  160. }
  161. // private members
  162. private DefaultListModel listModel;
  163. private JList list;
  164. private JButton add;
  165. private JButton remove;
  166. private JButton moveUp, moveDown;
  167. private JButton reset;
  168. private JLabel caption;
  169. private JPanel buttons;
  170. private void updateButtons()
  171. {
  172. int index = list.getSelectedIndex();
  173. remove.setEnabled(index != -1 && listModel.getSize() != 0);
  174. moveUp.setEnabled(index > 0);
  175. moveDown.setEnabled(index != -1 && index != listModel.getSize() - 1);
  176. }
  177. private void reloadContextList(String contextMenu)
  178. {
  179. listModel.clear();
  180. StringTokenizer st = new StringTokenizer(contextMenu);
  181. while (st.hasMoreTokens())
  182. {
  183. String actionName = st.nextToken();
  184. if (actionName.equals("-"))
  185. listModel.addElement(new AbstractContextOptionPane.MenuItem("-", "-"));
  186. else
  187. {
  188. EditAction action = actionContext.getAction(actionName);
  189. if (action == null)
  190. continue;
  191. String label = action.getLabel();
  192. if (label == null)
  193. continue;
  194. listModel.addElement(new AbstractContextOptionPane.MenuItem(actionName, label));
  195. }
  196. }
  197. }
  198. static class MenuItem
  199. {
  200. String actionName;
  201. String label;
  202. MenuItem(String actionName, String label)
  203. {
  204. this.actionName = actionName;
  205. this.label = GUIUtilities.prettifyMenuLabel(label);
  206. }
  207. public String toString()
  208. {
  209. return label;
  210. }
  211. }
  212. class ActionHandler implements ActionListener
  213. {
  214. @Override
  215. public void actionPerformed(ActionEvent evt)
  216. {
  217. Object source = evt.getSource();
  218. if (source == add)
  219. {
  220. ContextAddDialog dialog = new ContextAddDialog(
  221. AbstractContextOptionPane.this, actionContext);
  222. String selection = dialog.getSelection();
  223. if (selection == null)
  224. return;
  225. int index = list.getSelectedIndex();
  226. if (index == -1)
  227. index = listModel.getSize();
  228. else
  229. index++;
  230. MenuItem menuItem;
  231. if (selection.equals("-"))
  232. menuItem = new AbstractContextOptionPane.MenuItem("-", "-");
  233. else
  234. {
  235. EditAction action = actionContext.getAction(selection);
  236. String label = action
  237. .getLabel();
  238. menuItem = new AbstractContextOptionPane.MenuItem(selection,
  239. label);
  240. }
  241. listModel.insertElementAt(menuItem, index);
  242. list.setSelectedIndex(index);
  243. list.ensureIndexIsVisible(index);
  244. } else if (source == remove)
  245. {
  246. int index = list.getSelectedIndex();
  247. listModel.removeElementAt(index);
  248. if (listModel.getSize() != 0)
  249. {
  250. list.setSelectedIndex(
  251. Math.min(listModel.getSize() - 1,
  252. index));
  253. }
  254. updateButtons();
  255. } else if (source == moveUp)
  256. {
  257. int index = list.getSelectedIndex();
  258. Object selected = list.getSelectedValue();
  259. listModel.removeElementAt(index);
  260. listModel.insertElementAt(selected, index - 1);
  261. list.setSelectedIndex(index - 1);
  262. list.ensureIndexIsVisible(index - 1);
  263. } else if (source == moveDown)
  264. {
  265. int index = list.getSelectedIndex();
  266. Object selected = list.getSelectedValue();
  267. listModel.removeElementAt(index);
  268. listModel.insertElementAt(selected, index + 1);
  269. list.setSelectedIndex(index + 1);
  270. list.ensureIndexIsVisible(index + 1);
  271. } else if (source == reset)
  272. {
  273. String dialogType = "options.context.reset.dialog";
  274. int result = GUIUtilities.confirm(list, dialogType, null,
  275. JOptionPane.YES_NO_OPTION,
  276. JOptionPane.WARNING_MESSAGE);
  277. if (result == JOptionPane.YES_OPTION)
  278. {
  279. // the user should be able to cancel the options dialog
  280. // so we need to modify the list, not the actual property
  281. // since the default value is not available,
  282. // we reset, fetch default value and re-set to original
  283. String orgContext = jEdit.getProperty("view.context");
  284. jEdit.resetProperty("view.context");
  285. String defaultContext = jEdit.getProperty("view.context");
  286. jEdit.setProperty("view.context", orgContext);
  287. reloadContextList(defaultContext);
  288. // reset selection if user had more buttons than default
  289. list.setSelectedIndex(0);
  290. list.ensureIndexIsVisible(0);
  291. updateButtons();
  292. }
  293. }
  294. }
  295. }
  296. class ListHandler implements ListSelectionListener
  297. {
  298. @Override
  299. public void valueChanged(ListSelectionEvent evt)
  300. {
  301. updateButtons();
  302. }
  303. }
  304. }