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
28// java import
29import javax.swing.*;
30import java.util.Objects;
31
32/**
33 * This provides a wrapper to the Object class to allow it to be
34 displayed/manipulated as a GUI object.
35*/
36@SuppressWarnings("serial")
37public class XObject extends JLabel {
38    private Object object;
39    private static boolean useHashCodeRepresentation = true;
40    public final static XObject NULL_OBJECT = new XObject("null");
41    public XObject (Object object, Icon icon) {
42        this(object);
43        setIcon(icon);
44    }
45
46    public XObject (Object object) {
47        setObject(object);
48        setHorizontalAlignment(SwingConstants.LEFT);
49    }
50
51    public boolean equals(Object o) {
52        if (o instanceof XObject) {
53            return Objects.equals(object, ((XObject)o).getObject());
54        }
55        return false;
56    }
57
58    @Override
59    public int hashCode() {
60        return object.hashCode();
61    }
62
63
64    public Object getObject() {
65        return object;
66    }
67
68    //if true 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}
114