KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > TabData


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */
/*
19  * TabData.java
20  *
21  * Created on May 26, 2003, 3:54 PM
22  */

23
24 package org.netbeans.swing.tabcontrol;
25
26 import javax.swing.*;
27 import java.awt.*;
28
29 /**
30  * Class representing data needed to represent a component in a tab. While
31  * immutable to client code, changes to the data model may change the values
32  * returned by the methods of this class. TabData objects are the data
33  * component of TabDataModel.
34  * <p/>
35  * TabData objects implement their <code>equals()</code> and
36  * <code>hashCode</code> contract based on the equality of the user object and
37  * the text. The icon and the tooltip text are not considered when testing
38  * equality.
39  *
40  * @author Tim Boudreau
41  * @see TabDataModel
42  * @see DefaultTabDataModel
43  */

44 public final class TabData implements Comparable JavaDoc {
45     //Fields below are intentionally package-private, not private.
46
Icon icon;
47     String JavaDoc txt;
48     String JavaDoc tip;
49     Object JavaDoc userObject;
50
51     /**
52      * Create a new TabData object.
53      *
54      * @param userObject The object or component that should be displayed when
55      * the tab is selected. For use in TabbedContainer, this
56      * should be an instance of <code>java.awt.Component</code>
57      * @param i The icon for the tab
58      * @param caption The caption for the tab
59      * @param tooltip The tooltip for the tab
60      */

61     public TabData(Object JavaDoc userObject, Icon i, String JavaDoc caption, String JavaDoc tooltip) {
62         this.userObject = userObject;
63         icon = i;
64         txt = caption;
65         tip = tooltip;
66     }
67     
68     
69     public Object JavaDoc getUserObject() {
70         return userObject;
71     }
72
73     /**
74      * The component for the tab. Returns null if the user object is not an
75      * instance of Component.
76      *
77      * @return The component
78      */

79     public Component getComponent() {
80         if (userObject instanceof Component) {
81             return (Component) userObject;
82         } else {
83             return null;
84         }
85     }
86
87     /**
88      * The icon for the tab. Note that this method is guaranteed to return
89      * non-null - if the icon specified is null, a 0-width, 0-height icon whose
90      * paintIcon method is a no-op will be returned.
91      *
92      * @return The icon
93      */

94     public Icon getIcon() {
95         if (icon == null) {
96             return NO_ICON;
97         } else {
98             return icon;
99         }
100     }
101
102     /**
103      * The text for the tab
104      *
105      * @return The text
106      */

107     public String JavaDoc getText() {
108         return txt;
109     }
110
111     /**
112      * The tooltip for the tab
113      *
114      * @return The tooltip text
115      */

116     public String JavaDoc getTooltip() {
117         return tip;
118     }
119
120     /**
121      * Get a string representation of this object
122      *
123      * @return String representation of this object
124      */

125     public String JavaDoc toString() {
126         return txt;
127     }
128
129     /**
130      * Returns true if the text and component properties of this TabData object
131      * match the passed one. Tooltip and icon equality are not evaluated.
132      */

133     public boolean equals(Object JavaDoc o) {
134         if (o == this)
135             return true;
136         if (o instanceof TabData) {
137             TabData td = (TabData) o;
138             boolean result = td.userObject.equals(userObject)
139                     && td.txt.equals(txt);
140             return result;
141         } else {
142             return false;
143         }
144     }
145
146     /**
147      * Munges the text and component hash codes
148      */

149     public int hashCode() {
150         return (txt == null ? 0 : txt.hashCode())
151                 ^ (userObject == null ? 0 : userObject.hashCode());
152     }
153
154     /**
155      * Compares the text based on java.lang.String's implementation of
156      * Comparable.
157      */

158     public int compareTo(Object JavaDoc o) {
159         String JavaDoc arg1, arg2;
160         arg1 = getText();
161         if (o instanceof TabData) {
162             arg2 = ((TabData) o).getText();
163         } else {
164             arg2 = null;
165         }
166         if (arg2 == null) {
167             if (arg1 == null) {
168                 // both with null-name, equal
169
return 0;
170             } else {
171                 // any name before null-name
172
return 1;
173             }
174         } else {
175             if (arg1 == null) {
176                 // null-name after any name
177
return -1;
178             } else {
179                 // compare by names
180
return arg1.compareTo(arg2);
181             }
182         }
183     }
184
185     /**
186      * An empty icon to be used when null is passed for the icon - internally we
187      * don't support null icons, but Components may legally have them
188      */

189     static final Icon NO_ICON = new Icon() {
190         public int getIconHeight() {
191             return 0;
192         }
193
194         public int getIconWidth() {
195             return 0;
196         }
197
198         public void paintIcon(Component c, Graphics g, int x, int y) {
199         }
200
201         public String JavaDoc toString() {
202             return "empty icon";
203         }; //NOI18N
204
};
205 }
206
Popular Tags