001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2006-2009 Pentaho and others
008// Copyright (C) 2006-2007 CINCOM SYSTEMS, INC.
009// All Rights Reserved.
010*/
011package mondrian.gui;
012
013import java.awt.*;
014import java.util.*;
015import java.util.List;
016import javax.swing.*;
017import javax.swing.event.CellEditorListener;
018import javax.swing.event.ChangeEvent;
019import javax.swing.tree.*;
020
021/**
022 *
023 * @author sarora
024 */
025public class SchemaTreeCellEditor
026    extends javax.swing.tree.DefaultTreeCellEditor
027{
028    private final ClassLoader myClassLoader;
029    JComboBox listEditor;
030    final List<CellEditorListener> listeners =
031        new ArrayList<CellEditorListener>();
032
033    /** Creates a new instance of SchemaTreeCellEditor */
034    public SchemaTreeCellEditor(
035        Workbench workbench,
036        JTree tree,
037        DefaultTreeCellRenderer renderer,
038        TreeCellEditor editor)
039    {
040        super(tree, renderer, editor);
041        myClassLoader = this.getClass().getClassLoader();
042    }
043
044    public Component getTreeCellEditorComponent(
045        JTree tree,
046        Object value,
047        boolean isSelected,
048        boolean expanded,
049        boolean leaf,
050        int row)
051    {
052        if (value instanceof MondrianGuiDef.RelationOrJoin) {
053            String valueClass = value.getClass().getName();
054            String simpleName[] = valueClass.split("[$.]", 0);
055
056            return super.getTreeCellEditorComponent(
057                tree,
058                simpleName[simpleName.length - 1],
059                isSelected,
060                expanded,
061                leaf,
062                row);
063        } else {
064            return null;
065        }
066    }
067
068    public boolean isCellEditable(EventObject event) {
069        return false;
070    }
071
072    protected void fireEditingStopped() {
073        ChangeEvent ce = new ChangeEvent(this);
074        for (int i = listeners.size() - 1; i >= 0; i--) {
075            listeners.get(i).editingStopped(ce);
076        }
077    }
078
079    public void addCellEditorListener(CellEditorListener l) {
080        listeners.add(l);
081    }
082
083    public void removeCellEditorListener(CellEditorListener l) {
084        listeners.remove(l);
085    }
086
087    public void setValueAt(JTree tree) {
088        String retValue;
089        MondrianGuiDef.RelationOrJoin relationObj = null;
090
091        retValue = (String) getCellEditorValue();
092        if (retValue.equals("Join")) {
093            relationObj =
094                new MondrianGuiDef.Join(
095                    "", "",
096                    new MondrianGuiDef.Table(
097                        "", "Table 1", "", null),
098                    "", "",
099                    new MondrianGuiDef.Table(
100                        "", "Table 2", "", null));
101        } else if (retValue.equals("Table")) {
102            relationObj = new MondrianGuiDef.Table("", "Table", "", null);
103        }
104
105        TreePath tpath = tree.getSelectionPath();
106        if (tpath != null) {
107            Object value = tpath.getLastPathComponent();
108            TreePath parentpath = tpath.getParentPath();
109            if (parentpath != null) {
110                Object parent = parentpath.getLastPathComponent();
111                if (parent instanceof MondrianGuiDef.Hierarchy) {
112                    ((MondrianGuiDef.Hierarchy) parent).relation = relationObj;
113                } else if (parent instanceof MondrianGuiDef.Closure) {
114                    ((MondrianGuiDef.Closure) parent).table =
115                        (MondrianGuiDef.Table)relationObj;
116                } else if (parent instanceof MondrianGuiDef.Join) {
117                    int indexOfChild =
118                        tree.getModel().getIndexOfChild(parent, value);
119                    switch (indexOfChild) {
120                    case 0:
121                        ((MondrianGuiDef.Join) parent).left = relationObj;
122                        break;
123                    case 1:
124                        ((MondrianGuiDef.Join) parent).right = relationObj;
125                        break;
126                    }
127                }
128                tree.setSelectionPath(
129                    parentpath.pathByAddingChild(relationObj));
130            }
131        }
132    }
133
134    public boolean stopCellEditing() {
135        boolean retValue;
136
137        setValueAt(super.tree);
138        retValue = super.stopCellEditing();
139        return retValue;
140    }
141}
142
143// End SchemaTreeCellEditor.java