001    /*
002    // $Id: //open/mondrian/src/main/mondrian/gui/SchemaTreeCellEditor.java#5 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2006-2008 Julian Hyde and others
007    // Copyright (C) 2006-2007 CINCOM SYSTEMS, INC.
008    // All Rights Reserved.
009    // You must accept the terms of that agreement to use this software.
010    */
011    package mondrian.gui;
012    
013    import java.awt.Component;
014    import java.awt.event.MouseEvent;
015    import java.util.ArrayList;
016    import java.util.EventObject;
017    import javax.swing.ImageIcon;
018    import javax.swing.JComboBox;
019    import javax.swing.JTree;
020    import javax.swing.event.CellEditorListener;
021    import javax.swing.event.ChangeEvent;
022    import javax.swing.tree.DefaultTreeCellRenderer;
023    import javax.swing.tree.TreeCellEditor;
024    import javax.swing.tree.TreePath;
025    
026    /**
027     *
028     * @author sarora
029     * @version $Id: //open/mondrian/src/main/mondrian/gui/SchemaTreeCellEditor.java#5 $
030     */
031    public class SchemaTreeCellEditor extends javax.swing.tree.DefaultTreeCellEditor {
032    
033        private final ClassLoader myClassLoader;
034        JComboBox listEditor;
035        ArrayList listeners;
036        //private final ResourceBundle resources;
037    
038        /** Creates a new instance of SchemaTreeCellEditor */
039        public SchemaTreeCellEditor(Workbench workbench, JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor) {
040            super(tree, renderer, editor);
041            listeners = new ArrayList();
042            myClassLoader = this.getClass().getClassLoader();
043            //resources = ResourceBundle.getBundle("mondrian.gui.resources.gui");
044            renderer.setLeafIcon(new ImageIcon(myClassLoader.getResource(workbench.getResourceConverter().getGUIReference("table"))));
045            renderer.setOpenIcon(new ImageIcon(myClassLoader.getResource(workbench.getResourceConverter().getGUIReference("join"))));
046            renderer.setClosedIcon(new ImageIcon(myClassLoader.getResource(workbench.getResourceConverter().getGUIReference("join"))));
047    
048            /*
049            listEditor = new JComboBox(new String[] {"Join", "Table"});
050            editor.addItemListener(new ItemListener() {
051                public void itemStateChanged(ItemEvent e) {
052                    //System.out.println("item state changed ="+listEditor.getSelectedItem());
053                    //if (listEditor.isDisplayable()) listEditor.setPopupVisible(false);
054                    System.out.println("Item listener called");
055                    stopCellEditing();
056                }
057            });
058             */
059    
060        }
061    
062        public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
063            Component retValue = null;
064    
065    
066            if (value instanceof MondrianGuiDef.RelationOrJoin) {
067                String valueClass = value.getClass().getName();
068                String simpleName[] = valueClass.split("[$.]",0);
069    
070                retValue = super.getTreeCellEditorComponent(tree, simpleName[simpleName.length - 1], isSelected, expanded, leaf, row);
071                /*
072                retValue.setPreferredSize(null);
073                retValue.setPreferredSize(new java.awt.Dimension(retValue.getPreferredSize().width + 1, 20)); //Do not remove this
074                retValue.setMaximumSize(new java.awt.Dimension(retValue.getPreferredSize().width + 1, 20)); //Do not remove this
075                 */
076                /*
077                if (listEditor.isDisplayable()) {
078                    listEditor.setPopupVisible(true);
079                }
080                return listEditor;
081                 */
082            }
083    
084            return retValue;
085        }
086    
087        public boolean isCellEditable(EventObject event) {
088            boolean editable;
089            //retValue = super.isCellEditable(event);
090    
091            if (event != null) {
092                if (event.getSource() instanceof JTree) {
093                    if (event instanceof MouseEvent) {
094                        TreePath path = tree.getPathForLocation(
095                                ((MouseEvent)event).getX(),
096                                ((MouseEvent)event).getY());
097                        editable = (lastPath != null && path != null &&  lastPath.equals(path));
098                        if (path != null) {
099                            Object value = path.getLastPathComponent();
100                            TreePath parentPath = path.getParentPath();
101                            Object parent = (parentPath == null ? null : parentPath.getLastPathComponent());
102                            if (value instanceof MondrianGuiDef.RelationOrJoin && (parent instanceof MondrianGuiDef.Hierarchy || parent instanceof MondrianGuiDef.RelationOrJoin)) {
103                                // editing of relation(cube fact table is not allowed
104                                //===System.out.println("Super iscelleditable="+ super.isCellEditable(event)); //editable;
105                                if (((MouseEvent)event).getClickCount() == 2) {
106                                    return true;
107                                }
108                                return false;
109                            } else {
110                                return false;
111                            }
112                        }
113                    }
114                }
115            }
116            return false;
117        }
118    
119        public Object getCellEditorValue() {
120            Object retValue;
121    
122            retValue = super.getCellEditorValue();
123            /*
124            System.out.println("Selected "+retValue);
125    
126            if (retValue.equals("Join")) {
127                return new MondrianGuiDef.Join("","",new MondrianGuiDef.Table(), "", "", new MondrianGuiDef.Table());
128            } else if (retValue.equals("Table")) {
129                return new MondrianGuiDef.Table();
130    
131            }
132            return null;
133             */
134            return retValue;
135        }
136    
137    
138        protected void fireEditingStopped() {
139            ChangeEvent ce = new ChangeEvent(this);
140            for (int i = listeners.size() - 1; i >= 0; i--) {
141                ((CellEditorListener)listeners.get(i)).editingStopped(ce);
142            }
143        }
144    
145        public void addCellEditorListener(CellEditorListener l) {
146            listeners.add(l);
147        }
148    
149        public void removeCellEditorListener(CellEditorListener l) {
150            listeners.remove(l);
151        }
152    
153        public void setValueAt(JTree tree) {
154            String retValue;
155            MondrianGuiDef.RelationOrJoin relationObj = null;
156    
157            retValue = (String) getCellEditorValue();
158            if (retValue.equals("Join")) {
159                relationObj = new MondrianGuiDef.Join("","",new MondrianGuiDef.Table("","Table 1",""), "", "", new MondrianGuiDef.Table("","Table 2",""));
160            } else if (retValue.equals("Table")) {
161                relationObj = new MondrianGuiDef.Table("","Table","");
162    
163            }
164    
165            TreePath tpath = tree.getSelectionPath();
166            if (tpath != null) {
167                Object value = tpath.getLastPathComponent();
168                TreePath parentpath = tpath.getParentPath();
169                if (parentpath != null) {
170                    Object parent = parentpath.getLastPathComponent();
171                    if (parent instanceof MondrianGuiDef.Hierarchy) {
172                        ((MondrianGuiDef.Hierarchy) parent).relation = relationObj;
173                    } else if (parent instanceof MondrianGuiDef.Closure) {
174                        ((MondrianGuiDef.Closure) parent).table = (MondrianGuiDef.Table)relationObj;
175                    } else if (parent instanceof MondrianGuiDef.Join) {
176                        int indexOfChild = tree.getModel().getIndexOfChild(parent, value);
177                        switch (indexOfChild) {
178                        case 0:
179                            ((MondrianGuiDef.Join) parent).left = relationObj;
180                            break;
181                        case 1:
182                            ((MondrianGuiDef.Join) parent).right = relationObj;
183                            break;
184                        }
185                    }
186                    tree.setSelectionPath(parentpath.pathByAddingChild(relationObj));
187                }
188            }
189        }
190    
191        public boolean stopCellEditing() {
192            boolean retValue;
193    
194            setValueAt(super.tree);
195            retValue = super.stopCellEditing();
196            return retValue;
197        }
198    }
199    
200    // End SchemaTreeCellEditor.java