View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  /******************************************************************************
25  * SocietyModel.java
26  *
27  *****************************************************************************/
28  
29  package zeus.visualiser.society;
30  
31  import java.io.*;
32  import java.util.*;
33  import java.awt.*;
34  import javax.swing.event.*;
35  
36  import zeus.util.*;
37  import zeus.concepts.Relationship;
38  import zeus.gui.*;
39  import zeus.gui.graph.*;
40  import zeus.generator.agent.AcquaintanceModel;
41  
42  public class SocietyModel extends AbstractGraphModel {
43    static final int SUPERIORS    = 0;
44    static final int SUBORDINATES = 1;
45    static final int PEERS        = 2;
46    static final int COWORKERS    = 3;
47  
48    protected boolean           isNodeEditable = true;
49    protected Hashtable         nodeTable      = new Hashtable();
50    protected Properties        imageTable     = new Properties();
51    protected EventListenerList listeners      = new EventListenerList();
52    protected int               view           = 0;
53    protected BitSet            links          =
54       new BitSet(AcquaintanceModel.RELATIONS_LIST.size());
55  
56    public SocietyModel() {
57       try {
58          imageTable.load(
59             new FileInputStream(SystemProps.getProperty("application.gif"))
60          );
61       }
62       catch(Exception e) {
63       }
64       reset();
65    }
66  
67    public void reset() {
68       nodeTable.clear();
69       for(int i = 0; i < links.size(); i++ )
70          links.set(i);
71       fireGraphStructureChanged();
72    }
73  
74    public Enumeration nodes() {
75       return nodeTable.elements();
76    }
77  
78    public void setValue(GraphNode node, Object user_object) {
79       SocietyModelEntry agent = (SocietyModelEntry)node.getUserObject();
80       if ( user_object == null )
81          agent.icon = null;
82       else
83          agent.setIcon((String)user_object);
84       fireGraphNodeStateChanged(node);
85       fireChanged();
86    }
87  
88    public boolean isNodeEditable(GraphNode node) {
89       return isNodeEditable;
90    }
91  
92    public Color getLinkColor(GraphNode from, GraphNode to) {
93       int type;
94       if ( (type = getLinkType(from,to)) != -1 )
95          return getColor(type);
96       return Color.black;
97    }
98    public boolean isLinkVisible(GraphNode from, GraphNode to) {
99       int type;
100      if ( (type = getLinkType(from,to)) != -1 )
101         return links.get(type);
102      return false;
103   }
104 
105   public Vector getViewRelations(GraphNode node) {
106      Vector output = new Vector();
107      // first compute from me to others
108      SocietyModelEntry agent = (SocietyModelEntry)node.getUserObject();
109      String base = agent.getName();
110      Relationship[] others = agent.getRelations();
111      for(int j = 0; j < others.length; j++ ) {
112         if ( Misc.whichPosition(others[j].getRelation(),
113                 AcquaintanceModel.RELATIONS_LIST) == view )
114            output.addElement(nodeTable.get(others[j].getName()));
115      }
116 
117      // next compute from others to me
118      Enumeration enum = nodeTable.elements();
119      GraphNode node1;
120      while( enum.hasMoreElements() ) {
121         node1 = (GraphNode)enum.nextElement();
122         if ( node1 != node && !output.contains(node1) ) {
123            agent = (SocietyModelEntry)node1.getUserObject();
124            others = agent.getRelations();
125            for(int j = 0; j < others.length; j++ ) {
126               if ( others[j].getName().equals(base) &&
127 	           Misc.whichPosition(others[j].getRelation(),
128                       AcquaintanceModel.RELATIONS_LIST) == view )
129               output.addElement(node1);
130            }
131         }
132      }
133 
134      return output;
135   }
136 
137   protected int getLinkType(GraphNode node1, GraphNode node2) {
138      SocietyModelEntry agent1 = (SocietyModelEntry)node1.getUserObject();
139      SocietyModelEntry agent2 = (SocietyModelEntry)node2.getUserObject();
140      String agentId = agent2.getName();
141      Relationship[] others = agent1.getRelations();
142      for(int j = 0; j < others.length; j++ ) {
143         if ( others[j].getName().equals(agentId) ) {
144            return Misc.whichPosition(others[j].getRelation(),
145                                      AcquaintanceModel.RELATIONS_LIST);
146         }
147      }
148      return -1;
149   }
150 
151   public Color   getColor(int type)      { return ColorManager.getColor(type); }
152   public boolean isLinkVisible(int type) { return links.get(type); }
153   public void    setView(int type)       { this.view = type; }
154   public int     getView()               { return view; }
155 
156   public void showLinks(int type, boolean state) {
157      if ( state )
158         links.set(type);
159      else
160         links.clear(type);
161   }
162 
163   public void removeNodes(GraphNode[] input) {
164      SocietyModelEntry agent;
165      String agentId;
166      for(int i = 0; i < input.length; i++ ) {
167         agent = (SocietyModelEntry)input[i].getUserObject();
168         agentId = agent.getName();
169         nodeTable.remove(agentId);
170         fireGraphNodeRemoved(input[i]);
171      }
172      fireChanged();
173   }
174 
175     // 1.3 try all case variations to see if we can get a result. 
176    public GraphNode getNode(String name)  {
177      GraphNode result = (GraphNode)nodeTable.get(name);
178      if (result == null) {
179         result = (GraphNode) nodeTable.get(name.toLowerCase()); 
180      }
181      if (result == null) { 
182         result = (GraphNode) nodeTable.get(name.toUpperCase()); 
183      }
184      
185         return result;
186      }
187 
188 
189   public GraphNode addAgent(String name, String type)  {
190      SocietyModelEntry agent;
191      GraphNode node;
192 
193      String icon = imageTable.getProperty(name);
194      if ( icon == null ) {
195         icon = SystemProps.getProperty("gif.dir");
196         icon += File.separator + "visualiser" + File.separator +
197                 type.toLowerCase() + ".gif";
198      }
199      else {
200         char sys_char = File.separatorChar;
201         char zeus_char = SystemProps.getProperty("file.separator").charAt(0);
202         icon = icon.replace(zeus_char,sys_char);
203      }
204 
205      if ( nodeTable.containsKey(name) )  {
206         node = (GraphNode)nodeTable.get(name);
207         agent = (SocietyModelEntry)node.getUserObject();
208         if ( agent.getIcon().equals(icon) ) {
209            agent.setIcon(icon);
210            fireGraphNodeStateChanged(node);
211         }
212      }
213      else {
214         agent = new SocietyModelEntry(name,icon);
215         node = new GraphNode(agent);
216         nodeTable.put(name.toLowerCase(),node); // 1.3 si, tlc ()
217         fireGraphNodeAdded(node);
218      }
219      return node;
220   }
221 
222   public void addAgents(Vector input) {
223      String type = SystemProps.getProperty("agent.names.agent");
224      for(int i = 0; i < input.size(); i++ ) 
225         addAgent((String)input.elementAt(i),type);
226   }
227 
228   public GraphNode addAgent(String name) {
229      String type = SystemProps.getProperty("agent.names.agent");
230      return addAgent(name,type);
231   }
232 
233   public void addRelations(String name, Vector List) {
234      GraphNode node1 = (GraphNode)nodeTable.get(name);
235      if ( node1 == null ) node1 = addAgent(name);
236 
237      SocietyModelEntry agent = (SocietyModelEntry)node1.getUserObject();
238      agent.addRelations(List);
239 
240      GraphNode node2;
241      Relationship relation;
242      for(int j = 0; j < List.size(); j++ ) {
243         relation = (Relationship)List.elementAt(j);
244         node2 = (GraphNode)nodeTable.get(relation.getName());
245         if ( node2 != null ) {
246            int position = Misc.whichPosition(relation.getRelation(),
247               AcquaintanceModel.RELATIONS_LIST);
248            switch( position ) {
249               case SUPERIORS:
250                    node2.addChild(node1);
251                    node1.addParent(node2);
252                    fireGraphNodeStateChanged(node2);
253                    break;
254               case SUBORDINATES:
255                    node1.addChild(node2);
256                    node2.addParent(node1);
257                    fireGraphNodeStateChanged(node2);
258                    break;
259               default:
260                    node1.addSibling(node2);
261                    node2.addSibling(node1);
262                    fireGraphNodeStateChanged(node2);
263                    break;
264            }
265         }
266      }
267      fireGraphNodeStateChanged(node1);
268   }
269 
270   public void addChangeListener(ChangeListener x) {
271      listeners.add(ChangeListener.class, x);
272   }
273   public void removeChangeListener(ChangeListener x) {
274      listeners.remove(ChangeListener.class, x);
275   }
276 
277   protected void fireChanged() {
278      ChangeEvent c = new ChangeEvent(this);
279      Object[] list = listeners.getListenerList();
280      for(int i= list.length-2; i >= 0; i -=2) {
281         if (list[i] == ChangeListener.class) {
282            ChangeListener cl = (ChangeListener)list[i+1];
283            cl.stateChanged(c);
284         }
285      }
286   }
287 }