PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/options/ContextOptionPane.java

#
Java | 384 lines | 310 code | 48 blank | 26 comment | 49 complexity | fb78a6431a9cd532834c456db142e255 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 4386 2002-12-15 00:23:53Z 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. ActionHandler actionHandler = new ActionHandler();
  73. add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
  74. add.setToolTipText(jEdit.getProperty("options.context.add"));
  75. add.addActionListener(actionHandler);
  76. buttons.add(add);
  77. buttons.add(Box.createHorizontalStrut(6));
  78. remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
  79. remove.setToolTipText(jEdit.getProperty("options.context.remove"));
  80. remove.addActionListener(actionHandler);
  81. buttons.add(remove);
  82. buttons.add(Box.createHorizontalStrut(6));
  83. moveUp = new RolloverButton(GUIUtilities.loadIcon("ArrowU.png"));
  84. moveUp.setToolTipText(jEdit.getProperty("options.context.moveUp"));
  85. moveUp.addActionListener(actionHandler);
  86. buttons.add(moveUp);
  87. buttons.add(Box.createHorizontalStrut(6));
  88. moveDown = new RolloverButton(GUIUtilities.loadIcon("ArrowD.png"));
  89. moveDown.setToolTipText(jEdit.getProperty("options.context.moveDown"));
  90. moveDown.addActionListener(actionHandler);
  91. buttons.add(moveDown);
  92. buttons.add(Box.createGlue());
  93. updateButtons();
  94. add(BorderLayout.SOUTH,buttons);
  95. }
  96. static class MenuItemCompare implements MiscUtilities.Compare
  97. {
  98. public int compare(Object obj1, Object obj2)
  99. {
  100. return MiscUtilities.compareStrings(
  101. ((MenuItem)obj1).label,
  102. ((MenuItem)obj2).label,
  103. true);
  104. }
  105. }
  106. protected void _save()
  107. {
  108. StringBuffer buf = new StringBuffer();
  109. for(int i = 0; i < listModel.getSize(); i++)
  110. {
  111. if(i != 0)
  112. buf.append(' ');
  113. buf.append(((MenuItem)listModel.elementAt(i)).actionName);
  114. }
  115. jEdit.setProperty("view.context",buf.toString());
  116. }
  117. // private members
  118. private DefaultListModel listModel;
  119. private JList list;
  120. private JButton add;
  121. private JButton remove;
  122. private JButton moveUp, moveDown;
  123. private void updateButtons()
  124. {
  125. int index = list.getSelectedIndex();
  126. remove.setEnabled(index != -1 && listModel.getSize() != 0);
  127. moveUp.setEnabled(index > 0);
  128. moveDown.setEnabled(index != -1 && index != listModel.getSize() - 1);
  129. }
  130. static class MenuItem
  131. {
  132. String actionName;
  133. String label;
  134. MenuItem(String actionName, String label)
  135. {
  136. this.actionName = actionName;
  137. this.label = GUIUtilities.prettifyMenuLabel(label);
  138. }
  139. public String toString()
  140. {
  141. return label;
  142. }
  143. }
  144. class ActionHandler implements ActionListener
  145. {
  146. public void actionPerformed(ActionEvent evt)
  147. {
  148. Object source = evt.getSource();
  149. if(source == add)
  150. {
  151. ContextAddDialog dialog = new ContextAddDialog(
  152. ContextOptionPane.this);
  153. String selection = dialog.getSelection();
  154. if(selection == null)
  155. return;
  156. int index = list.getSelectedIndex();
  157. if(index == -1)
  158. index = listModel.getSize();
  159. else
  160. index++;
  161. MenuItem menuItem;
  162. if(selection.equals("-"))
  163. menuItem = new ContextOptionPane.MenuItem("-","-");
  164. else
  165. {
  166. menuItem = new ContextOptionPane.MenuItem(selection,
  167. jEdit.getAction(selection)
  168. .getLabel());
  169. }
  170. listModel.insertElementAt(menuItem,index);
  171. list.setSelectedIndex(index);
  172. list.ensureIndexIsVisible(index);
  173. }
  174. else if(source == remove)
  175. {
  176. int index = list.getSelectedIndex();
  177. listModel.removeElementAt(index);
  178. updateButtons();
  179. }
  180. else if(source == moveUp)
  181. {
  182. int index = list.getSelectedIndex();
  183. Object selected = list.getSelectedValue();
  184. listModel.removeElementAt(index);
  185. listModel.insertElementAt(selected,index-1);
  186. list.setSelectedIndex(index-1);
  187. list.ensureIndexIsVisible(index - 1);
  188. }
  189. else if(source == moveDown)
  190. {
  191. int index = list.getSelectedIndex();
  192. Object selected = list.getSelectedValue();
  193. listModel.removeElementAt(index);
  194. listModel.insertElementAt(selected,index+1);
  195. list.setSelectedIndex(index+1);
  196. list.ensureIndexIsVisible(index+1);
  197. }
  198. }
  199. }
  200. class ListHandler implements ListSelectionListener
  201. {
  202. public void valueChanged(ListSelectionEvent evt)
  203. {
  204. updateButtons();
  205. }
  206. }
  207. }
  208. class ContextAddDialog extends EnhancedDialog
  209. {
  210. public ContextAddDialog(Component comp)
  211. {
  212. super(GUIUtilities.getParentDialog(comp),
  213. jEdit.getProperty("options.context.add.title"),
  214. true);
  215. JPanel content = new JPanel(new BorderLayout());
  216. content.setBorder(new EmptyBorder(12,12,12,12));
  217. setContentPane(content);
  218. ActionHandler actionHandler = new ActionHandler();
  219. ButtonGroup grp = new ButtonGroup();
  220. JPanel typePanel = new JPanel(new GridLayout(3,1,6,6));
  221. typePanel.setBorder(new EmptyBorder(0,0,6,0));
  222. typePanel.add(new JLabel(
  223. jEdit.getProperty("options.context.add.caption")));
  224. separator = new JRadioButton(jEdit.getProperty("options.context"
  225. + ".add.separator"));
  226. separator.addActionListener(actionHandler);
  227. grp.add(separator);
  228. typePanel.add(separator);
  229. action = new JRadioButton(jEdit.getProperty("options.context"
  230. + ".add.action"));
  231. action.addActionListener(actionHandler);
  232. grp.add(action);
  233. action.setSelected(true);
  234. typePanel.add(action);
  235. content.add(BorderLayout.NORTH,typePanel);
  236. JPanel actionPanel = new JPanel(new BorderLayout(6,6));
  237. ActionSet[] actionsList = jEdit.getActionSets();
  238. Vector vec = new Vector(actionsList.length);
  239. for(int i = 0; i < actionsList.length; i++)
  240. {
  241. ActionSet actionSet = actionsList[i];
  242. if(actionSet.getActionCount() != 0)
  243. vec.addElement(actionSet);
  244. }
  245. combo = new JComboBox(vec);
  246. combo.addActionListener(actionHandler);
  247. actionPanel.add(BorderLayout.NORTH,combo);
  248. list = new JList();
  249. list.setVisibleRowCount(8);
  250. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  251. actionPanel.add(BorderLayout.CENTER,new JScrollPane(list));
  252. content.add(BorderLayout.CENTER,actionPanel);
  253. JPanel southPanel = new JPanel();
  254. southPanel.setLayout(new BoxLayout(southPanel,BoxLayout.X_AXIS));
  255. southPanel.setBorder(new EmptyBorder(12,0,0,0));
  256. southPanel.add(Box.createGlue());
  257. ok = new JButton(jEdit.getProperty("common.ok"));
  258. ok.addActionListener(actionHandler);
  259. getRootPane().setDefaultButton(ok);
  260. southPanel.add(ok);
  261. southPanel.add(Box.createHorizontalStrut(6));
  262. cancel = new JButton(jEdit.getProperty("common.cancel"));
  263. cancel.addActionListener(actionHandler);
  264. southPanel.add(cancel);
  265. southPanel.add(Box.createGlue());
  266. content.add(BorderLayout.SOUTH,southPanel);
  267. updateList();
  268. pack();
  269. setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
  270. show();
  271. }
  272. public void ok()
  273. {
  274. isOK = true;
  275. dispose();
  276. }
  277. public void cancel()
  278. {
  279. dispose();
  280. }
  281. public String getSelection()
  282. {
  283. if(!isOK)
  284. return null;
  285. if(separator.isSelected())
  286. return "-";
  287. else if(action.isSelected())
  288. {
  289. return ((ContextOptionPane.MenuItem)list.getSelectedValue())
  290. .actionName;
  291. }
  292. else
  293. throw new InternalError();
  294. }
  295. // private members
  296. private boolean isOK;
  297. private JRadioButton separator, action;
  298. private JComboBox combo;
  299. private JList list;
  300. private JButton ok, cancel;
  301. private void updateList()
  302. {
  303. ActionSet actionSet = (ActionSet)combo.getSelectedItem();
  304. EditAction[] actions = actionSet.getActions();
  305. Vector listModel = new Vector(actions.length);
  306. for(int i = 0; i < actions.length; i++)
  307. {
  308. EditAction action = actions[i];
  309. String label = action.getLabel();
  310. if(label == null)
  311. continue;
  312. listModel.addElement(new ContextOptionPane.MenuItem(
  313. action.getName(),label));
  314. }
  315. MiscUtilities.quicksort(listModel,new ContextOptionPane.MenuItemCompare());
  316. list.setListData(listModel);
  317. }
  318. class ActionHandler implements ActionListener
  319. {
  320. public void actionPerformed(ActionEvent evt)
  321. {
  322. Object source = evt.getSource();
  323. if(source instanceof JRadioButton)
  324. {
  325. combo.setEnabled(action.isSelected());
  326. list.setEnabled(action.isSelected());
  327. }
  328. if(source == ok)
  329. ok();
  330. else if(source == cancel)
  331. cancel();
  332. else if(source == combo)
  333. updateList();
  334. }
  335. }
  336. }