001    /*
002    // $Id: //open/mondrian/src/main/mondrian/gui/SchemaPropertyCellRenderer.java#12 $
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) 2002-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 javax.swing.*;
014    import java.awt.*;
015    import javax.swing.border.*;
016    import javax.swing.table.TableCellRenderer;
017    
018    /**
019     *
020     * @author  sean
021     * @version $Id: //open/mondrian/src/main/mondrian/gui/SchemaPropertyCellRenderer.java#12 $
022     */
023    public class SchemaPropertyCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
024    
025        Workbench workbench;
026    
027        JLabel stringRenderer;
028        JCheckBox booleanRenderer;
029        JLabel integerRenderer;
030        JTable tableRenderer;
031        JComboBox listRenderer;
032    
033        JComboBox relationList;  // Join, Table
034        JTable relationTable;
035        JPanel relationRenderer,rlPanel;;
036        JScrollPane jScrollPaneT;
037        /*  all objects of this class will use this color value to render attribute column
038         *  this value is initialized by SchemaExplorer to the scrollpane background color value.
039         */
040        static public Color attributeBackground ;
041    
042        /** Creates a new instance of SchemaPropertyCellRenderer */
043        public SchemaPropertyCellRenderer(Workbench wb) {
044    
045            workbench = wb;
046    
047            super.setBackground(attributeBackground);
048    
049            stringRenderer = new JLabel();
050            stringRenderer.setFont(Font.decode("Dialog"));
051    
052            booleanRenderer = new JCheckBox();
053            booleanRenderer.setBackground(Color.white);
054            integerRenderer = new JLabel();
055            integerRenderer.setHorizontalAlignment(JTextField.RIGHT);
056            integerRenderer.setFont(Font.decode("Courier"));
057    
058    
059            listRenderer = new JComboBox(MondrianGuiDef.Measure._aggregator_values);
060            listRenderer.setMaximumSize(stringRenderer.getMaximumSize());
061            listRenderer.setFont(Font.decode("Dialog"));
062            listRenderer.setBackground(Color.white);
063            //listRenderer.setModel(new ComboBoxModel());
064            listRenderer.setBorder(new EmptyBorder(0, 0, 0, 0)); //super.noFocusBorder);
065            listRenderer.setRenderer(new ListRenderer(listRenderer.getRenderer()));
066    
067            /*
068            relationListRenderer = new JComboBox(new String[] {"Join", "Table"});
069            relationListRenderer.setMaximumSize(stringRenderer.getMaximumSize());
070            relationListRenderer.setFont(Font.decode("Dialog"));
071            relationListRenderer.setBackground(Color.white);
072             */
073            relationRenderer = new JPanel();
074    
075            rlPanel = new JPanel();
076            relationList = new JComboBox(new String[] {workbench.getResourceConverter().getString("schemaPropertyCellRenderer.join","Join"),
077                                                        workbench.getResourceConverter().getString("schemaPropertyCellRenderer.table","Table")});
078            relationList.setMaximumSize(new Dimension(55,22));
079            relationList.setPreferredSize(new Dimension(55,22));
080            relationList.setMinimumSize(new Dimension(55,22));
081            relationList.setFont(Font.decode("Dialog"));
082            relationList.setBackground(Color.white);
083    
084            relationTable = new JTable();
085            relationTable.setBackground(new java.awt.Color(255, 204, 204));
086            relationTable.setTableHeader(null); // to remove table headers 'Property', 'Value''
087    
088            jScrollPaneT = new JScrollPane();
089            jScrollPaneT.setViewportBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 255), 2));
090            //jScrollPaneT.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
091            jScrollPaneT.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
092            jScrollPaneT.setViewportView(relationTable);
093    
094            relationRenderer.setLayout(new BorderLayout());
095            rlPanel.add(relationList);
096            relationRenderer.add(rlPanel,java.awt.BorderLayout.WEST);
097            relationRenderer.add(jScrollPaneT,java.awt.BorderLayout.CENTER);
098    
099    
100            relationRenderer.setBackground(Color.white);
101    
102            //relationRenderer.add(jScrollPaneT,java.awt.BorderLayout.CENTER);
103    
104            //JPanel relPanel = new JPanel();  // default flowlayout
105            //relPanel.add(relationList);
106            //relPanel.add(jScrollPaneT);
107            //relationRenderer.add(relationTable);
108            //relationRenderer.add(relPanel,java.awt.BorderLayout.CENTER);
109            //relationRenderer.add(jScrollPaneT);
110    
111            tableRenderer = new JTable();
112        }
113    
114        public JCheckBox getBooleanRenderer() {
115            return booleanRenderer;
116        }
117    
118        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
119    
120            if (column == 1) {
121    
122                /*if ((((PropertyTableModel) table.getModel()).target.getClass() == MondrianGuiDef.Measure.class) && (row==1)) {
123                    listRenderer.setSelectedItem((String)value);
124                    return listRenderer;
125                } else */
126                stringRenderer.setOpaque(false);
127                stringRenderer.setToolTipText(null);
128                stringRenderer.setBackground(Color.white);
129    
130                if (value instanceof String) {
131                    stringRenderer.setText((String)value);
132                    return stringRenderer;
133                } else if (value instanceof Boolean) {
134                    booleanRenderer.setSelected((Boolean) value);
135                    return booleanRenderer;
136                } else if (value instanceof Integer) {
137                    integerRenderer.setText(value.toString());
138                    return integerRenderer;
139                } else if (value == null) {
140                    return null;
141                } else if (value.getClass() == MondrianGuiDef.Join.class) {
142    
143                    stringRenderer.setText(generateJoinStr(value));
144    
145                    stringRenderer.setToolTipText(workbench.getResourceConverter().getString("schemaPropertyCellRenderer.selectJoinObject",
146                            "Select the Join/Table object from Schema tree to edit."));
147                    stringRenderer.setOpaque(true);
148                    stringRenderer.setBackground(Color.LIGHT_GRAY); //new java.awt.Color(184, 207, 229)
149                    return stringRenderer;
150    
151                    /* 2: Displaying Join in nested pink boxes
152                    SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
153                    relationTable.setDefaultRenderer(Object.class, spcr);
154                    PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_JOIN);
155                    relationTable.setModel(ptm);
156                    relationTable.getColumnModel().getColumn(0).setMaxWidth(100);
157                    relationTable.getColumnModel().getColumn(0).setMinWidth(100);
158                    setTableRendererHeight(relationTable, relationRenderer); //setTableRendererHeight();
159                    return relationRenderer;
160                     */
161                    /* 1: original version of displaying Join type values
162                    SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer();
163                    tableRenderer.setDefaultRenderer(Object.class, spcr);
164                    PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_JOIN);
165                    tableRenderer.setModel(ptm);
166                    return tableRenderer;
167                     **/
168                } else if (value.getClass() == MondrianGuiDef.OrdinalExpression.class) {
169                    SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer(workbench);
170                    tableRenderer.setDefaultRenderer(Object.class, spcr);
171                    //===PropertyTableModel ptm = new PropertyTableModel(value,SchemaExplorer.DEF_SQL);
172                    PropertyTableModel ptm = new PropertyTableModel(workbench, ((MondrianGuiDef.OrdinalExpression)value).expressions[0],SchemaExplorer.DEF_SQL);
173                    tableRenderer.setModel(ptm);
174                    tableRenderer.getColumnModel().getColumn(0).setMaxWidth(100);
175                    tableRenderer.getColumnModel().getColumn(0).setMinWidth(100);
176                    return tableRenderer;
177                } else if (value.getClass() == MondrianGuiDef.Formula.class) {
178                    SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer(workbench);
179                    tableRenderer.setDefaultRenderer(Object.class, spcr);
180                    PropertyTableModel ptm = new PropertyTableModel(workbench, value,SchemaExplorer.DEF_FORMULA);
181                    tableRenderer.setModel(ptm);
182                    tableRenderer.getColumnModel().getColumn(0).setMaxWidth(100);
183                    tableRenderer.getColumnModel().getColumn(0).setMinWidth(100);
184                    return tableRenderer;
185                } else if (value.getClass() == MondrianGuiDef.CalculatedMemberProperty.class) {
186                    SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer(workbench);
187                    tableRenderer.setDefaultRenderer(Object.class, spcr);
188                    PropertyTableModel ptm = new PropertyTableModel(workbench, value,SchemaExplorer.DEF_CALCULATED_MEMBER_PROPERTY);
189                    tableRenderer.setModel(ptm);
190                    tableRenderer.getColumnModel().getColumn(0).setMaxWidth(100);
191                    tableRenderer.getColumnModel().getColumn(0).setMinWidth(100);
192                    return tableRenderer;
193                } else if (value.getClass() == MondrianGuiDef.RelationOrJoin.class) {
194                    // REVIEW: Seems impossible to get here: RelationOrJoin is an
195                    // abstract class.
196                    SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer(workbench);
197                    tableRenderer.setDefaultRenderer(Object.class, spcr);
198                    PropertyTableModel ptm = new PropertyTableModel(workbench, value,SchemaExplorer.DEF_RELATION);
199                    tableRenderer.setModel(ptm);
200                    return tableRenderer;
201                } else if (value.getClass() == MondrianGuiDef.Table.class) {
202                    SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer(workbench);
203                    tableRenderer.setDefaultRenderer(Object.class, spcr);
204                    PropertyTableModel ptm = new PropertyTableModel(workbench, value,SchemaExplorer.DEF_TABLE);
205                    tableRenderer.setModel(ptm);
206                    tableRenderer.getColumnModel().getColumn(0).setMaxWidth(100);
207                    tableRenderer.getColumnModel().getColumn(0).setMinWidth(100);
208                    return tableRenderer;
209                } else if (value.getClass() == MondrianGuiDef.AggFactCount.class) {
210                    SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer(workbench);
211                    tableRenderer.setDefaultRenderer(Object.class, spcr);
212                    PropertyTableModel ptm = new PropertyTableModel(workbench, value,SchemaExplorer.DEF_AGG_FACT_COUNT);
213                    tableRenderer.setModel(ptm);
214                    tableRenderer.getColumnModel().getColumn(0).setMaxWidth(100);
215                    tableRenderer.getColumnModel().getColumn(0).setMinWidth(100);
216                    return tableRenderer;
217                } else if (value.getClass() == MondrianGuiDef.Closure.class) {
218                    SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer(workbench);
219                    tableRenderer.setDefaultRenderer(Object.class, spcr);
220                    PropertyTableModel ptm = new PropertyTableModel(workbench, value,SchemaExplorer.DEF_CLOSURE);
221                    tableRenderer.setModel(ptm);
222                    tableRenderer.getColumnModel().getColumn(0).setMaxWidth(100);
223                    tableRenderer.getColumnModel().getColumn(0).setMinWidth(100);
224                    setTableRendererHeight(tableRenderer, null);
225                    return tableRenderer;
226                } else if (value.getClass() == MondrianGuiDef.Property.class) {
227                    SchemaPropertyCellRenderer spcr = new SchemaPropertyCellRenderer(workbench);
228                    tableRenderer.setDefaultRenderer(Object.class, spcr);
229                    PropertyTableModel ptm = new PropertyTableModel(workbench, value,SchemaExplorer.DEF_PROPERTY);
230                    tableRenderer.setModel(ptm);
231                    return tableRenderer;
232                } else {
233                    return null;
234                }
235    
236            } else {
237                if (value instanceof String) {
238                    // use data from workbenchInfo.properties as tooltip when available
239                    PropertyTableModel tableModel = (PropertyTableModel) table.getModel();
240                    String className = (tableModel.target.getClass()).getName();
241                    int pos = className.lastIndexOf("$");
242                    String tooltip = null;
243                    if (pos > 0) {
244                        String tipName = (className.substring(pos + 1)) + "," +
245                            tableModel.getRowName(row);
246                        tooltip = workbench.getTooltip(tipName);
247                    }
248                    stringRenderer.setToolTipText(tooltip);
249                    stringRenderer.setText((String)value);
250                    stringRenderer.setOpaque(true);
251                    stringRenderer.setBackground(new java.awt.Color(221, 221, 221));
252                    if (isSelected && hasFocus) {
253                        table.editCellAt(row, 1);
254                    }
255                    return stringRenderer;
256                }
257            }
258            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
259    
260        }
261    
262        private String generateJoinStr(Object value) {
263            MondrianGuiDef.Join currentJoin = (MondrianGuiDef.Join)value;
264            String joinStr = "<html>" + generateLeftRightStr(currentJoin.left) + " <b>JOIN</b> " + generateLeftRightStr(currentJoin.right) + "</html>";
265            //===String joinStr = "<html>" + " <b>JOIN</b> (<blockquote>" + generateLeftRightStr(currentJoin.left) + "<br>" + generateLeftRightStr(currentJoin.right) + "<br>)</blockquote>" +"</html>";
266            return joinStr;
267        }
268    
269        private String generateLeftRightStr(Object value) {
270            MondrianGuiDef.RelationOrJoin currentObj = (MondrianGuiDef.RelationOrJoin) value;
271            if (currentObj instanceof MondrianGuiDef.Table)
272                return (((MondrianGuiDef.Table) currentObj).alias == null || ((MondrianGuiDef.Table) currentObj).alias.equals("") ? ((MondrianGuiDef.Table) currentObj).name : ((MondrianGuiDef.Table) currentObj).alias);
273            MondrianGuiDef.Join currentJoin = (MondrianGuiDef.Join)currentObj;
274            String joinStr = "(" + generateLeftRightStr(currentJoin.left) + " <b>JOIN</b> " + generateLeftRightStr(currentJoin.right) + ")";
275            //===String joinStr = " <b>JOIN</b> (<blockquote>" + generateLeftRightStr(currentJoin.left) + "<br>" + generateLeftRightStr(currentJoin.right) + "<br>)</blockquote>" ;
276            return joinStr;
277        }
278    
279        void setTableRendererHeight(JTable relationTable, JPanel relationRenderer) {
280            int tableH = 0;
281            int tableW = 0;
282            Object value = null;
283            for (int i = 0; i < relationTable.getRowCount(); i++) {
284    
285                TableCellRenderer renderer = relationTable.getCellRenderer(i, 1);
286                Component comp = renderer.getTableCellRendererComponent(relationTable, relationTable.getValueAt(i, 1), false, false, i, 1);
287                try {
288                    int height = 0;
289                    int width = 0;
290                    if (comp != null) {
291                        height = comp.getMaximumSize().height;
292                        width = comp.getMaximumSize().width;
293                        relationTable.setRowHeight(i, height);
294                    }
295    
296                    value = relationTable.getValueAt(i, 1);
297                    if (value instanceof MondrianGuiDef.RelationOrJoin) {
298                        tableH += comp.getPreferredSize().height;
299                        tableW = Math.max(tableW, comp.getPreferredSize().width + stringRenderer.getMaximumSize().width);
300                    } else if (value == null) {
301                        tableH += stringRenderer.getMaximumSize().height;
302                        tableW = Math.max(tableW, stringRenderer.getMaximumSize().width * 2);
303                    } else {
304                        tableH += height;
305                        tableW = Math.max(tableW, width * 2);
306    
307                    }
308    
309                } catch (Exception ea) {
310    
311                }
312            }
313            //===relationTable.setPreferredSize(new Dimension(200,200));  //table height  changes
314            //===relationTable.setPreferredScrollableViewportSize(new Dimension(200,200)); //scrollpane adjusts to new table height, it also changes scrollpanes' preferred size values'
315            relationTable.setPreferredSize(new Dimension(tableW,tableH));  //table height  changes
316            relationTable.setPreferredScrollableViewportSize(relationTable.getPreferredSize()); //scrollpane adjusts to new table height, it also changes scrollpanes' preferred size values'
317            //relationTable.revalidate(); // not required.
318    
319            // lets set the parent panel now
320            //jPanel1.setSize(jScrollPane1.getPreferredSize()); no, does not change green panel size
321            if (relationRenderer != null) {
322                relationRenderer.setPreferredSize(jScrollPaneT.getPreferredSize()); //good, it changes panel size
323                relationRenderer.setMaximumSize(jScrollPaneT.getPreferredSize()); //good, it changes panel size
324            }
325    
326        }
327    
328     /*
329        private void setTableRendererHeight2() {
330            for (int i = 0; i < relationTable.getRowCount(); i++) {
331                TableCellRenderer renderer = relationTable.getCellRenderer(i, 1);
332                Component comp = renderer.getTableCellRendererComponent(relationTable, relationTable.getValueAt(i, 1), false, false, i, 1);
333                try {
334                    int height = comp.getMaximumSize().height;
335                    relationTable.setRowHeight(i, height);
336                } catch (Exception ea) {
337    
338                }
339    
340            }
341        }
342      */
343    }
344    
345    // End SchemaPropertyCellRenderer.java