src/jdk.jconsole/share/classes/sun/tools/jconsole/inspector/XTextField.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
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2004, 2013, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This list implements the drag and drop functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
@SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class XTextField extends JPanel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    implements DocumentListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
               ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private XObject selectedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    protected JTextField textField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static boolean allowNullSelection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected final static int COMPATIBLE_VALUE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    protected final static int CURRENT_VALUE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected final static int NULL_VALUE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private JButton button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private XOperations operation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    //used in XTestFieldEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public XTextField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        super(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        add(textField = new JTextField(),BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        textField.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public XTextField(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this(value,value.toString().length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public XTextField(Object value, int colWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this(value,value.getClass(),colWidth, true, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public XTextField(Object value,
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    71
                      Class<?> expectedClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                      int colWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                      boolean isCallable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                      JButton button,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                      XOperations operation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        super(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.button = button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.operation = operation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        add(textField = new JTextField(value.toString(),colWidth),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if(isCallable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            textField.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        boolean fieldEditable = Utils.isEditableType(expectedClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (fieldEditable && isCallable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            textField.setEditable(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            textField.setEditable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static void setNullSelectionAllowed(boolean allowNullSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        XTextField.allowNullSelection = allowNullSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static boolean getNullSelectionAllowed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return allowNullSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   101
    protected void init(Object value, Class<?> expectedClass) {
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   102
         boolean fieldEditable =  Utils.isEditableType(expectedClass.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        clearObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            textField.setText(value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            //null String value for the moment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            textField.setText("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        textField.setToolTipText(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (fieldEditable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (!textField.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                textField.setEditable(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (textField.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                textField.setEditable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private synchronized void clearObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        textField.getDocument().removeDocumentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        selectedObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        setDefaultColors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private synchronized void setDefaultColors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        //  if (fore != null) textField.setForeground(fore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // if (back != null)  textField.setBackground(back);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void setHorizontalAlignment(int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        textField.setHorizontalAlignment(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    //can be overwritten
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    protected JMenuItem buildJMenuItem(XObject xobject, int valueType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (valueType == COMPATIBLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return new JMenuItem(xobject.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        else if (valueType == CURRENT_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return new JMenuItem("> "+xobject.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        else if (valueType == NULL_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return new JMenuItem("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // ACTIONLISTENER IMPLEMENTATION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (e.getSource() instanceof JTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if(operation != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                operation.performInvokeRequest(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * This method returns either the user inputted String, or an XObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * if one was dropped on the input field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (selectedObject!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (selectedObject == XObject.NULL_OBJECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                //null case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                return selectedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return  textField.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public void changedUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // the user typed something, so remove references
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   185
        // to the object that was dropped.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        clearObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public void removeUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // the user typed something, so remove references
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   191
        // to the object that was dropped.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        clearObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public void insertUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // the user typed something, so remove references
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   197
        // to the object that was dropped.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        clearObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}