001: /*
002: * SimplePreferencesPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.executequery.gui.prefs;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Color;
026: import java.awt.Graphics;
027: import java.awt.event.MouseEvent;
028: import java.awt.event.MouseListener;
029: import javax.swing.DefaultCellEditor;
030: import javax.swing.JCheckBox;
031: import javax.swing.JColorChooser;
032: import javax.swing.JComboBox;
033: import javax.swing.JLabel;
034: import javax.swing.JPanel;
035: import javax.swing.JScrollPane;
036: import javax.swing.JTable;
037: import javax.swing.JViewport;
038: import javax.swing.table.AbstractTableModel;
039: import javax.swing.table.TableColumn;
040: import javax.swing.table.TableColumnModel;
041: import org.executequery.Constants;
042: import org.executequery.GUIUtilities;
043: import org.underworldlabs.util.SystemProperties;
044: import org.executequery.components.table.CategoryHeaderCellRenderer;
045: import org.underworldlabs.swing.table.CheckBoxTableCellRenderer;
046: import org.underworldlabs.swing.table.ColourTableCellRenderer;
047: import org.underworldlabs.swing.table.ComboBoxCellRenderer;
048: import org.underworldlabs.swing.table.EachRowEditor;
049: import org.underworldlabs.swing.table.EachRowRenderer;
050: import org.executequery.components.table.FileSelectionTableCell;
051: import org.underworldlabs.swing.table.NumberCellEditor;
052: import org.underworldlabs.swing.table.StringCellEditor;
053:
054: /* ----------------------------------------------------------
055: * CVS NOTE: Changes to the CVS repository prior to the
056: * release of version 3.0.0beta1 has meant a
057: * resetting of CVS revision numbers.
058: * ----------------------------------------------------------
059: */
060:
061: /**
062: * Properties panel base.
063: *
064: * @author Takis Diakoumis
065: * @version $Revision: 1.5 $
066: * @date $Date: 2006/09/26 13:16:02 $
067: */
068: public class SimplePreferencesPanel extends JPanel implements
069: MouseListener {
070:
071: /** the table grid colour */
072: private static Color GRID_COLOR;
073:
074: /** the gutter width */
075: private static int GUTTER_WIDTH;
076:
077: /** fixed row height for a preference value */
078: protected final int VALUE_ROW_HEIGHT = 20;
079:
080: /** fixed row height for a preference header */
081: protected final int CATEGORY_ROW_HEIGHT = 18;
082:
083: /** preferences array that this panel displays */
084: private UserPreference[] preferences;
085:
086: /** the table display */
087: private JTable table;
088:
089: /** the table model */
090: private PreferencesTableModel tableModel;
091:
092: static {
093: GRID_COLOR = Color.LIGHT_GRAY;
094: GUTTER_WIDTH = 10;
095: }
096:
097: /** Creates a new instance of SimplePreferencesPanel */
098: public SimplePreferencesPanel(UserPreference[] preferences) {
099: super (new BorderLayout());
100: this .preferences = preferences;
101:
102: try {
103: init();
104: } catch (Exception e) {
105: e.printStackTrace();
106: }
107:
108: }
109:
110: private void init() throws Exception {
111:
112: tableModel = new PreferencesTableModel();
113:
114: table = new JTable(tableModel);
115: table.setCellSelectionEnabled(true);
116: table.setColumnSelectionAllowed(false);
117: table.setRowSelectionAllowed(false);
118: table.setFont(PropertiesBase.panelFont);
119: table.setTableHeader(null);
120:
121: EachRowEditor rowEditor = new EachRowEditor(table);
122:
123: // lazily create as required
124: FileSelectionTableCell fileRenderer = null;
125: ColourTableCellRenderer colourRenderer = null;
126: CheckBoxTableCellRenderer checkBoxRenderer = null;
127: CategoryHeaderCellRenderer categoryRenderer = null;
128: ComboBoxCellRenderer comboRenderer = null;
129:
130: EachRowRenderer rowRendererKeys = null;
131: EachRowRenderer rowRendererValues = new EachRowRenderer();
132:
133: for (int i = 0; i < preferences.length; i++) {
134: int type = preferences[i].getType();
135: DefaultCellEditor editor = null;
136:
137: switch (type) {
138: case UserPreference.STRING_TYPE:
139: String[] values = preferences[i].getAvailableValues();
140: if (values != null && values.length > 0) {
141: editor = new DefaultCellEditor(new TableComboBox(
142: values));
143: rowEditor.setEditorAt(i, editor);
144:
145: if (comboRenderer == null) {
146: comboRenderer = new ComboBoxCellRenderer();
147: }
148:
149: rowRendererValues.add(i, comboRenderer);
150: } else {
151: /*
152: final StringCellEditor stringEditor = new StringCellEditor();
153: stringEditor.setFont(PropertiesBase.panelFont);
154: editor = new DefaultCellEditor(stringEditor) {
155: public Object getCellEditorValue() {
156: return stringEditor.getEditorValue();
157: }
158: };
159: */
160: rowEditor.setEditorAt(i, new DefaultCellEditor(
161: new StringCellEditor()));
162: //rowEditor.setEditorAt(i, editor);
163: }
164: break;
165: case UserPreference.INTEGER_TYPE:
166: final NumberCellEditor numEditor = new NumberCellEditor(
167: preferences[i].getMaxLength(), true);
168: numEditor.setFont(PropertiesBase.panelFont);
169:
170: editor = new DefaultCellEditor(numEditor) {
171: public Object getCellEditorValue() {
172: return numEditor.getStringValue();
173: }
174: };
175:
176: rowEditor.setEditorAt(i, editor);
177: break;
178: case UserPreference.BOOLEAN_TYPE:
179:
180: if (checkBoxRenderer == null) {
181: checkBoxRenderer = new CheckBoxTableCellRenderer();
182: checkBoxRenderer
183: .setHorizontalAlignment(JLabel.LEFT);
184: }
185:
186: rowRendererValues.add(i, checkBoxRenderer);
187: rowEditor.setEditorAt(i, new DefaultCellEditor(
188: new JCheckBox()));
189: break;
190: case UserPreference.COLOUR_TYPE:
191:
192: if (colourRenderer == null) {
193: colourRenderer = new ColourTableCellRenderer();
194: colourRenderer.setFont(PropertiesBase.panelFont);
195: table.addMouseListener(this );
196: }
197:
198: rowRendererValues.add(i, colourRenderer);
199: break;
200: case UserPreference.CATEGORY_TYPE:
201:
202: if (categoryRenderer == null) {
203: categoryRenderer = new CategoryHeaderCellRenderer();
204: }
205: if (rowRendererKeys == null) {
206: rowRendererKeys = new EachRowRenderer();
207: }
208:
209: rowRendererValues.add(i, categoryRenderer);
210: rowRendererKeys.add(i, categoryRenderer);
211: break;
212: case UserPreference.FILE_TYPE:
213:
214: if (fileRenderer == null) {
215: fileRenderer = new FileSelectionTableCell();
216: fileRenderer.setFont(PropertiesBase.panelFont);
217: }
218:
219: rowRendererValues.add(i, fileRenderer);
220: rowEditor.setEditorAt(i, fileRenderer);
221: break;
222:
223: }
224:
225: if (type == UserPreference.CATEGORY_TYPE) {
226: table.setRowHeight(i, CATEGORY_ROW_HEIGHT);
227: } else {
228: table.setRowHeight(i, VALUE_ROW_HEIGHT);
229: }
230:
231: }
232:
233: table.setGridColor(GRID_COLOR);
234: TableColumnModel tcm = table.getColumnModel();
235:
236: TableColumn column = tcm.getColumn(2);
237: column.setCellRenderer(rowRendererValues);
238: column.setCellEditor(rowEditor);
239:
240: column = tcm.getColumn(1);
241: column.setCellRenderer(rowRendererKeys);
242:
243: column = tcm.getColumn(0);
244: column.setMaxWidth(GUTTER_WIDTH);
245: column.setMinWidth(GUTTER_WIDTH);
246: column.setPreferredWidth(GUTTER_WIDTH);
247: column.setCellRenderer(categoryRenderer);
248:
249: DisplayViewport viewport = new DisplayViewport(table);
250: JScrollPane scroller = new JScrollPane();
251: scroller.setViewport(viewport);
252: add(scroller, BorderLayout.CENTER);
253:
254: }
255:
256: protected void restoreDefaults() {
257:
258: for (int i = 0; i < preferences.length; i++) {
259:
260: switch (preferences[i].getType()) {
261: case UserPreference.STRING_TYPE:
262: case UserPreference.INTEGER_TYPE:
263: preferences[i].reset(SystemProperties.getProperty(
264: "defaults", preferences[i].getKey()));
265: break;
266: case UserPreference.BOOLEAN_TYPE:
267: preferences[i].reset(new Boolean(SystemProperties
268: .getProperty("defaults", preferences[i]
269: .getKey())));
270: break;
271: case UserPreference.COLOUR_TYPE:
272: preferences[i].reset(SystemProperties
273: .getColourProperty("defaults", preferences[i]
274: .getKey()));
275: break;
276: }
277:
278: }
279: tableModel.fireTableDataChanged();
280:
281: // reset the row heights
282: for (int i = 0; i < preferences.length; i++) {
283: int type = preferences[i].getType();
284: if (type == UserPreference.CATEGORY_TYPE) {
285: table.setRowHeight(i, CATEGORY_ROW_HEIGHT);
286: } else {
287: table.setRowHeight(i, VALUE_ROW_HEIGHT);
288: }
289: }
290:
291: }
292:
293: public UserPreference[] getPreferences() {
294: return preferences;
295: }
296:
297: protected void savePreferences() {
298: // stop table editing
299: if (table.isEditing()) {
300: table.editingStopped(null);
301: }
302:
303: String propertiesName = "user";
304:
305: // set the new properties
306: for (int i = 0; i < preferences.length; i++) {
307:
308: if (preferences[i].getType() != UserPreference.CATEGORY_TYPE) {
309: SystemProperties.setProperty(propertiesName,
310: preferences[i].getKey(), preferences[i]
311: .getSaveValue());
312: }
313:
314: }
315:
316: }
317:
318: public void mouseClicked(MouseEvent evt) {
319: int valueColumn = 2;
320: int row = table.rowAtPoint(evt.getPoint());
321: int col = table.columnAtPoint(evt.getPoint());
322:
323: if (row == -1) {
324: return;
325: }
326:
327: if (col == valueColumn) {
328:
329: if (preferences[row].getType() == UserPreference.COLOUR_TYPE) {
330: Color oldColor = (Color) preferences[row].getValue();
331: Color newColor = JColorChooser
332: .showDialog(GUIUtilities.getParentFrame(),
333: "Select Colour", (Color) tableModel
334: .getValueAt(row, valueColumn));
335:
336: if (newColor != null) {
337: tableModel.setValueAt(newColor, row, valueColumn);
338: firePropertyChange(Constants.COLOUR_PREFERENCE,
339: oldColor, newColor);
340: }
341:
342: }
343:
344: }
345:
346: }
347:
348: public void mousePressed(MouseEvent e) {
349: }
350:
351: public void mouseReleased(MouseEvent e) {
352: }
353:
354: public void mouseEntered(MouseEvent e) {
355: }
356:
357: public void mouseExited(MouseEvent e) {
358: }
359:
360: class PreferencesTableModel extends AbstractTableModel {
361:
362: public void setValueAt(Object value, int row, int column) {
363: UserPreference preference = preferences[row];
364: preference.setValue(value);
365: }
366:
367: public int getRowCount() {
368: return preferences.length;
369: }
370:
371: public int getColumnCount() {
372: return 3;
373: }
374:
375: public Object getValueAt(int row, int column) {
376: UserPreference preference = preferences[row];
377:
378: switch (column) {
379: case 1:
380: return preference.getDisplayedKey();
381: case 2:
382: return preference.getValue();
383: default:
384: return Constants.EMPTY;
385: }
386:
387: }
388:
389: public boolean isCellEditable(int row, int column) {
390: UserPreference preference = preferences[row];
391: return (preference.getType() != UserPreference.CATEGORY_TYPE)
392: && (column == 2);
393: }
394:
395: } // class PreferencesTableModel
396:
397: class DisplayViewport extends JViewport {
398:
399: protected DisplayViewport(JTable _table) {
400: setView(_table);
401: setBackground(_table.getBackground());
402: }
403:
404: public void paintComponent(Graphics g) {
405: super .paintComponent(g);
406: int viewHeight = getViewSize().height;
407: g.setColor(GRID_COLOR);
408: g.fillRect(0, viewHeight - 1, GUTTER_WIDTH, getHeight()
409: - viewHeight + 1);
410: }
411:
412: } // class DisplayViewport
413:
414: class TableComboBox extends JComboBox {
415:
416: public TableComboBox(String[] values) {
417: super (values);
418: setFont(PropertiesBase.panelFont);
419: }
420:
421: } // class TableComboBox
422:
423: }
|