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.modules.mobility.end2end.ui.treeview;
043:
044: import javax.swing.*;
045: import javax.swing.event.ChangeListener;
046: import javax.swing.plaf.ActionMapUIResource;
047: import java.awt.event.*;
048:
049: public class MultiStateCheckBox extends JCheckBox {
050: public static enum State {
051: SELECTED, UNSELECTED, MIXED;
052: }
053:
054: protected final MultiStateModel model;
055: private static final String PROP_PRESSED = "pressed"; //NOI18N
056: private static final String PROP_RELEASED = "released"; //NOI18N
057:
058: public MultiStateCheckBox(String text, State initial) {
059: super (text);
060: super .addMouseListener(new MouseAdapter() {
061: public void mousePressed(final MouseEvent e) {
062: grabFocus();
063: model.nextState();
064: }
065: });
066: ActionMap map = new ActionMapUIResource();
067: map.put(PROP_PRESSED, new AbstractAction() { //NOI18N
068: public void actionPerformed(final ActionEvent e) {
069: grabFocus();
070: model.nextState();
071: }
072: });
073: map.put(PROP_RELEASED, null); //NOI18N
074: SwingUtilities.replaceUIActionMap(this , map);
075: // set the model to the adapted model
076: model = new MultiStateModel(getModel());
077: setModel(model);
078: setState(initial);
079: }
080:
081: public MultiStateCheckBox(String text) {
082: this (text, null);
083: }
084:
085: public MultiStateCheckBox() {
086: this (null);
087: }
088:
089: public void addMouseListener(final MouseListener l) {
090: }
091:
092: final public void setState(final State state) {
093: model.setState(state);
094: }
095:
096: public State getState() {
097: return model.getState();
098: }
099:
100: private class MultiStateModel implements ButtonModel {
101: private final ButtonModel other;
102:
103: private MultiStateModel(ButtonModel other) {
104: this .other = other;
105: }
106:
107: protected void setState(final State state) {
108: if (state == State.MIXED) {
109: other.setArmed(true);
110: setPressed(true);
111: setSelected(true);
112: } else if (state == State.SELECTED) {
113: other.setArmed(false);
114: setPressed(false);
115: setSelected(true);
116: } else {
117: other.setArmed(false);
118: setPressed(false);
119: setSelected(false);
120: }
121: }
122:
123: protected State getState() {
124: if (isSelected() && !isArmed()) {
125: return State.SELECTED;
126: } else if (isSelected() && isArmed()) {
127: return State.MIXED;
128: } else {
129: return State.UNSELECTED;
130: }
131: }
132:
133: protected void nextState() {
134: setState(getState() == State.UNSELECTED ? State.SELECTED
135: : State.UNSELECTED);
136: }
137:
138: public void setArmed(final boolean b) {
139: }
140:
141: public void setEnabled(final boolean b) {
142: setFocusable(b);
143: other.setEnabled(b);
144: }
145:
146: public boolean isArmed() {
147: return other.isArmed();
148: }
149:
150: public boolean isSelected() {
151: return other.isSelected();
152: }
153:
154: public boolean isEnabled() {
155: return other.isEnabled();
156: }
157:
158: public boolean isPressed() {
159: return other.isPressed();
160: }
161:
162: public boolean isRollover() {
163: return other.isRollover();
164: }
165:
166: public void setSelected(final boolean b) {
167: other.setSelected(b);
168: }
169:
170: public void setPressed(final boolean b) {
171: other.setPressed(b);
172: }
173:
174: public void setRollover(final boolean b) {
175: other.setRollover(b);
176: }
177:
178: public void setMnemonic(final int key) {
179: other.setMnemonic(key);
180: }
181:
182: public int getMnemonic() {
183: return other.getMnemonic();
184: }
185:
186: public void setActionCommand(final String s) {
187: other.setActionCommand(s);
188: }
189:
190: public String getActionCommand() {
191: return other.getActionCommand();
192: }
193:
194: public void setGroup(final ButtonGroup group) {
195: other.setGroup(group);
196: }
197:
198: public void addActionListener(final ActionListener l) {
199: other.addActionListener(l);
200: }
201:
202: public void removeActionListener(final ActionListener l) {
203: other.removeActionListener(l);
204: }
205:
206: public void addItemListener(final ItemListener l) {
207: other.addItemListener(l);
208: }
209:
210: public void removeItemListener(final ItemListener l) {
211: other.removeItemListener(l);
212: }
213:
214: public void addChangeListener(final ChangeListener l) {
215: other.addChangeListener(l);
216: }
217:
218: public void removeChangeListener(final ChangeListener l) {
219: other.removeChangeListener(l);
220: }
221:
222: public Object[] getSelectedObjects() {
223: return other.getSelectedObjects();
224: }
225: }
226: }
|