KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > popupswitcher > SwitcherTableItem


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
20 package org.netbeans.swing.popupswitcher;
21
22 import javax.swing.Icon JavaDoc;
23
24 /**
25  * Represents one item in <code>SwitcherTable</class>.
26  *
27  * @see SwitcherTable
28  *
29  * @author mkrauskopf
30  */

31 public class SwitcherTableItem implements Comparable JavaDoc {
32
33     /** Item's name. Base name used by the <code>SwitcherTable</code> */
34     private String JavaDoc name;
35
36     /** Item's html-based name. */
37     private String JavaDoc htmlName;
38
39     /** Item's description. Text which can be used for arbitrary purpose. */
40     private String JavaDoc description;
41
42     /** Item's icon */
43     private Icon JavaDoc icon;
44     
45     /** Indicates whether this item is active or not */
46     private boolean active;
47     
48     /**
49      * Object to be activated. This is up to concrete <code>PopupSwitcher</code>
50      * implementation.
51      */

52     private Activatable activatable;
53     
54     /** Creates a new instance of SwitcherTableItem */
55     public SwitcherTableItem(Activatable activatable, String JavaDoc name) {
56         this(activatable, name, null);
57     }
58     
59     /** Creates a new instance of SwitcherTableItem */
60     public SwitcherTableItem(Activatable activatable, String JavaDoc name, Icon JavaDoc icon) {
61         this(activatable, name, name, icon, false);
62     }
63     
64     /** Creates a new instance of SwitcherTableItem */
65     public SwitcherTableItem(Activatable activatable, String JavaDoc name, String JavaDoc htmlName,
66             Icon JavaDoc icon, boolean active) {
67         this(activatable, name, htmlName, icon, active, null);
68     }
69     
70     /** Creates a new instance of SwitcherTableItem */
71     public SwitcherTableItem(Activatable activatable, String JavaDoc name, String JavaDoc htmlName,
72             Icon JavaDoc icon, boolean active, String JavaDoc description) {
73         this.activatable = activatable;
74         this.name = name;
75         this.htmlName = htmlName;
76         this.icon = icon;
77         this.active = active;
78         this.description = description;
79     }
80     
81     /**
82      * Calls <code>activate()</code> method of <code>Activatable</code> interface
83      * which has to be passed in a constructor.
84      *
85      * @see SwitcherTableItem.Activatable#activate
86      */

87     public void activate() {
88         activatable.activate();
89     }
90     
91     /** Returns item's name */
92     public String JavaDoc getName() {
93         return name;
94     }
95     
96     /** Returns item's html name. */
97     public String JavaDoc getHtmlName() {
98         return htmlName;
99     }
100     
101     /**
102      * Return item's description - the text which can be used for arbitrary
103      * purpose. E.g. </code>KeyboardPopupSwitcher</code> uses it for statusbar
104      * text.
105      */

106     public String JavaDoc getDescription() {
107         return description;
108     }
109     
110     /** Returns item's icon */
111     public Icon JavaDoc getIcon() {
112         return icon;
113     }
114     
115     /**
116      * Returns item's activatable object
117      */

118     public Activatable getActivatable() {
119         return activatable;
120     }
121     
122     /** Returns whether this item is active or not. */
123     public boolean isActive() {
124         return active;
125     }
126     
127     /** Returns human readable description of this item */
128     public String JavaDoc toString() {
129         return super.toString() + "[name=" + name + ", icon=" + icon + "]"; // NOI18N
130
}
131     
132     /**
133      * Returns true if the <code>name</code> and <code>activatable</code> are the
134      * same as passed one.
135      */

136     public boolean equals(Object JavaDoc o) {
137         if (o == this) {
138             return true;
139         }
140         if (o instanceof SwitcherTableItem) {
141             SwitcherTableItem item = (SwitcherTableItem) o;
142             boolean result = item.getName().equals(name) &&
143                     item.getActivatable().equals(activatable);
144             return result;
145         } else {
146             return false;
147         }
148     }
149     
150     /**
151      * Returns a hash code value for the item.
152      *
153      * @return int hashcode
154      */

155     public int hashCode() {
156         return (name == null ? 1 : name.hashCode()) * activatable.hashCode();
157     }
158     
159     /**
160      * Compares items based on theirs <code>name</code>s. Items which has
161      * null-name will be last.
162      */

163     public int compareTo(Object JavaDoc o) {
164         String JavaDoc name1 = getName();
165         String JavaDoc name2 = null;
166         if (o instanceof SwitcherTableItem) {
167             name2 = ((SwitcherTableItem) o).getName();
168         }
169         if (name2 == null) {
170             return (name1 == null ? 0 : -1);
171         } else {
172             return (name1 == null ? 1 : name1.compareToIgnoreCase(name2));
173         }
174     }
175     
176     /**
177      * This interface has to be implemented and passed to the
178      * <code>SwitcherTableItem</code> constructor.
179      */

180     public static interface Activatable {
181         /**
182          * Here should be code witch <em>activate</em> this item. The method
183          * <code>SwitcherTableItem.activate()</code> conveniently call this
184          * method. So you never need to call this method directly.
185          *
186          * @see SwitcherTableItem#activate
187          */

188         void activate();
189     }
190 }
191
Popular Tags