001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.swing.popupswitcher;
043:
044: import javax.swing.Icon;
045:
046: /**
047: * Represents one item in <code>SwitcherTable</class>.
048: *
049: * @see SwitcherTable
050: *
051: * @author mkrauskopf
052: */
053: public class SwitcherTableItem implements Comparable {
054:
055: /** Item's name. Base name used by the <code>SwitcherTable</code> */
056: private String name;
057:
058: /** Item's html-based name. */
059: private String htmlName;
060:
061: /** Item's description. Text which can be used for arbitrary purpose. */
062: private String description;
063:
064: /** Item's icon */
065: private Icon icon;
066:
067: /** Indicates whether this item is active or not */
068: private boolean active;
069:
070: /**
071: * Object to be activated. This is up to concrete <code>PopupSwitcher</code>
072: * implementation.
073: */
074: private Activatable activatable;
075:
076: /** Creates a new instance of SwitcherTableItem */
077: public SwitcherTableItem(Activatable activatable, String name) {
078: this (activatable, name, null);
079: }
080:
081: /** Creates a new instance of SwitcherTableItem */
082: public SwitcherTableItem(Activatable activatable, String name,
083: Icon icon) {
084: this (activatable, name, name, icon, false);
085: }
086:
087: /** Creates a new instance of SwitcherTableItem */
088: public SwitcherTableItem(Activatable activatable, String name,
089: String htmlName, Icon icon, boolean active) {
090: this (activatable, name, htmlName, icon, active, null);
091: }
092:
093: /** Creates a new instance of SwitcherTableItem */
094: public SwitcherTableItem(Activatable activatable, String name,
095: String htmlName, Icon icon, boolean active,
096: String description) {
097: this .activatable = activatable;
098: this .name = name;
099: this .htmlName = htmlName;
100: this .icon = icon;
101: this .active = active;
102: this .description = description;
103: }
104:
105: /**
106: * Calls <code>activate()</code> method of <code>Activatable</code> interface
107: * which has to be passed in a constructor.
108: *
109: * @see SwitcherTableItem.Activatable#activate
110: */
111: public void activate() {
112: activatable.activate();
113: }
114:
115: /** Returns item's name */
116: public String getName() {
117: return name;
118: }
119:
120: /** Returns item's html name. */
121: public String getHtmlName() {
122: return htmlName;
123: }
124:
125: /**
126: * Return item's description - the text which can be used for arbitrary
127: * purpose. E.g. </code>KeyboardPopupSwitcher</code> uses it for statusbar
128: * text.
129: */
130: public String getDescription() {
131: return description;
132: }
133:
134: /** Returns item's icon */
135: public Icon getIcon() {
136: return icon;
137: }
138:
139: /**
140: * Returns item's activatable object
141: */
142: public Activatable getActivatable() {
143: return activatable;
144: }
145:
146: /** Returns whether this item is active or not. */
147: public boolean isActive() {
148: return active;
149: }
150:
151: /** Returns human readable description of this item */
152: public String toString() {
153: return super .toString() + "[name=" + name + ", icon=" + icon
154: + "]"; // NOI18N
155: }
156:
157: /**
158: * Returns true if the <code>name</code> and <code>activatable</code> are the
159: * same as passed one.
160: */
161: public boolean equals(Object o) {
162: if (o == this ) {
163: return true;
164: }
165: if (o instanceof SwitcherTableItem) {
166: SwitcherTableItem item = (SwitcherTableItem) o;
167: boolean result = item.getName().equals(name)
168: && item.getActivatable().equals(activatable);
169: return result;
170: } else {
171: return false;
172: }
173: }
174:
175: /**
176: * Returns a hash code value for the item.
177: *
178: * @return int hashcode
179: */
180: public int hashCode() {
181: return (name == null ? 1 : name.hashCode())
182: * activatable.hashCode();
183: }
184:
185: /**
186: * Compares items based on theirs <code>name</code>s. Items which has
187: * null-name will be last.
188: */
189: public int compareTo(Object o) {
190: String name1 = getName();
191: String name2 = null;
192: if (o instanceof SwitcherTableItem) {
193: name2 = ((SwitcherTableItem) o).getName();
194: }
195: if (name2 == null) {
196: return (name1 == null ? 0 : -1);
197: } else {
198: return (name1 == null ? 1 : name1
199: .compareToIgnoreCase(name2));
200: }
201: }
202:
203: /**
204: * This interface has to be implemented and passed to the
205: * <code>SwitcherTableItem</code> constructor.
206: */
207: public static interface Activatable {
208: /**
209: * Here should be code witch <em>activate</em> this item. The method
210: * <code>SwitcherTableItem.activate()</code> conveniently call this
211: * method. So you never need to call this method directly.
212: *
213: * @see SwitcherTableItem#activate
214: */
215: void activate();
216: }
217: }
|