PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 390 lines | 316 code | 48 blank | 26 comment | 51 complexity | 08a7582a81c5fc923d4fd2d0111be155 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 5487 2006-06-23 22:58:12Z kpouer $
  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 = 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("common.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("common.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("common.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("common.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 Comparator
  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. if(listModel.getSize() != 0)
  179. {
  180. list.setSelectedIndex(
  181. Math.min(listModel.getSize()-1,
  182. index));
  183. }
  184. updateButtons();
  185. }
  186. else if(source == moveUp)
  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. else if(source == moveDown)
  196. {
  197. int index = list.getSelectedIndex();
  198. Object selected = list.getSelectedValue();
  199. listModel.removeElementAt(index);
  200. listModel.insertElementAt(selected,index+1);
  201. list.setSelectedIndex(index+1);
  202. list.ensureIndexIsVisible(index+1);
  203. }
  204. }
  205. }
  206. class ListHandler implements ListSelectionListener
  207. {
  208. public void valueChanged(ListSelectionEvent evt)
  209. {
  210. updateButtons();
  211. }
  212. }
  213. }
  214. class ContextAddDialog extends EnhancedDialog
  215. {
  216. public ContextAddDialog(Component comp)
  217. {
  218. super(GUIUtilities.getParentDialog(comp),
  219. jEdit.getProperty("options.context.add.title"),
  220. true);
  221. JPanel content = new JPanel(new BorderLayout());
  222. content.setBorder(new EmptyBorder(12,12,12,12));
  223. setContentPane(content);
  224. ActionHandler actionHandler = new ActionHandler();
  225. ButtonGroup grp = new ButtonGroup();
  226. JPanel typePanel = new JPanel(new GridLayout(3,1,6,6));
  227. typePanel.setBorder(new EmptyBorder(0,0,6,0));
  228. typePanel.add(new JLabel(
  229. jEdit.getProperty("options.context.add.caption")));
  230. separator = new JRadioButton(jEdit.getProperty("options.context"
  231. + ".add.separator"));
  232. separator.addActionListener(actionHandler);
  233. grp.add(separator);
  234. typePanel.add(separator);
  235. action = new JRadioButton(jEdit.getProperty("options.context"
  236. + ".add.action"));
  237. action.addActionListener(actionHandler);
  238. grp.add(action);
  239. action.setSelected(true);
  240. typePanel.add(action);
  241. content.add(BorderLayout.NORTH,typePanel);
  242. JPanel actionPanel = new JPanel(new BorderLayout(6,6));
  243. ActionSet[] actionsList = jEdit.getActionSets();
  244. Vector vec = new Vector(actionsList.length);
  245. for(int i = 0; i < actionsList.length; i++)
  246. {
  247. ActionSet actionSet = actionsList[i];
  248. if(actionSet.getActionCount() != 0)
  249. vec.addElement(actionSet);
  250. }
  251. combo = new JComboBox(vec);
  252. combo.addActionListener(actionHandler);
  253. actionPanel.add(BorderLayout.NORTH,combo);
  254. list = new JList();
  255. list.setVisibleRowCount(8);
  256. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  257. actionPanel.add(BorderLayout.CENTER,new JScrollPane(list));
  258. content.add(BorderLayout.CENTER,actionPanel);
  259. JPanel southPanel = new JPanel();
  260. southPanel.setLayout(new BoxLayout(southPanel,BoxLayout.X_AXIS));
  261. southPanel.setBorder(new EmptyBorder(12,0,0,0));
  262. southPanel.add(Box.createGlue());
  263. ok = new JButton(jEdit.getProperty("common.ok"));
  264. ok.addActionListener(actionHandler);
  265. getRootPane().setDefaultButton(ok);
  266. southPanel.add(ok);
  267. southPanel.add(Box.createHorizontalStrut(6));
  268. cancel = new JButton(jEdit.getProperty("common.cancel"));
  269. cancel.addActionListener(actionHandler);
  270. southPanel.add(cancel);
  271. southPanel.add(Box.createGlue());
  272. content.add(BorderLayout.SOUTH,southPanel);
  273. updateList();
  274. pack();
  275. setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
  276. setVisible(true);
  277. }
  278. public void ok()
  279. {
  280. isOK = true;
  281. dispose();
  282. }
  283. public void cancel()
  284. {
  285. dispose();
  286. }
  287. public String getSelection()
  288. {
  289. if(!isOK)
  290. return null;
  291. if(separator.isSelected())
  292. return "-";
  293. else if(action.isSelected())
  294. {
  295. return ((ContextOptionPane.MenuItem)list.getSelectedValue())
  296. .actionName;
  297. }
  298. else
  299. throw new InternalError();
  300. }
  301. // private members
  302. private boolean isOK;
  303. private JRadioButton separator, action;
  304. private JComboBox combo;
  305. private JList list;
  306. private JButton ok, cancel;
  307. private void updateList()
  308. {
  309. ActionSet actionSet = (ActionSet)combo.getSelectedItem();
  310. EditAction[] actions = actionSet.getActions();
  311. Vector listModel = new Vector(actions.length);
  312. for(int i = 0; i < actions.length; i++)
  313. {
  314. EditAction action = actions[i];
  315. String label = action.getLabel();
  316. if(label == null)
  317. continue;
  318. listModel.addElement(new ContextOptionPane.MenuItem(
  319. action.getName(),label));
  320. }
  321. Collections.sort(listModel,new ContextOptionPane.MenuItemCompare());
  322. list.setListData(listModel);
  323. }
  324. class ActionHandler implements ActionListener
  325. {
  326. public void actionPerformed(ActionEvent evt)
  327. {
  328. Object source = evt.getSource();
  329. if(source instanceof JRadioButton)
  330. {
  331. combo.setEnabled(action.isSelected());
  332. list.setEnabled(action.isSelected());
  333. }
  334. if(source == ok)
  335. ok();
  336. else if(source == cancel)
  337. cancel();
  338. else if(source == combo)
  339. updateList();
  340. }
  341. }
  342. }