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.List;
 import java.util.Map;
 
 
 import  org.apache.cayenne.map.Attribute;
 import  org.apache.cayenne.map.Entity;
 import  org.apache.cayenne.map.Relationship;

Swing TreeModel for Entity attributes and relationships

Since:
1.1
 
 public class EntityTreeModel implements TreeModel {
     protected Entity root;
     protected Map<ObjectObject[]> sortedChildren;

    
Filter for checking attributes and relationships
 
     protected EntityTreeFilter filter;
     
     public EntityTreeModel(Entity root) {
         this. = root;
          = Collections.synchronizedMap(new HashMap<ObjectObject[]>());
     }
 
     public Object getRoot() {
         return ;
     }
 
     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 () {
            Object key = node;
            Object[] sortedForNode = .get(key);
            if (sortedForNode == null) {
                Collection<? extends Attribute> attributes = entity.getAttributes();
                Collection<? extends Relationship> relationships = entity.getRelationships();
                List<Objectnodes = new Vector<Object>();
                                
                // combine two collections in an array
                for (Attribute attr : attributes) {
                    if ( == null || .attributeMatch(nodeattr)) {
                        nodes.add(attr);
                    }
                }
                for (Relationship rel : relationships) {
                    if ( == null || .relationshipMatch(noderel)) {
                        nodes.add(rel);
                    }
                }
                sortedForNode = nodes.toArray();
                
                Arrays.sort(sortedForNode, Comparators.getEntityChildrenComparator());
                .put(keysortedForNode);
            }
            return sortedForNode;
        }
    }
    
    
Removes children cache for specified entity.
    public void invalidate() {
        synchronized () {
            .clear();
        }
    }
    
    
Removes children cache for specified entity.
    public void invalidateChildren(Entity entity) {
        synchronized () {
            .remove(entity);
            
            for (Relationship rel : entity.getRelationships()) {
                .remove(rel);
            }
        }
    }
    private Entity entityForNonLeafNode(Object node) {
        if (node instanceof Entity) {
            return (Entity) node;
        }
        else if (node instanceof Relationship) {
            return ((Relationship) node).getTargetEntity();
        }
        String className = (node != null) ? node.getClass().getName() : "null";
        throw new IllegalArgumentException("Unexpected non-leaf node: " + className);
    }
    
    
Sets filter for attrs and rels
    public void setFilter(EntityTreeFilter filter) {
        this. = filter;
    }
    
    
Returns filter for attrs and rels
    public EntityTreeFilter getFilter() {
        return ;
    }
New to GrepCode? Check out our FAQ X