1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /******************************************************************************
25 * AttributeModel.java
26 *
27 * The underlying model for the Attribute 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 import zeus.concepts.*;
38 import zeus.concepts.fn.*;
39 import zeus.gui.editors.*;
40 import zeus.generator.event.*;
41
42 public class AttributeModel extends AbstractTableModel
43 implements ChangeListener,
44 ValidatingModel,
45 RenameListener {
46
47 public static final int ATTRIBUTE = 0;
48 public static final int VALUE = 1;
49 public static final int RESTRICTION = 2;
50
51 protected EventListenerList changeListeners = new EventListenerList();
52 protected String[] columnNames = { "Attribute", "Value",
53 "Restriction" };
54 protected String[][] data;
55 protected Fact fact;
56
57 protected int columns;
58 protected boolean restrictions;
59 protected boolean[] permissions;
60
61 public AttributeModel() {
62 columns = 2;
63 data = new String[0][columns];
64 permissions = new boolean[] {false, true, true};
65 }
66
67 public void reset(Fact fact) {
68 this.fact = fact;
69 if ( fact == null )
70 data = new String[0][columns];
71 else {
72 String[] attributes = fact.listAttributes();
73 ValueFunction[] values = fact.listValues();
74 data = new String[values.length][columns];
75 for(int i = 0; i < data.length; i++ ) {
76 data[i][ATTRIBUTE] = attributes[i];
77
78
79
80
81
82 data[i][VALUE] = values[i].toString();
83 }
84 }
85 fireTableDataChanged();
86 }
87
88 public Fact getData() { return fact; }
89 public int getColumnCount() { return columnNames.length; }
90 public int getRowCount() { return data.length; }
91
92 public boolean isCellEditable(int row, int col) {
93 return permissions[col];
94 }
95
96 public String getColumnName(int col) { return columnNames[col]; }
97 public Object getValueAt(int row, int col) { return data[row][col]; }
98
99 public boolean isValidEntry(int row, int column) {
100 switch(column) {
101 case VALUE:
102 if ( data[row][column] == null )
103 return true;
104 else if ( data[row][column].equals("") )
105 return true;
106 else
107 return ZeusParser.Expression(data[row][column]) != null;
108 case ATTRIBUTE:
109 return true;
110 case RESTRICTION:
111 return true;
112 }
113 return false;
114 }
115
116 public void setRestriction(String value, int row) {
117
118 if(value != null) {
119 data[row][RESTRICTION] = value;
120 }
121 }
122
123 public void setValueAt(Object aValue, int row, int column) {
124
125 if(column == RESTRICTION) {
126 if(aValue != null) {
127 data[row][RESTRICTION] = aValue.toString();
128 fireChanged();
129 fireTableCellUpdated(row,column);
130 }
131 return;
132 }
133
134 Core.ERROR(column == VALUE, 1, this);
135
136 String value = (aValue == null) ? null : ((String)aValue).trim();
137 if ( value.equals("") ) value = null;
138
139 if ( value == null ) {
140 if ( data[row][VALUE] == null )
141 return;
142 else {
143 data[row][VALUE] = value;
144 fact.setValue(data[row][ATTRIBUTE],fact.newVar());
145 }
146 }
147 else {
148
149 value = Misc.substitute(value,fact.getId(),Fact.THIS);
150 if ( data[row][VALUE] != null && data[row][VALUE].equals(value) )
151 return;
152 else {
153 data[row][VALUE] = value;
154 ValueFunction fn = ZeusParser.Expression(data[row][VALUE]);
155 if ( fn != null )
156 fact.setValue(data[row][ATTRIBUTE],fn);
157 }
158 }
159 fireChanged();
160 fireTableCellUpdated(row,column);
161 }
162
163 public void nameChanged(RenameEvent e) {
164 String prev = (String)e.getOriginal();
165 String curr = (String)e.getCurrent();
166 String s;
167 for(int i = 0; i < data.length; i++ ) {
168 if ( data[i][VALUE] != null ) {
169 s = Misc.substitute(data[i][VALUE],prev,curr);
170 if ( !s.equals(data[i][VALUE]) )
171 setValueAt(s,i,VALUE);
172 }
173 }
174 }
175 public void stateChanged(ChangeEvent e) {
176 reset(fact);
177 }
178
179 public void addChangeListener(ChangeListener x) {
180 changeListeners.add(ChangeListener.class, x);
181 }
182 public void removeChangeListener(ChangeListener x) {
183 changeListeners.remove(ChangeListener.class, x);
184 }
185
186 protected void fireChanged() {
187 ChangeEvent c = new ChangeEvent(this);
188 Object[] listeners = changeListeners.getListenerList();
189 for(int i= listeners.length-2; i >= 0; i -=2) {
190 if (listeners[i] == ChangeListener.class) {
191 ChangeListener cl = (ChangeListener)listeners[i+1];
192 cl.stateChanged(c);
193 }
194 }
195 }
196
197 public void enableRestrictions() {
198 if(restrictions == false) {
199 columns++;
200 restrictions = true;
201 reset(fact);
202 }
203 }
204
205 public void disableRestrictions() {
206 if(restrictions == true) {
207 columns--;
208 restrictions = false;
209 reset(fact);
210 }
211 }
212
213 public void setReadOnly(int column) {
214 permissions[column] = false;
215 }
216
217 public void setWriteable(int column) {
218 permissions[column] = true;
219 }
220
221 }