PageRenderTime 113ms CodeModel.GetById 71ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/options/AbbrevsOptionPane.java

#
Java | 509 lines | 384 code | 65 blank | 60 comment | 47 complexity | d888ceced7910243ee995481d7ccd053 MD5 | raw file
  1/*
  2 * AbbrevsOptionPane.java - Abbrevs options panel
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2000, 2001, 2002 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.options;
 24
 25//{{{ Imports
 26import javax.swing.border.EmptyBorder;
 27import javax.swing.event.*;
 28import javax.swing.table.*;
 29import javax.swing.*;
 30import java.awt.event.*;
 31import java.awt.*;
 32import java.util.*;
 33import org.gjt.sp.jedit.gui.*;
 34import org.gjt.sp.jedit.*;
 35//}}}
 36
 37//{{{ AbbrevsOptionPane class
 38/**
 39 * Abbrev editor.
 40 * @author Slava Pestov
 41 * @version $Id: AbbrevsOptionPane.java 4908 2003-11-02 21:16:38Z spestov $
 42 */
 43public class AbbrevsOptionPane extends AbstractOptionPane
 44{
 45	//{{{ AbbrevsOptionPane constructor
 46	public AbbrevsOptionPane()
 47	{
 48		super("abbrevs");
 49	} //}}}
 50
 51	//{{{ _init() method
 52	protected void _init()
 53	{
 54		setLayout(new BorderLayout());
 55
 56		JPanel panel = new JPanel(new BorderLayout(6,6));
 57
 58		expandOnInput = new JCheckBox(jEdit.getProperty("options.abbrevs"
 59			+ ".expandOnInput"),Abbrevs.getExpandOnInput());
 60
 61		panel.add(expandOnInput,BorderLayout.NORTH);
 62
 63		JPanel panel2 = new JPanel();
 64		panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
 65		panel2.setBorder(new EmptyBorder(0,0,6,0));
 66		panel2.add(Box.createGlue());
 67		JLabel label = new JLabel(jEdit.getProperty("options.abbrevs.set"));
 68		label.setBorder(new EmptyBorder(0,0,0,12));
 69		panel2.add(label);
 70
 71		Hashtable _modeAbbrevs = Abbrevs.getModeAbbrevs();
 72		modeAbbrevs = new Hashtable();
 73		Mode[] modes = jEdit.getModes();
 74		Arrays.sort(modes,new MiscUtilities.StringICaseCompare());
 75		String[] sets = new String[modes.length + 1];
 76		sets[0] = "global";
 77		for(int i = 0; i < modes.length; i++)
 78		{
 79			String name = modes[i].getName();
 80			sets[i+1] = name;
 81			modeAbbrevs.put(name,new AbbrevsModel((Hashtable)_modeAbbrevs.get(name)));
 82		}
 83
 84		setsComboBox = new JComboBox(sets);
 85		ActionHandler actionHandler = new ActionHandler();
 86		setsComboBox.addActionListener(actionHandler);
 87		panel2.add(setsComboBox);
 88		panel2.add(Box.createGlue());
 89		panel.add(panel2,BorderLayout.SOUTH);
 90
 91		add(BorderLayout.NORTH,panel);
 92
 93		globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs());
 94		abbrevsTable = new JTable(globalAbbrevs);
 95		abbrevsTable.getColumnModel().getColumn(1).setCellRenderer(
 96			new Renderer());
 97		abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
 98		abbrevsTable.getTableHeader().setReorderingAllowed(false);
 99		abbrevsTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
100		abbrevsTable.getSelectionModel().addListSelectionListener(
101			new SelectionHandler());
102		abbrevsTable.getSelectionModel().setSelectionMode(
103			ListSelectionModel.SINGLE_SELECTION);
104		abbrevsTable.addMouseListener(new TableMouseHandler());
105		Dimension d = abbrevsTable.getPreferredSize();
106		d.height = Math.min(d.height,200);
107		JScrollPane scroller = new JScrollPane(abbrevsTable);
108		scroller.setPreferredSize(d);
109		add(BorderLayout.CENTER,scroller);
110
111		JPanel buttons = new JPanel();
112		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
113		buttons.setBorder(new EmptyBorder(6,0,0,0));
114
115		add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
116		add.setToolTipText(jEdit.getProperty("options.abbrevs.add"));
117		add.addActionListener(actionHandler);
118		buttons.add(add);
119		remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
120		remove.setToolTipText(jEdit.getProperty("options.abbrevs.remove"));
121		remove.addActionListener(actionHandler);
122		buttons.add(remove);
123		edit = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png"));
124		edit.setToolTipText(jEdit.getProperty("options.abbrevs.edit"));
125		edit.addActionListener(actionHandler);
126		buttons.add(edit);
127		buttons.add(Box.createGlue());
128
129		add(BorderLayout.SOUTH,buttons);
130
131		updateEnabled();
132	} //}}}
133
134	//{{{ _save() method
135	protected void _save()
136	{
137		if(abbrevsTable.getCellEditor() != null)
138			abbrevsTable.getCellEditor().stopCellEditing();
139
140		Abbrevs.setExpandOnInput(expandOnInput.isSelected());
141
142		Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable());
143
144		Hashtable modeHash = new Hashtable();
145		Enumeration keys = modeAbbrevs.keys();
146		Enumeration values = modeAbbrevs.elements();
147		while(keys.hasMoreElements())
148		{
149			modeHash.put(keys.nextElement(),((AbbrevsModel)values.nextElement())
150				.toHashtable());
151		}
152		Abbrevs.setModeAbbrevs(modeHash);
153	} //}}}
154
155	//{{{ Private members
156
157	//{{{ Instance variables
158	private JComboBox setsComboBox;
159	private JCheckBox expandOnInput;
160	private JTable abbrevsTable;
161	private AbbrevsModel globalAbbrevs;
162	private Hashtable modeAbbrevs;
163	private JButton add;
164	private JButton edit;
165	private JButton remove;
166	//}}}
167
168	//{{{ updateEnabled() method
169	private void updateEnabled()
170	{
171		int selectedRow = abbrevsTable.getSelectedRow();
172		edit.setEnabled(selectedRow != -1);
173		remove.setEnabled(selectedRow != -1);
174	} //}}}
175
176	//{{{ edit() method
177	private void edit()
178	{
179		AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
180
181		int row = abbrevsTable.getSelectedRow();
182
183		String abbrev = (String)abbrevsModel.getValueAt(row,0);
184		String expansion = (String)abbrevsModel.getValueAt(row,1);
185		String oldAbbrev = abbrev;
186
187		EditAbbrevDialog dialog = new EditAbbrevDialog(
188			GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
189			abbrev,expansion,abbrevsModel.toHashtable());
190		abbrev = dialog.getAbbrev();
191		expansion = dialog.getExpansion();
192		if(abbrev != null && expansion != null)
193		{
194			for(int i = 0; i < abbrevsModel.getRowCount(); i++)
195			{
196				if(abbrevsModel.getValueAt(i,0).equals(oldAbbrev))
197				{
198					abbrevsModel.remove(i);
199					break;
200				}
201			}
202
203			add(abbrevsModel,abbrev,expansion);
204		}
205	} //}}}
206
207	//{{{ add() method
208	private void add(AbbrevsModel abbrevsModel, String abbrev,
209		String expansion)
210	{
211		for(int i = 0; i < abbrevsModel.getRowCount(); i++)
212		{
213			if(abbrevsModel.getValueAt(i,0).equals(abbrev))
214			{
215				abbrevsModel.remove(i);
216				break;
217			}
218		}
219
220		abbrevsModel.add(abbrev,expansion);
221		updateEnabled();
222	} //}}}
223
224	//}}}
225
226	//{{{ HeaderMouseHandler class
227	class HeaderMouseHandler extends MouseAdapter
228	{
229		public void mouseClicked(MouseEvent evt)
230		{
231			switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint()))
232			{
233			case 0:
234				((AbbrevsModel)abbrevsTable.getModel()).sort(0);
235				break;
236			case 1:
237				((AbbrevsModel)abbrevsTable.getModel()).sort(1);
238				break;
239			}
240		}
241	} //}}}
242
243	//{{{ TableMouseHandler class
244	class TableMouseHandler extends MouseAdapter
245	{
246		public void mouseClicked(MouseEvent evt)
247		{
248			if(evt.getClickCount() == 2)
249				edit();
250		}
251	} //}}}
252
253	//{{{ SelectionHandler class
254	class SelectionHandler implements ListSelectionListener
255	{
256		public void valueChanged(ListSelectionEvent evt)
257		{
258			updateEnabled();
259		}
260	} //}}}
261
262	//{{{ ActionHandler class
263	class ActionHandler implements ActionListener
264	{
265		public void actionPerformed(ActionEvent evt)
266		{
267			AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
268
269			Object source = evt.getSource();
270			if(source == setsComboBox)
271			{
272				String selected = (String)setsComboBox.getSelectedItem();
273				if(selected.equals("global"))
274				{
275					abbrevsTable.setModel(globalAbbrevs);
276				}
277				else
278				{
279					abbrevsTable.setModel((AbbrevsModel)
280						modeAbbrevs.get(selected));
281				}
282				updateEnabled();
283			}
284			else if(source == add)
285			{
286				EditAbbrevDialog dialog = new EditAbbrevDialog(
287					GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
288					null,null,abbrevsModel.toHashtable());
289				String abbrev = dialog.getAbbrev();
290				String expansion = dialog.getExpansion();
291				if(abbrev != null && abbrev.length() != 0
292					&& expansion != null
293					&& expansion.length() != 0)
294				{
295					add(abbrevsModel,abbrev,expansion);
296				}
297			}
298			else if(source == edit)
299			{
300				edit();
301			}
302			else if(source == remove)
303			{
304				int selectedRow = abbrevsTable.getSelectedRow();
305				abbrevsModel.remove(selectedRow);
306				updateEnabled();
307			}
308		}
309	} //}}}
310
311	//{{{ Renderer class
312	static class Renderer extends DefaultTableCellRenderer
313	{
314		public Component getTableCellRendererComponent(
315			JTable table,
316			Object value,
317			boolean isSelected,
318			boolean cellHasFocus,
319			int row,
320			int col)
321		{
322			String valueStr = value.toString();
323
324			// workaround for Swing's annoying processing of
325			// labels starting with <html>, which often breaks
326			if(valueStr.toLowerCase().startsWith("<html>"))
327				valueStr = " " + valueStr;
328			return super.getTableCellRendererComponent(table,valueStr,
329				isSelected,cellHasFocus,row,col);
330		}
331	} //}}}
332} //}}}
333
334//{{{ AbbrevsModel class
335class AbbrevsModel extends AbstractTableModel
336{
337	Vector abbrevs;
338	int lastSort;
339
340	//{{{ AbbrevsModel constructor
341	AbbrevsModel(Hashtable abbrevHash)
342	{
343		abbrevs = new Vector();
344
345		if(abbrevHash != null)
346		{
347			Enumeration abbrevEnum = abbrevHash.keys();
348			Enumeration expandEnum = abbrevHash.elements();
349
350			while(abbrevEnum.hasMoreElements())
351			{
352				abbrevs.addElement(new Abbrev((String)abbrevEnum.nextElement(),
353					(String)expandEnum.nextElement()));
354			}
355
356			sort(0);
357		}
358	} //}}}
359
360	//{{{ sort() method
361	void sort(int col)
362	{
363		lastSort = col;
364		MiscUtilities.quicksort(abbrevs,new AbbrevCompare(col));
365		fireTableDataChanged();
366	} //}}}
367
368	//{{{ add() method
369	void add(String abbrev, String expansion)
370	{
371		abbrevs.addElement(new Abbrev(abbrev,expansion));
372		sort(lastSort);
373	} //}}}
374
375	//{{{ remove() method
376	void remove(int index)
377	{
378		abbrevs.removeElementAt(index);
379		fireTableStructureChanged();
380	} //}}}
381
382	//{{{ toHashtable() method
383	public Hashtable toHashtable()
384	{
385		Hashtable hash = new Hashtable();
386		for(int i = 0; i < abbrevs.size(); i++)
387		{
388			Abbrev abbrev = (Abbrev)abbrevs.elementAt(i);
389			if(abbrev.abbrev.length() > 0
390				&& abbrev.expand.length() > 0)
391			{
392				hash.put(abbrev.abbrev,abbrev.expand);
393			}
394		}
395		return hash;
396	} //}}}
397
398	//{{{ getColumnCount() method
399	public int getColumnCount()
400	{
401		return 2;
402	} //}}}
403
404	//{{{ getRowCount() method
405	public int getRowCount()
406	{
407		return abbrevs.size();
408	} //}}}
409
410	//{{{ getValueAt() method
411	public Object getValueAt(int row, int col)
412	{
413		Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
414		switch(col)
415		{
416		case 0:
417			return abbrev.abbrev;
418		case 1:
419			return abbrev.expand;
420		default:
421			return null;
422		}
423	} //}}}
424
425	//{{{ isCellEditable() method
426	public boolean isCellEditable(int row, int col)
427	{
428		return false;
429	} //}}}
430
431	//{{{ setValueAt() method
432	public void setValueAt(Object value, int row, int col)
433	{
434		if(value == null)
435			value = "";
436
437		Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
438
439		if(col == 0)
440			abbrev.abbrev = (String)value;
441		else
442			abbrev.expand = (String)value;
443
444		fireTableRowsUpdated(row,row);
445	} //}}}
446
447	//{{{ getColumnName() method
448	public String getColumnName(int index)
449	{
450		switch(index)
451		{
452		case 0:
453			return jEdit.getProperty("options.abbrevs.abbrev");
454		case 1:
455			return jEdit.getProperty("options.abbrevs.expand");
456		default:
457			return null;
458		}
459	} //}}}
460
461	//{{{ AbbrevCompare class
462	class AbbrevCompare implements MiscUtilities.Compare
463	{
464		int col;
465
466		AbbrevCompare(int col)
467		{
468			this.col = col;
469		}
470
471		public int compare(Object obj1, Object obj2)
472		{
473			Abbrev a1 = (Abbrev)obj1;
474			Abbrev a2 = (Abbrev)obj2;
475
476			if(col == 0)
477			{
478				String abbrev1 = a1.abbrev.toLowerCase();
479				String abbrev2 = a2.abbrev.toLowerCase();
480
481				return MiscUtilities.compareStrings(
482					abbrev1,abbrev2,true);
483			}
484			else
485			{
486				String expand1 = a1.expand.toLowerCase();
487				String expand2 = a2.expand.toLowerCase();
488
489				return MiscUtilities.compareStrings(
490					expand1,expand2,true);
491			}
492		}
493	} //}}}
494} //}}}
495
496//{{{ Abbrev class
497class Abbrev
498{
499	Abbrev() {}
500
501	Abbrev(String abbrev, String expand)
502	{
503		this.abbrev = abbrev;
504		this.expand = expand;
505	}
506
507	String abbrev;
508	String expand;
509} //}}}