PageRenderTime 56ms CodeModel.GetById 36ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/options/AbbrevsOptionPane.java

#
Java | 425 lines | 341 code | 59 blank | 25 comment | 40 complexity | 9f4c7e1d2d90396b7bb54e305f4346ac MD5 | raw file
  1/*
  2 * AbbrevsOptionPane.java - Abbrevs options panel
  3 * Copyright (C) 1999, 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.EmptyBorder;
 23import javax.swing.event.*;
 24import javax.swing.table.*;
 25import javax.swing.*;
 26import java.awt.event.*;
 27import java.awt.*;
 28import java.util.*;
 29import org.gjt.sp.jedit.gui.EditAbbrevDialog;
 30import org.gjt.sp.jedit.*;
 31
 32/**
 33 * Abbrev editor.
 34 * @author Slava Pestov
 35 * @version $Id: AbbrevsOptionPane.java 3828 2001-10-07 10:42:45Z spestov $
 36 */
 37public class AbbrevsOptionPane extends AbstractOptionPane
 38{
 39	public AbbrevsOptionPane()
 40	{
 41		super("abbrevs");
 42	}
 43
 44	// protected members
 45	protected void _init()
 46	{
 47		setLayout(new BorderLayout());
 48
 49		JPanel panel = new JPanel(new BorderLayout());
 50
 51		JPanel panel2 = new JPanel();
 52		panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
 53		panel2.setBorder(new EmptyBorder(0,0,6,0));
 54
 55		panel2.add(Box.createGlue());
 56
 57		expandOnInput = new JCheckBox(jEdit.getProperty("options.abbrevs"
 58			+ ".expandOnInput"),Abbrevs.getExpandOnInput());
 59		panel2.add(expandOnInput);
 60
 61		panel2.add(Box.createGlue());
 62
 63		panel.add(panel2,BorderLayout.NORTH);
 64
 65		JPanel panel3 = new JPanel();
 66		panel3.setLayout(new BoxLayout(panel3,BoxLayout.X_AXIS));
 67		panel3.setBorder(new EmptyBorder(0,0,6,0));
 68		panel3.add(Box.createGlue());
 69		JLabel label = new JLabel(jEdit.getProperty("options.abbrevs.set"));
 70		label.setBorder(new EmptyBorder(0,0,0,12));
 71		panel3.add(label);
 72
 73		Hashtable _modeAbbrevs = Abbrevs.getModeAbbrevs();
 74		modeAbbrevs = new Hashtable();
 75		Mode[] modes = jEdit.getModes();
 76		String[] sets = new String[modes.length + 1];
 77		sets[0] = "global";
 78		for(int i = 0; i < modes.length; i++)
 79		{
 80			String name = modes[i].getName();
 81			sets[i+1] = name;
 82			modeAbbrevs.put(name,new AbbrevsModel((Hashtable)_modeAbbrevs.get(name)));
 83		}
 84
 85		setsComboBox = new JComboBox(sets);
 86		ActionHandler actionHandler = new ActionHandler();
 87		setsComboBox.addActionListener(actionHandler);
 88		panel3.add(setsComboBox);
 89		panel3.add(Box.createGlue());
 90		panel.add(panel3,BorderLayout.SOUTH);
 91
 92		add(BorderLayout.NORTH,panel);
 93
 94		globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs());
 95		abbrevsTable = new JTable(globalAbbrevs);
 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		Dimension d = abbrevsTable.getPreferredSize();
102		d.height = Math.min(d.height,200);
103		JScrollPane scroller = new JScrollPane(abbrevsTable);
104		scroller.setPreferredSize(d);
105		add(BorderLayout.CENTER,scroller);
106
107		JPanel buttons = new JPanel();
108		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
109		buttons.setBorder(new EmptyBorder(6,0,0,0));
110
111		buttons.add(Box.createGlue());
112		add = new JButton(jEdit.getProperty("options.abbrevs.add"));
113		add.addActionListener(actionHandler);
114		buttons.add(add);
115		buttons.add(Box.createHorizontalStrut(6));
116		edit = new JButton(jEdit.getProperty("options.abbrevs.edit"));
117		edit.addActionListener(actionHandler);
118		buttons.add(edit);
119		buttons.add(Box.createHorizontalStrut(6));
120		remove = new JButton(jEdit.getProperty("options.abbrevs.remove"));
121		remove.addActionListener(actionHandler);
122		buttons.add(remove);
123		buttons.add(Box.createGlue());
124
125		add(BorderLayout.SOUTH,buttons);
126
127		updateEnabled();
128	}
129
130	protected void _save()
131	{
132		if(abbrevsTable.getCellEditor() != null)
133			abbrevsTable.getCellEditor().stopCellEditing();
134
135		Abbrevs.setExpandOnInput(expandOnInput.isSelected());
136
137		Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable());
138
139		Hashtable modeHash = new Hashtable();
140		Enumeration keys = modeAbbrevs.keys();
141		Enumeration values = modeAbbrevs.elements();
142		while(keys.hasMoreElements())
143		{
144			modeHash.put(keys.nextElement(),((AbbrevsModel)values.nextElement())
145				.toHashtable());
146		}
147		Abbrevs.setModeAbbrevs(modeHash);
148	}
149
150	// private members
151	private JComboBox setsComboBox;
152	private JCheckBox expandOnInput;
153	private JTable abbrevsTable;
154	private AbbrevsModel globalAbbrevs;
155	private Hashtable modeAbbrevs;
156	private JButton add;
157	private JButton edit;
158	private JButton remove;
159
160	private void updateEnabled()
161	{
162		int selectedRow = abbrevsTable.getSelectedRow();
163		edit.setEnabled(selectedRow != -1);
164		remove.setEnabled(selectedRow != -1);
165	}
166
167	class HeaderMouseHandler extends MouseAdapter
168	{
169		public void mouseClicked(MouseEvent evt)
170		{
171			switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint()))
172			{
173			case 0:
174				((AbbrevsModel)abbrevsTable.getModel()).sort(0);
175				break;
176			case 1:
177				((AbbrevsModel)abbrevsTable.getModel()).sort(1);
178				break;
179			}
180		}
181	}
182
183	class SelectionHandler implements ListSelectionListener
184	{
185		public void valueChanged(ListSelectionEvent evt)
186		{
187			updateEnabled();
188		}
189	}
190
191	class ActionHandler implements ActionListener
192	{
193		public void actionPerformed(ActionEvent evt)
194		{
195			AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
196
197			Object source = evt.getSource();
198			if(source == setsComboBox)
199			{
200				String selected = (String)setsComboBox.getSelectedItem();
201				if(selected.equals("global"))
202				{
203					abbrevsTable.setModel(globalAbbrevs);
204				}
205				else
206				{
207					abbrevsTable.setModel((AbbrevsModel)
208						modeAbbrevs.get(selected));
209				}
210				updateEnabled();
211			}
212			else if(source == add)
213			{
214				EditAbbrevDialog dialog = new EditAbbrevDialog(
215					AbbrevsOptionPane.this,
216					null,null);
217				String abbrev = dialog.getAbbrev();
218				String expansion = dialog.getExpansion();
219				if(abbrev != null && abbrev.length() != 0
220					&& expansion != null
221					&& expansion.length() != 0)
222				{
223					abbrevsModel.add(abbrev,expansion);
224					int index = abbrevsModel.getRowCount() - 1;
225					abbrevsTable.getSelectionModel()
226						.setSelectionInterval(index,index);
227					Rectangle rect = abbrevsTable.getCellRect(
228						index,0,true);
229					abbrevsTable.scrollRectToVisible(rect);
230					updateEnabled();
231				}
232			}
233			else if(source == edit)
234			{
235				int row = abbrevsTable.getSelectedRow();
236
237				String abbrev = (String)abbrevsModel.getValueAt(row,0);
238				String expansion = (String)abbrevsModel.getValueAt(row,1);
239
240				EditAbbrevDialog dialog = new EditAbbrevDialog(
241					AbbrevsOptionPane.this,
242					abbrev,expansion);
243				abbrev = dialog.getAbbrev();
244				expansion = dialog.getExpansion();
245				if(abbrev != null && expansion != null)
246				{
247					abbrevsModel.setValueAt(abbrev,row,0);
248					abbrevsModel.setValueAt(expansion,row,1);
249				}
250			}
251			else if(source == remove)
252			{
253				int selectedRow = abbrevsTable.getSelectedRow();
254				abbrevsModel.remove(selectedRow);
255				updateEnabled();
256			}
257		}
258	}
259}
260
261class AbbrevsModel extends AbstractTableModel
262{
263	Vector abbrevs;
264
265	AbbrevsModel()
266	{
267		abbrevs = new Vector();
268	}
269
270	AbbrevsModel(Hashtable abbrevHash)
271	{
272		this();
273
274		if(abbrevHash != null)
275		{
276			Enumeration abbrevEnum = abbrevHash.keys();
277			Enumeration expandEnum = abbrevHash.elements();
278
279			while(abbrevEnum.hasMoreElements())
280			{
281				abbrevs.addElement(new Abbrev((String)abbrevEnum.nextElement(),
282					(String)expandEnum.nextElement()));
283			}
284
285			sort(0);
286		}
287	}
288
289	void sort(int col)
290	{
291		MiscUtilities.quicksort(abbrevs,new AbbrevCompare(col));
292		fireTableDataChanged();
293	}
294
295	void add(String abbrev, String expansion)
296	{
297		abbrevs.addElement(new Abbrev(abbrev,expansion));
298		fireTableStructureChanged();
299	}
300
301	void remove(int index)
302	{
303		abbrevs.removeElementAt(index);
304		fireTableStructureChanged();
305	}
306
307	public Hashtable toHashtable()
308	{
309		Hashtable hash = new Hashtable();
310		for(int i = 0; i < abbrevs.size(); i++)
311		{
312			Abbrev abbrev = (Abbrev)abbrevs.elementAt(i);
313			if(abbrev.abbrev.length() > 0
314				&& abbrev.expand.length() > 0)
315			{
316				hash.put(abbrev.abbrev,abbrev.expand);
317			}
318		}
319		return hash;
320	}
321
322	public int getColumnCount()
323	{
324		return 2;
325	}
326
327	public int getRowCount()
328	{
329		return abbrevs.size();
330	}
331
332	public Object getValueAt(int row, int col)
333	{
334		Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
335		switch(col)
336		{
337		case 0:
338			return abbrev.abbrev;
339		case 1:
340			return abbrev.expand;
341		default:
342			return null;
343		}
344	}
345
346	public boolean isCellEditable(int row, int col)
347	{
348		return false;
349	}
350
351	public void setValueAt(Object value, int row, int col)
352	{
353		if(value == null)
354			value = "";
355
356		Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
357
358		if(col == 0)
359			abbrev.abbrev = (String)value;
360		else
361			abbrev.expand = (String)value;
362
363		fireTableRowsUpdated(row,row);
364	}
365
366	public String getColumnName(int index)
367	{
368		switch(index)
369		{
370		case 0:
371			return jEdit.getProperty("options.abbrevs.abbrev");
372		case 1:
373			return jEdit.getProperty("options.abbrevs.expand");
374		default:
375			return null;
376		}
377	}
378
379	class AbbrevCompare implements MiscUtilities.Compare
380	{
381		int col;
382
383		AbbrevCompare(int col)
384		{
385			this.col = col;
386		}
387
388		public int compare(Object obj1, Object obj2)
389		{
390			Abbrev a1 = (Abbrev)obj1;
391			Abbrev a2 = (Abbrev)obj2;
392
393			if(col == 0)
394			{
395				String abbrev1 = a1.abbrev.toLowerCase();
396				String abbrev2 = a2.abbrev.toLowerCase();
397
398				return MiscUtilities.compareStrings(
399					abbrev1,abbrev2,true);
400			}
401			else
402			{
403				String expand1 = a1.expand.toLowerCase();
404				String expand2 = a2.expand.toLowerCase();
405
406				return MiscUtilities.compareStrings(
407					expand1,expand2,true);
408			}
409		}
410	}
411}
412
413class Abbrev
414{
415	Abbrev() {}
416
417	Abbrev(String abbrev, String expand)
418	{
419		this.abbrev = abbrev;
420		this.expand = expand;
421	}
422
423	String abbrev;
424	String expand;
425}