PageRenderTime 108ms CodeModel.GetById 88ms app.highlight 16ms RepoModel.GetById 1ms 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
  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 4386 2002-12-15 00:23:53Z spestov $
 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 = (String)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("options.context.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("options.context.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("options.context.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("options.context.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 MiscUtilities.Compare
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				updateButtons();
200			}
201			else if(source == moveUp)
202			{
203				int index = list.getSelectedIndex();
204				Object selected = list.getSelectedValue();
205				listModel.removeElementAt(index);
206				listModel.insertElementAt(selected,index-1);
207				list.setSelectedIndex(index-1);
208				list.ensureIndexIsVisible(index - 1);
209			}
210			else if(source == moveDown)
211			{
212				int index = list.getSelectedIndex();
213				Object selected = list.getSelectedValue();
214				listModel.removeElementAt(index);
215				listModel.insertElementAt(selected,index+1);
216				list.setSelectedIndex(index+1);
217				list.ensureIndexIsVisible(index+1);
218			}
219		}
220	}
221
222	class ListHandler implements ListSelectionListener
223	{
224		public void valueChanged(ListSelectionEvent evt)
225		{
226			updateButtons();
227		}
228	}
229}
230
231class ContextAddDialog extends EnhancedDialog
232{
233	public ContextAddDialog(Component comp)
234	{
235		super(GUIUtilities.getParentDialog(comp),
236			jEdit.getProperty("options.context.add.title"),
237			true);
238
239		JPanel content = new JPanel(new BorderLayout());
240		content.setBorder(new EmptyBorder(12,12,12,12));
241		setContentPane(content);
242
243		ActionHandler actionHandler = new ActionHandler();
244		ButtonGroup grp = new ButtonGroup();
245
246		JPanel typePanel = new JPanel(new GridLayout(3,1,6,6));
247		typePanel.setBorder(new EmptyBorder(0,0,6,0));
248		typePanel.add(new JLabel(
249			jEdit.getProperty("options.context.add.caption")));
250
251		separator = new JRadioButton(jEdit.getProperty("options.context"
252			+ ".add.separator"));
253		separator.addActionListener(actionHandler);
254		grp.add(separator);
255		typePanel.add(separator);
256
257		action = new JRadioButton(jEdit.getProperty("options.context"
258			+ ".add.action"));
259		action.addActionListener(actionHandler);
260		grp.add(action);
261		action.setSelected(true);
262		typePanel.add(action);
263
264		content.add(BorderLayout.NORTH,typePanel);
265
266		JPanel actionPanel = new JPanel(new BorderLayout(6,6));
267
268		ActionSet[] actionsList = jEdit.getActionSets();
269		Vector vec = new Vector(actionsList.length);
270		for(int i = 0; i < actionsList.length; i++)
271		{
272			ActionSet actionSet = actionsList[i];
273			if(actionSet.getActionCount() != 0)
274				vec.addElement(actionSet);
275		}
276		combo = new JComboBox(vec);
277		combo.addActionListener(actionHandler);
278		actionPanel.add(BorderLayout.NORTH,combo);
279
280		list = new JList();
281		list.setVisibleRowCount(8);
282		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
283		actionPanel.add(BorderLayout.CENTER,new JScrollPane(list));
284
285		content.add(BorderLayout.CENTER,actionPanel);
286
287		JPanel southPanel = new JPanel();
288		southPanel.setLayout(new BoxLayout(southPanel,BoxLayout.X_AXIS));
289		southPanel.setBorder(new EmptyBorder(12,0,0,0));
290		southPanel.add(Box.createGlue());
291		ok = new JButton(jEdit.getProperty("common.ok"));
292		ok.addActionListener(actionHandler);
293		getRootPane().setDefaultButton(ok);
294		southPanel.add(ok);
295		southPanel.add(Box.createHorizontalStrut(6));
296		cancel = new JButton(jEdit.getProperty("common.cancel"));
297		cancel.addActionListener(actionHandler);
298		southPanel.add(cancel);
299		southPanel.add(Box.createGlue());
300
301		content.add(BorderLayout.SOUTH,southPanel);
302
303		updateList();
304
305		pack();
306		setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
307		show();
308	}
309
310	public void ok()
311	{
312		isOK = true;
313		dispose();
314	}
315
316	public void cancel()
317	{
318		dispose();
319	}
320
321	public String getSelection()
322	{
323		if(!isOK)
324			return null;
325
326		if(separator.isSelected())
327			return "-";
328		else if(action.isSelected())
329		{
330			return ((ContextOptionPane.MenuItem)list.getSelectedValue())
331				.actionName;
332		}
333		else
334			throw new InternalError();
335	}
336
337	// private members
338	private boolean isOK;
339	private JRadioButton separator, action;
340	private JComboBox combo;
341	private JList list;
342	private JButton ok, cancel;
343
344	private void updateList()
345	{
346		ActionSet actionSet = (ActionSet)combo.getSelectedItem();
347		EditAction[] actions = actionSet.getActions();
348		Vector listModel = new Vector(actions.length);
349
350		for(int i = 0; i < actions.length; i++)
351		{
352			EditAction action = actions[i];
353			String label = action.getLabel();
354			if(label == null)
355				continue;
356
357			listModel.addElement(new ContextOptionPane.MenuItem(
358				action.getName(),label));
359		}
360
361		MiscUtilities.quicksort(listModel,new ContextOptionPane.MenuItemCompare());
362
363		list.setListData(listModel);
364	}
365
366	class ActionHandler implements ActionListener
367	{
368		public void actionPerformed(ActionEvent evt)
369		{
370			Object source = evt.getSource();
371			if(source instanceof JRadioButton)
372			{
373				combo.setEnabled(action.isSelected());
374				list.setEnabled(action.isSelected());
375			}
376			if(source == ok)
377				ok();
378			else if(source == cancel)
379				cancel();
380			else if(source == combo)
381				updateList();
382		}
383	}
384}