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  * TaskNodePanel.java
26  *
27  * Panel through which task attributes are entered
28  ***************************************************************************/
29  
30  package zeus.generator.task;
31  
32  import java.awt.*;
33  import java.awt.event.*;
34  import java.util.*;
35  import javax.swing.*;
36  import javax.swing.event.*;
37  
38  import zeus.util.*;
39  import zeus.concepts.*;
40  import zeus.concepts.fn.*;
41  import zeus.generator.*;
42  import zeus.generator.event.*;
43  import zeus.generator.util.*;
44  import zeus.gui.fields.*;
45  import zeus.gui.help.*;
46  
47  public class GroupManager extends FactModel
48                            implements ActionListener, FactModelListener {
49  
50       protected static int           group_count = 0;
51       protected DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
52       protected String               selectedGroup;
53       protected Hashtable            table = new Hashtable();
54       protected OrderedHashtable     backup = new OrderedHashtable();
55  
56       public GroupManager(OntologyDb ontologyDb, AttributeModel attributeModel,
57                            boolean isVariable, int type, Fact[] input) {
58          super(ontologyDb,attributeModel,isVariable,type,input);
59       }
60  
61       public DefaultComboBoxModel getComboBoxModel() { return comboModel; }
62  
63       public void resetManager(Hashtable input, Fact[] backup_input) {
64          resetManager(input);
65          for(int i = 0; i < backup_input.length; i++ )
66             backup.put(backup_input[i].getId(),backup_input[i]);
67       }
68       public void resetManager(Hashtable input) {
69          backup.clear();
70          selectedGroup = null;
71          this.table = input;
72          if ( comboModel.getSize() > 0 ) comboModel.removeAllElements();
73          String group;
74          Enumeration enum = table.keys();
75          while( enum.hasMoreElements() ) {
76             group = (String)enum.nextElement();
77             comboModel.addElement(group);
78             if ( selectedGroup == null )
79                selectedGroup = group;
80          }
81          if ( selectedGroup != null ) {
82             comboModel.setSelectedItem(selectedGroup);
83          }
84       }
85  
86       public Hashtable getManagerData() {
87          // first do deselect operation to save current state
88          if ( selectedGroup != null ) {
89             Fact[] input = super.getData();
90             table.put(selectedGroup,factVector(input));
91          }
92          return table;
93       }
94  
95       public void actionPerformed(ActionEvent e) {
96          String item = (String)comboModel.getSelectedItem();
97          Fact[] input;
98          Vector List;
99          // first do deselect operation
100         if ( selectedGroup != null ) {
101            input = super.getData();
102            table.put(selectedGroup,factVector(input));
103         }
104         // then do select op
105         List = (Vector)table.get(item);
106         if ( List == null && selectedGroup != null ) {
107            // assume we are renaming
108            List = (Vector)table.get(selectedGroup);
109            table.remove(selectedGroup);
110            table.put(item,List);
111            String prevSelection = selectedGroup;
112            selectedGroup = null;
113            comboModel.removeElement(prevSelection);
114            comboModel.addElement(item);
115            comboModel.setSelectedItem(item);
116            fireNameChanged(comboModel,prevSelection,item);
117         }
118         else if ( List != null )
119            super.reset(factArray(List));
120         selectedGroup = item;
121      }
122 
123      public void factModelChanged(FactModelEvent e) {
124         Fact f1 = e.getFact();
125         Fact f2;
126         Fact[] input;
127         Vector List;
128         String value, value1, prev, curr;
129         String[] attribute;
130         Enumeration enum;
131 
132         switch( e.getEventType() ) {
133            case FactModelEvent.FACT_ADDED:
134                 enum = table.elements();
135                 while( enum.hasMoreElements() ) {
136                    List = (Vector)enum.nextElement();
137                    List.addElement(new Fact(f1));
138                 }
139                 backup.put(f1.getId(),f1);
140                 input = new Fact[1];
141                 input[0] = new Fact(f1);
142                 super.addRows(input);
143                 break;
144 
145            case FactModelEvent.FACT_REMOVED:
146                 enum = table.elements();
147                 while( enum.hasMoreElements() ) {
148                    List = (Vector)enum.nextElement();
149                    for(int i = 0; i < List.size(); i++ ) {
150                       f2 = (Fact)List.elementAt(i);
151                       if ( f1.getId().equals(f2.getId()) )
152                          List.removeElementAt(i--);
153                    }
154                 }
155                 backup.remove(f1.getId());
156                 input = new Fact[1];
157                 input[0] = f1;
158                 super.removeRows(input);
159                 break;
160 
161            case FactModelEvent.FACT_ID_CHANGED:
162                 prev = e.getPreviousId();
163                 curr = e.getCurrentId();
164 
165                 enum = table.elements();
166                 while( enum.hasMoreElements() ) {
167                    List = (Vector)enum.nextElement();
168                    for(int i = 0; i < List.size(); i++ ) {
169                       f2 = (Fact)List.elementAt(i);
170                       if ( prev.equals(f2.getId()) )
171                          f2.setId(f1.ID());
172                       attribute = f2.listAttributes();
173                       for(int j = 0; j < attribute.length; j++ ) {
174                          value = f2.getValue(attribute[j]);
175                          value1 = Misc.substitute(value,prev,curr);
176                          if ( !value1.equals(value) )
177                             f2.setValue(attribute[j],value1);
178                       }
179                    }
180                 }
181 
182                 input = super.getData();
183                 for(int i = 0; i < input.length; i++ )  {
184                    if ( input[i].getId().equals(prev) )
185                       input[i].setId(f1.ID());
186                       attribute = input[i].listAttributes();
187                       for(int j = 0; j < attribute.length; j++ ) {
188                          value = input[i].getValue(attribute[j]);
189                          value1 = Misc.substitute(value,prev,curr);
190                          if ( !value1.equals(value) )
191                             input[i].setValue(attribute[j],value1);
192                       }
193 
194                 }
195                 backup.reKey(prev,curr,f1);
196                 super.reset(input);
197                 fireNameChanged(f1,prev,curr);
198                 break;
199         }
200      }
201 
202      protected Fact[] factArray(Vector input) {
203         Fact[] output = new Fact[input.size()];
204         for(int i = 0; i < output.length; i++ )
205            output[i] = (Fact)input.elementAt(i);
206         return output;
207      }
208 
209      protected Vector factVector(Fact[] input) {
210         Vector output = new Vector();
211         for(int i = 0; i < input.length; i++ )
212            output.addElement(input[i]);
213         return output;
214      }
215 
216      public void newGroup() {
217         String name = "group" + (group_count++);
218         while( table.containsKey(name) )
219            name = "group" + (group_count++);
220         Vector List = new Vector();
221         Enumeration enum = backup.elements();
222         while( enum.hasMoreElements() )
223            List.addElement(new Fact((Fact)enum.nextElement()));
224         table.put(name,List);
225         comboModel.addElement(name);
226         comboModel.setSelectedItem(name);
227         fireChanged();
228      }
229 
230      public void deleteGroup() {
231         if ( selectedGroup == null ) return;
232         if ( comboModel.getSize() <= 1 ) {
233            JOptionPane.showMessageDialog(null,
234               "Cannot delete group.\nAt least one group must exist.",
235               "Error", JOptionPane.ERROR_MESSAGE);
236            return;
237         }
238 
239         String prevSelection = selectedGroup;
240         selectedGroup = null;
241         comboModel.removeElement(prevSelection);
242         table.remove(prevSelection);
243         comboModel.setSelectedItem(comboModel.getElementAt(0));
244         fireChanged();
245      }
246 
247      public void copyGroup() {
248         if ( selectedGroup == null ) return;
249         Fact[] input = super.getData();
250         Vector ListCopy = new Vector();
251         for(int i = 0; i < input.length; i++ )
252            ListCopy.addElement(new Fact(input[i]));
253 
254 	String name = "group" + (group_count++);
255         while( table.containsKey(name) )
256            name = "group" + (group_count++);
257         table.put(name,ListCopy);
258 
259         comboModel.addElement(name);
260         comboModel.setSelectedItem(name);
261         fireChanged();
262      }
263 }