PageRenderTime 136ms CodeModel.GetById 122ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/options/ShortcutsOptionPane.java

#
Java | 357 lines | 278 code | 50 blank | 29 comment | 39 complexity | d48bff4106b8ccdc93c4664abd0ef449 MD5 | raw file
  1/*
  2 * ShortcutsOptionPane.java - Shortcuts options panel
  3 * Copyright (C) 1999, 2000, 2001 Slava Pestov
  4 * Copyright (C) 2001 Dirk Moebius
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version 2
  9 * of the License, or any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 19 */
 20
 21package org.gjt.sp.jedit.options;
 22
 23import javax.swing.table.*;
 24import javax.swing.*;
 25import java.awt.event.*;
 26import java.awt.*;
 27import java.util.*;
 28import org.gjt.sp.jedit.gui.GrabKeyDialog;
 29import org.gjt.sp.jedit.*;
 30
 31/**
 32 * Key binding editor.
 33 * @author Slava Pestov
 34 * @version $Id: ShortcutsOptionPane.java 3828 2001-10-07 10:42:45Z spestov $
 35 */
 36public class ShortcutsOptionPane extends AbstractOptionPane
 37{
 38	public ShortcutsOptionPane()
 39	{
 40		super("shortcuts");
 41	}
 42
 43	// protected members
 44	protected void _init()
 45	{
 46		allBindings = new Vector();
 47
 48		setLayout(new BorderLayout(12,12));
 49
 50		initModels();
 51
 52		selectModel = new JComboBox(models);
 53		selectModel.addActionListener(new ActionHandler());
 54
 55		Box north = Box.createHorizontalBox();
 56		north.add(new JLabel(jEdit.getProperty(
 57			"options.shortcuts.select.label")));
 58		north.add(Box.createHorizontalStrut(6));
 59		north.add(selectModel);
 60
 61		keyTable = new JTable(currentModel);
 62		keyTable.getTableHeader().setReorderingAllowed(false);
 63		keyTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
 64		keyTable.addMouseListener(new TableMouseHandler());
 65		Dimension d = keyTable.getPreferredSize();
 66		d.height = Math.min(d.height,200);
 67		JScrollPane scroller = new JScrollPane(keyTable);
 68		scroller.setPreferredSize(d);
 69
 70		add(BorderLayout.NORTH,north);
 71		add(BorderLayout.CENTER,scroller);
 72	}
 73
 74	protected void _save()
 75	{
 76		if(keyTable.getCellEditor() != null)
 77			keyTable.getCellEditor().stopCellEditing();
 78
 79		Enumeration e = models.elements();
 80		while(e.hasMoreElements())
 81			((ShortcutsModel)e.nextElement()).save();
 82
 83		Macros.loadMacros();
 84	}
 85
 86	private void initModels()
 87	{
 88		models = new Vector();
 89		ActionSet[] actionSets = jEdit.getActionSets();
 90		for(int i = 0; i < actionSets.length; i++)
 91		{
 92			ActionSet actionSet = actionSets[i];
 93			if(actionSet.getActionCount() != 0)
 94			{
 95				models.addElement(createModel(actionSet.getLabel(),
 96					actionSet.getActions()));
 97			}
 98		}
 99		currentModel = (ShortcutsModel)models.elementAt(0);
100	}
101
102	private ShortcutsModel createModel(String modelLabel, EditAction[] actions)
103	{
104		Vector bindings = new Vector(actions.length);
105
106		for(int i = 0; i < actions.length; i++)
107		{
108			EditAction action = actions[i];
109
110			String name = action.getName();
111			String label = action.getLabel();
112			// Skip certain actions this way (ENTER, TAB)
113			if(label == null)
114				continue;
115
116			label = GUIUtilities.prettifyMenuLabel(label);
117			addBindings(name,label,bindings);
118		}
119
120		return new ShortcutsModel(modelLabel,bindings);
121	}
122
123	private void addBindings(String name, String label, Vector bindings)
124	{
125		GrabKeyDialog.KeyBinding b[] = new GrabKeyDialog.KeyBinding[2];
126
127		b[0] = createBinding(name,label,
128			jEdit.getProperty(name + ".shortcut"));
129		b[1] = createBinding(name,label,
130			jEdit.getProperty(name + ".shortcut2"));
131
132		bindings.addElement(b);
133	}
134
135	private GrabKeyDialog.KeyBinding createBinding(String name,
136		String label, String shortcut)
137	{
138		if(shortcut != null && shortcut.length() == 0)
139			shortcut = null;
140
141		GrabKeyDialog.KeyBinding binding
142			= new GrabKeyDialog.KeyBinding(name,label,shortcut,false);
143
144		allBindings.addElement(binding);
145		return binding;
146	}
147
148	// private members
149	private JTable keyTable;
150	private Vector models;
151	private ShortcutsModel currentModel;
152	private JComboBox selectModel;
153	private Vector allBindings;
154
155	class HeaderMouseHandler extends MouseAdapter
156	{
157		public void mouseClicked(MouseEvent evt)
158		{
159			switch(keyTable.getTableHeader().columnAtPoint(evt.getPoint()))
160			{
161			case 0:
162				currentModel.sort(0);
163				break;
164			case 1:
165				currentModel.sort(1);
166				break;
167			case 2:
168				currentModel.sort(2);
169				break;
170			}
171		}
172	}
173
174	class TableMouseHandler extends MouseAdapter
175	{
176		public void mouseClicked(MouseEvent evt)
177		{
178			int row = keyTable.getSelectedRow();
179			int col = keyTable.getSelectedColumn();
180			if(col != 0 && row != -1)
181			{
182				 GrabKeyDialog gkd = new GrabKeyDialog(
183					ShortcutsOptionPane.this,
184					currentModel.getBindingAt(row,col-1),
185					allBindings);
186				if(gkd.isOK())
187					currentModel.setValueAt(
188						gkd.getShortcut(),row,col);
189			}
190		}
191	}
192
193	class ActionHandler implements ActionListener
194	{
195		public void actionPerformed(ActionEvent evt)
196		{
197			ShortcutsModel newModel
198				= (ShortcutsModel)selectModel.getSelectedItem();
199
200			if(currentModel != newModel)
201			{
202				currentModel = newModel;
203				keyTable.setModel(currentModel);
204			}
205		}
206	}
207
208	class ShortcutsModel extends AbstractTableModel
209	{
210		private Vector bindings;
211		private String name;
212
213		ShortcutsModel(String name, Vector bindings)
214		{
215			this.name = name;
216			this.bindings = bindings;
217			sort(0);
218		}
219
220		public void sort(int col)
221		{
222			MiscUtilities.quicksort(bindings,new KeyCompare(col));
223			fireTableDataChanged();
224		}
225
226		public int getColumnCount()
227		{
228			return 3;
229		}
230
231		public int getRowCount()
232		{
233			return bindings.size();
234		}
235
236		public Object getValueAt(int row, int col)
237		{
238			switch(col)
239			{
240			case 0:
241				return getBindingAt(row,0).label;
242			case 1:
243				return getBindingAt(row,0).shortcut;
244			case 2:
245				return getBindingAt(row,1).shortcut;
246			default:
247				return null;
248			}
249		}
250
251		public void setValueAt(Object value, int row, int col)
252		{
253			if(col == 0)
254				return;
255
256			getBindingAt(row,col-1).shortcut = (String)value;
257
258			// redraw the whole table because a second shortcut
259			// might have changed, too
260			fireTableDataChanged();
261		}
262
263		public String getColumnName(int index)
264		{
265			switch(index)
266			{
267			case 0:
268				return jEdit.getProperty("options.shortcuts.name");
269			case 1:
270				return jEdit.getProperty("options.shortcuts.shortcut1");
271			case 2:
272				return jEdit.getProperty("options.shortcuts.shortcut2");
273			default:
274				return null;
275			}
276		}
277
278		public void save()
279		{
280			Enumeration enum = bindings.elements();
281			while(enum.hasMoreElements())
282			{
283				GrabKeyDialog.KeyBinding binding[]
284					= (GrabKeyDialog.KeyBinding[])
285						enum.nextElement();
286				jEdit.setProperty(
287					binding[0].name + ".shortcut",
288					binding[0].shortcut);
289				jEdit.setProperty(
290					binding[1].name + ".shortcut2",
291					binding[1].shortcut);
292			}
293		}
294
295		public GrabKeyDialog.KeyBinding getBindingAt(int row, int nr)
296		{
297			GrabKeyDialog.KeyBinding binding[]
298				= (GrabKeyDialog.KeyBinding[])
299					bindings.elementAt(row);
300			return binding[nr];
301		}
302
303		public String toString()
304		{
305			return name;
306		}
307
308		class KeyCompare implements MiscUtilities.Compare
309		{
310			int col;
311
312			KeyCompare(int col)
313			{
314				this.col = col;
315			}
316
317			public int compare(Object obj1, Object obj2)
318			{
319				GrabKeyDialog.KeyBinding[] k1
320					= (GrabKeyDialog.KeyBinding[])obj1;
321				GrabKeyDialog.KeyBinding[] k2
322					= (GrabKeyDialog.KeyBinding[])obj2;
323
324				String label1 = k1[0].label.toLowerCase();
325				String label2 = k2[0].label.toLowerCase();
326
327				if(col == 0)
328					return MiscUtilities.compareStrings(
329						label1,label2,true);
330				else
331				{
332					String shortcut1, shortcut2;
333
334					if(col == 1)
335					{
336						shortcut1 = k1[0].shortcut;
337						shortcut2 = k2[0].shortcut;
338					}
339					else
340					{
341						shortcut1 = k1[1].shortcut;
342						shortcut2 = k2[1].shortcut;
343					}
344
345					if(shortcut1 == null && shortcut2 != null)
346						return 1;
347					else if(shortcut2 == null && shortcut1 != null)
348						return -1;
349					else if(shortcut1 == null && shortcut2 == null)
350						return MiscUtilities.compareStrings(label1,label2,true);
351					else
352						return MiscUtilities.compareStrings(shortcut1,shortcut2,true);
353				}
354			}
355		}
356	}
357}