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  * TaskModel.java
26  *
27  * The underlying model for the Task Table 
28  *****************************************************************************/
29  
30  package zeus.generator.agent;
31  
32  import java.util.*;
33  import java.awt.event.*;
34  import javax.swing.JOptionPane;
35  import javax.swing.table.*;
36  import javax.swing.event.*;
37  
38  import zeus.util.*;
39  import zeus.generator.*;
40  
41  public class TaskModel extends AbstractTableModel
42                         implements ChangeListener {
43  
44    static final int TASK = 0;
45    static final int TYPE = 1;
46    static final int ID   = 2;
47  
48    protected String[]          columnNames = { "Task", "Type" };
49    protected Vector            data;
50    protected GeneratorModel    genmodel;
51    protected EventListenerList changeListeners = new EventListenerList();
52  
53    protected boolean[]         permissions;
54  
55    public TaskModel(String[] taskId, GeneratorModel genmodel) {
56  
57      permissions = new boolean[] { true, false, false };
58      this.genmodel = genmodel;
59      genmodel.addChangeListener(this);
60  
61      data = new Vector();
62      String[] element;
63      for(int i = 0; i < taskId.length; i++ ) {
64         element = new String[3];
65         element[ID] = taskId[i];
66         element[TASK] = genmodel.getTaskName(taskId[i]);
67         element[TYPE] = genmodel.getTaskType(taskId[i]);
68         data.addElement(element);
69      }
70      genmodel.addChangeListener(this);
71    }
72  
73    public int     getColumnCount()                 { return columnNames.length; }
74    public int     getRowCount()                    { return data.size(); }
75  
76    public boolean isCellEditable(int row, int col) {
77      return permissions[col];
78    }
79  
80    public String  getColumnName(int col)           { return columnNames[col]; }
81  
82    public Object  getValueAt(int row, int col) {
83       return ((String[])data.elementAt(row))[col];
84    }
85  
86    public String[] getData() {
87       String[] output = new String[data.size()];
88       String[] array;
89       for(int i = 0; i < output.length; i++ ) {
90          array = (String[])data.elementAt(i);
91          output[i] = array[ID];
92       }
93       return output;
94    }
95  
96    public void setValueAt(Object aValue, int row, int column) {
97      // prevents the table being accidently loaded with a null value
98      // current table implementation needs this - possibly because of a bug
99      if (aValue == null || aValue.toString().equals(""))
100        return;
101 
102     String[] element = (String[])data.elementAt(row);
103     switch(column) {
104        case TASK:
105             String task = (String)aValue;
106             if ( element[TASK].equals(task) ) return;
107             if ( contains(task) ) return;
108             String newId = genmodel.reverseTaskNameLookup(task);
109             if ( newId != null ) {
110                element[ID] = newId;
111                element[TASK] = task;
112             }
113             else {
114                genmodel.renameTask(element[ID],task);
115                element[TASK] = task;
116             }
117 	    fireTableCellUpdated(row,column);
118             fireChanged();
119             break;
120     }
121   }
122 
123   public void addNewRow(String type) {
124     String taskId = genmodel.createNewTaskId();
125     genmodel.createNewTask(taskId,type);
126     String[] element = new String[3];
127     element[ID] = taskId;
128     element[TASK] = genmodel.getTaskName(taskId);
129     element[TYPE] = genmodel.getTaskType(taskId);
130     data.addElement(element);
131     int size = data.size();
132     fireTableRowsInserted(size-2,size-1);
133     fireChanged();
134   }
135 
136   public void addNewRow(String type, String task) {
137     if ( contains(task) ) return;
138 
139     String taskId = genmodel.reverseTaskNameLookup(task);
140     String[] element = new String[3];
141     element[ID] = taskId;
142     element[TASK] = task;
143     element[TYPE] = type;
144     data.addElement(element);
145     int size = data.size();
146     fireTableRowsInserted(size-2,size-1);
147     fireChanged();
148   }
149 
150   public void removeRow(int row) {
151      data.removeElementAt(row);
152      fireTableRowsDeleted(row,row);
153      fireChanged();
154   }
155 
156   protected boolean contains(String task) {
157     // check that model does not already contain task
158     String[] element;
159     for(int i = 0; i < data.size(); i++ ) {
160        element = (String[])data.elementAt(i);
161        if ( task.equals(element[TASK]) ) {
162           JOptionPane.showMessageDialog(null,
163              "Attempting to add an already\nexisting task",
164              "Error", JOptionPane.ERROR_MESSAGE);
165           return true;
166        }
167     }
168     return false;
169   }
170 
171   public void stateChanged(ChangeEvent e) {
172     for(int i = 0; i < data.size(); i++ ) {
173        String[] element = (String[])data.elementAt(i);
174        if ( !genmodel.containsTask(element[ID]) ) {
175           data.removeElementAt(i);
176           fireTableRowsDeleted(i,i);
177           i--;
178        }
179        else {
180           element[TASK] = genmodel.getTaskName(element[ID]);
181           element[TYPE] = genmodel.getTaskType(element[ID]);
182        }
183     }
184     fireTableDataChanged();
185   }
186 
187   public void setWriteable(int column) {
188     permissions[column] = true;
189   }
190 
191   public void setReadOnly(int column) {
192     permissions[column] = false;
193   }
194 
195   //------------------------------------------------------------------------
196   // Event Methods
197   //------------------------------------------------------------------------
198 
199   public void addChangeListener(ChangeListener x) {
200     changeListeners.add(ChangeListener.class, x);
201   }
202 
203   public void removeChangeListener(ChangeListener x) {
204     changeListeners.remove(ChangeListener.class, x);
205   }
206 
207   protected void fireChanged() {
208      ChangeEvent c = new ChangeEvent(this);
209      Object[] listeners = changeListeners.getListenerList();
210      for(int i= listeners.length-2; i >= 0; i -=2) {
211         if (listeners[i] == ChangeListener.class) {
212            ChangeListener cl = (ChangeListener)listeners[i+1];
213            cl.stateChanged(c);
214         }
215      }
216   }
217 }