PageRenderTime 134ms CodeModel.GetById 97ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

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

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