001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.event.ItemEvent;
023: import java.awt.event.ItemListener;
024:
025: import javax.accessibility.Accessible;
026: import javax.accessibility.AccessibleContext;
027: import javax.accessibility.AccessibleRole;
028:
029: import org.apache.harmony.x.swing.internal.nls.Messages;
030:
031: public class JToggleButton extends AbstractButton implements Accessible {
032:
033: // TODO implement
034: protected class AccessibleJToggleButton extends
035: AccessibleAbstractButton implements ItemListener {
036: public AccessibleJToggleButton() {
037: }
038:
039: public AccessibleRole getAccessibleRole() {
040: return AccessibleRole.TOGGLE_BUTTON;
041: }
042:
043: public void itemStateChanged(final ItemEvent e) {
044: throw new UnsupportedOperationException(Messages
045: .getString("swing.27")); //$NON-NLS-1$
046: }
047: };
048:
049: public static class ToggleButtonModel extends DefaultButtonModel {
050:
051: @Override
052: public void setPressed(final boolean pressed) {
053: boolean oldPressed = isPressed();
054: if (oldPressed != pressed && !pressed && isArmed()) {
055: setSelected(!isSelected());
056: }
057: super .setPressed(pressed);
058: }
059:
060: @Override
061: public void setSelected(boolean selected) {
062: // The method changed according to HARMONY-4658.
063: // Now super.setSelected(selected) divided if group!=null
064: if (group != null) {
065:
066: if (group.getSelection() == this ) {
067: return;
068: }
069:
070: toggleState(SELECTED);
071:
072: if (selected) {
073: group.setSelected(this , true);
074: }
075:
076: int state = selected ? ItemEvent.SELECTED
077: : ItemEvent.DESELECTED;
078: ItemEvent event = new ItemEvent(this ,
079: ItemEvent.ITEM_STATE_CHANGED, this , state);
080: fireItemStateChanged(event);
081:
082: } else {
083: super .setSelected(selected);
084: }
085: }
086: }
087:
088: private static final String UI_CLASS_ID = "ToggleButtonUI";
089:
090: public JToggleButton(final String text, final Icon icon,
091: final boolean selected) {
092: setModel(new ToggleButtonModel());
093: setSelected(selected);
094: init(text, icon);
095: }
096:
097: public JToggleButton(final String text, final Icon icon) {
098: this (text, icon, false);
099: }
100:
101: public JToggleButton(final Icon icon, final boolean selected) {
102: this (null, icon, selected);
103: }
104:
105: public JToggleButton(final Icon icon) {
106: this (null, icon, false);
107: }
108:
109: public JToggleButton(final Action action) {
110: setModel(new ToggleButtonModel());
111: setAction(action);
112: init(getText(), getIcon());
113: }
114:
115: public JToggleButton(final String text, final boolean selected) {
116: this (text, null, selected);
117: }
118:
119: public JToggleButton(final String text) {
120: this (text, null, false);
121: }
122:
123: public JToggleButton() {
124: this (null, null, false);
125: }
126:
127: public AccessibleContext getAccessibleContext() {
128: return (accessibleContext == null) ? (accessibleContext = new AccessibleJToggleButton())
129: : accessibleContext;
130: }
131:
132: public String getUIClassID() {
133: return UI_CLASS_ID;
134: }
135: }
|