PageRenderTime 91ms CodeModel.GetById 43ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 491 lines | 368 code | 63 blank | 60 comment | 43 complexity | c896462d4cb58291fba65404237ba7a4 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.EditAbbrevDialog;
 34import org.gjt.sp.jedit.*;
 35//}}}
 36
 37//{{{ AbbrevsOptionPane class
 38/**
 39 * Abbrev editor.
 40 * @author Slava Pestov
 41 * @version $Id: AbbrevsOptionPane.java 4300 2002-08-12 18:32:36Z 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		String[] sets = new String[modes.length + 1];
 75		sets[0] = "global";
 76		for(int i = 0; i < modes.length; i++)
 77		{
 78			String name = modes[i].getName();
 79			sets[i+1] = name;
 80			modeAbbrevs.put(name,new AbbrevsModel((Hashtable)_modeAbbrevs.get(name)));
 81		}
 82
 83		setsComboBox = new JComboBox(sets);
 84		ActionHandler actionHandler = new ActionHandler();
 85		setsComboBox.addActionListener(actionHandler);
 86		panel2.add(setsComboBox);
 87		panel2.add(Box.createGlue());
 88		panel.add(panel2,BorderLayout.SOUTH);
 89
 90		add(BorderLayout.NORTH,panel);
 91
 92		globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs());
 93		abbrevsTable = new JTable(globalAbbrevs);
 94		abbrevsTable.getColumnModel().getColumn(1).setCellRenderer(
 95			new Renderer());
 96		abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
 97		abbrevsTable.getTableHeader().setReorderingAllowed(false);
 98		abbrevsTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
 99		abbrevsTable.getSelectionModel().addListSelectionListener(
100			new SelectionHandler());
101		abbrevsTable.addMouseListener(new TableMouseHandler());
102		Dimension d = abbrevsTable.getPreferredSize();
103		d.height = Math.min(d.height,200);
104		JScrollPane scroller = new JScrollPane(abbrevsTable);
105		scroller.setPreferredSize(d);
106		add(BorderLayout.CENTER,scroller);
107
108		JPanel buttons = new JPanel();
109		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
110		buttons.setBorder(new EmptyBorder(6,0,0,0));
111
112		buttons.add(Box.createGlue());
113		add = new JButton(jEdit.getProperty("options.abbrevs.add"));
114		add.addActionListener(actionHandler);
115		buttons.add(add);
116		buttons.add(Box.createHorizontalStrut(6));
117		edit = new JButton(jEdit.getProperty("options.abbrevs.edit"));
118		edit.addActionListener(actionHandler);
119		buttons.add(edit);
120		buttons.add(Box.createHorizontalStrut(6));
121		remove = new JButton(jEdit.getProperty("options.abbrevs.remove"));
122		remove.addActionListener(actionHandler);
123		buttons.add(remove);
124		buttons.add(Box.createGlue());
125
126		add(BorderLayout.SOUTH,buttons);
127
128		updateEnabled();
129	} //}}}
130
131	//{{{ _save() method
132	protected void _save()
133	{
134		if(abbrevsTable.getCellEditor() != null)
135			abbrevsTable.getCellEditor().stopCellEditing();
136
137		Abbrevs.setExpandOnInput(expandOnInput.isSelected());
138
139		Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable());
140
141		Hashtable modeHash = new Hashtable();
142		Enumeration keys = modeAbbrevs.keys();
143		Enumeration values = modeAbbrevs.elements();
144		while(keys.hasMoreElements())
145		{
146			modeHash.put(keys.nextElement(),((AbbrevsModel)values.nextElement())
147				.toHashtable());
148		}
149		Abbrevs.setModeAbbrevs(modeHash);
150	} //}}}
151
152	//{{{ Private members
153
154	//{{{ Instance variables
155	private JComboBox setsComboBox;
156	private JCheckBox expandOnInput;
157	private JTable abbrevsTable;
158	private AbbrevsModel globalAbbrevs;
159	private Hashtable modeAbbrevs;
160	private JButton add;
161	private JButton edit;
162	private JButton remove;
163	//}}}
164
165	//{{{ updateEnabled() method
166	private void updateEnabled()
167	{
168		int selectedRow = abbrevsTable.getSelectedRow();
169		edit.setEnabled(selectedRow != -1);
170		remove.setEnabled(selectedRow != -1);
171	} //}}}
172
173	//{{{ edit() method
174	private void edit()
175	{
176		AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
177
178		int row = abbrevsTable.getSelectedRow();
179
180		String abbrev = (String)abbrevsModel.getValueAt(row,0);
181		String expansion = (String)abbrevsModel.getValueAt(row,1);
182	
183		EditAbbrevDialog dialog = new EditAbbrevDialog(
184			AbbrevsOptionPane.this,
185			abbrev,expansion);
186		abbrev = dialog.getAbbrev();
187		expansion = dialog.getExpansion();
188		if(abbrev != null && expansion != null)
189		{
190			abbrevsModel.setValueAt(abbrev,row,0);
191			abbrevsModel.setValueAt(expansion,row,1);
192		}
193	} //}}}
194
195	//}}}
196
197	//{{{ HeaderMouseHandler class
198	class HeaderMouseHandler extends MouseAdapter
199	{
200		public void mouseClicked(MouseEvent evt)
201		{
202			switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint()))
203			{
204			case 0:
205				((AbbrevsModel)abbrevsTable.getModel()).sort(0);
206				break;
207			case 1:
208				((AbbrevsModel)abbrevsTable.getModel()).sort(1);
209				break;
210			}
211		}
212	} //}}}
213
214	//{{{ TableMouseHandler class
215	class TableMouseHandler extends MouseAdapter
216	{
217		public void mouseClicked(MouseEvent evt)
218		{
219			if(evt.getClickCount() == 2)
220				edit();
221		}
222	} //}}}
223
224	//{{{ SelectionHandler class
225	class SelectionHandler implements ListSelectionListener
226	{
227		public void valueChanged(ListSelectionEvent evt)
228		{
229			updateEnabled();
230		}
231	} //}}}
232
233	//{{{ ActionHandler class
234	class ActionHandler implements ActionListener
235	{
236		public void actionPerformed(ActionEvent evt)
237		{
238			AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
239
240			Object source = evt.getSource();
241			if(source == setsComboBox)
242			{
243				String selected = (String)setsComboBox.getSelectedItem();
244				if(selected.equals("global"))
245				{
246					abbrevsTable.setModel(globalAbbrevs);
247				}
248				else
249				{
250					abbrevsTable.setModel((AbbrevsModel)
251						modeAbbrevs.get(selected));
252				}
253				updateEnabled();
254			}
255			else if(source == add)
256			{
257				EditAbbrevDialog dialog = new EditAbbrevDialog(
258					AbbrevsOptionPane.this,
259					null,null);
260				String abbrev = dialog.getAbbrev();
261				String expansion = dialog.getExpansion();
262				if(abbrev != null && abbrev.length() != 0
263					&& expansion != null
264					&& expansion.length() != 0)
265				{
266					abbrevsModel.add(abbrev,expansion);
267					int index = abbrevsModel.getRowCount() - 1;
268					abbrevsTable.getSelectionModel()
269						.setSelectionInterval(index,index);
270					Rectangle rect = abbrevsTable.getCellRect(
271						index,0,true);
272					abbrevsTable.scrollRectToVisible(rect);
273					updateEnabled();
274				}
275			}
276			else if(source == edit)
277			{
278				edit();
279			}
280			else if(source == remove)
281			{
282				int selectedRow = abbrevsTable.getSelectedRow();
283				abbrevsModel.remove(selectedRow);
284				updateEnabled();
285			}
286		}
287	} //}}}
288
289	//{{{ Renderer class
290	static class Renderer extends DefaultTableCellRenderer
291	{
292		public Component getTableCellRendererComponent(
293			JTable table,
294			Object value,
295			boolean isSelected,
296			boolean cellHasFocus,
297			int row,
298			int col)
299		{
300			String valueStr = value.toString();
301
302			// workaround for Swing's annoying processing of
303			// labels starting with <html>, which often breaks
304			if(valueStr.toLowerCase().startsWith("<html>"))
305				valueStr = " " + valueStr;
306			return super.getTableCellRendererComponent(table,valueStr,
307				isSelected,cellHasFocus,row,col);
308		}
309	} //}}}
310} //}}}
311
312//{{{ AbbrevsModel class
313class AbbrevsModel extends AbstractTableModel
314{
315	Vector abbrevs;
316
317	//{{{ AbbrevsModel constructor
318	AbbrevsModel()
319	{
320		abbrevs = new Vector();
321	} //}}}
322
323	//{{{ AbbrevsModel constructor
324	AbbrevsModel(Hashtable abbrevHash)
325	{
326		this();
327
328		if(abbrevHash != null)
329		{
330			Enumeration abbrevEnum = abbrevHash.keys();
331			Enumeration expandEnum = abbrevHash.elements();
332
333			while(abbrevEnum.hasMoreElements())
334			{
335				abbrevs.addElement(new Abbrev((String)abbrevEnum.nextElement(),
336					(String)expandEnum.nextElement()));
337			}
338
339			sort(0);
340		}
341	} //}}}
342
343	//{{{ sort() method
344	void sort(int col)
345	{
346		MiscUtilities.quicksort(abbrevs,new AbbrevCompare(col));
347		fireTableDataChanged();
348	} //}}}
349
350	//{{{ add() method
351	void add(String abbrev, String expansion)
352	{
353		abbrevs.addElement(new Abbrev(abbrev,expansion));
354		fireTableStructureChanged();
355	} //}}}
356
357	//{{{ remove() method
358	void remove(int index)
359	{
360		abbrevs.removeElementAt(index);
361		fireTableStructureChanged();
362	} //}}}
363
364	//{{{ toHashtable() method
365	public Hashtable toHashtable()
366	{
367		Hashtable hash = new Hashtable();
368		for(int i = 0; i < abbrevs.size(); i++)
369		{
370			Abbrev abbrev = (Abbrev)abbrevs.elementAt(i);
371			if(abbrev.abbrev.length() > 0
372				&& abbrev.expand.length() > 0)
373			{
374				hash.put(abbrev.abbrev,abbrev.expand);
375			}
376		}
377		return hash;
378	} //}}}
379
380	//{{{ getColumnCount() method
381	public int getColumnCount()
382	{
383		return 2;
384	} //}}}
385
386	//{{{ getRowCount() method
387	public int getRowCount()
388	{
389		return abbrevs.size();
390	} //}}}
391
392	//{{{ getValueAt() method
393	public Object getValueAt(int row, int col)
394	{
395		Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
396		switch(col)
397		{
398		case 0:
399			return abbrev.abbrev;
400		case 1:
401			return abbrev.expand;
402		default:
403			return null;
404		}
405	} //}}}
406
407	//{{{ isCellEditable() method
408	public boolean isCellEditable(int row, int col)
409	{
410		return false;
411	} //}}}
412
413	//{{{ setValueAt() method
414	public void setValueAt(Object value, int row, int col)
415	{
416		if(value == null)
417			value = "";
418
419		Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
420
421		if(col == 0)
422			abbrev.abbrev = (String)value;
423		else
424			abbrev.expand = (String)value;
425
426		fireTableRowsUpdated(row,row);
427	} //}}}
428
429	//{{{ getColumnName() method
430	public String getColumnName(int index)
431	{
432		switch(index)
433		{
434		case 0:
435			return jEdit.getProperty("options.abbrevs.abbrev");
436		case 1:
437			return jEdit.getProperty("options.abbrevs.expand");
438		default:
439			return null;
440		}
441	} //}}}
442
443	//{{{ AbbrevCompare class
444	class AbbrevCompare implements MiscUtilities.Compare
445	{
446		int col;
447
448		AbbrevCompare(int col)
449		{
450			this.col = col;
451		}
452
453		public int compare(Object obj1, Object obj2)
454		{
455			Abbrev a1 = (Abbrev)obj1;
456			Abbrev a2 = (Abbrev)obj2;
457
458			if(col == 0)
459			{
460				String abbrev1 = a1.abbrev.toLowerCase();
461				String abbrev2 = a2.abbrev.toLowerCase();
462
463				return MiscUtilities.compareStrings(
464					abbrev1,abbrev2,true);
465			}
466			else
467			{
468				String expand1 = a1.expand.toLowerCase();
469				String expand2 = a2.expand.toLowerCase();
470
471				return MiscUtilities.compareStrings(
472					expand1,expand2,true);
473			}
474		}
475	} //}}}
476} //}}}
477
478//{{{ Abbrev class
479class Abbrev
480{
481	Abbrev() {}
482
483	Abbrev(String abbrev, String expand)
484	{
485		this.abbrev = abbrev;
486		this.expand = expand;
487	}
488
489	String abbrev;
490	String expand;
491} //}}}