001: /*
002: * AbbrevsOptionPane.java - Abbrevs options panel
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999, 2000, 2001, 2002 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.options;
024:
025: //{{{ Imports
026: import javax.swing.border.EmptyBorder;
027: import javax.swing.event.*;
028: import javax.swing.table.*;
029: import javax.swing.*;
030: import java.awt.event.*;
031: import java.awt.*;
032: import java.util.*;
033: import org.gjt.sp.jedit.gui.*;
034: import org.gjt.sp.jedit.*;
035: import org.gjt.sp.util.StandardUtilities;
036:
037: //}}}
038:
039: //{{{ AbbrevsOptionPane class
040: /**
041: * Abbrev editor.
042: * @author Slava Pestov
043: * @version $Id: AbbrevsOptionPane.java 5570 2006-07-11 09:27:07Z kpouer $
044: */
045: public class AbbrevsOptionPane extends AbstractOptionPane {
046: //{{{ AbbrevsOptionPane constructor
047: public AbbrevsOptionPane() {
048: super ("abbrevs");
049: } //}}}
050:
051: //{{{ _init() method
052: protected void _init() {
053: setLayout(new BorderLayout());
054:
055: JPanel panel = new JPanel(new BorderLayout(6, 6));
056:
057: expandOnInput = new JCheckBox(jEdit
058: .getProperty("options.abbrevs" + ".expandOnInput"),
059: Abbrevs.getExpandOnInput());
060:
061: panel.add(expandOnInput, BorderLayout.NORTH);
062:
063: JPanel panel2 = new JPanel();
064: panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
065: panel2.setBorder(new EmptyBorder(0, 0, 6, 0));
066: panel2.add(Box.createGlue());
067: JLabel label = new JLabel(jEdit
068: .getProperty("options.abbrevs.set"));
069: label.setBorder(new EmptyBorder(0, 0, 0, 12));
070: panel2.add(label);
071:
072: Hashtable _modeAbbrevs = Abbrevs.getModeAbbrevs();
073: modeAbbrevs = new Hashtable();
074: Mode[] modes = jEdit.getModes();
075: Arrays.sort(modes, new MiscUtilities.StringICaseCompare());
076: String[] sets = new String[modes.length + 1];
077: sets[0] = "global";
078: for (int i = 0; i < modes.length; i++) {
079: String name = modes[i].getName();
080: sets[i + 1] = name;
081: modeAbbrevs.put(name, new AbbrevsModel(
082: (Hashtable) _modeAbbrevs.get(name)));
083: }
084:
085: setsComboBox = new JComboBox(sets);
086: ActionHandler actionHandler = new ActionHandler();
087: setsComboBox.addActionListener(actionHandler);
088: panel2.add(setsComboBox);
089: panel2.add(Box.createGlue());
090: panel.add(panel2, BorderLayout.SOUTH);
091:
092: add(BorderLayout.NORTH, panel);
093:
094: globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs());
095: abbrevsTable = new JTable(globalAbbrevs);
096: abbrevsTable.getColumnModel().getColumn(1).setCellRenderer(
097: new Renderer());
098: abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
099: abbrevsTable.getTableHeader().setReorderingAllowed(false);
100: abbrevsTable.getTableHeader().addMouseListener(
101: new HeaderMouseHandler());
102: abbrevsTable.getSelectionModel().addListSelectionListener(
103: new SelectionHandler());
104: abbrevsTable.getSelectionModel().setSelectionMode(
105: ListSelectionModel.SINGLE_SELECTION);
106: abbrevsTable.addMouseListener(new TableMouseHandler());
107: Dimension d = abbrevsTable.getPreferredSize();
108: d.height = Math.min(d.height, 200);
109: JScrollPane scroller = new JScrollPane(abbrevsTable);
110: scroller.setPreferredSize(d);
111: add(BorderLayout.CENTER, scroller);
112:
113: JPanel buttons = new JPanel();
114: buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
115: buttons.setBorder(new EmptyBorder(6, 0, 0, 0));
116:
117: add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
118: add.setToolTipText(jEdit.getProperty("options.abbrevs.add"));
119: add.addActionListener(actionHandler);
120: buttons.add(add);
121: remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
122: remove.setToolTipText(jEdit
123: .getProperty("options.abbrevs.remove"));
124: remove.addActionListener(actionHandler);
125: buttons.add(remove);
126: edit = new RolloverButton(GUIUtilities
127: .loadIcon("ButtonProperties.png"));
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(
135: "options.abbrevs.combobox.index", 0));
136: updateEnabled();
137: } //}}}
138:
139: //{{{ _save() method
140: protected void _save() {
141: if (abbrevsTable.getCellEditor() != null)
142: abbrevsTable.getCellEditor().stopCellEditing();
143:
144: Abbrevs.setExpandOnInput(expandOnInput.isSelected());
145:
146: Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable());
147:
148: Hashtable modeHash = new Hashtable();
149: Enumeration keys = modeAbbrevs.keys();
150: Enumeration values = modeAbbrevs.elements();
151: while (keys.hasMoreElements()) {
152: modeHash.put(keys.nextElement(), ((AbbrevsModel) values
153: .nextElement()).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 Hashtable modeAbbrevs;
166: private JButton add;
167: private JButton edit;
168: private JButton remove;
169:
170: //}}}
171:
172: //{{{ updateEnabled() method
173: private void updateEnabled() {
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: AbbrevsModel abbrevsModel = (AbbrevsModel) abbrevsTable
182: .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(GUIUtilities
191: .getParentDialog(AbbrevsOptionPane.this ), abbrev,
192: expansion, abbrevsModel.toHashtable());
193: abbrev = dialog.getAbbrev();
194: expansion = dialog.getExpansion();
195: if (abbrev != null && expansion != null) {
196: for (int i = 0; i < abbrevsModel.getRowCount(); i++) {
197: if (abbrevsModel.getValueAt(i, 0).equals(oldAbbrev)) {
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: for (int i = 0; i < abbrevsModel.getRowCount(); i++) {
211: if (abbrevsModel.getValueAt(i, 0).equals(abbrev)) {
212: abbrevsModel.remove(i);
213: break;
214: }
215: }
216:
217: abbrevsModel.add(abbrev, expansion);
218: updateEnabled();
219: } //}}}
220:
221: //}}}
222:
223: //{{{ HeaderMouseHandler class
224: class HeaderMouseHandler extends MouseAdapter {
225: public void mouseClicked(MouseEvent evt) {
226: switch (abbrevsTable.getTableHeader().columnAtPoint(
227: evt.getPoint())) {
228: case 0:
229: ((AbbrevsModel) abbrevsTable.getModel()).sort(0);
230: break;
231: case 1:
232: ((AbbrevsModel) abbrevsTable.getModel()).sort(1);
233: break;
234: }
235: }
236: } //}}}
237:
238: //{{{ TableMouseHandler class
239: class TableMouseHandler extends MouseAdapter {
240: public void mouseClicked(MouseEvent evt) {
241: if (evt.getClickCount() == 2)
242: edit();
243: }
244: } //}}}
245:
246: //{{{ SelectionHandler class
247: class SelectionHandler implements ListSelectionListener {
248: public void valueChanged(ListSelectionEvent evt) {
249: updateEnabled();
250: }
251: } //}}}
252:
253: //{{{ ActionHandler class
254: class ActionHandler implements ActionListener {
255: public void actionPerformed(ActionEvent evt) {
256: AbbrevsModel abbrevsModel = (AbbrevsModel) abbrevsTable
257: .getModel();
258:
259: Object source = evt.getSource();
260: if (source == setsComboBox) {
261: jEdit.setIntegerProperty(
262: "options.abbrevs.combobox.index", setsComboBox
263: .getSelectedIndex());
264: String selected = (String) setsComboBox
265: .getSelectedItem();
266: if (selected.equals("global")) {
267: abbrevsTable.setModel(globalAbbrevs);
268: } else {
269: abbrevsTable.setModel((AbbrevsModel) modeAbbrevs
270: .get(selected));
271: }
272: updateEnabled();
273: } else if (source == add) {
274: EditAbbrevDialog dialog = new EditAbbrevDialog(
275: GUIUtilities
276: .getParentDialog(AbbrevsOptionPane.this ),
277: null, null, abbrevsModel.toHashtable());
278: String abbrev = dialog.getAbbrev();
279: String expansion = dialog.getExpansion();
280: if (abbrev != null && abbrev.length() != 0
281: && expansion != null && expansion.length() != 0) {
282: add(abbrevsModel, abbrev, expansion);
283: }
284: } else if (source == edit) {
285: edit();
286: } else if (source == remove) {
287: int selectedRow = abbrevsTable.getSelectedRow();
288: abbrevsModel.remove(selectedRow);
289: updateEnabled();
290: }
291: }
292: } //}}}
293:
294: //{{{ Renderer class
295: static class Renderer extends DefaultTableCellRenderer {
296: public Component getTableCellRendererComponent(JTable table,
297: Object value, boolean isSelected, boolean cellHasFocus,
298: int row, int col) {
299: String valueStr = value.toString();
300:
301: // workaround for Swing's annoying processing of
302: // labels starting with <html>, which often breaks
303: if (valueStr.toLowerCase().startsWith("<html>"))
304: valueStr = ' ' + valueStr;
305: return super .getTableCellRendererComponent(table, valueStr,
306: isSelected, cellHasFocus, row, col);
307: }
308: } //}}}
309: } //}}}
310:
311: //{{{ AbbrevsModel class
312: class AbbrevsModel extends AbstractTableModel {
313: Vector abbrevs;
314: int lastSort;
315:
316: //{{{ AbbrevsModel constructor
317: AbbrevsModel(Hashtable abbrevHash) {
318: abbrevs = new Vector();
319:
320: if (abbrevHash != null) {
321: Enumeration abbrevEnum = abbrevHash.keys();
322: Enumeration expandEnum = abbrevHash.elements();
323:
324: while (abbrevEnum.hasMoreElements()) {
325: abbrevs.addElement(new Abbrev((String) abbrevEnum
326: .nextElement(), (String) expandEnum
327: .nextElement()));
328: }
329:
330: sort(0);
331: }
332: } //}}}
333:
334: //{{{ sort() method
335: void sort(int col) {
336: lastSort = col;
337: Collections.sort(abbrevs, new AbbrevCompare(col));
338: fireTableDataChanged();
339: } //}}}
340:
341: //{{{ add() method
342: void add(String abbrev, String expansion) {
343: abbrevs.addElement(new Abbrev(abbrev, expansion));
344: sort(lastSort);
345: } //}}}
346:
347: //{{{ remove() method
348: void remove(int index) {
349: abbrevs.removeElementAt(index);
350: fireTableStructureChanged();
351: } //}}}
352:
353: //{{{ toHashtable() method
354: public Hashtable toHashtable() {
355: Hashtable hash = new Hashtable();
356: for (int i = 0; i < abbrevs.size(); i++) {
357: Abbrev abbrev = (Abbrev) abbrevs.elementAt(i);
358: if (abbrev.abbrev.length() > 0
359: && abbrev.expand.length() > 0) {
360: hash.put(abbrev.abbrev, abbrev.expand);
361: }
362: }
363: return hash;
364: } //}}}
365:
366: //{{{ getColumnCount() method
367: public int getColumnCount() {
368: return 2;
369: } //}}}
370:
371: //{{{ getRowCount() method
372: public int getRowCount() {
373: return abbrevs.size();
374: } //}}}
375:
376: //{{{ getValueAt() method
377: public Object getValueAt(int row, int col) {
378: Abbrev abbrev = (Abbrev) abbrevs.elementAt(row);
379: switch (col) {
380: case 0:
381: return abbrev.abbrev;
382: case 1:
383: return abbrev.expand;
384: default:
385: return null;
386: }
387: } //}}}
388:
389: //{{{ isCellEditable() method
390: public boolean isCellEditable(int row, int col) {
391: return false;
392: } //}}}
393:
394: //{{{ setValueAt() method
395: public void setValueAt(Object value, int row, int col) {
396: if (value == null)
397: value = "";
398:
399: Abbrev abbrev = (Abbrev) abbrevs.elementAt(row);
400:
401: if (col == 0)
402: abbrev.abbrev = (String) value;
403: else
404: abbrev.expand = (String) value;
405:
406: fireTableRowsUpdated(row, row);
407: } //}}}
408:
409: //{{{ getColumnName() method
410: public String getColumnName(int index) {
411: switch (index) {
412: case 0:
413: return jEdit.getProperty("options.abbrevs.abbrev");
414: case 1:
415: return jEdit.getProperty("options.abbrevs.expand");
416: default:
417: return null;
418: }
419: } //}}}
420:
421: //{{{ AbbrevCompare class
422: static class AbbrevCompare implements Comparator {
423: int col;
424:
425: AbbrevCompare(int col) {
426: this .col = col;
427: }
428:
429: public int compare(Object obj1, Object obj2) {
430: Abbrev a1 = (Abbrev) obj1;
431: Abbrev a2 = (Abbrev) obj2;
432:
433: if (col == 0) {
434: String abbrev1 = a1.abbrev.toLowerCase();
435: String abbrev2 = a2.abbrev.toLowerCase();
436:
437: return StandardUtilities.compareStrings(abbrev1,
438: abbrev2, true);
439: } else {
440: String expand1 = a1.expand.toLowerCase();
441: String expand2 = a2.expand.toLowerCase();
442:
443: return StandardUtilities.compareStrings(expand1,
444: expand2, true);
445: }
446: }
447: } //}}}
448: } //}}}
449:
450: //{{{ Abbrev class
451: class Abbrev {
452: Abbrev() {
453: }
454:
455: Abbrev(String abbrev, String expand) {
456: this .abbrev = abbrev;
457: this .expand = expand;
458: }
459:
460: String abbrev;
461: String expand;
462: } //}}}
|