1 56 package org.objectstyle.cayenne.modeler.util; 57 58 import java.net.URL ; 59 import java.util.ArrayList ; 60 import java.util.Arrays ; 61 import java.util.Collection ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 import java.util.Set ; 65 66 import javax.swing.ImageIcon ; 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 83 public final class ModelerUtil { 84 85 90 public static String getObjectName(Object 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 ) { 98 return (String ) object; 99 } 100 else { 101 try { 102 return (String ) PropertyUtils.getSimpleProperty(object, "name"); 104 } 105 catch (Exception ex) { 106 return null; 107 } 108 } 109 } 110 111 115 public static ImageIcon buildIcon(String path) { 116 ClassLoader cl = ModelerUtil.class.getClassLoader(); 117 URL url = cl.getResource(ModelerConstants.RESOURCE_PATH + path); 118 return new ImageIcon (url); 119 } 120 121 124 public static Collection getDbAttributeNames( 125 ProjectController mediator, 126 DbEntity entity) { 127 128 Set keys = entity.getAttributeMap().keySet(); 129 List list = new ArrayList (keys.size()); 130 list.add(""); 131 list.addAll(keys); 132 return list; 133 } 134 135 public static String [] getRegisteredTypeNames() { 136 String [] srcList = new ExtendedTypeMap().getRegisteredTypeNames(); 137 Arrays.sort(srcList); 138 139 String [] finalList = new String [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 nodes = domain.getDataNodes(); 148 149 Iterator 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 |