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