KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > util > 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.modeler.util;
57
58 import java.awt.Component JavaDoc;
59
60 import javax.swing.DefaultListCellRenderer JavaDoc;
61 import javax.swing.ImageIcon JavaDoc;
62 import javax.swing.JList JavaDoc;
63 import javax.swing.JTree JavaDoc;
64 import javax.swing.ListCellRenderer JavaDoc;
65 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
66 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
67 import javax.swing.tree.TreeCellRenderer JavaDoc;
68
69 import org.objectstyle.cayenne.access.DataDomain;
70 import org.objectstyle.cayenne.access.DataNode;
71 import org.objectstyle.cayenne.map.Attribute;
72 import org.objectstyle.cayenne.map.DataMap;
73 import org.objectstyle.cayenne.map.DbEntity;
74 import org.objectstyle.cayenne.map.DerivedDbEntity;
75 import org.objectstyle.cayenne.map.Entity;
76 import org.objectstyle.cayenne.map.MapObject;
77 import org.objectstyle.cayenne.map.MappingNamespace;
78 import org.objectstyle.cayenne.map.ObjEntity;
79 import org.objectstyle.cayenne.map.Procedure;
80 import org.objectstyle.cayenne.map.Relationship;
81 import org.objectstyle.cayenne.query.Query;
82
83 /**
84  * Utility class that serves as a factory for various project renderers.
85  *
86  * @since 1.1
87  * @author Andrei Adamchik
88  */

89 public final class CellRenderers {
90
91     // common icons
92
protected static ImageIcon JavaDoc domainIcon;
93     protected static ImageIcon JavaDoc nodeIcon;
94     protected static ImageIcon JavaDoc mapIcon;
95     protected static ImageIcon JavaDoc dbEntityIcon;
96     protected static ImageIcon JavaDoc objEntityIcon;
97     protected static ImageIcon JavaDoc relationshipIcon;
98     protected static ImageIcon JavaDoc attributeIcon;
99     protected static ImageIcon JavaDoc derivedDbEntityIcon;
100     protected static ImageIcon JavaDoc procedureIcon;
101     protected static ImageIcon JavaDoc queryIcon;
102
103     static {
104         domainIcon = ModelerUtil.buildIcon("icon-dom.gif");
105         nodeIcon = ModelerUtil.buildIcon("icon-node.gif");
106         mapIcon = ModelerUtil.buildIcon("icon-datamap.gif");
107         dbEntityIcon = ModelerUtil.buildIcon("icon-dbentity.gif");
108         objEntityIcon = ModelerUtil.buildIcon("icon-objentity.gif");
109         derivedDbEntityIcon = ModelerUtil.buildIcon("icon-derived-dbentity.gif");
110         procedureIcon = ModelerUtil.buildIcon("icon-stored-procedure.gif");
111         queryIcon = ModelerUtil.buildIcon("icon-query.gif");
112         relationshipIcon = ModelerUtil.buildIcon("icon-relationship.gif");
113         attributeIcon = ModelerUtil.buildIcon("icon-attribute.gif");
114     }
115
116     public static ImageIcon JavaDoc iconForObject(Object JavaDoc object) {
117         if (object == null) {
118             return null;
119         }
120
121         if (object instanceof DataDomain) {
122             return domainIcon;
123         }
124         else if (object instanceof DataNode) {
125             return nodeIcon;
126         }
127         else if (object instanceof DataMap) {
128             return mapIcon;
129         }
130         else if (object instanceof Entity) {
131             Entity entity = (Entity) object;
132             if (entity instanceof DerivedDbEntity) {
133                 return derivedDbEntityIcon;
134             }
135             else if (entity instanceof DbEntity) {
136                 return dbEntityIcon;
137             }
138             else if (entity instanceof ObjEntity) {
139                 return objEntityIcon;
140             }
141         }
142         else if (object instanceof Procedure) {
143             return procedureIcon;
144         }
145         else if (object instanceof Query) {
146             return queryIcon;
147         }
148         else if (object instanceof Relationship) {
149             return relationshipIcon;
150         }
151         else if (object instanceof Attribute) {
152             return attributeIcon;
153         }
154
155         return null;
156     }
157
158     /**
159      * Returns a TreeCellRenderer to display Cayenne project tree nodes with icons.
160      */

161     public static TreeCellRenderer JavaDoc treeRenderer() {
162         return new TreeRenderer();
163     }
164
165     /**
166      * Returns a ListCellRenderer to display Cayenne project tree nodes without icons.
167      */

168     public static ListCellRenderer JavaDoc listRenderer() {
169         return new ListRenderer(false);
170     }
171
172     /**
173      * Returns a ListCellRenderer to display Cayenne project tree nodes with icons.
174      */

175     public static ListCellRenderer JavaDoc listRendererWithIcons() {
176         return new ListRenderer(true);
177     }
178
179     /**
180      * Returns a ListCellRenderer to display Cayenne project tree nodes with icons.
181      */

182     public static ListCellRenderer JavaDoc entityListRendererWithIcons(MappingNamespace namespace) {
183         return new EntityRenderer(namespace);
184     }
185
186     final static class EntityRenderer extends DefaultListCellRenderer JavaDoc {
187         MappingNamespace namespace;
188
189         EntityRenderer(MappingNamespace namespace) {
190             this.namespace = namespace;
191         }
192
193         /**
194           * Will trim the value to fit defined size.
195           */

196         public Component JavaDoc getListCellRendererComponent(
197             JList JavaDoc list,
198             Object JavaDoc value,
199             int index,
200             boolean isSelected,
201             boolean cellHasFocus) {
202
203             // the sequence is important - call super with converted value,
204
// then set an icon, and then return "this"
205
ImageIcon JavaDoc icon = CellRenderers.iconForObject(value);
206
207             if (value instanceof MapObject) {
208                 MapObject mapObject = (MapObject) value;
209                 String JavaDoc label = mapObject.getName();
210
211                 if (mapObject instanceof Entity) {
212                     Entity entity = (Entity) mapObject;
213
214                     // for different namespace display its name
215
DataMap dataMap = entity.getDataMap();
216                     if (dataMap != null && dataMap != this.namespace) {
217                         label += " (" + dataMap.getName() + ")";
218                     }
219                 }
220                 
221                 value = label;
222             }
223
224             super.getListCellRendererComponent(
225                 list,
226                 value,
227                 index,
228                 isSelected,
229                 cellHasFocus);
230
231             setIcon(icon);
232
233             return this;
234         }
235     }
236
237     final static class ListRenderer extends DefaultListCellRenderer JavaDoc {
238         boolean showIcons;
239
240         ListRenderer(boolean showIcons) {
241             this.showIcons = showIcons;
242         }
243
244         /**
245           * Will trim the value to fit defined size.
246           */

247         public Component JavaDoc getListCellRendererComponent(
248             JList JavaDoc list,
249             Object JavaDoc value,
250             int index,
251             boolean isSelected,
252             boolean cellHasFocus) {
253
254             // the sequence is important - call super with converted value,
255
// then set an icon, and then return "this"
256

257             Object JavaDoc renderedValue = ModelerUtil.getObjectName(value);
258             if (renderedValue == null) {
259                 // render NULL as empty string
260
renderedValue = " ";
261             }
262
263             super.getListCellRendererComponent(
264                 list,
265                 renderedValue,
266                 index,
267                 isSelected,
268                 cellHasFocus);
269
270             if (showIcons) {
271                 setIcon(iconForObject(value));
272             }
273
274             return this;
275         }
276     }
277
278     final static class TreeRenderer extends DefaultTreeCellRenderer JavaDoc {
279         public Component JavaDoc getTreeCellRendererComponent(
280             JTree JavaDoc tree,
281             Object JavaDoc value,
282             boolean sel,
283             boolean expanded,
284             boolean leaf,
285             int row,
286             boolean hasFocus) {
287
288             // the sequence is important - call super,
289
// then set an icon, and then return "this"
290

291             super.getTreeCellRendererComponent(
292                 tree,
293                 value,
294                 sel,
295                 expanded,
296                 leaf,
297                 row,
298                 hasFocus);
299
300             DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) value;
301             setIcon(iconForObject(node.getUserObject()));
302
303             return this;
304         }
305     }
306 }
307
Popular Tags