J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package sun.tools.jconsole.inspector; |
| 26 | |
| 27 | // java import |
| 28 | import javax.swing.*; |
| 29 | |
| 30 | // |
| 31 | |
| 32 | // java import |
| 33 | import java.io.*; |
| 34 | import java.awt.*; |
| 35 | import java.awt.dnd.*; |
| 36 | import java.awt.datatransfer.*; |
| 37 | import java.net.*; |
| 38 | // |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * This provides a wrapper to the Object class to allow it to be |
| 43 | displayed/manipulated as a GUI object. |
| 44 | */ |
| 45 | @SuppressWarnings("serial") |
| 46 | public class XObject extends JLabel { |
| 47 | private Object object; |
| 48 | private static boolean useHashCodeRepresentation = true; |
| 49 | public final static XObject NULL_OBJECT = new XObject("null"); |
| 50 | public XObject (Object object, Icon icon) { |
| 51 | this(object); |
| 52 | setIcon(icon); |
| 53 | } |
| 54 | |
| 55 | public XObject (Object object) { |
| 56 | setObject(object); |
| 57 | setHorizontalAlignment(SwingConstants.LEFT); |
| 58 | } |
| 59 | |
| 60 | public boolean equals(Object o) { |
| 61 | try { |
| 62 | if (o instanceof XObject) { |
| 63 | return object.equals(((XObject)o).getObject()); |
| 64 | } |
| 65 | } |
| 66 | catch (Throwable t) { |
| 67 | System.out.println("Error comparing XObjects"+ |
| 68 | t.getMessage()); |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | public Object getObject() { |
| 75 | return object; |
| 76 | } |
| 77 | |
| 78 | //if true the the object.hashcode is added to the label |
| 79 | public static void |
| 80 | useHashCodeRepresentation(boolean useHashCodeRepresentation) { |
| 81 | XObject.useHashCodeRepresentation = useHashCodeRepresentation; |
| 82 | } |
| 83 | |
| 84 | public static boolean hashCodeRepresentation() { |
| 85 | return useHashCodeRepresentation; |
| 86 | } |
| 87 | |
| 88 | public void setObject(Object object) { |
| 89 | this.object = object; |
| 90 | // if the object is not a swing component, |
| 91 | // use default icon |
| 92 | try { |
| 93 | String text = null; |
| 94 | if (object instanceof JLabel) { |
| 95 | setIcon(((JLabel)object).getIcon()); |
| 96 | if (getText() != null) { |
| 97 | text = ((JLabel)object).getText(); |
| 98 | |
| 99 | } |
| 100 | } |
| 101 | else if (object instanceof JButton) { |
| 102 | setIcon(((JButton)object).getIcon()); |
| 103 | if (getText() != null) { |
| 104 | text = ((JButton)object).getText(); |
| 105 | } |
| 106 | } |
| 107 | else if (getText() != null) { |
| 108 | text = object.toString(); |
| 109 | setIcon(IconManager.DEFAULT_XOBJECT); |
| 110 | } |
| 111 | if (text != null) { |
| 112 | if (useHashCodeRepresentation && (this != NULL_OBJECT)) { |
| 113 | text = text + " ("+object.hashCode()+")"; |
| 114 | } |
| 115 | setText(text); |
| 116 | } |
| 117 | } |
| 118 | catch (Exception e) { |
| 119 | System.out.println("Error setting XObject object :"+ |
| 120 | e.getMessage()); |
| 121 | } |
| 122 | } |
| 123 | } |