001    /**
002     * planningtool - A Planning Tool with Critiquing Support.
003     * 
004     * Copyright (C) 2006 olale
005    
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010    
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015    
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019    
020     * Contact information: 
021     * E-mail: olale@ida.liu.se
022     *         olale@lysator.liu.se
023     */
024    package se.liu.ida.critiquer.resources;
025    
026    import java.util.ArrayList;
027    import java.util.Vector;
028    
029    import javax.swing.event.TreeModelEvent;
030    import javax.swing.event.TreeModelListener;
031    import javax.swing.tree.TreeModel;
032    import javax.swing.tree.TreePath;
033    
034    import se.liu.ida.critiquer.mics.ReferenceHolder;
035    
036    /**
037     * 
038     * The organization model represents the available resources that may be used
039     * for this scenario
040     * 
041     * To change what resources are used, we must change the constructor so that
042     * another agent factory is used to produce our set of agents
043     * 
044     * TODO: Preferably this should be selectable when the planning tool starts, or
045     * at least configurable from the command line.
046     * 
047     * @author olale
048     * 
049     */
050    public class OrganizationModel implements TreeModel {
051    
052            private Agent                                                             rootAgent;
053    
054            private transient ArrayList<TreeModelListener> listeners = new ArrayList<TreeModelListener>();
055    
056            /**
057         * Create a default hierarchy
058         */
059    
060            public OrganizationModel() {
061                    setRootAgent(ReferenceHolder.agentFactory.getAgentStructure());
062                    ReferenceHolder.organizationModel = this;
063            }
064    
065            /**
066         * Get all concrete agents that are not a combination of several agents in a
067         * group
068         * 
069         * @return all agent which have no children 
070         */
071            public Vector<Agent> getAllAgents() {
072                    ArrayList<Agent> queue = new ArrayList<Agent>();
073                    queue.add(rootAgent);
074                    Vector<Agent> allAgents = new Vector<Agent>();
075                    while (!queue.isEmpty()) {
076                            Agent currentAgent = queue.remove(0);
077                            if (currentAgent.getChildren().isEmpty()) {
078                                    allAgents.add(currentAgent);
079                            } else {
080                                    queue.addAll(currentAgent.getChildren());
081                            }
082    
083                    }
084    
085                    return allAgents;
086    
087            }
088    
089            public Object getRoot() {
090                    return rootAgent;
091            }
092    
093            public void setRootAgent(Agent rootAgent) {
094                    this.rootAgent = rootAgent;
095            }
096    
097            public Object getChild(Object parent, int index) {
098                    return ((Agent) parent).getChildren().get(index);
099            }
100    
101            public int getChildCount(Object parent) {
102                    return ((Agent) parent).getChildren().size();
103            }
104    
105            public boolean isLeaf(Object node) {
106                    return ((Agent) node).getChildren().isEmpty();
107            }
108    
109            public void valueForPathChanged(TreePath path, Object newValue) {
110                    for (TreeModelListener listener : listeners) {
111                            listener.treeNodesChanged(new TreeModelEvent(newValue, path));
112                    }
113            }
114    
115            public int getIndexOfChild(Object parent, Object child) {
116                    return ((Agent) parent).getChildren().indexOf(child);
117            }
118    
119            public void addTreeModelListener(TreeModelListener l) {
120                    listeners.add(l);
121            }
122    
123            public void removeTreeModelListener(TreeModelListener l) {
124                    listeners.remove(l);
125            }
126    
127    }