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;
 
 import java.util.Map;
 
 
Swing TreeModel for Entity attributes and relationships

Author(s):
Andrei Adamchik
Since:
1.1
 
 public class EntityTreeModel implements TreeModel {
     protected Entity root;
     protected Map sortedChildren;
 
     // TODO: in the future replace with a more generic filter 
     // to allow arbitrary tree customization
     protected boolean hideAttributes;
 
     public EntityTreeModel(Entity root) {
         this. = root;
          = Collections.synchronizedMap(new HashMap());
     }
 
     public Object getRoot() {
         return ;
     }
 
     public boolean isHideAttributes() {
         return ;
     }
 
     public void setHideAttributes(boolean hideAttributes) {
         this. = hideAttributes;
     }
 
     public Object getChild(Object nodeint index) {
         return sortedChildren(node)[index];
     }
 
     public int getChildCount(Object node) {
         return (node instanceof Attribute) ? 0 : sortedChildren(node).length;
     }
 
     public boolean isLeaf(Object node) {
         return getChildCount(node) == 0;
     }
 
     public void valueForPathChanged(TreePath arg0Object arg1) {
         // do nothing...
     }
 
     public int getIndexOfChild(Object nodeObject child) {
         if (node instanceof Attribute) {
             return -1;
         }
 
         // wonder if linear search will be faster, considering that
         // this comparator uses reflection?
         return Arrays.binarySearch(
             sortedChildren(node),
             child,
             Comparators.getNamedObjectComparator());
     }
 
     public void addTreeModelListener(TreeModelListener listener) {
         // do nothing...
     }
    public void removeTreeModelListener(TreeModelListener listener) {
        // do nothing...
    }
    private Object[] sortedChildren(Object node) {
        Entity entity = entityForNonLeafNode(node);
        
        // may happen in incomplete relationships
        if(entity == null) {
            return new Object[0];
        }
        synchronized () {
            String key = entity.getName();
            Object[] sortedForNode = (Object[]) .get(key);
            if (sortedForNode == null) {
                Collection attributes = entity.getAttributes();
                Collection relationships = entity.getRelationships();
                // combine two collections in an array
                int alen = () ? 0 : attributes.size();
                int rlen = relationships.size();
                sortedForNode = new Object[alen + rlen];
                if (!) {
                    Iterator ait = attributes.iterator();
                    for (int i = 0; i < aleni++) {
                        sortedForNode[i] = ait.next();
                    }
                }
                Iterator rit = relationships.iterator();
                for (int i = 0; i < rleni++) {
                    sortedForNode[alen + i] = rit.next();
                }
                Arrays.sort(sortedForNode, Comparators.getEntityChildrenComparator());
                .put(keysortedForNode);
            }
            return sortedForNode;
        }
    }
    private Entity entityForNonLeafNode(Object node) {
        if (node instanceof Entity) {
            return (Entitynode;
        }
        else if (node instanceof Relationship) {
            return ((Relationshipnode).getTargetEntity();
        }
        String className = (node != null) ? node.getClass().getName() : "null";
        throw new IllegalArgumentException("Unexpected non-leaf node: " + className);
    }
New to GrepCode? Check out our FAQ X