Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.apache.cayenne.modeler.util;
 
 
 
Utility class that serves as a factory for various project renderers.

Author(s):
Andrei Adamchik
Since:
1.1
 
 public final class CellRenderers {
 
     // common icons
     protected static ImageIcon domainIcon;
     protected static ImageIcon nodeIcon;
     protected static ImageIcon mapIcon;
     protected static ImageIcon dbEntityIcon;
     protected static ImageIcon objEntityIcon;
     protected static ImageIcon relationshipIcon;
     protected static ImageIcon attributeIcon;
     protected static ImageIcon derivedDbEntityIcon;
     protected static ImageIcon procedureIcon;
     protected static ImageIcon queryIcon;
 
     static {
          = ModelerUtil.buildIcon("icon-dom.gif");
          = ModelerUtil.buildIcon("icon-node.gif");
          = ModelerUtil.buildIcon("icon-datamap.gif");
          = ModelerUtil.buildIcon("icon-dbentity.gif");
          = ModelerUtil.buildIcon("icon-objentity.gif");
          = ModelerUtil.buildIcon("icon-derived-dbentity.gif");
          = ModelerUtil.buildIcon("icon-stored-procedure.gif");
          = ModelerUtil.buildIcon("icon-query.gif");
          = ModelerUtil.buildIcon("icon-relationship.gif");
          = ModelerUtil.buildIcon("icon-attribute.gif");
     }
 
     public static ImageIcon iconForObject(Object object) {
         if (object == null) {
             return null;
         }
 
         if (object instanceof DataDomain) {
             return ;
         }
         else if (object instanceof DataNode) {
             return ;
         }
         else if (object instanceof DataMap) {
             return ;
         }
         else if (object instanceof Entity) {
             Entity entity = (Entityobject;
             if (entity instanceof DerivedDbEntity) {
                 return ;
             }
             else if (entity instanceof DbEntity) {
                return ;
            }
            else if (entity instanceof ObjEntity) {
                return ;
            }
        }
        else if (object instanceof Procedure) {
            return ;
        }
        else if (object instanceof Query) {
            return ;
        }
        else if (object instanceof Relationship) {
            return ;
        }
        else if (object instanceof Attribute) {
            return ;
        }
        return null;
    }

    
Returns a TreeCellRenderer to display Cayenne project tree nodes with icons.
    public static TreeCellRenderer treeRenderer() {
        return new TreeRenderer();
    }

    
Returns a ListCellRenderer to display Cayenne project tree nodes without icons.
    public static ListCellRenderer listRenderer() {
        return new ListRenderer(false);
    }

    
Returns a ListCellRenderer to display Cayenne project tree nodes with icons.
    public static ListCellRenderer listRendererWithIcons() {
        return new ListRenderer(true);
    }

    
Returns a ListCellRenderer to display Cayenne project tree nodes with icons.
    public static ListCellRenderer entityListRendererWithIcons(MappingNamespace namespace) {
        return new EntityRenderer(namespace);
    }
    final static class EntityRenderer extends DefaultListCellRenderer {
        MappingNamespace namespace;
        EntityRenderer(MappingNamespace namespace) {
            this. = namespace;
        }

        
Will trim the value to fit defined size.
        public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
            // the sequence is important - call super with converted value,
            // then set an icon, and then return "this" 
            ImageIcon icon = CellRenderers.iconForObject(value);
            if (value instanceof CayenneMapEntry) {
                CayenneMapEntry mapObject = (CayenneMapEntryvalue;
                String label = mapObject.getName();
                if (mapObject instanceof Entity) {
                    Entity entity = (EntitymapObject;
                    // for different namespace display its name
                    DataMap dataMap = entity.getDataMap();
                    if (dataMap != null && dataMap != this.) {
                        label += " (" + dataMap.getName() + ")";
                    }
                }
                
                value = label;
            }
            super.getListCellRendererComponent(
                list,
                value,
                index,
                isSelected,
                cellHasFocus);
            setIcon(icon);
            return this;
        }
    }
    final static class ListRenderer extends DefaultListCellRenderer {
        boolean showIcons;
        ListRenderer(boolean showIcons) {
            this. = showIcons;
        }

        
Will trim the value to fit defined size.
        public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
            // the sequence is important - call super with converted value,
            // then set an icon, and then return "this" 
            Object renderedValue = ModelerUtil.getObjectName(value);
            if (renderedValue == null) {
                // render NULL as empty string
                renderedValue = " ";
            }
            super.getListCellRendererComponent(
                list,
                renderedValue,
                index,
                isSelected,
                cellHasFocus);
            if () {
                setIcon(iconForObject(value));
            }
            return this;
        }
    }
    final static class TreeRenderer extends DefaultTreeCellRenderer {
        public Component getTreeCellRendererComponent(
            JTree tree,
            Object value,
            boolean sel,
            boolean expanded,
            boolean leaf,
            int row,
            boolean hasFocus) {
            // the sequence is important - call super,
            // then set an icon, and then return "this" 
            super.getTreeCellRendererComponent(
                tree,
                value,
                sel,
                expanded,
                leaf,
                row,
                hasFocus);
            DefaultMutableTreeNode node = (DefaultMutableTreeNodevalue;
            setIcon(iconForObject(node.getUserObject()));
            return this;
        }
    }