KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dataview > CellEditors


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.dataview;
57
58 import java.text.*;
59 import java.awt.*;
60 import java.awt.event.*;
61 import javax.swing.*;
62 import javax.swing.table.*;
63 import javax.swing.text.*;
64 import javax.swing.border.*;
65
66 public class CellEditors {
67   protected Border editStateBorder = new LineBorder(Color.black);
68   protected Border invalidStateBorder = new LineBorder(Color.red);
69
70   public FormattedFieldEditor createFormattedFieldEditor(
71       JFormattedTextField.AbstractFormatter formatter, int alignment) {
72     JFormattedTextField field = new JFormattedTextField(formatter);
73     if (alignment >= 0)
74       field.setHorizontalAlignment(alignment);
75     return new FormattedFieldEditor(field);
76   }
77
78   public FormattedFieldEditor createFormattedFieldEditor(
79       Format formatter, int alignment) {
80     JFormattedTextField field = new JFormattedTextField(formatter);
81     if (alignment >= 0)
82       field.setHorizontalAlignment(alignment);
83     return new FormattedFieldEditor(field);
84   }
85
86   public FormattedFieldEditor createFormattedFieldEditor(
87       String JavaDoc mask, int alignment) throws ParseException {
88     MaskFormatter formatter = new MaskFormatter(mask);
89     return createFormattedFieldEditor(formatter, alignment);
90   }
91
92   public TextFieldEditor createTextFieldEditor(int alignment) {
93     JTextField field = new JTextField();
94     if (alignment >= 0)
95       field.setHorizontalAlignment(alignment);
96     return new TextFieldEditor(field);
97   }
98
99   public CheckBoxEditor createCheckBoxEditor() {
100     JCheckBox checkBox = new JCheckBox();
101     return new CheckBoxEditor(checkBox);
102   }
103
104   public ComboBoxEditor createComboBoxEditor(
105       ComboBoxModel model, ListCellRenderer renderer) {
106     JComboBox comboBox = new JComboBox(model);
107     if (renderer != null)
108       comboBox.setRenderer(renderer);
109     return new ComboBoxEditor(comboBox);
110   }
111
112   public ButtonEditor createButtonEditor(JButton button) {
113     return new ButtonEditor(button);
114   }
115
116   public SpinnerEditor createSpinnerEditor(
117       SpinnerModel model, Format format) {
118     JSpinner spinner = new JSpinner(model);
119     if (format != null) {
120       JFormattedTextField field = ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();
121       JFormattedTextField.AbstractFormatter formatter = field.getFormatter();
122       if (formatter instanceof InternationalFormatter)
123         ((InternationalFormatter)formatter).setFormat(format);
124     }
125     return new SpinnerEditor(spinner);
126   }
127
128   public TableCellEditor createTableCellEditor(ObjEntityViewField field) {
129     CellRenderers cellRenderers = new CellRenderers();
130     TableCellEditor editor = null;
131     Format format = field.getEditFormat();
132     int dataType = field.getDataType().getValue();
133     boolean lookup = field.isLookup();
134     int alignment;
135
136     switch (dataType) {
137       case DataTypeEnum.INTEGER_TYPE_VALUE:
138       case DataTypeEnum.DOUBLE_TYPE_VALUE:
139       case DataTypeEnum.MONEY_TYPE_VALUE:
140       case DataTypeEnum.PERCENT_TYPE_VALUE:
141         alignment = JTextField.RIGHT;
142         break;
143       default:
144         alignment = JTextField.LEFT;
145         break;
146     }
147
148     if (lookup) {
149       ComboBoxModel comboData =
150           new DefaultComboBoxModel(field.getLookupValues());
151       ListCellRenderer comboRenderer =
152           cellRenderers.createListCellRenderer(field);
153       editor = createComboBoxEditor(comboData, comboRenderer);
154     } else if (format != null) {
155       if (format instanceof MapFormat) {
156         MapFormat mapFormat = (MapFormat)format;
157         ComboBoxModel comboData =
158           new DefaultComboBoxModel((mapFormat).getValues());
159         ListCellRenderer comboRenderer =
160           cellRenderers.createFormatListCellRenderer(
161           mapFormat, mapFormat.getNullFormat(), null, -1);
162         editor = createComboBoxEditor(comboData, comboRenderer);
163       } else {
164         editor = createFormattedFieldEditor(format, alignment);
165       }
166     } else {
167       if (dataType == DataTypeEnum.BOOLEAN_TYPE_VALUE)
168         editor = createCheckBoxEditor();
169       else
170         editor = createTextFieldEditor(alignment);
171     }
172
173     return editor;
174   }
175
176   public void installEditors(JTable table) {
177     TableModel m = table.getModel();
178     if (!(m instanceof DOTableModel))
179       return;
180     DOTableModel model = (DOTableModel)m;
181     TableColumnModel columnModel = table.getColumnModel();
182     int columnCount = model.getColumnCount();
183     for (int i = 0; i < columnCount; i++) {
184       ObjEntityViewField field = model.getField(i);
185       TableCellEditor editor = createTableCellEditor(field);
186       TableColumn column = columnModel.getColumn(i);
187       column.setCellEditor(editor);
188     }
189   }
190
191   public class FormattedFieldEditor extends DefaultCellEditor {
192     public FormattedFieldEditor(final JFormattedTextField field) {
193       super(field);
194       field.removeActionListener(delegate);
195       this.clickCountToStart = 2;
196       delegate = new EditorDelegate() {
197         public void setValue(Object JavaDoc value) {
198           field.setValue(value);
199         }
200         public Object JavaDoc getCellEditorValue() {
201           return field.getValue();
202         }
203         public boolean stopCellEditing() {
204           if (field.isEditValid()) {
205             try {field.commitEdit();}
206             catch (ParseException ex) {}
207           } else {
208             field.setBorder(invalidStateBorder);
209             return false;
210           }
211           return super.stopCellEditing();
212         }
213       };
214       field.addActionListener(delegate);
215     }
216     public Component getTableCellEditorComponent(JTable table, Object JavaDoc value,
217                          boolean isSelected,
218                          int row, int column) {
219       delegate.setValue(value);
220       if (((JFormattedTextField)editorComponent).isEditValid())
221         editorComponent.setBorder(editStateBorder);
222       else
223         editorComponent.setBorder(invalidStateBorder);
224       return editorComponent;
225     }
226   }
227
228   public class TextFieldEditor extends DefaultCellEditor {
229     protected Border editStateBorder = new LineBorder(Color.black);
230     public TextFieldEditor(JTextField field) {
231       super(field);
232       field.setBorder(editStateBorder);
233     }
234   }
235
236   public class CheckBoxEditor extends DefaultCellEditor {
237     public CheckBoxEditor(JCheckBox checkBox) {
238       super(checkBox);
239       checkBox.setBorder(editStateBorder);
240       checkBox.setHorizontalAlignment(JCheckBox.CENTER);
241     }
242   }
243
244   public class ComboBoxEditor extends DefaultCellEditor {
245     public ComboBoxEditor(JComboBox comboBox) {
246       super(comboBox);
247       comboBox.setBorder(editStateBorder);
248     }
249   }
250
251   public class SpinnerEditor extends DefaultCellEditor {
252     public SpinnerEditor(final JSpinner spinner) {
253       super(new JTextField());
254       editorComponent = spinner;
255       spinner.setBorder(editStateBorder);
256       spinner.getEditor().setBorder(null);
257       final JFormattedTextField field =
258           ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();
259       field.setBorder(null);
260       delegate = new EditorDelegate() {
261         public void setValue(Object JavaDoc value) {
262           spinner.setValue(value);
263         }
264         public Object JavaDoc getCellEditorValue() {
265           return spinner.getValue();
266         }
267         public boolean stopCellEditing() {
268           if (field.isEditValid()) {
269             try {field.commitEdit();}
270             catch (ParseException ex) {}
271           } else {
272             field.setBorder(invalidStateBorder);
273             return false;
274           }
275           return super.stopCellEditing();
276         }
277       };
278       field.addActionListener(delegate);
279     }
280 // public Component getTableCellEditorComponent(JTable table, Object value,
281
// boolean isSelected, int row, int column) {
282
// JSpinner spinner = (JSpinner)editorComponent;
283
// spinner.setValue(value);
284
// return spinner;
285
// }
286
// public Object getCellEditorValue() {
287
// JSpinner spinner = (JSpinner)editorComponent;
288
// return spinner.getValue();
289
// }
290
}
291
292   public class ButtonEditor extends DefaultCellEditor {
293     protected Object JavaDoc currentValue;
294
295     public ButtonEditor(JButton button) {
296       super(new JCheckBox());
297       editorComponent = button;
298       setClickCountToStart(1);
299       button.addActionListener(new ActionListener() {
300         public void actionPerformed(ActionEvent e) {
301           fireEditingStopped();
302         }
303       });
304       button.setBorder(editStateBorder);
305       button.setFocusPainted(false);
306     }
307     protected void fireEditingStopped() {
308       super.fireEditingStopped();
309     }
310     public Object JavaDoc getCellEditorValue() {
311       return currentValue;
312     }
313     public Component getTableCellEditorComponent(JTable table,
314         Object JavaDoc value,
315         boolean isSelected,
316         int row,
317         int column) {
318       ((JButton)editorComponent).setText(String.valueOf(value));
319       currentValue = value;
320       return editorComponent;
321     }
322   }
323 }
Popular Tags