KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > gui > SchemaPropertyCellEditor


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/gui/SchemaPropertyCellEditor.java#8 $
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 // Copyright (C) 2006-2007 CINCOM SYSTEMS, INC.
8 // All Rights Reserved.
9 // You must accept the terms of that agreement to use this software.
10 */

11 package mondrian.gui;
12
13 import java.awt.event.FocusAdapter JavaDoc;
14 import java.awt.event.ItemListener JavaDoc;
15 import java.awt.event.MouseAdapter JavaDoc;
16
17 import javax.swing.*;
18 import javax.swing.event.CellEditorListener JavaDoc;
19 import javax.swing.event.ChangeEvent JavaDoc;
20 import java.awt.*;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.KeyAdapter JavaDoc;
24 import java.awt.event.KeyEvent JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.awt.event.MouseListener JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.EventObject JavaDoc;
29 import java.util.Vector JavaDoc;
30 import javax.swing.border.*;
31 import javax.swing.text.JTextComponent JavaDoc;
32
33 /**
34  *
35  * @author sean
36  * @version $Id: //open/mondrian/src/main/mondrian/gui/SchemaPropertyCellEditor.java#8 $
37  */

38 public class SchemaPropertyCellEditor implements javax.swing.table.TableCellEditor JavaDoc {
39     ArrayList JavaDoc listeners;
40     JTextField stringEditor;
41     JCheckBox booleanEditor;
42     JTextField integerEditor;
43     JTable tableEditor;
44     Component JavaDoc activeEditor;
45     JComboBox listEditor;
46     JComboBox relationList; // Join, Table
47
JTable relationTable;
48     JPanel relationRenderer;
49
50     JDBCMetaData jdbcMetaData;
51     ComboBoxModel allOptions, selOptions;
52     String JavaDoc listEditorValue ;
53     MouseListener JavaDoc ml;
54     ItemListener JavaDoc il;
55     ActionListener JavaDoc al;
56
57     String JavaDoc noSelect = "-- No Selection --";
58     FocusAdapter JavaDoc editorFocus;
59
60     public SchemaPropertyCellEditor(JDBCMetaData jdbcMetaData) {
61         this();
62         this.jdbcMetaData = jdbcMetaData;
63     }
64
65     /** Creates a new instance of SchemaPropertyCellEditor */
66     public SchemaPropertyCellEditor() {
67
68
69
70         listeners = new ArrayList JavaDoc();
71         stringEditor = new JTextField();
72         stringEditor.setFont(Font.decode("Dialog"));
73         stringEditor.setBorder(null);
74
75         booleanEditor = new JCheckBox();
76         booleanEditor.setBackground(Color.white);
77
78         integerEditor = new JTextField();
79         integerEditor.setBorder(null);
80         integerEditor.setHorizontalAlignment(JTextField.RIGHT);
81         integerEditor.setFont(Font.decode("Courier"));
82
83         tableEditor = new JTable();
84
85         listEditor = new JComboBox();
86         listEditor.setEditable(true);
87         listEditor.setMaximumSize(stringEditor.getMaximumSize());
88         listEditor.setFont(Font.decode("Dialog"));
89         listEditor.setBackground(Color.white);
90         listEditor.setBorder(new EmptyBorder(0, 0, 0, 0)); //super.noFocusBorder);
91

92         /*
93          // MouseListener for JComboBox does not execurte when ComboBox is editable.
94
95         ml = new MouseAdapter() {
96             boolean all = true;
97             public void mouseClicked(MouseEvent e) {
98                 System.out.println("Mouse clicked ");}
99
100             public void mouseReleased(MouseEvent e) {
101                 System.out.println("mouse relased listeditor");
102                 if (listEditor.isDisplayable()) listEditor.setPopupVisible(true);
103             }
104
105             public void mousePressed(MouseEvent e) {
106                 System.out.println("Mouse pressed "+e.getClickCount());
107                 if(e.getClickCount() == 2) {
108                     if (all) {
109                         listEditor.setModel(allOptions); } else {
110                         listEditor.setModel(selOptions); }
111                     all = !all;
112                 }
113             }
114         };
115          // ItemListener executes twice for each change in selected item.
116         il = new ItemListener() {
117             boolean all = true;
118             public void itemStateChanged(ItemEvent e) {
119                 System.out.println("itemlistener executed ="+listEditor.getSelectedIndex());
120
121                 if (((JComboBox)e.getItemSelectable()).getSelectedIndex() == 0) { // 0 index refers to less or more options
122                     if (all) {
123                         listEditor.setModel(allOptions); } else {
124                         listEditor.setModel(selOptions); }
125                     //((JComboBox)e.getItemSelectable()).setSelectedIndex(-1);
126                     ((JComboBox)e.getItemSelectable()).setPopupVisible(true);
127                     all = !all;
128
129                 }
130
131                 if (listEditor.isDisplayable()) listEditor.setPopupVisible(true);
132
133             }
134         };
135          */

136
137         al = new ActionListener JavaDoc() {
138             boolean all = true;
139             public void actionPerformed(ActionEvent JavaDoc e) {
140                 //System.out.println(listEditor.getSelectedIndex()+"getActionCommand=="+e.getActionCommand());
141
if (e.getActionCommand().equals("comboBoxChanged") && listEditor.getSelectedIndex() == 0) { // 0 index refers to less or more options
142
if (all) {
143                         listEditor.setModel(allOptions);
144                     } else {
145                         listEditor.setModel(selOptions);
146                     }
147                     listEditor.setSelectedIndex(-1);
148                     all = !all;
149
150                 }
151                 if (listEditor.isDisplayable()) {
152                     listEditor.setPopupVisible(true);
153                 }
154             }
155
156         };
157
158
159         JTextComponent JavaDoc editor = (JTextComponent JavaDoc) listEditor.getEditor().getEditorComponent();
160
161         editor.addMouseListener(new MouseAdapter JavaDoc() {
162             public void mousePressed(MouseEvent JavaDoc e) {
163                 if (listEditor.isDisplayable()) {
164                     listEditor.setPopupVisible(true);
165                 }
166             }
167         });
168
169         editor.addKeyListener(new KeyAdapter JavaDoc() {
170
171             public void keyPressed(KeyEvent JavaDoc e) {
172                 if (listEditor.isDisplayable()) {
173                     listEditor.setPopupVisible(true);
174                 }
175             }
176             public void keyReleased(KeyEvent JavaDoc e) {
177                 //listEditor.setSelectedItem(((JTextComponent) e.getSource()).getText());
178
if (e.getKeyCode()==KeyEvent.VK_ESCAPE) {
179                     listEditor.setSelectedItem(listEditorValue);
180                     listEditor.getEditor().setItem(listEditorValue);
181                 }
182             }
183         });
184
185         //====editor.addFocusListener(editorFocus);
186
/*
187         editor.addFocusListener(new FocusAdapter() {
188             public void focusGained(FocusEvent e) {System.out.println("--Combo Box Editor GAINED focus"+activeEditor.getClass()); }
189             public void focusLost(FocusEvent e) {System.out.println("--Combo Box Editor LOST focus"+activeEditor.getClass());}
190         });
191          */

192         relationRenderer = new JPanel();
193         //relationRenderer.setLayout(new BorderLayout()); // default flowlayout
194
relationList = new JComboBox( new String JavaDoc[] {"Join", "Table"} );
195         relationList.setMaximumSize(stringEditor.getMaximumSize());
196         relationTable = new JTable();
197         relationRenderer.add(relationList);
198         relationRenderer.add(relationTable);
199
200     }
201
202     public Component JavaDoc getTableCellEditorComponent(final JTable table, Object JavaDoc value, boolean isSelected, final int row, final int column) {
203
204         PropertyTableModel tableModel = (PropertyTableModel) table.getModel();
205         Class JavaDoc parentClassz = null;
206         if (tableModel.getParentTarget() != null) {
207             parentClassz = tableModel.getParentTarget().getClass();}
208         Class JavaDoc targetClassz = tableModel.target.getClass();
209         String JavaDoc propertyName = tableModel.getRowName(row);
210         String JavaDoc selectedFactTable = tableModel.getFactTable();
211         String JavaDoc selectedFactTableSchema = tableModel.getFactTableSchema();
212         listEditorValue=null; // reset value of combo-box
213

214         if (targetClassz == MondrianGuiDef.UserDefinedFunction.class && propertyName.equals("className")) {
215             Vector JavaDoc udfs = getUdfs();
216             ComboBoxModel cAlludfs = new DefaultComboBoxModel(udfs);
217
218             listEditor.setEditable(true);
219             listEditor.setToolTipText(null);
220             //listEditor.removeMouseListener(ml);
221
listEditor.removeActionListener(al);
222
223             listEditor.setModel(cAlludfs);
224             listEditor.setSelectedItem((String JavaDoc)value);
225             listEditorValue = (String JavaDoc)value;
226             activeEditor= listEditor;
227         } else if (targetClassz == MondrianGuiDef.Measure.class && propertyName.equals("formatString")) {
228             Vector JavaDoc formatStrs = getFormatStrings();
229             ComboBoxModel cAllformatStrs = new DefaultComboBoxModel(formatStrs);
230
231             listEditor.setEditable(true);
232             listEditor.setToolTipText(null);
233             listEditor.removeActionListener(al);
234
235             listEditor.setModel(cAllformatStrs);
236             listEditor.setSelectedItem((String JavaDoc)value);
237             listEditorValue = (String JavaDoc)value;
238             activeEditor= listEditor;
239         } else if (targetClassz == MondrianGuiDef.Measure.class && propertyName.equals("aggregator")) {
240             listEditor.setEditable(false);
241             listEditor.setToolTipText(null);
242             listEditor.removeActionListener(al);
243             listEditor.setModel(new DefaultComboBoxModel(MondrianGuiDef.Measure._aggregator_values));
244             listEditor.setSelectedItem((String JavaDoc)value);
245             activeEditor= listEditor;
246
247         } else if (targetClassz == MondrianGuiDef.Measure.class && propertyName.equals("datatype")) {
248             listEditor.setEditable(false);
249             listEditor.setToolTipText(null);
250             listEditor.removeActionListener(al);
251             listEditor.setModel(new DefaultComboBoxModel(MondrianGuiDef.Measure._datatype_values));
252             listEditor.setSelectedItem((String JavaDoc)value);
253             activeEditor= listEditor;
254
255         } else if (targetClassz == MondrianGuiDef.SQL.class && propertyName.equals("dialect")) {
256             listEditor.setEditable(false);
257             listEditor.setToolTipText(null);
258             listEditor.removeActionListener(al);
259             listEditor.setModel(new DefaultComboBoxModel(MondrianGuiDef.SQL._dialect_values));
260             listEditor.setSelectedItem((String JavaDoc)value);
261             activeEditor= listEditor;
262
263         } else if (targetClassz == MondrianGuiDef.Level.class && propertyName.equals("hideMemberIf")) {
264             listEditor.setEditable(false);
265             listEditor.setToolTipText(null);
266             listEditor.removeActionListener(al);
267             listEditor.setModel(new DefaultComboBoxModel(MondrianGuiDef.Level._hideMemberIf_values));
268             listEditor.setSelectedItem((String JavaDoc)value);
269             activeEditor= listEditor;
270
271         } else if (targetClassz == MondrianGuiDef.Level.class && propertyName.equals("levelType")) {
272             listEditor.setEditable(false);
273             listEditor.setToolTipText(null);
274             listEditor.removeActionListener(al);
275             listEditor.setModel(new DefaultComboBoxModel(MondrianGuiDef.Level._levelType_values));
276             listEditor.setSelectedItem((String JavaDoc)value);
277             activeEditor= listEditor;
278
279         } else if (targetClassz == MondrianGuiDef.Level.class && propertyName.equals("type")) {
280             listEditor.setEditable(false);
281             listEditor.setToolTipText(null);
282             listEditor.removeActionListener(al);
283             listEditor.setModel(new DefaultComboBoxModel(MondrianGuiDef.Level._type_values));
284             listEditor.setSelectedItem((String JavaDoc)value);
285             activeEditor= listEditor;
286
287         } else if (targetClassz == MondrianGuiDef.Dimension.class && propertyName.equals("type")) {
288             listEditor.setEditable(false);
289             listEditor.setToolTipText(null);
290             listEditor.removeActionListener(al);
291             listEditor.setModel(new DefaultComboBoxModel(MondrianGuiDef.Dimension._type_values));
292             listEditor.setSelectedItem((String JavaDoc)value);
293             activeEditor= listEditor;
294
295         } else if ( targetClassz == MondrianGuiDef.DimensionUsage.class && propertyName.equals("source")) {
296             Vector JavaDoc source = getSource();
297             ComboBoxModel cAllsource = new DefaultComboBoxModel(source);
298
299             listEditor.setEditable(true);
300             listEditor.setToolTipText(null);
301             listEditor.removeActionListener(al);
302
303             listEditor.setModel(cAllsource);
304             listEditor.setSelectedItem((String JavaDoc)value);
305             listEditorValue = (String JavaDoc)value;
306             activeEditor= listEditor;
307
308         } else if ((tableModel.target instanceof MondrianGuiDef.Grant || tableModel.target instanceof MondrianGuiDef.MemberGrant) && propertyName.equals("access")){
309
310             listEditor.setEditable(false);
311             listEditor.setToolTipText(null);
312             listEditor.removeActionListener(al);
313
314             ComboBoxModel cAccess = new DefaultComboBoxModel(MondrianGuiDef.Grant._access_values);
315
316             if ( targetClassz == MondrianGuiDef.SchemaGrant.class ) {
317                 cAccess = new DefaultComboBoxModel(new String JavaDoc[] {"all", "none", "all_dimensions"} );
318             } else if ( targetClassz == MondrianGuiDef.CubeGrant.class ||
319                     targetClassz == MondrianGuiDef.DimensionGrant.class ||
320                     targetClassz == MondrianGuiDef.MemberGrant.class) {
321                 cAccess = new DefaultComboBoxModel(new String JavaDoc[] {"all", "none"} );
322             } else if (targetClassz == MondrianGuiDef.HierarchyGrant.class ) {
323                 cAccess = new DefaultComboBoxModel(new String JavaDoc[] {"all", "none", "custom"} );
324             }
325             listEditor.setModel(cAccess);
326
327             listEditor.setSelectedItem((String JavaDoc)value);
328             activeEditor= listEditor;
329
330         } else if ((targetClassz == MondrianGuiDef.DimensionGrant.class && propertyName.equals("dimension")) ||
331                 (targetClassz == MondrianGuiDef.HierarchyGrant.class && propertyName.equals("hierarchy"))
332                 ){
333             Vector JavaDoc source = getDimensions();
334             ComboBoxModel cAllsource = new DefaultComboBoxModel(source);
335
336             listEditor.setEditable(false);
337             listEditor.setToolTipText(null);
338             listEditor.removeActionListener(al);
339
340             listEditor.setModel(cAllsource);
341             listEditor.setSelectedItem((String JavaDoc)value);
342             listEditorValue = (String JavaDoc)value;
343             activeEditor= listEditor;
344
345         } else if ((targetClassz == MondrianGuiDef.HierarchyGrant.class && (propertyName.equals("topLevel") || propertyName.equals("bottomLevel")))
346         ){
347             Vector JavaDoc source = getLevels(( (MondrianGuiDef.HierarchyGrant) tableModel.target).hierarchy);
348             ComboBoxModel cAllsource = new DefaultComboBoxModel(source);
349
350             listEditor.setEditable(false);
351             listEditor.setToolTipText(null);
352             listEditor.removeActionListener(al);
353
354             listEditor.setModel(cAllsource);
355             listEditor.setSelectedItem((String JavaDoc)value);
356             listEditorValue = (String JavaDoc)value;
357             activeEditor= listEditor;
358
359         } else if (( (targetClassz == MondrianGuiDef.VirtualCubeDimension.class || targetClassz == MondrianGuiDef.VirtualCubeMeasure.class) &&
360                 propertyName.equals("cubeName")) ||
361                 (targetClassz == MondrianGuiDef.CubeGrant.class && propertyName.equals("cube"))
362                 ){
363             Vector JavaDoc source = getCubes();
364             ComboBoxModel cAllsource = new DefaultComboBoxModel(source);
365
366             listEditor.setEditable(false);
367             listEditor.setToolTipText(null);
368             listEditor.removeActionListener(al);
369
370             listEditor.setModel(cAllsource);
371             listEditor.setSelectedItem((String JavaDoc)value);
372             listEditorValue = (String JavaDoc)value;
373             activeEditor= listEditor;
374             //===================================================================================
375
} else if ( (targetClassz == MondrianGuiDef.Dimension.class && propertyName.equals("foreignKey")) ||
376                 (targetClassz == MondrianGuiDef.DimensionUsage.class && propertyName.equals("foreignKey")) ||
377                 (targetClassz == MondrianGuiDef.Measure.class && propertyName.equals("column")) ) {
378             Vector JavaDoc fks = new Vector JavaDoc(jdbcMetaData.getFactTableFKs(selectedFactTableSchema, selectedFactTable)); //===
379
fks.add(0, "<< All Columns >>");
380             Vector JavaDoc allcols = new Vector JavaDoc(jdbcMetaData.getAllColumns(selectedFactTableSchema, selectedFactTable));
381             ComboBoxModel cFks = new DefaultComboBoxModel(fks);
382
383             listEditor.setEditable(true);
384             listEditor.setToolTipText(null);
385             listEditor.removeActionListener(al);
386             if ((fks.size() > 1) && propertyName.equals("foreignKey")){
387                 allcols.add(0, "<< Foreign keys >>");
388                 ComboBoxModel cAllcols = new DefaultComboBoxModel(allcols);
389                 listEditor.setModel(cFks);
390                 //listEditor.setToolTipText("Relavant Options shows Foreign keys and All Columns in selected Fact Table");
391
selOptions = cFks;
392                 allOptions = cAllcols;
393                 //listEditor.addItemListener(il); // executes twice for each selection
394
//listEditor.addMouseListener(ml); //does not execute when ComboBox isEditable true.
395
listEditor.addActionListener(al);
396             } else {
397                 ComboBoxModel cAllcols = new DefaultComboBoxModel(allcols);
398                 listEditor.setModel(cAllcols);
399             }
400
401             listEditor.setSelectedItem((String JavaDoc)value);
402             listEditorValue = (String JavaDoc)value;
403             activeEditor= listEditor;
404         } else if (targetClassz == MondrianGuiDef.Hierarchy.class && propertyName.equals("primaryKey")) {
405             MondrianGuiDef.Hierarchy hProps = (MondrianGuiDef.Hierarchy) tableModel.getValue();
406             String JavaDoc pkTable = hProps.primaryKeyTable;
407
408             String JavaDoc schemaName = null;
409             Vector JavaDoc allcols = jdbcMetaData.getAllColumns(schemaName, pkTable);
410             String JavaDoc pk = jdbcMetaData.getTablePK(schemaName, pkTable);
411
412             ComboBoxModel cAllcols = new DefaultComboBoxModel(allcols);
413
414             listEditor.setEditable(true);
415             listEditor.setToolTipText(null);
416             listEditor.removeActionListener(al);
417
418             listEditor.setModel(cAllcols);
419             if (value == null || ((String JavaDoc) value).equals("")) {
420                 listEditor.setSelectedItem(pk);
421             } else {
422                 listEditor.setSelectedItem((String JavaDoc)value);
423                 listEditorValue = (String JavaDoc)value;
424             }
425             activeEditor= listEditor;
426         } else if ( (targetClassz == MondrianGuiDef.Level.class && propertyName.equals("column")) ||
427                 (targetClassz == MondrianGuiDef.Property.class && propertyName.equals("column"))
428                 ) {
429
430             MondrianGuiDef.Level lProps;
431             if (targetClassz == MondrianGuiDef.Level.class) {
432                 lProps = (MondrianGuiDef.Level) tableModel.getValue();
433             } else {
434                 lProps = (MondrianGuiDef.Level) this.getParentObject();
435             }
436
437             String JavaDoc lTable = lProps.table;
438             Vector JavaDoc allcols = jdbcMetaData.getAllColumns(null, lTable); //===
439
ComboBoxModel cAllcols = new DefaultComboBoxModel(allcols);
440
441             listEditor.setEditable(true);
442             listEditor.setToolTipText(null);
443             listEditor.removeActionListener(al);
444
445             listEditor.setModel(cAllcols);
446             listEditor.setSelectedItem((String JavaDoc)value);
447             listEditorValue = (String JavaDoc)value;
448             activeEditor= listEditor;
449             //===================================================================================
450

451         } else if (targetClassz == MondrianGuiDef.Property.class && propertyName.equals("type")) {
452             listEditor.setEditable(false);
453             listEditor.setToolTipText(null);
454             listEditor.removeActionListener(al);
455             listEditor.setModel(new DefaultComboBoxModel(MondrianGuiDef.Property._type_values));
456             listEditor.setSelectedItem((String JavaDoc)value);
457             activeEditor= listEditor;
458
459         } else if (targetClassz == MondrianGuiDef.Table.class && propertyName.equals("schema")) {
460             Vector JavaDoc allschemas = jdbcMetaData.getAllSchemas();
461             ComboBoxModel cAllschemas = new DefaultComboBoxModel(allschemas);
462
463             listEditor.setEditable(true);
464             listEditor.setToolTipText(null);
465             listEditor.removeActionListener(al);
466
467             listEditor.setModel(cAllschemas);
468             listEditor.setSelectedItem((String JavaDoc)value);
469             listEditorValue = (String JavaDoc)value;
470             activeEditor= listEditor;
471         } else if(( targetClassz == MondrianGuiDef.Table.class && propertyName.equals("name")) ||
472             (targetClassz == MondrianGuiDef.Hierarchy.class && propertyName.equals("primaryKeyTable")) ||
473             (targetClassz == MondrianGuiDef.Level.class && propertyName.equals("table"))
474             ) {
475             String JavaDoc schema = "";
476             if (targetClassz == MondrianGuiDef.Table.class) {
477                 MondrianGuiDef.Table tProps = (MondrianGuiDef.Table) tableModel.getValue();
478                 schema = tProps.schema;
479             }
480             Vector JavaDoc factTables = new Vector JavaDoc(jdbcMetaData.getFactTables(schema));
481             Vector JavaDoc allTablesMinusFact = new Vector JavaDoc(jdbcMetaData.getAllTables(schema, selectedFactTable));
482             Vector JavaDoc allTables = new Vector JavaDoc(jdbcMetaData.getAllTables(schema));
483             Vector JavaDoc dimeTables = new Vector JavaDoc(jdbcMetaData.getDimensionTables(schema, selectedFactTable));
484
485             ComboBoxModel cFactTables = new DefaultComboBoxModel(factTables); //suggestive fact tables
486
ComboBoxModel cAllTables = new DefaultComboBoxModel((allTablesMinusFact.size() >0)?allTablesMinusFact:allTables); // all tables of selected schema
487
ComboBoxModel cDimeTables = new DefaultComboBoxModel(dimeTables); // suggestive dimension tables based on selected fact table .
488

489             listEditor.setEditable(true);
490             listEditor.setToolTipText(null);
491             listEditor.removeActionListener(al);
492             listEditor.setModel(cAllTables);
493             allOptions = cAllTables;
494             boolean toggleModel = false;
495             if (parentClassz == MondrianGuiDef.Cube.class) {
496                 cAllTables = new DefaultComboBoxModel(allTables);
497                 allOptions = cAllTables;
498                 if (factTables.size() > 0) {
499                     ((DefaultComboBoxModel) cFactTables).insertElementAt("<< All Tables >>",0);
500                     ((DefaultComboBoxModel) cAllTables).insertElementAt("<< Fact Tables >>",0);
501                     listEditor.setModel(cFactTables);
502                     //listEditor.setToolTipText("Double-Click to toggle between Fact tables and All tables");
503
selOptions = cFactTables;
504                     toggleModel=true;
505                 }
506             } else {
507                 if (dimeTables.size() > 0) {
508                     ((DefaultComboBoxModel) cDimeTables).insertElementAt("<< All Tables >>",0);
509                     ((DefaultComboBoxModel) cAllTables).insertElementAt("<< Dimension Tables >>",0);
510                     listEditor.setModel(cDimeTables);
511                     //listEditor.setToolTipText("Double-Click to toggle between Dimension tables and All tables");
512
selOptions = cDimeTables;
513                     toggleModel=true;
514                 }
515             }
516
517             if (toggleModel) {
518                 listEditor.addActionListener(al);}
519             listEditor.setSelectedItem((String JavaDoc)value);
520             listEditorValue = (String JavaDoc)value;
521             activeEditor= listEditor;
522         } else if (value instanceof String JavaDoc) {
523             activeEditor = stringEditor;
524             stringEditor.setText((String JavaDoc)value);
525         } else if (value instanceof Boolean JavaDoc) {
526             activeEditor = booleanEditor;
527             booleanEditor.setSelected((Boolean JavaDoc) value);
528         } else if (value instanceof Integer JavaDoc) {
529             activeEditor = integerEditor;
530             integerEditor.setText((String JavaDoc)value);
531         } else if (value == null ) {
532             value = "";
533             activeEditor = stringEditor;
534             stringEditor.setText((String JavaDoc)value);
535         } else if (value.getClass() == MondrianGuiDef.Join.class) {
536             SchemaPropertyCellEditor spce = new SchemaPropertyCellEditor();
537             tableEditor.setDefaultEditor(Object JavaDoc.class, spce);
538             SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
539             tableEditor.setDefaultRenderer(Object JavaDoc.class, spcr);
540             PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_JOIN);
541             tableEditor.setModel(ptm);
542             activeEditor = tableEditor;
543         } else if (value.getClass() == MondrianGuiDef.NameExpression.class) {
544             return null;
545         } else if (value.getClass() == MondrianGuiDef.Relation.class) {
546             SchemaPropertyCellEditor spce = new SchemaPropertyCellEditor();
547             tableEditor.setDefaultEditor(Object JavaDoc.class, spce);
548             SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
549             tableEditor.setDefaultRenderer(Object JavaDoc.class, spcr);
550             PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_RELATION);
551             tableEditor.setModel(ptm);
552             activeEditor = tableEditor;
553             return null;
554         } else if (value.getClass() == MondrianGuiDef.OrdinalExpression.class) {
555             SchemaPropertyCellEditor spce = new SchemaPropertyCellEditor();
556             tableEditor.setDefaultEditor(Object JavaDoc.class, spce);
557             SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
558             tableEditor.setDefaultRenderer(Object JavaDoc.class, spcr);
559             //===PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_SQL);
560
PropertyTableModel ptm = new PropertyTableModel(((MondrianGuiDef.OrdinalExpression)value).expressions[0],SchemaExplorer.DEF_SQL);
561             ptm.setParentTarget(((PropertyTableModel) table.getModel()).target);
562             tableEditor.setModel(ptm);
563             activeEditor = tableEditor;
564         } else if (value.getClass() == MondrianGuiDef.Formula.class) {
565             SchemaPropertyCellEditor spce = new SchemaPropertyCellEditor(jdbcMetaData);
566             tableEditor.setDefaultEditor(Object JavaDoc.class, spce);
567             SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
568             tableEditor.setDefaultRenderer(Object JavaDoc.class, spcr);
569             PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_FORMULA);
570             tableEditor.setModel(ptm);
571             tableEditor.getColumnModel().getColumn(0).setMaxWidth(100);
572             tableEditor.getColumnModel().getColumn(0).setMinWidth(100);
573             activeEditor = tableEditor;
574         } else if (value.getClass() == MondrianGuiDef.CalculatedMemberProperty.class) {
575             SchemaPropertyCellEditor spce = new SchemaPropertyCellEditor(jdbcMetaData);
576             tableEditor.setDefaultEditor(Object JavaDoc.class, spce);
577             SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
578             tableEditor.setDefaultRenderer(Object JavaDoc.class, spcr);
579             PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_CALCULATED_MEMBER_PROPERTY);
580             tableEditor.setModel(ptm);
581             tableEditor.getColumnModel().getColumn(0).setMaxWidth(100);
582             tableEditor.getColumnModel().getColumn(0).setMinWidth(100);
583             activeEditor = tableEditor;
584         } else if (value.getClass() == MondrianGuiDef.Table.class) {
585             SchemaPropertyCellEditor spce = new SchemaPropertyCellEditor(jdbcMetaData);
586             // adding cell editing stopped listeners to nested property of type table
587
// so that any change in value of table fields are reflected in tree
588
for (int i = listeners.size() - 1; i >= 0; i--) {
589                 spce.addCellEditorListener(((CellEditorListener JavaDoc)listeners.get(i)));
590             }
591             tableEditor.setDefaultEditor(Object JavaDoc.class, spce);
592             SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
593             tableEditor.setDefaultRenderer(Object JavaDoc.class, spcr);
594             PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_TABLE);
595             ptm.setFactTable(selectedFactTable);
596             if (targetClassz == MondrianGuiDef.Cube.class) {
597                 ptm.setParentTarget(((PropertyTableModel) table.getModel()).target);}
598             tableEditor.setModel(ptm);
599             tableEditor.getColumnModel().getColumn(0).setMaxWidth(100);
600             tableEditor.getColumnModel().getColumn(0).setMinWidth(100);
601             activeEditor = tableEditor;
602         } else if (value.getClass() == MondrianGuiDef.AggFactCount.class) {
603             SchemaPropertyCellEditor spce = new SchemaPropertyCellEditor(jdbcMetaData);
604             // adding cell editing stopped listeners to nested property of type table
605
// so that any change in value of table fields are reflected in tree
606
for (int i = listeners.size() - 1; i >= 0; i--) {
607                 spce.addCellEditorListener(((CellEditorListener JavaDoc)listeners.get(i)));
608             }
609             tableEditor.setDefaultEditor(Object JavaDoc.class, spce);
610             SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
611             tableEditor.setDefaultRenderer(Object JavaDoc.class, spcr);
612             PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_AGG_FACT_COUNT);
613             ptm.setFactTable(selectedFactTable);
614             /*
615             if (targetClassz == MondrianGuiDef.Cube.class) {
616                 ptm.setParentTarget(((PropertyTableModel) table.getModel()).target);}
617              */

618             tableEditor.setModel(ptm);
619             tableEditor.getColumnModel().getColumn(0).setMaxWidth(100);
620             tableEditor.getColumnModel().getColumn(0).setMinWidth(100);
621             activeEditor = tableEditor;
622         } else if (value.getClass() == MondrianGuiDef.Closure.class) {
623             SchemaPropertyCellEditor spce = new SchemaPropertyCellEditor(jdbcMetaData);
624             // adding cell editing stopped listeners to nested property of type table
625
// so that any change in value of table fields are reflected in tree
626
for (int i = listeners.size() - 1; i >= 0; i--) {
627                 spce.addCellEditorListener(((CellEditorListener JavaDoc)listeners.get(i)));
628             }
629             tableEditor.setDefaultEditor(Object JavaDoc.class, spce);
630             SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
631             tableEditor.setDefaultRenderer(Object JavaDoc.class, spcr);
632             PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_CLOSURE);
633             ptm.setFactTable(selectedFactTable);
634             if (targetClassz == MondrianGuiDef.Level.class) {
635                 ptm.setParentTarget(((PropertyTableModel) table.getModel()).target);}
636             tableEditor.setModel(ptm);
637             tableEditor.getColumnModel().getColumn(0).setMaxWidth(100);
638             tableEditor.getColumnModel().getColumn(0).setMinWidth(100);
639             spcr.setTableRendererHeight(tableEditor, null);
640             activeEditor = tableEditor;
641         } else if (value.getClass() == MondrianGuiDef.Property.class) {
642             SchemaPropertyCellEditor spce = new SchemaPropertyCellEditor();
643             tableEditor.setDefaultEditor(Object JavaDoc.class, spce);
644             SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
645             tableEditor.setDefaultRenderer(Object JavaDoc.class, spcr);
646             PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_PROPERTY);
647             tableEditor.setModel(ptm);
648             activeEditor = tableEditor;
649         } else {
650             value = "";
651             activeEditor = stringEditor;
652             stringEditor.setText((String JavaDoc)value);
653         }
654         activeEditor.setVisible(true);
655
656         /* Focus lost on table was supposed to save the last edited value in table model object
657                  but this activeeditor saved the last edited value in the new table model object
658             activeEditor.addFocusListener(new FocusAdapter() {
659                 public void focusGained(FocusEvent e) {System.out.println("**Editor GAINED focus "+this.getClass()); }
660                 public void focusLost(FocusEvent e) {System.out.println("**Editor LOST focus "+this.getClass());
661
662                     Object value = table.getCellEditor(row, column).getCellEditorValue();
663                     table.getModel().setValueAt(value, row, column);
664                         Object value = table.getCellEditor().getCellEditorValue();
665                         table.setValueAt(value, row, column);
666                 }
667             });
668          */

669
670         table.changeSelection(row, column, false, false);
671         activeEditor.setBackground(new java.awt.Color JavaDoc(224, 249, 255));
672         activeEditor.requestFocusInWindow();
673         return activeEditor;
674     }
675
676     /** Adds a listener to the list that's notified when the editor
677      * stops, or cancels editing.
678      *
679      * @param l the CellEditorListener
680      *
681      */

682     public void addCellEditorListener(CellEditorListener JavaDoc l) {
683         listeners.add(l);
684     }
685
686     /** Tells the editor to cancel editing and not accept any partially
687      * edited value.
688      *
689      */

690     public void cancelCellEditing() {
691         if (activeEditor != null) {
692             activeEditor.setVisible(false);
693             fireEditingCancelled();
694         }
695     }
696
697     /** Returns the value contained in the editor.
698      * @return the value contained in the editor
699      *
700      */

701     public Object JavaDoc getCellEditorValue() {
702         if (activeEditor == stringEditor) {
703             return stringEditor.getText();
704         } else if (activeEditor == booleanEditor) {
705             return booleanEditor.isSelected();
706         } else if (activeEditor == listEditor) {
707             if (listEditor.isEditable()) {
708                 return listEditor.getEditor().getItem(); // returns the edited value from combox box
709
} else {
710                 if(listEditor.getSelectedItem() == noSelect) {
711                     return null; // blank selection
712
}
713                 return listEditor.getSelectedItem(); //// returns the selected value from combox box
714
}
715         } else if (activeEditor == tableEditor) {
716             return ((PropertyTableModel) tableEditor.getModel()).getValue();
717         }
718
719         return null;
720     }
721
722     /** Asks the editor if it can start editing using <code>anEvent</code>.
723      * <code>anEvent</code> is in the invoking component coordinate system.
724      * The editor can not assume the Component returned by
725      * <code>getCellEditorComponent</code> is installed. This method
726      * is intended for the use of client to avoid the cost of setting up
727      * and installing the editor component if editing is not possible.
728      * If editing can be started this method returns true.
729      *
730      * @param anEvent the event the editor should use to consider
731      * whether to begin editing or not
732      * @return true if editing can be started
733      * @see #shouldSelectCell
734      *
735      */

736     public boolean isCellEditable(EventObject JavaDoc anEvent) {
737         return true;
738     }
739
740     /** Removes a listener from the list that's notified
741      *
742      * @param l the CellEditorListener
743      *
744      */

745     public void removeCellEditorListener(CellEditorListener JavaDoc l) {
746         listeners.remove(l);
747     }
748
749     /** Returns true if the editing cell should be selected, false otherwise.
750      * Typically, the return value is true, because is most cases the editing
751      * cell should be selected. However, it is useful to return false to
752      * keep the selection from changing for some types of edits.
753      * eg. A table that contains a column of check boxes, the user might
754      * want to be able to change those checkboxes without altering the
755      * selection. (See Netscape Communicator for just such an example)
756      * Of course, it is up to the client of the editor to use the return
757      * value, but it doesn't need to if it doesn't want to.
758      *
759      * @param anEvent the event the editor should use to start
760      * editing
761      * @return true if the editor would like the editing cell to be selected;
762      * otherwise returns false
763      * @see #isCellEditable
764      *
765      */

766     public boolean shouldSelectCell(EventObject JavaDoc anEvent) {
767         return true;
768     }
769
770     /** Tells the editor to stop editing and accept any partially edited
771      * value as the value of the editor. The editor returns false if
772      * editing was not stopped; this is useful for editors that validate
773      * and can not accept invalid entries.
774      *
775      * @return true if editing was stopped; false otherwise
776      *
777      */

778     public boolean stopCellEditing() {
779         if (activeEditor != null) {
780             /* save the nested table as well */
781             if (activeEditor == tableEditor) {
782                 if (tableEditor.isEditing()) {
783                     /*
784                     System.out.println(" tableEditor.isEditing()=="+tableEditor.isEditing());
785                     System.out.println(" tableEditor.getEditingRow()=="+tableEditor.getEditingRow());
786                     System.out.println(" tableEditor.getEditingColumn()=="+tableEditor.getEditingColumn());
787                      */

788                     ArrayList JavaDoc nestedTableEditors = new ArrayList JavaDoc();
789                     JTable nestedTableEditor = tableEditor;
790                     // get the list of nested tables from outer->inner sequence, descending towards innermost nested table
791
// so that we can stop the editing in this order.
792
while (nestedTableEditor != null) {
793                         nestedTableEditors.add(nestedTableEditor);
794                         SchemaPropertyCellEditor sce = (SchemaPropertyCellEditor) nestedTableEditor.getCellEditor();
795                         if (sce != null && sce.activeEditor == sce.tableEditor && sce.tableEditor.isEditing()) {
796                             nestedTableEditor = sce.tableEditor; //
797
//tableEditor.editingStopped(null);
798
} else {
799                             nestedTableEditor = null;
800                         }
801                     }
802                     for (int i=nestedTableEditors.size()-1; i>=0; i--) {
803                         ((JTable) nestedTableEditors.get(i)).editingStopped(null);
804                     }
805                         /*
806                         SchemaPropertyCellEditor sce = (SchemaPropertyCellEditor) tableEditor.getCellEditor();
807                         if (sce != null) {
808                             //tableEditor.editingStopped(null);
809                             Object value = sce.getCellEditorValue();
810                             tableEditor.setValueAt(value, tableEditor.getEditingRow(), tableEditor.getEditingColumn());
811                             //sce.stopCellEditing();
812                         }
813                          **/

814
815                 }
816
817             }
818             activeEditor.setVisible(false);
819             fireEditingStopped();
820         }
821         return true;
822     }
823
824     protected void fireEditingStopped() {
825         ChangeEvent JavaDoc ce = new ChangeEvent JavaDoc(this);
826         for (int i = listeners.size() - 1; i >= 0; i--) {
827             ((CellEditorListener JavaDoc)listeners.get(i)).editingStopped(ce);
828         }
829     }
830
831     protected void fireEditingCancelled() {
832         ChangeEvent JavaDoc ce = new ChangeEvent JavaDoc(this);
833         for (int i = listeners.size() - 1; i >= 0; i--) {
834             ((CellEditorListener JavaDoc)listeners.get(i)).editingCanceled(ce);
835         }
836     }
837     private Vector JavaDoc getUdfs() {
838         Vector JavaDoc udfs = new Vector JavaDoc();
839         MondrianGuiDef.Schema s = this.getSchema();
840         if (s != null) {
841             MondrianGuiDef.UserDefinedFunction [] u = s.userDefinedFunctions;
842             for (int i=0; i<u.length; i++){
843                 if (!(u[i].className== null || udfs.contains(u[i].className))) {
844                     udfs.add(u[i].className);
845                 }
846             }
847         }
848
849         return udfs;
850     }
851
852     private Vector JavaDoc getFormatStrings() {
853         Vector JavaDoc fs = new Vector JavaDoc();
854         MondrianGuiDef.Schema s = this.getSchema();
855         if (s != null) {
856             MondrianGuiDef.Cube [] c = s.cubes;
857             for (int i=0; i<c.length; i++){
858                 MondrianGuiDef.Measure [] m = c[i].measures;
859                 for (int j=0; j<m.length; j++){
860                     if (!(m[j].formatString == null || fs.contains(m[j].formatString))) {
861                         fs.add(m[j].formatString);
862                     }
863                 }
864             }
865         }
866         return fs;
867     }
868
869     private MondrianGuiDef.Schema getSchema() {
870         SchemaExplorer se = this.getSchemaExplorer();
871         return (se ==null)?null:se.getSchema();
872     }
873
874     private Object JavaDoc getParentObject() {
875         SchemaExplorer se = this.getSchemaExplorer();
876         if (se != null) {
877             Object JavaDoc po = se.getParentObject();
878             return po;
879         }
880         return null;
881     }
882
883     private SchemaExplorer getSchemaExplorer() {
884         SchemaExplorer se =null;
885         for (int i = listeners.size() - 1; i >= 0; i--) {
886             CellEditorListener JavaDoc cel = ((CellEditorListener JavaDoc)listeners.get(i));
887             if (cel instanceof SchemaExplorer) {
888                 se = (SchemaExplorer) cel;
889                 break;
890             }
891         }
892         return se;
893     }
894
895     private Vector JavaDoc getSource() { //shared dimensions in schema
896
Vector JavaDoc source = new Vector JavaDoc();
897         MondrianGuiDef.Schema s = this.getSchema();
898         if (s != null) {
899             MondrianGuiDef.Dimension [] u = s.dimensions;
900             for (int i=0; i<u.length; i++){
901                 source.add(u[i].name);
902             }
903         }
904         return source;
905     }
906
907     private Vector JavaDoc getCubes() {
908         Vector JavaDoc source = new Vector JavaDoc();
909         //===source.add(noSelect);
910
MondrianGuiDef.Schema s = this.getSchema();
911         if (s != null) {
912             MondrianGuiDef.Cube [] u = s.cubes;
913             for (int i=0; i<u.length; i++){
914                 source.add(u[i].name);
915             }
916         }
917         return source;
918     }
919
920     private void generatePrimaryKeyTables(Object JavaDoc relation, Vector JavaDoc v) {
921         if (relation == null) {
922             return;
923         }
924         if (relation instanceof MondrianGuiDef.Table) {
925             String JavaDoc sname = ((MondrianGuiDef.Table) relation).schema;
926             v.add( ((sname==null || sname.equals(""))?"":sname+ "->") + ((MondrianGuiDef.Table) relation).name);
927             return;
928         }
929         MondrianGuiDef.Join currentJoin = (MondrianGuiDef.Join)relation;
930         generatePrimaryKeyTables(currentJoin.left, v);
931         generatePrimaryKeyTables(currentJoin.right, v);
932         return ;
933     }
934
935     private Vector JavaDoc getDimensions() {
936         Vector JavaDoc dims = new Vector JavaDoc();
937         Object JavaDoc po = getParentObject() ; //cubegrant
938
if (po != null) {
939             MondrianGuiDef.CubeGrant parent = (MondrianGuiDef.CubeGrant) po;
940             if (! (parent.cube == null || parent.cube.equals(""))) {
941                 MondrianGuiDef.Schema s = getSchema();
942                 if (s != null) {
943                     for (int i=0; i<s.cubes.length ; i++) {
944                         if (s.cubes[i].name.equals(parent.cube)) {
945                             for (int j=0; j<s.cubes[i].dimensions.length; j++) {
946                                 dims.add(s.cubes[i].dimensions[j].name);
947                             }
948                             break;
949                         }
950                     }
951                 }
952             }
953         }
954         return dims;
955     }
956
957     private String JavaDoc cacheCube = "";
958     private String JavaDoc cacheHierarchy = "";
959     private Vector JavaDoc hlevels = new Vector JavaDoc();
960
961     private Vector JavaDoc getLevels(String JavaDoc hierarchy) {
962         if (! (hierarchy == null || hierarchy.equals(""))) {
963             if (hierarchy.startsWith("[") && hierarchy.endsWith("]")) {
964                 hierarchy = hierarchy.substring(1, hierarchy.length()-1);
965             }
966             Object JavaDoc po = getParentObject() ; //cubegrant
967
if (po != null) {
968                 MondrianGuiDef.CubeGrant parent = (MondrianGuiDef.CubeGrant) po;
969                 if (! (parent.cube == null || parent.cube.equals(""))) {
970                     if (cacheCube.equals(parent.cube) && cacheHierarchy.equals(hierarchy)) {
971                         return hlevels;
972                     } else {
973                         hlevels = new Vector JavaDoc();
974                         cacheCube = parent.cube;
975                         cacheHierarchy = hierarchy;
976                         MondrianGuiDef.Schema s = getSchema();
977                         if (s != null) {
978                             // search for cube in schema
979
for (int i=0; i<s.cubes.length ; i++) {
980                                 if (s.cubes[i].name.equals(parent.cube)) {
981                                     // serach for hierarchy in cube
982
for (int j=0; j<s.cubes[i].dimensions.length; j++) {
983                                         if (s.cubes[i].dimensions[j].name.equals(hierarchy)) {
984                                             MondrianGuiDef.Dimension d = null;
985                                             if (s.cubes[i].dimensions[j] instanceof MondrianGuiDef.Dimension) {
986                                                d = (MondrianGuiDef.Dimension)s.cubes[i].dimensions[j];
987                                             } else {
988                                                MondrianGuiDef.DimensionUsage d2 = (MondrianGuiDef.DimensionUsage)s.cubes[i].dimensions[j];
989                                                for (int m=0; m< s.dimensions.length; m++) {
990                                                     if (s.dimensions[m].name.equals(d2.source)) {
991                                                         d = s.dimensions[m];
992                                                         break;
993                                                     }
994                                                }
995                                             }
996                                             if (d.hierarchies[0] !=null) {
997                                                 for (int k=0; k<d.hierarchies[0].levels.length; k++ ) {
998                                                     hlevels.add(d.hierarchies[0].levels[k].name);
999                                                 }
1000                                            }
1001                                            break;
1002                                        }
1003                                    }
1004                                    break;
1005                                }
1006                            }
1007                        }
1008                    }
1009                }
1010            }
1011        }
1012        return hlevels;
1013    }
1014
1015    /* // Not required for time being
1016    class MapComboBoxModel extends DefaultComboBoxModel {
1017        Map objectsMap;
1018
1019        public MapComboBoxModel(Map m) {
1020            super(m.keySet().toArray());
1021            objectsMap = m;
1022        }
1023
1024        public Object getListEditorValue() {
1025            return objectsMap.get(super.getSelectedItem());
1026        }
1027    }
1028     */

1029}
1030
1031
1032// End SchemaPropertyCellEditor.java
1033
Popular Tags