KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > gui > PropertyTableModel


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/gui/PropertyTableModel.java#7 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2002-2007 Julian Hyde and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.gui;
11
12 import java.lang.reflect.Field JavaDoc;
13 import java.util.*;
14
15 /**
16  *
17  * @author sean
18  * @version $Id: //open/mondrian/src/main/mondrian/gui/PropertyTableModel.java#7 $
19  */

20 public class PropertyTableModel extends javax.swing.table.AbstractTableModel JavaDoc {
21     private Object JavaDoc parentTarget; // parent of target
22
private String JavaDoc factTable; // selected fact table
23
private String JavaDoc factTableSchema; // selected fact table schema
24
private ArrayList names; // List of names for this object's siblings already existing in parent'
25
private String JavaDoc errorMsg = null; // error msg when property value could not be set.
26

27     String JavaDoc[] propertyNames;
28     Object JavaDoc target;
29
30     public PropertyTableModel(Object JavaDoc t, String JavaDoc[] pNames) {
31         super();
32
33         propertyNames = pNames;
34         target = t;
35     }
36
37     public String JavaDoc getColumnName(int i) {
38         if (i == 0) {
39             // === return "Property";
40
return "Attribute";
41         } else if (i==1) {
42             return "Value";
43         }
44
45         return "?";
46     }
47
48 /**
49    * Copyright (C) 2006, 2007 CINCOM SYSTEMS, INC.
50    * All Rights Reserved
51    */

52     // get property name for given row no.
53
public String JavaDoc getRowName(int i) {
54         String JavaDoc pName = propertyNames[i];
55         int j=-1;
56         if ((j=pName.indexOf('|')) != -1) { //"|"
57
return pName.substring(0,j).trim();
58         } else {
59             return propertyNames[i]; }
60     }
61
62     public boolean isCellEditable(int row, int col) {
63         if (col == 1) {
64             Object JavaDoc cellObj = getValueAt(row, col);
65             if (cellObj instanceof MondrianGuiDef.Join) {
66                 return false;
67             } else {
68                 return true;
69             }
70         }
71
72         return false;
73     }
74
75     /** Returns the number of columns in the model. A
76      * <code>JTable</code> uses this method to determine how many columns it
77      * should create and display by default.
78      *
79      * @return the number of columns in the model
80      * @see #getRowCount
81      *
82      */

83     public int getColumnCount() {
84         return 2; //that's 'Property' and 'Value'
85
}
86
87     /** Returns the number of rows in the model. A
88      * <code>JTable</code> uses this method to determine how many rows it
89      * should display. This method should be quick, as it
90      * is called frequently during rendering.
91      *
92      * @return the number of rows in the model
93      * @see #getColumnCount
94      *
95      */

96     public int getRowCount() {
97         return propertyNames.length;
98     }
99
100 /**
101    * Copyright (C) 2006, 2007 CINCOM SYSTEMS, INC.
102    * All Rights Reserved
103    */

104     /** Returns the value for the cell at <code>columnIndex</code> and
105      * <code>rowIndex</code>.
106      *
107      * @param rowIndex the row whose value is to be queried
108      * @param columnIndex the column whose value is to be queried
109      * @return the value Object at the specified cell
110      *
111      */

112     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
113         if (columnIndex == 0) {
114             return propertyNames[rowIndex];
115         } else {
116             try {
117                 String JavaDoc pName = propertyNames[rowIndex];
118                 if ((pName.indexOf('|')) != -1) { //"formula | formulaElement.cdata"
119
/* This is for special cases where more than one field refers to the same value.
120                      * For eg. calculated memeber's formula and formulaelement.cdata refers to the same formula string.
121                      * These cases arise to handle xml standards where an attribute can also appear as an xml tag.
122                      */

123                     Object JavaDoc obj = null;
124                     String JavaDoc[] pNames = pName.split("\\|",0); // split field names on | to form an array of property names strings that are optional.
125
for(int j=0; j<pNames.length; j++) {
126                         if ((pNames[j].indexOf('.')) != -1) {
127                             String JavaDoc[] pNamesField = pNames[j].trim().split("\\.",0); // split string on . to form an array of property name within the property name.
128
if(pNamesField.length >1) {
129                                 Field JavaDoc f = target.getClass().getField(pNamesField[0].trim());
130                                 obj = f.get(target);
131                                 if (obj != null) {
132                                     Field JavaDoc f2 = obj.getClass().getField(pNamesField[1].trim());
133                                     Object JavaDoc obj2 = f2.get(obj);
134                                     return obj2;
135                                 }
136                             }
137                             return null;
138                         } else {
139                             Field JavaDoc f = target.getClass().getField(pNames[j].trim());
140                             obj = f.get(target);
141                             if (obj != null) {
142                                 return obj;
143                             }
144                         }
145                     }
146                     return obj;
147                 } else {
148                     // default case where one field refers to one value.
149
Field JavaDoc f = target.getClass().getField(propertyNames[rowIndex]);
150
151                     Object JavaDoc obj = f.get(target);
152                     return obj;
153                 }
154             } catch (Exception JavaDoc ex) {
155                 ex.printStackTrace();
156                 return "#ERROR";
157             }
158         }
159     }
160
161 /**
162    * Copyright (C) 2006, 2007 CINCOM SYSTEMS, INC.
163    * All Rights Reserved
164    */

165     public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
166         setErrorMsg(null);
167         try {
168             String JavaDoc pName = propertyNames[rowIndex];
169             int i=-1;
170             if ((i=pName.indexOf('|')) != -1) { //"formula | formulaElement.cdata"
171
Field JavaDoc f = target.getClass().getField(propertyNames[rowIndex].substring(0,i).trim()); // save value in the first field name
172
f.set(target,aValue);
173                 // delete the value from second and remaining field names
174
String JavaDoc[] pNames = pName.split("\\|",0); // split field names on | to form an array of property names strings that are optional.
175
for(int j=1; j<pNames.length; j++) {
176                     String JavaDoc[] pNamesField = pNames[j].trim().split("\\.",0); // split string on . to form an array of property name within the property name.
177
Field JavaDoc f2 = target.getClass().getField(pNamesField[0].trim());
178                     f2.set(target,null);
179                 }
180
181             } else if ( (target instanceof MondrianGuiDef.Level) && (pName.equals("ordinalExp")) ) {
182                 ((MondrianGuiDef.Level) target).ordinalExp.expressions[0] = (MondrianGuiDef.SQL) aValue;
183                     /*
184                     Field f = target.getClass().getField(propertyNames[rowIndex]);
185                     f.set(((MondrianGuiDef.Level) target).ordinalExp.expressions[0], aValue);
186                      */

187             } else if ( (target instanceof MondrianGuiDef.Table && pName.equals("name")) ||
188                     (target instanceof MondrianGuiDef.Hierarchy && pName.equals("primaryKeyTable")) ||
189                     (target instanceof MondrianGuiDef.Level && pName.equals("table"))
190                     ) {
191                 // updating all table values
192
if (aValue != null) { // split and save only if value exists
193
String JavaDoc[] aValues = ((String JavaDoc) aValue).split("->");
194                     if (aValues.length == 2) {
195                         if (target instanceof MondrianGuiDef.Table) {
196                             ((MondrianGuiDef.Table) target).name = aValues[1];
197                             ((MondrianGuiDef.Table) target).schema = aValues[0];
198                             fireTableDataChanged(); // to refresh the value in schema field also alongwith table name
199
} else {
200                             Field JavaDoc f = target.getClass().getField(propertyNames[rowIndex]);
201                             f.set(target,aValues[1]);
202                         }
203                     } else {
204                         Field JavaDoc f = target.getClass().getField(propertyNames[rowIndex]);
205                         f.set(target,aValue);
206                     }
207                 }
208
209             } else if ( (target instanceof MondrianGuiDef.Dimension && pName.equals("foreignKey")) ||
210                     (target instanceof MondrianGuiDef.DimensionUsage && pName.equals("foreignKey")) ||
211                     (target instanceof MondrianGuiDef.Measure && pName.equals("column")) ||
212                     (target instanceof MondrianGuiDef.Hierarchy && pName.equals("primaryKey")) ||
213                     (target instanceof MondrianGuiDef.Level && pName.equals("column")) ||
214                     (target instanceof MondrianGuiDef.Property && pName.equals("column"))
215                     ) {
216                 // updating all column values
217
if (aValue != null) { // split and save only if value exists
218
String JavaDoc[] aValues = ((String JavaDoc) aValue).split("->");
219                     Field JavaDoc f = target.getClass().getField(propertyNames[rowIndex]);
220                     f.set(target, aValues[aValues.length-1]); // save the last value in the array from split
221
}
222
223             } else {
224                 if ( propertyNames[rowIndex].equals("name")
225                 && (! (target instanceof MondrianGuiDef.Table))
226                 && (! aValue.equals(target.getClass().getField(propertyNames[rowIndex]).get(target)))
227                 && duplicateName(aValue) ) {
228                     setErrorMsg("Error setting name property. '"+aValue+"' already exists");
229                 } else {
230                     Field JavaDoc f = target.getClass().getField(propertyNames[rowIndex]);
231                     f.set(target,aValue);
232                 }
233             }
234         } catch (Exception JavaDoc ex) {
235             ex.printStackTrace();
236         }
237     }
238
239     public Object JavaDoc getValue() {
240         return target;
241     }
242
243 /**
244    * Copyright (C) 2006, 2007 CINCOM SYSTEMS, INC.
245    * All Rights Reserved
246    */

247     public Object JavaDoc getParentTarget() {
248         return parentTarget;
249     }
250
251     public void setParentTarget(Object JavaDoc parentTarget) {
252         this.parentTarget = parentTarget;
253     }
254
255     public String JavaDoc getFactTable() {
256         return factTable;
257     }
258
259     public void setFactTable(String JavaDoc factTable) {
260         this.factTable = factTable;
261     }
262
263     public String JavaDoc getFactTableSchema() {
264         return factTableSchema;
265     }
266
267     public void setFactTableSchema(String JavaDoc factTableSchema) {
268         this.factTableSchema = factTableSchema;
269     }
270
271     private boolean duplicateName(Object JavaDoc aValue) {
272         return (names!=null && names.contains(aValue));
273     }
274
275     public ArrayList getNames() {
276         return names;
277     }
278
279     public void setNames(ArrayList names) {
280         this.names = names;
281     }
282
283     public String JavaDoc getErrorMsg() {
284         return errorMsg;
285     }
286
287     public void setErrorMsg(String JavaDoc errorMsg) {
288         this.errorMsg = errorMsg;
289     }
290 }
291
292 // End PropertyTableModel.java
293
Popular Tags