1/*
2 * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.tools.jconsole.inspector;
27
28import java.awt.Color;
29import java.awt.Component;
30import java.awt.Font;
31import javax.swing.JTable;
32import javax.swing.table.DefaultTableCellRenderer;
33import javax.swing.table.DefaultTableModel;
34import javax.swing.table.TableCellRenderer;
35
36@SuppressWarnings("serial") // JDK implementation class
37public abstract class XTable extends JTable {
38    static final int NAME_COLUMN = 0;
39    static final int VALUE_COLUMN = 1;
40    private Color defaultColor, editableColor, errorColor;
41    private Font normalFont, boldFont;
42
43    public XTable () {
44        super();
45        @SuppressWarnings("serial")
46        final TableSorter sorter = new TableSorter();
47        setModel(sorter);
48        sorter.addMouseListenerToHeaderInTable(this);
49        setRowSelectionAllowed(false);
50        setColumnSelectionAllowed(false);
51        setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
52    }
53
54    Color getDefaultColor() {
55        return defaultColor;
56    }
57
58    Color getEditableColor() {
59        return editableColor;
60    }
61
62    /**
63     * Called by TableSorter if a mouse event requests to sort the rows.
64     * @param column the column against which the rows are sorted
65     */
66    void sortRequested(int column) {
67        // This is a hook for subclasses
68    }
69
70    /**
71     * This returns the select index as the table was at initialization
72     */
73    public int getSelectedIndex() {
74        return convertRowToIndex(getSelectedRow());
75    }
76
77    /*
78     * Converts the row into index (before sorting)
79     */
80    public int convertRowToIndex(int row) {
81        if (row == -1) return row;
82        if (getModel() instanceof TableSorter) {
83            return ((TableSorter) getModel()).getIndexOfRow(row);
84        } else {
85            return row;
86        }
87    }
88
89    public void emptyTable() {
90        DefaultTableModel model = (DefaultTableModel)getModel();
91        while (model.getRowCount()>0)
92            model.removeRow(0);
93    }
94
95    public abstract boolean isTableEditable();
96    public abstract boolean isColumnEditable(int column);
97    public abstract boolean isReadable(int row);
98    public abstract boolean isWritable(int row);
99    public abstract boolean isCellError(int row, int col);
100    public abstract boolean isAttributeViewable(int row, int col);
101    public abstract void setTableValue(Object value,int row);
102    public abstract Object getValue(int row);
103    public abstract String getClassName(int row);
104    public abstract String getValueName(int row);
105
106    public boolean isReadWrite(int row) {
107        return (isReadable(row) && isWritable(row));
108    }
109
110    //JTable re-implementation
111
112    //attribute can be editable even if unavailable
113    @Override
114    public boolean isCellEditable(int row, int col) {
115        return ((isTableEditable() && isColumnEditable(col)
116                 &&  isWritable(row)
117                 && Utils.isEditableType(getClassName(row))));
118    }
119
120    //attribute can be droppable even if unavailable
121    public boolean isCellDroppable(int row, int col) {
122        return (isTableEditable() && isColumnEditable(col)
123                && isWritable(row));
124    }
125
126    //returns null, means no tool tip
127    public String getToolTip(int row, int column) {
128        return null;
129    }
130
131    /**
132     * This method sets read write rows to be blue, and other rows to be their
133     * default rendered colour.
134     */
135    @Override
136    public TableCellRenderer getCellRenderer(int row, int column) {
137        DefaultTableCellRenderer tcr =
138            (DefaultTableCellRenderer) super.getCellRenderer(row,column);
139        tcr.setToolTipText(getToolTip(row,column));
140        if (defaultColor == null) {
141            defaultColor = tcr.getForeground();
142            editableColor = Color.blue;
143            errorColor = Color.red;
144            // this sometimes happens for some reason
145            if (defaultColor == null) {
146                return tcr;
147            }
148        }
149        if (column != VALUE_COLUMN) {
150            tcr.setForeground(defaultColor);
151            return tcr;
152        }
153        if (isCellError(row,column)) {
154            tcr.setForeground(errorColor);
155        } else if (isCellEditable(row, column)) {
156            tcr.setForeground(editableColor);
157        } else {
158            tcr.setForeground(defaultColor);
159        }
160        return tcr;
161    }
162
163    @Override
164    public Component prepareRenderer(TableCellRenderer renderer,
165                                     int row, int column) {
166        Component comp = super.prepareRenderer(renderer, row, column);
167
168        if (normalFont == null) {
169            normalFont = comp.getFont();
170            boldFont = normalFont.deriveFont(Font.BOLD);
171        }
172
173        if (column == VALUE_COLUMN && isAttributeViewable(row, VALUE_COLUMN)) {
174            comp.setFont(boldFont);
175        } else {
176            comp.setFont(normalFont);
177        }
178
179        return comp;
180    }
181}
182