PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/options/ContextOptionPane.java

#
Java | 380 lines | 306 code | 48 blank | 26 comment | 49 complexity | 5a5266571f7471f549a5ac9579ba32ac 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. * ContextOptionPane.java - Context menu options panel
  3. * Copyright (C) 2000, 2001 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. package org.gjt.sp.jedit.options;
  20. import javax.swing.border.*;
  21. import javax.swing.event.*;
  22. import javax.swing.*;
  23. import java.awt.event.*;
  24. import java.awt.*;
  25. import java.util.*;
  26. import org.gjt.sp.jedit.gui.*;
  27. import org.gjt.sp.jedit.*;
  28. /**
  29. * Right-click context menu editor.
  30. * @author Slava Pestov
  31. * @version $Id: ContextOptionPane.java 3840 2001-10-18 07:41:23Z spestov $
  32. */
  33. public class ContextOptionPane extends AbstractOptionPane
  34. {
  35. public ContextOptionPane()
  36. {
  37. super("context");
  38. }
  39. // protected members
  40. protected void _init()
  41. {
  42. setLayout(new BorderLayout());
  43. JLabel caption = new JLabel(jEdit.getProperty(
  44. "options.context.caption"));
  45. add(BorderLayout.NORTH,caption);
  46. String contextMenu = jEdit.getProperty("view.context");
  47. StringTokenizer st = new StringTokenizer(contextMenu);
  48. listModel = new DefaultListModel();
  49. while(st.hasMoreTokens())
  50. {
  51. String actionName = (String)st.nextToken();
  52. if(actionName.equals("-"))
  53. listModel.addElement(new ContextOptionPane.MenuItem("-","-"));
  54. else
  55. {
  56. EditAction action = jEdit.getAction(actionName);
  57. if(action == null)
  58. continue;
  59. String label = action.getLabel();
  60. if(label == null)
  61. continue;
  62. listModel.addElement(new ContextOptionPane.MenuItem(actionName,label));
  63. }
  64. }
  65. list = new JList(listModel);
  66. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  67. list.addListSelectionListener(new ListHandler());
  68. add(BorderLayout.CENTER,new JScrollPane(list));
  69. JPanel buttons = new JPanel();
  70. buttons.setBorder(new EmptyBorder(3,0,0,0));
  71. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  72. buttons.add(Box.createGlue());
  73. ActionHandler actionHandler = new ActionHandler();
  74. add = new JButton(jEdit.getProperty("options.context.add"));
  75. add.addActionListener(actionHandler);
  76. buttons.add(add);
  77. buttons.add(Box.createHorizontalStrut(6));
  78. remove = new JButton(jEdit.getProperty("options.context.remove"));
  79. remove.addActionListener(actionHandler);
  80. buttons.add(remove);
  81. buttons.add(Box.createHorizontalStrut(6));
  82. moveUp = new JButton(jEdit.getProperty("options.context.moveUp"));
  83. moveUp.addActionListener(actionHandler);
  84. buttons.add(moveUp);
  85. buttons.add(Box.createHorizontalStrut(6));
  86. moveDown = new JButton(jEdit.getProperty("options.context.moveDown"));
  87. moveDown.addActionListener(actionHandler);
  88. buttons.add(moveDown);
  89. buttons.add(Box.createGlue());
  90. updateButtons();
  91. add(BorderLayout.SOUTH,buttons);
  92. }
  93. static class MenuItemCompare implements MiscUtilities.Compare
  94. {
  95. public int compare(Object obj1, Object obj2)
  96. {
  97. return MiscUtilities.compareStrings(
  98. ((MenuItem)obj1).label,
  99. ((MenuItem)obj2).label,
  100. true);
  101. }
  102. }
  103. protected void _save()
  104. {
  105. StringBuffer buf = new StringBuffer();
  106. for(int i = 0; i < listModel.getSize(); i++)
  107. {
  108. if(i != 0)
  109. buf.append(' ');
  110. buf.append(((MenuItem)listModel.elementAt(i)).actionName);
  111. }
  112. jEdit.setProperty("view.context",buf.toString());
  113. }
  114. // private members
  115. private DefaultListModel listModel;
  116. private JList list;
  117. private JButton add;
  118. private JButton remove;
  119. private JButton moveUp, moveDown;
  120. private void updateButtons()
  121. {
  122. int index = list.getSelectedIndex();
  123. remove.setEnabled(index != -1 && listModel.getSize() != 0);
  124. moveUp.setEnabled(index > 0);
  125. moveDown.setEnabled(index != -1 && index != listModel.getSize() - 1);
  126. }
  127. static class MenuItem
  128. {
  129. String actionName;
  130. String label;
  131. MenuItem(String actionName, String label)
  132. {
  133. this.actionName = actionName;
  134. this.label = GUIUtilities.prettifyMenuLabel(label);
  135. }
  136. public String toString()
  137. {
  138. return label;
  139. }
  140. }
  141. class ActionHandler implements ActionListener
  142. {
  143. public void actionPerformed(ActionEvent evt)
  144. {
  145. Object source = evt.getSource();
  146. if(source == add)
  147. {
  148. ContextAddDialog dialog = new ContextAddDialog(
  149. ContextOptionPane.this);
  150. String selection = dialog.getSelection();
  151. if(selection == null)
  152. return;
  153. int index = list.getSelectedIndex();
  154. if(index == -1)
  155. index = listModel.getSize();
  156. else
  157. index++;
  158. MenuItem menuItem;
  159. if(selection.equals("-"))
  160. menuItem = new ContextOptionPane.MenuItem("-","-");
  161. else
  162. {
  163. menuItem = new ContextOptionPane.MenuItem(selection,
  164. jEdit.getAction(selection)
  165. .getLabel());
  166. }
  167. listModel.insertElementAt(menuItem,index);
  168. list.setSelectedIndex(index);
  169. list.ensureIndexIsVisible(index);
  170. }
  171. else if(source == remove)
  172. {
  173. int index = list.getSelectedIndex();
  174. listModel.removeElementAt(index);
  175. updateButtons();
  176. }
  177. else if(source == moveUp)
  178. {
  179. int index = list.getSelectedIndex();
  180. Object selected = list.getSelectedValue();
  181. listModel.removeElementAt(index);
  182. listModel.insertElementAt(selected,index-1);
  183. list.setSelectedIndex(index-1);
  184. list.ensureIndexIsVisible(index - 1);
  185. }
  186. else if(source == moveDown)
  187. {
  188. int index = list.getSelectedIndex();
  189. Object selected = list.getSelectedValue();
  190. listModel.removeElementAt(index);
  191. listModel.insertElementAt(selected,index+1);
  192. list.setSelectedIndex(index+1);
  193. list.ensureIndexIsVisible(index+1);
  194. }
  195. }
  196. }
  197. class ListHandler implements ListSelectionListener
  198. {
  199. public void valueChanged(ListSelectionEvent evt)
  200. {
  201. updateButtons();
  202. }
  203. }
  204. }
  205. class ContextAddDialog extends EnhancedDialog
  206. {
  207. public ContextAddDialog(Component comp)
  208. {
  209. super(JOptionPane.getFrameForComponent(comp),
  210. jEdit.getProperty("options.context.add.title"),
  211. true);
  212. JPanel content = new JPanel(new BorderLayout());
  213. content.setBorder(new EmptyBorder(12,12,12,12));
  214. setContentPane(content);
  215. ActionHandler actionHandler = new ActionHandler();
  216. ButtonGroup grp = new ButtonGroup();
  217. JPanel typePanel = new JPanel(new GridLayout(3,1,6,6));
  218. typePanel.setBorder(new EmptyBorder(0,0,6,0));
  219. typePanel.add(new JLabel(
  220. jEdit.getProperty("options.context.add.caption")));
  221. separator = new JRadioButton(jEdit.getProperty("options.context"
  222. + ".add.separator"));
  223. separator.addActionListener(actionHandler);
  224. grp.add(separator);
  225. typePanel.add(separator);
  226. action = new JRadioButton(jEdit.getProperty("options.context"
  227. + ".add.action"));
  228. action.addActionListener(actionHandler);
  229. grp.add(action);
  230. action.setSelected(true);
  231. typePanel.add(action);
  232. content.add(BorderLayout.NORTH,typePanel);
  233. JPanel actionPanel = new JPanel(new BorderLayout(6,6));
  234. ActionSet[] actionsList = jEdit.getActionSets();
  235. Vector vec = new Vector(actionsList.length);
  236. for(int i = 0; i < actionsList.length; i++)
  237. {
  238. ActionSet actionSet = actionsList[i];
  239. if(actionSet.getActionCount() != 0)
  240. vec.addElement(actionSet);
  241. }
  242. combo = new JComboBox(vec);
  243. combo.addActionListener(actionHandler);
  244. actionPanel.add(BorderLayout.NORTH,combo);
  245. list = new JList();
  246. list.setVisibleRowCount(8);
  247. actionPanel.add(BorderLayout.CENTER,new JScrollPane(list));
  248. content.add(BorderLayout.CENTER,actionPanel);
  249. JPanel southPanel = new JPanel();
  250. southPanel.setLayout(new BoxLayout(southPanel,BoxLayout.X_AXIS));
  251. southPanel.setBorder(new EmptyBorder(12,0,0,0));
  252. southPanel.add(Box.createGlue());
  253. ok = new JButton(jEdit.getProperty("common.ok"));
  254. ok.addActionListener(actionHandler);
  255. getRootPane().setDefaultButton(ok);
  256. southPanel.add(ok);
  257. southPanel.add(Box.createHorizontalStrut(6));
  258. cancel = new JButton(jEdit.getProperty("common.cancel"));
  259. cancel.addActionListener(actionHandler);
  260. southPanel.add(cancel);
  261. southPanel.add(Box.createGlue());
  262. content.add(BorderLayout.SOUTH,southPanel);
  263. updateList();
  264. pack();
  265. setLocationRelativeTo(JOptionPane.getFrameForComponent(comp));
  266. show();
  267. }
  268. public void ok()
  269. {
  270. isOK = true;
  271. dispose();
  272. }
  273. public void cancel()
  274. {
  275. dispose();
  276. }
  277. public String getSelection()
  278. {
  279. if(!isOK)
  280. return null;
  281. if(separator.isSelected())
  282. return "-";
  283. else if(action.isSelected())
  284. {
  285. return ((ContextOptionPane.MenuItem)list.getSelectedValue())
  286. .actionName;
  287. }
  288. else
  289. throw new InternalError();
  290. }
  291. // private members
  292. private boolean isOK;
  293. private JRadioButton separator, action;
  294. private JComboBox combo;
  295. private JList list;
  296. private JButton ok, cancel;
  297. private void updateList()
  298. {
  299. ActionSet actionSet = (ActionSet)combo.getSelectedItem();
  300. EditAction[] actions = actionSet.getActions();
  301. Vector listModel = new Vector(actions.length);
  302. for(int i = 0; i < actions.length; i++)
  303. {
  304. EditAction action = actions[i];
  305. String label = action.getLabel();
  306. if(label == null)
  307. continue;
  308. listModel.addElement(new ContextOptionPane.MenuItem(
  309. action.getName(),label));
  310. }
  311. MiscUtilities.quicksort(listModel,new ContextOptionPane.MenuItemCompare());
  312. list.setListData(listModel);
  313. }
  314. class ActionHandler implements ActionListener
  315. {
  316. public void actionPerformed(ActionEvent evt)
  317. {
  318. Object source = evt.getSource();
  319. if(source instanceof JRadioButton)
  320. {
  321. combo.setEnabled(action.isSelected());
  322. list.setEnabled(action.isSelected());
  323. }
  324. if(source == ok)
  325. ok();
  326. else if(source == cancel)
  327. cancel();
  328. else if(source == combo)
  329. updateList();
  330. }
  331. }
  332. }