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  * TaskLinkModel.java
26  *
27  * The underlying model for the Applicability TaskLink Table
28  *****************************************************************************/
29  
30  package zeus.generator.task;
31  
32  import java.util.*;
33  import javax.swing.*;
34  import javax.swing.table.*;
35  import javax.swing.event.*;
36  
37  import zeus.util.*;
38  import zeus.concepts.*;
39  import zeus.concepts.fn.*;
40  import zeus.generator.*;
41  import zeus.generator.event.*;
42  import zeus.generator.util.*;
43  import zeus.gui.editors.*;
44  
45  public class TaskLinkModel extends AbstractTableModel
46                                implements ChangeListener,
47                                           RenameListener,
48                                           LinkNodeSelectionListener {
49  
50    static final int LEFT = 0;
51    static final int RIGHT = 1;
52  
53    static final String SEPARATOR = "::";
54  
55    protected static final String[] columnNames = {
56       "From/Postconditions", "To/Preconditions"
57    };
58  
59    protected GroupManager    leftGroupManager;
60    protected GroupManager    rightGroupManager;
61  
62    protected EventListenerList changeListeners = new EventListenerList();
63    protected Vector data     = new Vector();
64    protected String nodeName = null;
65    protected String leftNode = null, leftGroup = null,
66                     leftType = null, leftArg = null;
67    protected String rightNode = null, rightGroup = null,
68                     rightType = null, rightArg = null;
69  
70    public TaskLinkModel(GroupManager leftGroupManager,
71                         GroupManager rightGroupManager) {
72  
73       this.leftGroupManager = leftGroupManager;
74       this.rightGroupManager = rightGroupManager;
75       leftGroupManager.addChangeListener(this);
76       rightGroupManager.addChangeListener(this);
77       leftGroupManager.addRenameListener(this);
78       rightGroupManager.addRenameListener(this);
79    }
80  
81    public void reset(String nodeName, TaskLink[] input) {
82       this.nodeName = nodeName;
83       leftNode = rightNode = leftGroup = rightGroup =
84          leftType = rightType = leftArg = rightArg = null;
85  
86       int r = data.size();
87       data.removeAllElements();
88       if ( r != 0 ) fireTableRowsDeleted(0,r-1);
89  
90       for(int i = 0; i < input.length; i++ ) {
91          if ( input[i].referencesNode(nodeName) )
92             data.addElement(new TaskLink(input[i]));
93       }
94       fireTableRowsInserted(0,data.size()-1);
95       fireTableStructureChanged(); // swing bug? force redraw of table
96    }
97  
98    public TaskLink[] getData() {
99       TaskLink[] output = new TaskLink[data.size()];
100      for(int i = 0; i < data.size(); i++ )
101         output[i] = (TaskLink)data.elementAt(i);
102      return output;
103   }
104 
105   public void removeRows(int[] rows) {
106      for(int i = 0; i < rows.length; i++ ) {
107         data.removeElementAt(rows[i]-i);
108         fireTableRowsDeleted(rows[i]-i,rows[i]-i);
109      }
110      fireTableStructureChanged(); // swing bug? force redraw of table
111      fireChanged();
112   }
113 
114   public void addNewRow()  {
115      TaskLink link = getAddition();
116      if ( link != null ) {
117         data.addElement(link);
118         int size = data.size();
119         fireTableRowsInserted(size-1,size-1);
120         fireTableStructureChanged(); // swing bug? force redraw of table
121         fireChanged();
122      }
123   }
124 
125   protected TaskLink getAddition() {
126      if ( leftNode == null || leftArg == null || leftGroup == null ||
127           leftType == null || rightNode == null || rightArg == null ||
128           rightGroup == null || rightType == null ) {
129         JOptionPane.showMessageDialog(null,
130            "Attempting to add improperly defined link","Error",
131             JOptionPane.ERROR_MESSAGE);
132         return null;
133      }
134      else if ( !rightType.equals(leftType) ) {
135         JOptionPane.showMessageDialog(null,
136            "Attempting to add improperly defined link\n" +
137            "Left and right fact types must be the same","Error",
138             JOptionPane.ERROR_MESSAGE);
139         return null;
140      }
141      return new TaskLink(leftNode,leftGroup,leftArg,
142                          rightNode,rightGroup,rightArg);
143   }
144 
145   protected Vector getConditions() {
146      Vector items = new Vector();
147      Vector List;
148      Fact f1;
149 
150      Hashtable input = leftGroupManager.getManagerData();
151      Enumeration enum = input.elements();
152      while( enum.hasMoreElements() ) {
153         List = (Vector)enum.nextElement();
154         for(int i = 0; i < List.size(); i++ ) {
155            f1 = (Fact)List.elementAt(i);
156            if ( !items.contains(f1.getId()) )
157               items.addElement(f1.getId());
158         }
159      }
160 
161      input = rightGroupManager.getManagerData();
162      enum = input.elements();
163      while( enum.hasMoreElements() ) {
164         List = (Vector)enum.nextElement();
165         for(int i = 0; i < List.size(); i++ ) {
166            f1 = (Fact)List.elementAt(i);
167            if ( !items.contains(f1.getId()) )
168               items.addElement(f1.getId());
169         }
170      }
171 
172      return items;
173   }
174   // ----------------------------------------------------------------------
175 
176   public int     getColumnCount()                 { return columnNames.length;}
177   public boolean isCellEditable(int row, int col) { return false; }
178   public int     getRowCount()                    { return data.size(); }
179   public String  getColumnName(int col)           { return columnNames[col]; }
180 
181   public Object getValueAt (int row, int column)  {
182      TaskLink link = (TaskLink)data.elementAt(row);
183      switch(column) {
184         case LEFT:
185              return link.getLeftNode() + SEPARATOR + link.getLeftGroup() +
186 	            SEPARATOR + link.getLeftArg();
187         case RIGHT:
188              return link.getRightNode() + SEPARATOR + link.getRightGroup() +
189 	            SEPARATOR + link.getRightArg();
190      }
191      return null;
192   }
193 
194   public void setValueAt(Object aValue, int row, int column)  {
195      Core.ERROR(null,1,this);
196   }
197 
198   public void linkNodeSelected(LinkNodeSelectionEvent e) {
199     if ( e.getType().equals(TaskLinkBaseTreeModel.EFFECTS) ) {
200        leftNode = e.getNodeName();
201        leftGroup = e.getGroupName();
202        leftArg = e.getFactId();
203        leftType = e.getFactType();
204        if ( rightNode != null && leftNode.equals(rightNode) )
205           rightNode = rightGroup = rightArg = rightType = null;
206     }
207     else if ( e.getType().equals(TaskLinkBaseTreeModel.PRECONDITIONS) ) {
208        rightNode = e.getNodeName();
209        rightGroup = e.getGroupName();
210        rightArg = e.getFactId();
211        rightType = e.getFactType();
212        if ( leftNode != null && rightNode.equals(leftNode) )
213           leftNode = leftGroup = leftArg = leftType = null;
214     }
215   }
216 
217   public void stateChanged(ChangeEvent e) {
218      // Preconditions/Postconditions have changed!
219      // NEED to verify all links
220      if ( e.getSource() == leftGroupManager ||
221           e.getSource() == rightGroupManager ) {
222         Vector items = getConditions();
223         TaskLink link;
224         for(int i = 0; i < data.size(); i++ ) {
225            link = (TaskLink)data.elementAt(i);
226            if ( link.getLeftNode().equals(nodeName) ) {
227               if ( !items.contains(link.getLeftArg()) )
228                  data.removeElementAt(i--);
229            }
230            else if ( link.getRightNode().equals(nodeName) ) {
231              if ( !items.contains(link.getRightArg()) )
232                  data.removeElementAt(i--);
233            }
234         }
235         fireTableDataChanged();
236      }
237   }
238   public void nameChanged(RenameEvent e) {
239      // Preconditions/Postconditions ids have changed!
240      // NEED to update all ids in link database
241 
242      TaskLink link;
243      String prev = (String)e.getOriginal();
244      String curr = (String)e.getCurrent();
245 
246      if ( prev.equals(curr) ) return;
247      for(int i = 0; i < data.size(); i++ ) {
248         link = (TaskLink)data.elementAt(i);
249         if ( link.getLeftNode().equals(prev) )
250            link.setLeftNode(curr);
251         else if ( link.getLeftGroup().equals(prev) )
252            link.setLeftGroup(curr);
253         else if ( link.getLeftArg().equals(prev) )
254            link.setLeftArg(curr);
255 
256         else if ( link.getRightNode().equals(prev) )
257            link.setRightNode(curr);
258         else if ( link.getRightGroup().equals(prev) )
259            link.setRightGroup(curr);
260         else if ( link.getRightArg().equals(prev) )
261            link.setRightArg(curr);
262      }
263      if ( nodeName == null || nodeName.equals(prev) )     nodeName = curr;
264      if ( leftNode != null && leftNode.equals(prev) )     leftNode = curr;
265      if ( rightNode != null && rightNode.equals(prev) )   rightNode = curr;
266      if ( leftGroup != null && leftGroup.equals(prev) )   leftGroup = curr;
267      if ( rightGroup != null && rightGroup.equals(prev) ) rightGroup = curr;
268      if ( leftArg != null && leftArg.equals(prev) )       leftArg = curr;
269      if ( rightArg != null && rightArg.equals(prev) )     rightArg = curr;
270      // Note: Fact types cannot be renamed
271      fireTableDataChanged();
272   }
273 
274   public void addChangeListener(ChangeListener x) {
275      changeListeners.add(ChangeListener.class, x);
276   }
277   public void removeChangeListener(ChangeListener x) {
278      changeListeners.remove(ChangeListener.class, x);
279   }
280 
281   protected void fireChanged() {
282      ChangeEvent c = new ChangeEvent(this);
283      Object[] listeners = changeListeners.getListenerList();
284      for(int i= listeners.length-2; i >= 0; i -=2) {
285         if (listeners[i] == ChangeListener.class) {
286            ChangeListener cl = (ChangeListener)listeners[i+1];
287            cl.stateChanged(c);
288         }
289      }
290   }
291 }