Path: blob/master/src/java.desktop/macosx/classes/sun/lwawt/LWCheckboxPeer.java
41153 views
/*1* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package sun.lwawt;2728import java.awt.Checkbox;29import java.awt.CheckboxGroup;30import java.awt.Component;31import java.awt.Dimension;32import java.awt.event.ItemEvent;33import java.awt.event.ItemListener;34import java.awt.peer.CheckboxPeer;35import java.beans.Transient;3637import javax.swing.JCheckBox;38import javax.swing.JComponent;39import javax.swing.JRadioButton;40import javax.swing.JToggleButton;41import javax.swing.SwingUtilities;4243/**44* Lightweight implementation of {@link CheckboxPeer}. Delegates most of the45* work to the {@link JCheckBox} and {@link JRadioButton}, which are placed46* inside an empty {@link JComponent}.47*/48final class LWCheckboxPeer49extends LWComponentPeer<Checkbox, LWCheckboxPeer.CheckboxDelegate>50implements CheckboxPeer, ItemListener {5152LWCheckboxPeer(final Checkbox target,53final PlatformComponent platformComponent) {54super(target, platformComponent);55}5657@Override58CheckboxDelegate createDelegate() {59return new CheckboxDelegate();60}6162@Override63Component getDelegateFocusOwner() {64return getDelegate().getCurrentButton();65}6667@Override68void initializeImpl() {69super.initializeImpl();70setLabel(getTarget().getLabel());71setState(getTarget().getState());72setCheckboxGroup(getTarget().getCheckboxGroup());73}7475@Override76public void itemStateChanged(final ItemEvent e) {77// group.setSelectedCheckbox() will repaint the component78// to let LWCheckboxPeer correctly handle it we should call it79// after the current event is processed80SwingUtilities.invokeLater(new Runnable() {81@Override82public void run() {83boolean postEvent = true;84final CheckboxGroup group = getTarget().getCheckboxGroup();85if (group != null) {86if (e.getStateChange() == ItemEvent.SELECTED) {87if (group.getSelectedCheckbox() != getTarget()) {88group.setSelectedCheckbox(getTarget());89} else {90postEvent = false;91}92} else {93postEvent = false;94if (group.getSelectedCheckbox() == getTarget()) {95// Don't want to leave the group with no selected96// checkbox.97getTarget().setState(true);98}99}100} else {101getTarget().setState(e.getStateChange()102== ItemEvent.SELECTED);103}104if (postEvent) {105postEvent(new ItemEvent(getTarget(),106ItemEvent.ITEM_STATE_CHANGED,107getTarget().getLabel(),108e.getStateChange()));109}110}111});112}113114@Override115public void setCheckboxGroup(final CheckboxGroup g) {116synchronized (getDelegateLock()) {117getDelegate().getCurrentButton().removeItemListener(this);118getDelegate().setRadioButton(g != null);119getDelegate().getCurrentButton().addItemListener(this);120}121repaintPeer();122}123124@Override125public void setLabel(final String label) {126synchronized (getDelegateLock()) {127getDelegate().setText(label);128}129}130131@Override132public void setState(final boolean state) {133synchronized (getDelegateLock()) {134getDelegate().getCurrentButton().removeItemListener(this);135getDelegate().setSelected(state);136getDelegate().getCurrentButton().addItemListener(this);137}138repaintPeer();139}140141@Override142public boolean isFocusable() {143return true;144}145146@SuppressWarnings("serial")// Safe: outer class is non-serializable.147final class CheckboxDelegate extends JComponent {148149private final JCheckBox cb;150private final JRadioButton rb;151152CheckboxDelegate() {153super();154cb = new JCheckBox() {155@Override156public boolean hasFocus() {157return getTarget().hasFocus();158}159};160rb = new JRadioButton() {161@Override162public boolean hasFocus() {163return getTarget().hasFocus();164}165};166setLayout(null);167setRadioButton(false);168add(rb);169add(cb);170}171172@Override173public void setEnabled(final boolean enabled) {174super.setEnabled(enabled);175rb.setEnabled(enabled);176cb.setEnabled(enabled);177}178179@Override180public void setOpaque(final boolean isOpaque) {181super.setOpaque(isOpaque);182rb.setOpaque(isOpaque);183cb.setOpaque(isOpaque);184}185186@Override187@Deprecated188public void reshape(final int x, final int y, final int w,189final int h) {190super.reshape(x, y, w, h);191cb.setBounds(0, 0, w, h);192rb.setBounds(0, 0, w, h);193}194195@Override196public Dimension getPreferredSize() {197return getCurrentButton().getPreferredSize();198}199200@Override201@Transient202public Dimension getMinimumSize() {203return getCurrentButton().getMinimumSize();204}205206void setRadioButton(final boolean showRadioButton) {207rb.setVisible(showRadioButton);208cb.setVisible(!showRadioButton);209}210211@Transient212JToggleButton getCurrentButton() {213return cb.isVisible() ? cb : rb;214}215216void setText(final String label) {217cb.setText(label);218rb.setText(label);219}220221void setSelected(final boolean state) {222cb.setSelected(state);223rb.setSelected(state);224}225}226}227228229