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