KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > options > AbbrevsOptionPane


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
23 package org.gjt.sp.jedit.options;
24
25 //{{{ Imports
26
import javax.swing.border.EmptyBorder JavaDoc;
27 import javax.swing.event.*;
28 import javax.swing.table.*;
29 import javax.swing.*;
30 import java.awt.event.*;
31 import java.awt.*;
32 import java.util.*;
33 import org.gjt.sp.jedit.gui.*;
34 import org.gjt.sp.jedit.*;
35 import org.gjt.sp.util.StandardUtilities;
36 //}}}
37

38 //{{{ AbbrevsOptionPane class
39
/**
40  * Abbrev editor.
41  * @author Slava Pestov
42  * @version $Id: AbbrevsOptionPane.java 5570 2006-07-11 09:27:07Z kpouer $
43  */

44 public class AbbrevsOptionPane extends AbstractOptionPane
45 {
46     //{{{ AbbrevsOptionPane constructor
47
public AbbrevsOptionPane()
48     {
49         super("abbrevs");
50     } //}}}
51

52     //{{{ _init() method
53
protected void _init()
54     {
55         setLayout(new BorderLayout());
56
57         JPanel panel = new JPanel(new BorderLayout(6,6));
58
59         expandOnInput = new JCheckBox(jEdit.getProperty("options.abbrevs"
60             + ".expandOnInput"),Abbrevs.getExpandOnInput());
61
62         panel.add(expandOnInput,BorderLayout.NORTH);
63
64         JPanel panel2 = new JPanel();
65         panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
66         panel2.setBorder(new EmptyBorder JavaDoc(0,0,6,0));
67         panel2.add(Box.createGlue());
68         JLabel label = new JLabel(jEdit.getProperty("options.abbrevs.set"));
69         label.setBorder(new EmptyBorder JavaDoc(0,0,0,12));
70         panel2.add(label);
71
72         Hashtable _modeAbbrevs = Abbrevs.getModeAbbrevs();
73         modeAbbrevs = new Hashtable();
74         Mode[] modes = jEdit.getModes();
75         Arrays.sort(modes,new MiscUtilities.StringICaseCompare());
76         String JavaDoc[] sets = new String JavaDoc[modes.length + 1];
77         sets[0] = "global";
78         for(int i = 0; i < modes.length; i++)
79         {
80             String JavaDoc 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         panel2.add(setsComboBox);
89         panel2.add(Box.createGlue());
90         panel.add(panel2,BorderLayout.SOUTH);
91
92         add(BorderLayout.NORTH,panel);
93
94         globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs());
95         abbrevsTable = new JTable(globalAbbrevs);
96         abbrevsTable.getColumnModel().getColumn(1).setCellRenderer(
97             new Renderer());
98         abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
99         abbrevsTable.getTableHeader().setReorderingAllowed(false);
100         abbrevsTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
101         abbrevsTable.getSelectionModel().addListSelectionListener(
102             new SelectionHandler());
103         abbrevsTable.getSelectionModel().setSelectionMode(
104             ListSelectionModel.SINGLE_SELECTION);
105         abbrevsTable.addMouseListener(new TableMouseHandler());
106         Dimension d = abbrevsTable.getPreferredSize();
107         d.height = Math.min(d.height,200);
108         JScrollPane scroller = new JScrollPane(abbrevsTable);
109         scroller.setPreferredSize(d);
110         add(BorderLayout.CENTER,scroller);
111
112         JPanel buttons = new JPanel();
113         buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
114         buttons.setBorder(new EmptyBorder JavaDoc(6,0,0,0));
115
116         add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
117         add.setToolTipText(jEdit.getProperty("options.abbrevs.add"));
118         add.addActionListener(actionHandler);
119         buttons.add(add);
120         remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
121         remove.setToolTipText(jEdit.getProperty("options.abbrevs.remove"));
122         remove.addActionListener(actionHandler);
123         buttons.add(remove);
124         edit = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png"));
125         edit.setToolTipText(jEdit.getProperty("options.abbrevs.edit"));
126         edit.addActionListener(actionHandler);
127         buttons.add(edit);
128         buttons.add(Box.createGlue());
129
130         add(BorderLayout.SOUTH,buttons);
131         setsComboBox.setSelectedIndex(jEdit.getIntegerProperty("options.abbrevs.combobox.index", 0));
132         updateEnabled();
133     } //}}}
134

135     //{{{ _save() method
136
protected void _save()
137     {
138         if(abbrevsTable.getCellEditor() != null)
139             abbrevsTable.getCellEditor().stopCellEditing();
140
141         Abbrevs.setExpandOnInput(expandOnInput.isSelected());
142
143         Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable());
144
145         Hashtable modeHash = new Hashtable();
146         Enumeration keys = modeAbbrevs.keys();
147         Enumeration values = modeAbbrevs.elements();
148         while(keys.hasMoreElements())
149         {
150             modeHash.put(keys.nextElement(),((AbbrevsModel)values.nextElement())
151                 .toHashtable());
152         }
153         Abbrevs.setModeAbbrevs(modeHash);
154     } //}}}
155

156     //{{{ Private members
157

158     //{{{ Instance variables
159
private JComboBox setsComboBox;
160     private JCheckBox expandOnInput;
161     private JTable abbrevsTable;
162     private AbbrevsModel globalAbbrevs;
163     private Hashtable modeAbbrevs;
164     private JButton add;
165     private JButton edit;
166     private JButton remove;
167     //}}}
168

169     //{{{ updateEnabled() method
170
private void updateEnabled()
171     {
172         int selectedRow = abbrevsTable.getSelectedRow();
173         edit.setEnabled(selectedRow != -1);
174         remove.setEnabled(selectedRow != -1);
175     } //}}}
176

177     //{{{ edit() method
178
private void edit()
179     {
180         AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
181
182         int row = abbrevsTable.getSelectedRow();
183
184         String JavaDoc abbrev = (String JavaDoc)abbrevsModel.getValueAt(row,0);
185         String JavaDoc expansion = (String JavaDoc)abbrevsModel.getValueAt(row,1);
186         String JavaDoc oldAbbrev = abbrev;
187
188         EditAbbrevDialog dialog = new EditAbbrevDialog(
189             GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
190             abbrev,expansion,abbrevsModel.toHashtable());
191         abbrev = dialog.getAbbrev();
192         expansion = dialog.getExpansion();
193         if(abbrev != null && expansion != null)
194         {
195             for(int i = 0; i < abbrevsModel.getRowCount(); i++)
196             {
197                 if(abbrevsModel.getValueAt(i,0).equals(oldAbbrev))
198                 {
199                     abbrevsModel.remove(i);
200                     break;
201                 }
202             }
203
204             add(abbrevsModel,abbrev,expansion);
205         }
206     } //}}}
207

208     //{{{ add() method
209
private void add(AbbrevsModel abbrevsModel, String JavaDoc abbrev,
210         String JavaDoc expansion)
211     {
212         for(int i = 0; i < abbrevsModel.getRowCount(); i++)
213         {
214             if(abbrevsModel.getValueAt(i,0).equals(abbrev))
215             {
216                 abbrevsModel.remove(i);
217                 break;
218             }
219         }
220
221         abbrevsModel.add(abbrev,expansion);
222         updateEnabled();
223     } //}}}
224

225     //}}}
226

227     //{{{ HeaderMouseHandler class
228
class HeaderMouseHandler extends MouseAdapter
229     {
230         public void mouseClicked(MouseEvent evt)
231         {
232             switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint()))
233             {
234             case 0:
235                 ((AbbrevsModel)abbrevsTable.getModel()).sort(0);
236                 break;
237             case 1:
238                 ((AbbrevsModel)abbrevsTable.getModel()).sort(1);
239                 break;
240             }
241         }
242     } //}}}
243

244     //{{{ TableMouseHandler class
245
class TableMouseHandler extends MouseAdapter
246     {
247         public void mouseClicked(MouseEvent evt)
248         {
249             if(evt.getClickCount() == 2)
250                 edit();
251         }
252     } //}}}
253

254     //{{{ SelectionHandler class
255
class SelectionHandler implements ListSelectionListener
256     {
257         public void valueChanged(ListSelectionEvent evt)
258         {
259             updateEnabled();
260         }
261     } //}}}
262

263     //{{{ ActionHandler class
264
class ActionHandler implements ActionListener
265     {
266         public void actionPerformed(ActionEvent evt)
267         {
268             AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
269
270             Object JavaDoc source = evt.getSource();
271             if(source == setsComboBox)
272             {
273                 jEdit.setIntegerProperty("options.abbrevs.combobox.index", setsComboBox.getSelectedIndex());
274                 String JavaDoc selected = (String JavaDoc)setsComboBox.getSelectedItem();
275                 if(selected.equals("global"))
276                 {
277                     abbrevsTable.setModel(globalAbbrevs);
278                 }
279                 else
280                 {
281                     abbrevsTable.setModel((AbbrevsModel)
282                         modeAbbrevs.get(selected));
283                 }
284                 updateEnabled();
285             }
286             else if(source == add)
287             {
288                 EditAbbrevDialog dialog = new EditAbbrevDialog(
289                     GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
290                     null,null,abbrevsModel.toHashtable());
291                 String JavaDoc abbrev = dialog.getAbbrev();
292                 String JavaDoc expansion = dialog.getExpansion();
293                 if(abbrev != null && abbrev.length() != 0
294                     && expansion != null
295                     && expansion.length() != 0)
296                 {
297                     add(abbrevsModel,abbrev,expansion);
298                 }
299             }
300             else if(source == edit)
301             {
302                 edit();
303             }
304             else if(source == remove)
305             {
306                 int selectedRow = abbrevsTable.getSelectedRow();
307                 abbrevsModel.remove(selectedRow);
308                 updateEnabled();
309             }
310         }
311     } //}}}
312

313     //{{{ Renderer class
314
static class Renderer extends DefaultTableCellRenderer
315     {
316         public Component getTableCellRendererComponent(
317             JTable table,
318             Object JavaDoc value,
319             boolean isSelected,
320             boolean cellHasFocus,
321             int row,
322             int col)
323         {
324             String JavaDoc valueStr = value.toString();
325
326             // workaround for Swing's annoying processing of
327
// labels starting with <html>, which often breaks
328
if(valueStr.toLowerCase().startsWith("<html>"))
329                 valueStr = ' ' + valueStr;
330             return super.getTableCellRendererComponent(table,valueStr,
331                 isSelected,cellHasFocus,row,col);
332         }
333     } //}}}
334
} //}}}
335

336 //{{{ AbbrevsModel class
337
class AbbrevsModel extends AbstractTableModel
338 {
339     Vector abbrevs;
340     int lastSort;
341
342     //{{{ AbbrevsModel constructor
343
AbbrevsModel(Hashtable abbrevHash)
344     {
345         abbrevs = new Vector();
346
347         if(abbrevHash != null)
348         {
349             Enumeration abbrevEnum = abbrevHash.keys();
350             Enumeration expandEnum = abbrevHash.elements();
351
352             while(abbrevEnum.hasMoreElements())
353             {
354                 abbrevs.addElement(new Abbrev((String JavaDoc)abbrevEnum.nextElement(),
355                     (String JavaDoc)expandEnum.nextElement()));
356             }
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 JavaDoc abbrev, String JavaDoc expansion)
372     {
373         abbrevs.addElement(new Abbrev(abbrev,expansion));
374         sort(lastSort);
375     } //}}}
376

377     //{{{ remove() method
378
void remove(int index)
379     {
380         abbrevs.removeElementAt(index);
381         fireTableStructureChanged();
382     } //}}}
383

384     //{{{ toHashtable() method
385
public Hashtable toHashtable()
386     {
387         Hashtable hash = new Hashtable();
388         for(int i = 0; i < abbrevs.size(); i++)
389         {
390             Abbrev abbrev = (Abbrev)abbrevs.elementAt(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 JavaDoc getValueAt(int row, int col)
414     {
415         Abbrev abbrev = (Abbrev)abbrevs.elementAt(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     //{{{ isCellEditable() method
428
public boolean isCellEditable(int row, int col)
429     {
430         return false;
431     } //}}}
432

433     //{{{ setValueAt() method
434
public void setValueAt(Object JavaDoc value, int row, int col)
435     {
436         if(value == null)
437             value = "";
438
439         Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
440
441         if(col == 0)
442             abbrev.abbrev = (String JavaDoc)value;
443         else
444             abbrev.expand = (String JavaDoc)value;
445
446         fireTableRowsUpdated(row,row);
447     } //}}}
448

449     //{{{ getColumnName() method
450
public String JavaDoc getColumnName(int index)
451     {
452         switch(index)
453         {
454         case 0:
455             return jEdit.getProperty("options.abbrevs.abbrev");
456         case 1:
457             return jEdit.getProperty("options.abbrevs.expand");
458         default:
459             return null;
460         }
461     } //}}}
462

463     //{{{ AbbrevCompare class
464
static class AbbrevCompare implements Comparator
465     {
466         int col;
467
468         AbbrevCompare(int col)
469         {
470             this.col = col;
471         }
472
473         public int compare(Object JavaDoc obj1, Object JavaDoc obj2)
474         {
475             Abbrev a1 = (Abbrev)obj1;
476             Abbrev a2 = (Abbrev)obj2;
477
478             if(col == 0)
479             {
480                 String JavaDoc abbrev1 = a1.abbrev.toLowerCase();
481                 String JavaDoc abbrev2 = a2.abbrev.toLowerCase();
482
483                 return StandardUtilities.compareStrings(
484                     abbrev1,abbrev2,true);
485             }
486             else
487             {
488                 String JavaDoc expand1 = a1.expand.toLowerCase();
489                 String JavaDoc expand2 = a2.expand.toLowerCase();
490
491                 return StandardUtilities.compareStrings(
492                     expand1,expand2,true);
493             }
494         }
495     } //}}}
496
} //}}}
497

498 //{{{ Abbrev class
499
class Abbrev
500 {
501     Abbrev() {}
502
503     Abbrev(String JavaDoc abbrev, String JavaDoc expand)
504     {
505         this.abbrev = abbrev;
506         this.expand = expand;
507     }
508
509     String JavaDoc abbrev;
510     String JavaDoc expand;
511 } //}}}
512
Popular Tags