PageRenderTime 114ms CodeModel.GetById 39ms app.highlight 16ms RepoModel.GetById 55ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/options/AbbrevsOptionPane.java

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