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