KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.*;
59 import javax.swing.*;
60 import javax.swing.table.*;
61 import java.text.*;
62
63 public class CellRenderers {
64   public FormatRenderer createFormatTableCellRenderer(
65       Format format,
66       String JavaDoc nullText,
67       String JavaDoc invalidText,
68       int alignment) {
69     FormatRenderer renderer = new FormatRenderer(format, nullText, invalidText);
70     if (alignment >= 0)
71       renderer.setHorizontalAlignment(alignment);
72     return renderer;
73   }
74
75   public BooleanRenderer createBooleanTableCellRenderer() {
76     BooleanRenderer renderer = new BooleanRenderer();
77     return renderer;
78   }
79
80   public ObjectRenderer createDefaultTableCellRenderer(int alignment) {
81     ObjectRenderer renderer = new ObjectRenderer();
82     if (alignment >= 0)
83       renderer.setHorizontalAlignment(alignment);
84     return renderer;
85   }
86
87   public TableCellRenderer createTableCellRenderer(ObjEntityViewField field) {
88     TableCellRenderer renderer = null;
89     Format format = field.getDisplayFormat();
90     int dataType = field.getDataType().getValue();
91     int alignment;
92     String JavaDoc nullText = "";
93     String JavaDoc invalidText = "";
94
95     switch (dataType) {
96       case DataTypeEnum.INTEGER_TYPE_VALUE:
97       case DataTypeEnum.DOUBLE_TYPE_VALUE:
98       case DataTypeEnum.MONEY_TYPE_VALUE:
99       case DataTypeEnum.PERCENT_TYPE_VALUE:
100         alignment = JLabel.RIGHT;
101         break;
102       default:
103         alignment = JLabel.LEFT;
104         break;
105     }
106
107     if (format != null) {
108       renderer = createFormatTableCellRenderer(
109           format, nullText, invalidText, alignment);
110     } else {
111       if (dataType == DataTypeEnum.BOOLEAN_TYPE_VALUE)
112         renderer = createBooleanTableCellRenderer();
113       else
114         renderer = createDefaultTableCellRenderer(alignment);
115     }
116
117     return renderer;
118   }
119
120   public ListCellRenderer createFormatListCellRenderer(
121       Format format,
122       String JavaDoc nullText,
123       String JavaDoc invalidText,
124       int alignment) {
125     FormatListCellRenderer renderer = new FormatListCellRenderer(
126         format, nullText, invalidText);
127     if (alignment >= 0)
128       renderer.setHorizontalAlignment(alignment);
129     return renderer;
130   }
131
132   public ListCellRenderer createListCellRenderer(ObjEntityViewField field) {
133     ListCellRenderer renderer = null;
134     Format format = field.getDisplayFormat();
135     int dataType = field.getDataType().getValue();
136     int alignment;
137     String JavaDoc nullText = "";
138     String JavaDoc invalidText = "";
139
140     switch (dataType) {
141       case DataTypeEnum.INTEGER_TYPE_VALUE:
142       case DataTypeEnum.DOUBLE_TYPE_VALUE:
143       case DataTypeEnum.MONEY_TYPE_VALUE:
144       case DataTypeEnum.PERCENT_TYPE_VALUE:
145         alignment = JLabel.RIGHT;
146       default:
147         alignment = JLabel.LEFT;
148         break;
149     }
150
151     if (format != null) {
152       renderer = createFormatListCellRenderer(
153           format, nullText, invalidText, alignment);
154     } else {
155       renderer = new DefaultListCellRenderer();
156       ((DefaultListCellRenderer)renderer).setHorizontalAlignment(alignment);
157     }
158
159     return renderer;
160   }
161
162   public void installRenderers(JTable table) {
163     TableModel m = table.getModel();
164     if (!(m instanceof DOTableModel))
165       return;
166     DOTableModel model = (DOTableModel)m;
167     TableColumnModel columnModel = table.getColumnModel();
168     int columnCount = model.getColumnCount();
169     for (int i = 0; i < columnCount; i++) {
170       ObjEntityViewField field = model.getField(i);
171       TableCellRenderer renderer = createTableCellRenderer(field);
172       TableColumn column = columnModel.getColumn(i);
173       column.setCellRenderer(renderer);
174     }
175   }
176
177   public void installRenderer(JList list, ObjEntityViewField field) {
178     ListCellRenderer renderer = createListCellRenderer(field);
179     list.setCellRenderer(renderer);
180   }
181
182   public void installRenderer(JComboBox comboBox, ObjEntityViewField field) {
183     ListCellRenderer renderer = createListCellRenderer(field);
184     comboBox.setRenderer(renderer);
185   }
186
187   public class BooleanRenderer extends JCheckBox implements TableCellRenderer {
188     public BooleanRenderer() {
189       super();
190       setHorizontalAlignment(JLabel.CENTER);
191     }
192
193     public Component getTableCellRendererComponent(JTable table, Object JavaDoc value,
194         boolean isSelected, boolean hasFocus, int row, int column) {
195       if (isSelected) {
196         setForeground(table.getSelectionForeground());
197         super.setBackground(table.getSelectionBackground());
198       }
199       else {
200         setForeground(table.getForeground());
201         setBackground(table.getBackground());
202       }
203       setSelected((value != null && ((Boolean JavaDoc)value).booleanValue()));
204       return this;
205     }
206   }
207
208   public class ObjectRenderer extends DefaultTableCellRenderer {
209   }
210
211   public class FormatRenderer extends DefaultTableCellRenderer {
212     protected Format formatter;
213     protected String JavaDoc nullText = "";
214     protected String JavaDoc invalidText = "";
215
216     public FormatRenderer() {
217       this(null, null, null);
218     }
219
220     public FormatRenderer(Format formatter) {
221       this(formatter, null, null);
222     }
223
224     public FormatRenderer(Format formatter, String JavaDoc nullText) {
225       this(formatter, nullText, null);
226     }
227
228     public FormatRenderer(Format formatter, String JavaDoc nullText, String JavaDoc invalidText) {
229       setFormatter(formatter);
230       setNullText(nullText);
231       setInvalidText(invalidText);
232     }
233
234     public void setFormatter(Format formatter) {
235       this.formatter = formatter;
236     }
237     public Format getFormatter() {
238       return formatter;
239     }
240     public void setNullText(String JavaDoc nullText) {
241       this.nullText = (nullText != null ? nullText : this.nullText);
242     }
243     public String JavaDoc getNullText() {
244       return nullText;
245     }
246     public void setInvalidText(String JavaDoc invalidText) {
247       this.invalidText = (invalidText != null ? invalidText : this.invalidText);
248     }
249     public String JavaDoc getInvalidText() {
250       return invalidText;
251     }
252
253     public void setValue(Object JavaDoc value) {
254       String JavaDoc text;
255       try {
256         if (value == null)
257           text = nullText;
258         else if (formatter==null)
259           text = value.toString();
260         else
261           text = formatter.format(value);
262       }
263       catch (Exception JavaDoc ex) {
264         text = invalidText;
265       }
266       setText(text);
267     }
268   }
269
270   public class FormatListCellRenderer extends DefaultListCellRenderer {
271     protected Format formatter;
272     protected String JavaDoc nullText = "";
273     protected String JavaDoc invalidText = "";
274
275     public FormatListCellRenderer() {
276       this(null, null, null);
277     }
278
279     public FormatListCellRenderer(Format formatter) {
280       this(formatter, null, null);
281     }
282
283     public FormatListCellRenderer(Format formatter, String JavaDoc nullText) {
284       this(formatter, nullText, null);
285     }
286
287     public FormatListCellRenderer(Format formatter, String JavaDoc nullText, String JavaDoc invalidText) {
288       setFormatter(formatter);
289       setNullText(nullText);
290       setInvalidText(invalidText);
291     }
292
293     public void setFormatter(Format formatter) {
294       this.formatter = formatter;
295     }
296     public Format getFormatter() {
297       return formatter;
298     }
299     public void setNullText(String JavaDoc nullText) {
300       this.nullText = (nullText != null ? nullText : this.nullText);
301     }
302     public String JavaDoc getNullText() {
303       return nullText;
304     }
305     public void setInvalidText(String JavaDoc invalidText) {
306       this.invalidText = (invalidText != null ? invalidText : this.invalidText);
307     }
308     public String JavaDoc getInvalidText() {
309       return invalidText;
310     }
311
312     public Component getListCellRendererComponent(
313         JList list,
314         Object JavaDoc value,
315         int index,
316         boolean isSelected,
317         boolean cellHasFocus) {
318       String JavaDoc text;
319       try {
320         if (value == null)
321           text = nullText;
322         else if (formatter==null)
323           text = value.toString();
324         else
325           text = formatter.format(value);
326       }
327       catch (Exception JavaDoc ex) {
328         text = invalidText;
329       }
330       return super.getListCellRendererComponent(
331           list, text, index, isSelected, cellHasFocus);
332     }
333   }
334
335 }
Popular Tags