001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings;
022:
023: import java.awt.ItemSelectable;
024: import java.awt.event.ItemEvent;
025:
026: /**
027: * An implementation of a check box.
028: * This component has 3 looks: checkbox, radio button and toggle button
029: *
030: * <br>
031: * <b>This class is thread safe.</b>
032: **/
033: public class WingCheckBox extends WingButton implements ItemSelectable {
034: protected int look;
035: protected boolean selected;
036: protected RadioGroup group;
037:
038: protected Style stOnNormal;
039: protected Style stOnPressed;
040: protected Style stOnFocused;
041: protected Style stOnHover;
042: protected Style stOnDisabled;
043:
044: /**
045: * Creates a check box with no label and look CHECKBOX.
046: */
047: public WingCheckBox() {
048: this (CHECKBOX);
049: }
050:
051: /**
052: * Creates a check box with no label and specified look.
053: * WingCheckBox.CHECKBOX, WingCheckBox.RADIO and WingCheckBox.TOGGLE
054: * is allowed.
055: * @param look look of the checkbox
056: */
057: public WingCheckBox(int look) {
058: this (null, look);
059: }
060:
061: /**
062: * Creates a check box with label <code>label</code> and look CHECKBOX.
063: * @param label label
064: */
065: public WingCheckBox(Object label) {
066: this (label, CHECKBOX);
067: }
068:
069: /**
070: * Creates a check box with label <code>label</code> and specified look.
071: * WingCheckBox.CHECKBOX, WingCheckBox.RADIO and WingCheckBox.TOGGLE
072: * is allowed.
073: * @param look look of the checkbox
074: * @param label label
075: */
076: public WingCheckBox(Object label, int look) {
077: super (label);
078: this .look = look;
079: if (look != TOGGLE)
080: setHorizontalAlignment(LEFT);
081: }
082:
083: /**
084: * Loads skin resources.<br>
085: * <pre>
086: * styles:
087: * [optional styleID.]<look>.normal
088: * [optional styleID.]<look>.pressed
089: * [optional styleID.]<look>.focused
090: * [optional styleID.]<look>.hover
091: * [optional styleID.]<look>.disabled
092: * [optional styleID.]<look>.on.normal
093: * [optional styleID.]<look>.on.pressed
094: * [optional styleID.]<look>.on.focused
095: * [optional styleID.]<look>.on.hover
096: * [optional styleID.]<look>.on.disabled
097: *
098: * where <look>= "checkbox" or "radio" or "button"
099: * </pre>
100: * @see Style
101: * @see WingSkin
102: */
103: public void loadSkin() {
104: String type = (look == CHECKBOX) ? "checkbox"
105: : (look == RADIO) ? "radio" : "button";
106:
107: String idt = WingSkin.catKeys(styleId, type);
108:
109: stNormal = WingSkin.getStyle(idt, null, NORMAL, null);
110: stPressed = WingSkin.getStyle(idt, null, PRESSED, stNormal);
111: stFocused = WingSkin.getStyle(idt, null, FOCUSED, stNormal);
112: stHover = WingSkin.getStyle(idt, null, HOVER, stNormal);
113: stDisabled = WingSkin.getStyle(idt, null, DISABLED, stNormal);
114:
115: stOnNormal = WingSkin
116: .getStyle(idt, null, ON | NORMAL, stNormal);
117: stOnPressed = WingSkin.getStyle(idt, null, ON | PRESSED,
118: stOnNormal);
119: stOnFocused = WingSkin.getStyle(idt, null, ON | FOCUSED,
120: stOnNormal);
121: stOnHover = WingSkin
122: .getStyle(idt, null, ON | HOVER, stOnNormal);
123: stOnDisabled = WingSkin.getStyle(idt, null, ON | DISABLED,
124: stDisabled);
125: }
126:
127: /**
128: * Returns the style representing current checkbox state
129: * @return current style representing checkbox state
130: */
131: public Style getStyle() {
132: return (selected) ? ((!isEnabled()) ? stOnDisabled
133: : ((pressed && hover) || keyPressed) ? stOnPressed
134: : (hover) ? stOnHover : (focused) ? stOnFocused
135: : stOnNormal)
136: : ((!isEnabled()) ? stDisabled
137: : ((pressed && hover) || keyPressed) ? stPressed
138: : (hover) ? stHover
139: : (focused) ? stFocused
140: : stNormal);
141: }
142:
143: protected void buttonAction(int eventModifiers) {
144: if (group == null || !selected)
145: setSelected(!selected, true);
146: }
147:
148: /**
149: * Sets the selected state of the checkbox.
150: * @param select true if the checkbox is selected, otherwise false
151: **/
152: public void setSelected(boolean select) {
153: setSelected(select, false);
154: }
155:
156: protected void setSelected(boolean select, boolean notify) {
157: boolean oldSelected = selected;
158: selected = select;
159: if (oldSelected != select) {
160: repaint();
161: if (group != null)
162: group.setSelected(this , select);
163: if (notify)
164: wingProcessItemEvent(new ItemEvent(this ,
165: ItemEvent.ITEM_STATE_CHANGED, this ,
166: (select) ? ItemEvent.SELECTED
167: : ItemEvent.DESELECTED));
168: }
169: }
170:
171: /**
172: * Returns the state of the checkbox. True if the
173: * checkbox is selected, false if it's not.
174: * @return true if the checkbox is selected, otherwise false
175: **/
176: public boolean isSelected() {
177: return selected;
178: }
179:
180: /**
181: * Returns an array (length 1) containing the label or
182: * <code>null</code> if the checkbox is not selected.
183: *
184: * @return an array containing 1 Object: the label of the checkbox,
185: * if the item is selected; otherwise <code>null</code>
186: **/
187: public Object[] getSelectedObjects() {
188: if (selected) {
189: Object[] items = { label };
190: return items;
191: }
192: return null;
193: }
194:
195: }
|