/plugins/ProjectViewer/tags/pv_2_1_3_2/projectviewer/config/ContextOptionPane.java

# · Java · 431 lines · 327 code · 55 blank · 49 comment · 54 complexity · 31e19577128be47145d96067d3972113 MD5 · raw file

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