1 /*
   2  * Copyright (c) 2004, 2012, 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 
  26 package sun.tools.jconsole.inspector;
  27 
  28 // java import
  29 import javax.swing.*;
  30 
  31 /**
  32  * This provides a wrapper to the Object class to allow it to be
  33  displayed/manipulated as a GUI object.
  34 */
  35 @SuppressWarnings("serial")
  36 public class XObject extends JLabel {
  37     private Object object;
  38     private static boolean useHashCodeRepresentation = true;
  39     public final static XObject NULL_OBJECT = new XObject("null");
  40     public XObject (Object object, Icon icon) {
  41         this(object);
  42         setIcon(icon);
  43     }
  44 
  45     public XObject (Object object) {
  46         setObject(object);
  47         setHorizontalAlignment(SwingConstants.LEFT);
  48     }
  49 
  50     public boolean equals(Object o) {
  51         try {
  52             if (o instanceof XObject) {
  53                 return object.equals(((XObject)o).getObject());
  54             }
  55         }
  56         catch (Throwable t) {
  57             System.out.println("Error comparing XObjects"+
  58                                t.getMessage());
  59         }
  60         return false;
  61     }
  62 
  63 
  64     public Object getObject() {
  65         return object;
  66     }
  67 
  68     //if true the the object.hashcode is added to the label
  69     public static void
  70         useHashCodeRepresentation(boolean useHashCodeRepresentation) {
  71         XObject.useHashCodeRepresentation = useHashCodeRepresentation;
  72     }
  73 
  74     public static boolean hashCodeRepresentation() {
  75         return useHashCodeRepresentation;
  76     }
  77 
  78     public void setObject(Object object) {
  79         this.object = object;
  80         // if the object is not  a swing component,
  81         // use default icon
  82         try {
  83             String text = null;
  84             if (object instanceof JLabel) {
  85                 setIcon(((JLabel)object).getIcon());
  86                 if (getText() != null) {
  87                     text = ((JLabel)object).getText();
  88 
  89                 }
  90             }
  91             else if (object instanceof JButton) {
  92                 setIcon(((JButton)object).getIcon());
  93                 if (getText() != null) {
  94                     text = ((JButton)object).getText();
  95                 }
  96             }
  97             else if (getText() != null) {
  98                 text = object.toString();
  99                 setIcon(IconManager.DEFAULT_XOBJECT);
 100             }
 101             if (text != null) {
 102                 if (useHashCodeRepresentation && (this != NULL_OBJECT)) {
 103                     text = text + "     ("+object.hashCode()+")";
 104                 }
 105                 setText(text);
 106             }
 107         }
 108         catch (Exception e) {
 109              System.out.println("Error setting XObject object :"+
 110                                 e.getMessage());
 111         }
 112     }
 113 }