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