PageRenderTime 93ms CodeModel.GetById 46ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 367 lines | 288 code | 50 blank | 29 comment | 37 complexity | adf3b36ea2f6069376949cf6523e758f 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.*;
 30import org.gjt.sp.util.Log;
 31
 32/**
 33 * Key binding editor.
 34 * @author Slava Pestov
 35 * @version $Id: ShortcutsOptionPane.java 5527 2006-07-04 18:49:40Z ezust $
 36 */
 37public class ShortcutsOptionPane extends AbstractOptionPane
 38{
 39	public ShortcutsOptionPane()
 40	{
 41		super("shortcuts");
 42	}
 43
 44	// protected members
 45	protected void _init()
 46	{
 47		allBindings = new Vector();
 48
 49		setLayout(new BorderLayout(12,12));
 50
 51		initModels();
 52
 53		selectModel = new JComboBox(models);
 54		selectModel.addActionListener(new ActionHandler());
 55		selectModel.setToolTipText(jEdit.getProperty("options.shortcuts.select.tooltip"));
 56		Box north = Box.createHorizontalBox();
 57		north.add(new JLabel(jEdit.getProperty(
 58			"options.shortcuts.select.label")));
 59		north.add(Box.createHorizontalStrut(6));
 60		north.add(selectModel);
 61
 62		keyTable = new JTable(currentModel);
 63		keyTable.getTableHeader().setReorderingAllowed(false);
 64		keyTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
 65		keyTable.addMouseListener(new TableMouseHandler());
 66		Dimension d = keyTable.getPreferredSize();
 67		d.height = Math.min(d.height,200);
 68		JScrollPane scroller = new JScrollPane(keyTable);
 69		scroller.setPreferredSize(d);
 70
 71		add(BorderLayout.NORTH,north);
 72		add(BorderLayout.CENTER,scroller);
 73		selectModel.setSelectedIndex(jEdit.getIntegerProperty("options.shortcuts.select.index", 0));
 74	}
 75
 76	protected void _save()
 77	{
 78		if(keyTable.getCellEditor() != null)
 79			keyTable.getCellEditor().stopCellEditing();
 80
 81		Enumeration e = models.elements();
 82		while(e.hasMoreElements())
 83			((ShortcutsModel)e.nextElement()).save();
 84
 85		Macros.loadMacros();
 86	}
 87
 88	private void initModels()
 89	{
 90		models = new Vector();
 91		ActionSet[] actionSets = jEdit.getActionSets();
 92		for(int i = 0; i < actionSets.length; i++)
 93		{
 94			ActionSet actionSet = actionSets[i];
 95			if(actionSet.getActionCount() != 0)
 96			{
 97				String modelLabel = actionSet.getLabel();
 98				if(modelLabel == null)
 99				{
100					Log.log(Log.ERROR,this,"Empty action set: "
101						+ actionSet.getPluginJAR());
102				}
103				models.addElement(createModel(modelLabel,
104					actionSet.getActionNames()));
105			}
106		}
107		Collections.sort(models,new MiscUtilities.StringICaseCompare());
108		currentModel = (ShortcutsModel)models.elementAt(0);
109	}
110
111	private ShortcutsModel createModel(String modelLabel, String[] actions)
112	{
113		Vector bindings = new Vector(actions.length);
114
115		for(int i = 0; i < actions.length; i++)
116		{
117			String name = actions[i];
118			String label = jEdit.getProperty(actions[i] + ".label");
119			// Skip certain actions this way
120			if(label == null)
121				continue;
122
123			label = GUIUtilities.prettifyMenuLabel(label);
124			addBindings(name,label,bindings);
125		}
126
127		return new ShortcutsModel(modelLabel,bindings);
128	}
129
130	private void addBindings(String name, String label, Vector bindings)
131	{
132		GrabKeyDialog.KeyBinding b[] = new GrabKeyDialog.KeyBinding[2];
133
134		b[0] = createBinding(name,label,
135			jEdit.getProperty(name + ".shortcut"));
136		b[1] = createBinding(name,label,
137			jEdit.getProperty(name + ".shortcut2"));
138
139		bindings.addElement(b);
140	}
141
142	private GrabKeyDialog.KeyBinding createBinding(String name,
143		String label, String shortcut)
144	{
145		if(shortcut != null && shortcut.length() == 0)
146			shortcut = null;
147
148		GrabKeyDialog.KeyBinding binding
149			= new GrabKeyDialog.KeyBinding(name,label,shortcut,false);
150
151		allBindings.addElement(binding);
152		return binding;
153	}
154
155	// private members
156	private JTable keyTable;
157	private Vector models;
158	private ShortcutsModel currentModel;
159	private JComboBox selectModel;
160	private Vector allBindings;
161
162	class HeaderMouseHandler extends MouseAdapter
163	{
164		public void mouseClicked(MouseEvent evt)
165		{
166			switch(keyTable.getTableHeader().columnAtPoint(evt.getPoint()))
167			{
168			case 0:
169				currentModel.sort(0);
170				break;
171			case 1:
172				currentModel.sort(1);
173				break;
174			case 2:
175				currentModel.sort(2);
176				break;
177			}
178		}
179	}
180
181	class TableMouseHandler extends MouseAdapter
182	{
183		public void mouseClicked(MouseEvent evt)
184		{
185			int row = keyTable.getSelectedRow();
186			int col = keyTable.getSelectedColumn();
187			if(col != 0 && row != -1)
188			{
189				 GrabKeyDialog gkd = new GrabKeyDialog(
190					GUIUtilities.getParentDialog(
191					ShortcutsOptionPane.this),
192					currentModel.getBindingAt(row,col-1),
193					allBindings,null);
194				if(gkd.isOK())
195					currentModel.setValueAt(
196						gkd.getShortcut(),row,col);
197			}
198		}
199	}
200
201	class ActionHandler implements ActionListener
202	{
203		public void actionPerformed(ActionEvent evt)
204		{
205			ShortcutsModel newModel
206				= (ShortcutsModel)selectModel.getSelectedItem();
207			if(currentModel != newModel)
208			{
209				jEdit.setIntegerProperty("options.shortcuts.select.index", selectModel.getSelectedIndex());
210				currentModel = newModel;
211				keyTable.setModel(currentModel);
212			}
213		}
214	}
215
216	class ShortcutsModel extends AbstractTableModel
217	{
218		private Vector bindings;
219		private String name;
220
221		ShortcutsModel(String name, Vector bindings)
222		{
223			this.name = name;
224			this.bindings = bindings;
225			sort(0);
226		}
227
228		public void sort(int col)
229		{
230			Collections.sort(bindings,new KeyCompare(col));
231			fireTableDataChanged();
232		}
233
234		public int getColumnCount()
235		{
236			return 3;
237		}
238
239		public int getRowCount()
240		{
241			return bindings.size();
242		}
243
244		public Object getValueAt(int row, int col)
245		{
246			switch(col)
247			{
248			case 0:
249				return getBindingAt(row,0).label;
250			case 1:
251				return getBindingAt(row,0).shortcut;
252			case 2:
253				return getBindingAt(row,1).shortcut;
254			default:
255				return null;
256			}
257		}
258
259		public void setValueAt(Object value, int row, int col)
260		{
261			if(col == 0)
262				return;
263
264			getBindingAt(row,col-1).shortcut = (String)value;
265
266			// redraw the whole table because a second shortcut
267			// might have changed, too
268			fireTableDataChanged();
269		}
270
271		public String getColumnName(int index)
272		{
273			switch(index)
274			{
275			case 0:
276				return jEdit.getProperty("options.shortcuts.name");
277			case 1:
278				return jEdit.getProperty("options.shortcuts.shortcut1");
279			case 2:
280				return jEdit.getProperty("options.shortcuts.shortcut2");
281			default:
282				return null;
283			}
284		}
285
286		public void save()
287		{
288			Enumeration e = bindings.elements();
289			while(e.hasMoreElements())
290			{
291				GrabKeyDialog.KeyBinding binding[]
292					= (GrabKeyDialog.KeyBinding[])
293						e.nextElement();
294				jEdit.setProperty(
295					binding[0].name + ".shortcut",
296					binding[0].shortcut);
297				jEdit.setProperty(
298					binding[1].name + ".shortcut2",
299					binding[1].shortcut);
300			}
301		}
302
303		public GrabKeyDialog.KeyBinding getBindingAt(int row, int nr)
304		{
305			GrabKeyDialog.KeyBinding binding[]
306				= (GrabKeyDialog.KeyBinding[])
307					bindings.elementAt(row);
308			return binding[nr];
309		}
310
311		public String toString()
312		{
313			return name;
314		}
315
316		class KeyCompare implements Comparator
317		{
318			int col;
319
320			KeyCompare(int col)
321			{
322				this.col = col;
323			}
324
325			public int compare(Object obj1, Object obj2)
326			{
327				GrabKeyDialog.KeyBinding[] k1
328					= (GrabKeyDialog.KeyBinding[])obj1;
329				GrabKeyDialog.KeyBinding[] k2
330					= (GrabKeyDialog.KeyBinding[])obj2;
331
332				String label1 = k1[0].label.toLowerCase();
333				String label2 = k2[0].label.toLowerCase();
334
335				if(col == 0)
336					return MiscUtilities.compareStrings(
337						label1,label2,true);
338				else
339				{
340					String shortcut1, shortcut2;
341
342					if(col == 1)
343					{
344						shortcut1 = k1[0].shortcut;
345						shortcut2 = k2[0].shortcut;
346					}
347					else
348					{
349						shortcut1 = k1[1].shortcut;
350						shortcut2 = k2[1].shortcut;
351					}
352
353					if(shortcut1 == null && shortcut2 != null)
354						return 1;
355
356					if(shortcut2 == null && shortcut1 != null)
357						return -1;
358
359					if(shortcut1 == null)
360						return MiscUtilities.compareStrings(label1,label2,true);
361
362					return MiscUtilities.compareStrings(shortcut1,shortcut2,true);
363				}
364			}
365		}
366	}
367}