KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > util > ModelerUtil


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.net.URL JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.util.Arrays JavaDoc;
61 import java.util.Collection JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.List JavaDoc;
64 import java.util.Set JavaDoc;
65
66 import javax.swing.ImageIcon JavaDoc;
67
68 import org.apache.commons.beanutils.PropertyUtils;
69 import org.objectstyle.cayenne.access.DataDomain;
70 import org.objectstyle.cayenne.access.DataNode;
71 import org.objectstyle.cayenne.access.types.ExtendedTypeMap;
72 import org.objectstyle.cayenne.map.DataMap;
73 import org.objectstyle.cayenne.map.DbEntity;
74 import org.objectstyle.cayenne.map.MapObject;
75 import org.objectstyle.cayenne.modeler.ProjectController;
76 import org.objectstyle.cayenne.modeler.ModelerConstants;
77
78 /**
79  * Various unorganized utility methods used by CayenneModeler.
80  *
81  * @author Andrei Adamchik
82  */

83 public final class ModelerUtil {
84
85     /**
86      * Returns the "name" property of the object.
87      *
88      * @since 1.1
89      */

90     public static String JavaDoc getObjectName(Object JavaDoc object) {
91         if (object == null) {
92             return null;
93         }
94         else if (object instanceof MapObject) {
95             return ((MapObject) object).getName();
96         }
97         else if (object instanceof String JavaDoc) {
98             return (String JavaDoc) object;
99         }
100         else {
101             try {
102                 // use reflection
103
return (String JavaDoc) PropertyUtils.getSimpleProperty(object, "name");
104             }
105             catch (Exception JavaDoc ex) {
106                 return null;
107             }
108         }
109     }
110
111     /**
112      * Returns an icon, building it from an image file located at the shared resources
113      * folder for the modeler.
114      */

115     public static ImageIcon JavaDoc buildIcon(String JavaDoc path) {
116         ClassLoader JavaDoc cl = ModelerUtil.class.getClassLoader();
117         URL JavaDoc url = cl.getResource(ModelerConstants.RESOURCE_PATH + path);
118         return new ImageIcon JavaDoc(url);
119     }
120
121     /**
122      * Returns array of db attribute names for DbEntity mapped to current ObjEntity.
123      */

124     public static Collection JavaDoc getDbAttributeNames(
125             ProjectController mediator,
126             DbEntity entity) {
127
128         Set JavaDoc keys = entity.getAttributeMap().keySet();
129         List JavaDoc list = new ArrayList JavaDoc(keys.size());
130         list.add("");
131         list.addAll(keys);
132         return list;
133     }
134
135     public static String JavaDoc[] getRegisteredTypeNames() {
136         String JavaDoc[] srcList = new ExtendedTypeMap().getRegisteredTypeNames();
137         Arrays.sort(srcList);
138
139         String JavaDoc[] finalList = new String JavaDoc[srcList.length + 1];
140         System.arraycopy(srcList, 0, finalList, 1, srcList.length);
141         finalList[0] = "";
142
143         return finalList;
144     }
145
146     public static DataNode getNodeLinkedToMap(DataDomain domain, DataMap map) {
147         Collection JavaDoc nodes = domain.getDataNodes();
148
149         // go via an iterator in an indexed loop, since
150
// we already obtained the size
151
// (and index is required to initialize array)
152
Iterator JavaDoc nodesIt = nodes.iterator();
153         while (nodesIt.hasNext()) {
154             DataNode node = (DataNode) nodesIt.next();
155
156             if (node.getDataMaps().contains(map)) {
157                 return node;
158             }
159         }
160
161         return null;
162     }
163 }
Popular Tags