treefactoryimpl.java

来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 636 行 · 第 1/2 页

JAVA
636
字号
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas, Bettina Lademann                |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla.gui.internal.view;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;

import org.rapla.components.util.Assert;
import org.rapla.components.util.InverseComparator;
import org.rapla.entities.Category;
import org.rapla.entities.Named;
import org.rapla.entities.NamedComparator;
import org.rapla.entities.RaplaType;
import org.rapla.entities.User;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Period;
import org.rapla.entities.domain.Reservation;
import org.rapla.entities.domain.ReservationStartComparator;
import org.rapla.entities.dynamictype.Classifiable;
import org.rapla.entities.dynamictype.Classification;
import org.rapla.entities.dynamictype.ClassificationFilter;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.entities.dynamictype.DynamicTypeAnnotations;
import org.rapla.facade.Conflict;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.TreeFactory;
import org.rapla.gui.internal.common.CalendarSelectionModel;
import org.rapla.gui.toolkit.RecursiveNode;
import org.rapla.gui.toolkit.TreeToolTipRenderer;

public class TreeFactoryImpl extends RaplaGUIComponent implements TreeFactory
{
    public TreeFactoryImpl(RaplaContext sm) throws RaplaException {
        super(sm);
        this.defaultIcon = getIcon("icon.tree.default");
    }

    Icon defaultIcon;

    public TreeModel createClassifiableModel(Classifiable[] classifiables) throws RaplaException {
        return createClassifiableModel( classifiables, null );
    }

    public DefaultTreeModel createClassifiableModel(Classifiable[] classifiables,DynamicType[] dynamicTypes) throws RaplaException {
        boolean addOnlyTypesWithVisibleChildren = false;
        if ( dynamicTypes == null || dynamicTypes.length == 0){
            dynamicTypes = getQuery().getDynamicTypes( null );
            addOnlyTypesWithVisibleChildren = true;
        }
        Map nodeMap = new HashMap();

        for(int i=0; i < dynamicTypes.length; i++) {
            TreeNode node = new NamedNode(dynamicTypes[i]);
            nodeMap.put(dynamicTypes[i],node);
        }

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("ROOT");
        Comparator comp = null;
        if ( classifiables.length > 0 && classifiables[0] instanceof Reservation) {
            comp = new InverseComparator( new ReservationStartComparator( getLocale()) );
        } else {
            comp = new NamedComparator( getLocale());
        }
        Set sortedClassifiable = new TreeSet( comp );
        sortedClassifiable.addAll( Arrays.asList(classifiables ));
        for(Iterator it = sortedClassifiable.iterator();it.hasNext();)  {
            Classifiable classifiable = (Classifiable) it.next();
            Classification classification = classifiable.getClassification();
            NamedNode childNode = new NamedNode((Named)classifiable);
            DynamicType type = classification.getType();
            Assert.notNull(type);
            DefaultMutableTreeNode typeNode = (DefaultMutableTreeNode) nodeMap.get(type);
            Assert.notNull(typeNode);
            typeNode.add(childNode);

        }

        for(int i=0,count = 0; i < dynamicTypes.length; i++) {
            DefaultMutableTreeNode typeNode = (DefaultMutableTreeNode) nodeMap.get(dynamicTypes[i]);
            // Only add typeNodes with children
            if (!typeNode.isLeaf() ||  !addOnlyTypesWithVisibleChildren )
                root.insert(typeNode,count ++);
        }
        return new DefaultTreeModel(root);
    }

    public TreeCellRenderer createComplexRenderer() {
        return new ComplexTreeCellRenderer();
    }

    private boolean isInFilter( ClassificationFilter[] filter, Classifiable classifiable) {
        if ( filter.length == 0)
            return true;
        for ( int i=0;i< filter.length;i++)
        {
            if ( filter[i].matches( classifiable.getClassification())) {
                return true;
            }
        }
        return false;
    }

    private boolean isInFilter( ClassificationFilter[] filter, DynamicType type) {
        if ( filter.length == 0)
            return true;
        for ( int i=0;i< filter.length;i++)
        {
            if ( filter[i].getType().equals( type )) {
                return true;
            }
        }
        return false;
    }

    private boolean hasRulesFor( ClassificationFilter[] filter, DynamicType type) {
        if ( filter.length == 0)
            return false;
        for ( int i=0;i< filter.length;i++)
        {
            if ( filter[i].getType().equals( type ) && filter[i].ruleSize() > 0) 
            {
                return true;
            }
        }
        return false;
    }

    private Collection getConflicts(ClassificationFilter[] filter, User user) throws RaplaException {

        List result = new ArrayList();
        Conflict[] conflicts = getQuery().getConflicts( getQuery().today());
        for (int i=0;i<conflicts.length;i++)
        {
            Conflict conflict = conflicts[i];
            if (!isInFilter( filter, conflict.getAllocatable() ) )
            {
                continue;
            }
            if ( !isInFilter( filter, conflict.getReservation1() )
                    && !isInFilter( filter, conflict.getReservation2()) )
            {
                continue;
            }
            if ( user!= null && !user.equals( conflict.getReservation1().getOwner()) && !user.equals(conflict.getReservation1().getOwner()))
            {
                continue;
            }
            result.add(  conflict );
        }
        return result;
    }

    public DefaultTreeModel createModel(ClassificationFilter[] filter, User selectedUser) throws RaplaException {
        // Resources and Persons
        //   Add the resource types
        //     Add the resources
        //   Add the person types
        //     Add the persons
        // Eventtypes
        //   Add the event types
        // Conflicts (0)
        //   Add the conflicts
        // Users
        //   Add the users
        // Categories (the root category)

        // Add the periods

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("ROOT");
        TypeNode resourceRoot = new TypeNode(Allocatable.TYPE, CalendarSelectionModel.ALLOCATABLES_ROOT, getString("resources"));

        TypeNode eventRoot = new TypeNode(Reservation.TYPE,getString("reservations"));

        Map nodeMap = new HashMap();

        boolean resourcesFiltered = false;
        boolean eventsFiltered = false;

        DynamicType[] types = getQuery().getDynamicTypes( DynamicTypeAnnotations.VALUE_RESOURCE_CLASSIFICATION );
        for ( int i=0;i<types.length;i++) {
            DynamicType type= types[i];
            if ( hasRulesFor( filter, type ) ) {
                resourcesFiltered = true;
            }
            if ( !isInFilter( filter, type)) {
                resourcesFiltered = true;
                continue;
            }
                
            NamedNode node = new NamedNode( type);
            resourceRoot.add( node );
            nodeMap.put( type, node);
        }

        types = getQuery().getDynamicTypes( DynamicTypeAnnotations.VALUE_PERSON_CLASSIFICATION );
        for ( int i=0;i<types.length;i++) {
            DynamicType type= types[i];
            if ( hasRulesFor( filter, type ) ) {
                resourcesFiltered = true;
            }
            if ( !isInFilter( filter, type)) {
                resourcesFiltered = true;
                continue;
            }

            NamedNode node = new NamedNode( type);
            resourceRoot.add( node );
            nodeMap.put( type, node);
        }

        types = getQuery().getDynamicTypes( DynamicTypeAnnotations.VALUE_RESERVATION_CLASSIFICATION );
        for ( int i=0;i<types.length;i++) {
            DynamicType type= types[i];
            if ( hasRulesFor( filter, type ) ) {
                eventsFiltered = true;
            }
            if ( !isInFilter( filter, type)) {
                eventsFiltered = true;
                continue;
            }
            NamedNode node = new NamedNode( type);
            eventRoot.add( node );
        }

        eventRoot.setFiltered( eventsFiltered );
        resourceRoot.setFiltered( resourcesFiltered ) ;
        
        Comparator comp = new NamedComparator( getLocale());
        Set sortedClassifiable = new TreeSet( comp );
        sortedClassifiable.addAll( Arrays.asList( getQuery().getAllocatables() ));

        for(Iterator it = sortedClassifiable.iterator();it.hasNext();)
        {
            Classifiable classifiable = (Classifiable) it.next();
            if ( !isInFilter( filter, classifiable))
            {
                continue;
            }
            Classification classification = classifiable.getClassification();
            NamedNode childNode = new NamedNode((Named)classifiable);
            DynamicType type = classification.getType();
            Assert.notNull(type);
            DefaultMutableTreeNode typeNode = (DefaultMutableTreeNode) nodeMap.get(type);
            Assert.notNull(typeNode);
            typeNode.add(childNode);
        }

        root.add(resourceRoot);
        root.add(eventRoot);

        Collection conflicts = getConflicts( filter, selectedUser );

        if ( conflicts.size() > 0) {
            DefaultMutableTreeNode conflictRoot = new TypeNode(Conflict.TYPE, getI18n().format("conflictUC", new Integer(conflicts.size())) );
            for ( Iterator it = conflicts.iterator();it.hasNext();)
            {
                conflictRoot.add( new NamedNode( (Conflict)it.next()));
            }
            root.add( conflictRoot );
        }

        if ( isAdmin()) {
            CategoryNode categoryRoot = new CategoryNode( getI18n().getLocale(),null, getQuery().getSuperCategory());
            root.add( categoryRoot );

            DefaultMutableTreeNode userRoot = new TypeNode(User.TYPE, getString("users"));
            User[] userList = getQuery().getUsers();
            for ( int i=0;i<userList.length;i++) {
                NamedNode node = new NamedNode( userList[i]);
                userRoot.add( node );
            }
            root.add( userRoot);

            DefaultMutableTreeNode periodRoot = new TypeNode(Period.TYPE,getString("periods"));
            Period[] periodList = getQuery().getPeriods();
            for ( int i=0;i< periodList.length;i++) {
                NamedNode node = new NamedNode(periodList[i]);
                periodRoot.add( node );
            }
            root.add( periodRoot);
        }


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?