1 56 package org.objectstyle.cayenne.modeler.util; 57 58 import java.awt.Component ; 59 60 import javax.swing.DefaultListCellRenderer ; 61 import javax.swing.ImageIcon ; 62 import javax.swing.JList ; 63 import javax.swing.JTree ; 64 import javax.swing.ListCellRenderer ; 65 import javax.swing.tree.DefaultMutableTreeNode ; 66 import javax.swing.tree.DefaultTreeCellRenderer ; 67 import javax.swing.tree.TreeCellRenderer ; 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 89 public final class CellRenderers { 90 91 protected static ImageIcon domainIcon; 93 protected static ImageIcon nodeIcon; 94 protected static ImageIcon mapIcon; 95 protected static ImageIcon dbEntityIcon; 96 protected static ImageIcon objEntityIcon; 97 protected static ImageIcon relationshipIcon; 98 protected static ImageIcon attributeIcon; 99 protected static ImageIcon derivedDbEntityIcon; 100 protected static ImageIcon procedureIcon; 101 protected static ImageIcon 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 iconForObject(Object 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 161 public static TreeCellRenderer treeRenderer() { 162 return new TreeRenderer(); 163 } 164 165 168 public static ListCellRenderer listRenderer() { 169 return new ListRenderer(false); 170 } 171 172 175 public static ListCellRenderer listRendererWithIcons() { 176 return new ListRenderer(true); 177 } 178 179 182 public static ListCellRenderer entityListRendererWithIcons(MappingNamespace namespace) { 183 return new EntityRenderer(namespace); 184 } 185 186 final static class EntityRenderer extends DefaultListCellRenderer { 187 MappingNamespace namespace; 188 189 EntityRenderer(MappingNamespace namespace) { 190 this.namespace = namespace; 191 } 192 193 196 public Component getListCellRendererComponent( 197 JList list, 198 Object value, 199 int index, 200 boolean isSelected, 201 boolean cellHasFocus) { 202 203 ImageIcon icon = CellRenderers.iconForObject(value); 206 207 if (value instanceof MapObject) { 208 MapObject mapObject = (MapObject) value; 209 String label = mapObject.getName(); 210 211 if (mapObject instanceof Entity) { 212 Entity entity = (Entity) mapObject; 213 214 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 { 238 boolean showIcons; 239 240 ListRenderer(boolean showIcons) { 241 this.showIcons = showIcons; 242 } 243 244 247 public Component getListCellRendererComponent( 248 JList list, 249 Object value, 250 int index, 251 boolean isSelected, 252 boolean cellHasFocus) { 253 254 257 Object renderedValue = ModelerUtil.getObjectName(value); 258 if (renderedValue == null) { 259 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 { 279 public Component getTreeCellRendererComponent( 280 JTree tree, 281 Object value, 282 boolean sel, 283 boolean expanded, 284 boolean leaf, 285 int row, 286 boolean hasFocus) { 287 288 291 super.getTreeCellRendererComponent( 292 tree, 293 value, 294 sel, 295 expanded, 296 leaf, 297 row, 298 hasFocus); 299 300 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) value; 301 setIcon(iconForObject(node.getUserObject())); 302 303 return this; 304 } 305 } 306 } 307 | Popular Tags |