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  * HashtableModel.java
26  *
27  * The underlying model for the Task Table
28  *****************************************************************************/
29  
30  package zeus.generator.util;
31  
32  import java.util.*;
33  import javax.swing.table.*;
34  import javax.swing.event.*;
35  
36  import zeus.util.*;
37  
38  
39  public class HashtableModel extends AbstractTableModel {
40    protected static int keyCounter = 0;
41    protected static int valueCounter = 0;
42  
43    static final int KEY   = 0;
44    static final int VALUE = 1;
45  
46    protected static final String[] columnNames = {
47       "Key", "Value"
48    };
49  
50    protected Vector keys = new Vector();
51    protected Vector values = new Vector();
52    protected boolean changed = false;
53  
54    public HashtableModel() {
55    }
56  
57    public void addNewRow() {
58       String item = "key" + (keyCounter++);
59       while( keys.contains(item) )
60          item = "key" + (keyCounter++);
61       keys.addElement(item);
62  
63       item = "value" + (valueCounter++);
64       while( values.contains(item) )
65          item = "value" + (valueCounter++);
66       values.addElement(item);
67       fireTableRowsInserted(keys.size()-1,keys.size()-1);
68       changed = true;
69    }
70    public void removeRows(int[] rows) {
71       for(int i = 0; i < rows.length; i++ ) {
72          keys.removeElementAt(rows[i]-i);
73          values.removeElementAt(rows[i]-i);
74          fireTableRowsDeleted(rows[i]-i,rows[i]-i);
75       }
76       changed = true;
77    }
78  
79    public void reset(Hashtable input)  {
80      keys.removeAllElements();
81      values.removeAllElements();
82      Enumeration enum = input.keys();
83      Object key, value;
84      while( enum.hasMoreElements() ) {
85         key = enum.nextElement();
86         value = input.get(key);
87         keys.addElement(key);
88         values.addElement(value);
89      }
90      fireTableDataChanged();
91      changed = false;
92    }
93  
94    public Hashtable getData() {
95       Hashtable output = new Hashtable();
96       for(int i = 0; i < keys.size(); i++ )
97          output.put(keys.elementAt(i),values.elementAt(i));
98       return output;
99    }
100 
101   public boolean hasChanged() { return changed; }
102 
103   public int     getColumnCount()             { return columnNames.length; }
104   public int     getRowCount()                { return keys.size(); }
105   public String  getColumnName(int col)       { return columnNames[col]; }
106   public boolean isCellEditable(int r, int c) { return true; }
107 
108   public Object getValueAt(int row, int column) {
109      switch(column) {
110         case KEY:
111              return keys.elementAt(row);
112 
113 	case VALUE:
114              return values.elementAt(row);
115      }
116      return null;
117   }
118 
119   public void setValueAt(Object aValue, int row, int column) {
120      switch(column) {
121         case KEY:
122              if ( aValue.equals(keys.elementAt(row)) ) return;
123              keys.setElementAt(aValue,row);
124              break;
125 
126 	case VALUE:
127              if ( aValue.equals(values.elementAt(row)) ) return;
128              values.setElementAt(aValue,row);
129              break;
130      }
131      changed = true;
132   }
133 }