src/macosx/classes/sun/lwawt/LWCheckboxPeer.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  24  */
  25 
  26 
  27 package sun.lwawt;
  28 
  29 import java.awt.Checkbox;
  30 import java.awt.CheckboxGroup;
  31 import java.awt.Component;
  32 import java.awt.Dimension;
  33 import java.awt.event.ItemEvent;
  34 import java.awt.event.ItemListener;
  35 import java.awt.peer.CheckboxPeer;
  36 import java.beans.Transient;
  37 
  38 import javax.swing.JCheckBox;
  39 import javax.swing.JComponent;
  40 import javax.swing.JRadioButton;
  41 import javax.swing.JToggleButton;
  42 import javax.swing.SwingUtilities;
  43 





  44 final class LWCheckboxPeer
  45         extends LWComponentPeer<Checkbox, LWCheckboxPeer.CheckboxDelegate>
  46         implements CheckboxPeer, ItemListener {
  47 
  48     LWCheckboxPeer(final Checkbox target,
  49                    final PlatformComponent platformComponent) {
  50         super(target, platformComponent);
  51     }
  52 
  53     @Override
  54     protected CheckboxDelegate createDelegate() {
  55         return new CheckboxDelegate();
  56     }
  57 
  58     @Override
  59     protected Component getDelegateFocusOwner() {
  60         return getDelegate().getCurrentButton();
  61     }
  62 
  63     @Override
  64     void initializeImpl() {
  65         super.initializeImpl();
  66         setLabel(getTarget().getLabel());
  67         setState(getTarget().getState());
  68         setCheckboxGroup(getTarget().getCheckboxGroup());
  69     }
  70 
  71     @Override
  72     public void itemStateChanged(final ItemEvent e) {
  73         // group.setSelectedCheckbox() will repaint the component
  74         // to let LWCheckboxPeer correctly handle it we should call it
  75         // after the current event is processed
  76         SwingUtilities.invokeLater(new Runnable() {
  77             @Override
  78             public void run() {
  79                 boolean postEvent = true;


 120     @Override
 121     public void setLabel(final String label) {
 122         synchronized (getDelegateLock()) {
 123             getDelegate().setText(label);
 124         }
 125     }
 126 
 127     @Override
 128     public void setState(final boolean state) {
 129         synchronized (getDelegateLock()) {
 130             getDelegate().setSelected(state);
 131         }
 132         repaintPeer();
 133     }
 134 
 135     @Override
 136     public boolean isFocusable() {
 137         return true;
 138     }
 139 

 140     final class CheckboxDelegate extends JComponent {
 141 
 142         private final JCheckBox cb;
 143         private final JRadioButton rb;
 144 
 145         CheckboxDelegate() {
 146             super();
 147             cb = new JCheckBox() {
 148                 @Override
 149                 public boolean hasFocus() {
 150                     return getTarget().hasFocus();
 151                 }
 152             };
 153             rb = new JRadioButton() {
 154                 @Override
 155                 public boolean hasFocus() {
 156                     return getTarget().hasFocus();
 157                 }
 158             };
 159             setLayout(null);


   1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  24  */
  25 
  26 
  27 package sun.lwawt;
  28 
  29 import java.awt.Checkbox;
  30 import java.awt.CheckboxGroup;
  31 import java.awt.Component;
  32 import java.awt.Dimension;
  33 import java.awt.event.ItemEvent;
  34 import java.awt.event.ItemListener;
  35 import java.awt.peer.CheckboxPeer;
  36 import java.beans.Transient;
  37 
  38 import javax.swing.JCheckBox;
  39 import javax.swing.JComponent;
  40 import javax.swing.JRadioButton;
  41 import javax.swing.JToggleButton;
  42 import javax.swing.SwingUtilities;
  43 
  44 /**
  45  * Lightweight implementation of {@link CheckboxPeer}. Delegates most of the
  46  * work to the {@link JCheckBox} and {@link JRadioButton}, which are placed
  47  * inside an empty {@link JComponent}.
  48  */
  49 final class LWCheckboxPeer
  50         extends LWComponentPeer<Checkbox, LWCheckboxPeer.CheckboxDelegate>
  51         implements CheckboxPeer, ItemListener {
  52 
  53     LWCheckboxPeer(final Checkbox target,
  54                    final PlatformComponent platformComponent) {
  55         super(target, platformComponent);
  56     }
  57 
  58     @Override
  59     CheckboxDelegate createDelegate() {
  60         return new CheckboxDelegate();
  61     }
  62 
  63     @Override
  64     Component getDelegateFocusOwner() {
  65         return getDelegate().getCurrentButton();
  66     }
  67 
  68     @Override
  69     void initializeImpl() {
  70         super.initializeImpl();
  71         setLabel(getTarget().getLabel());
  72         setState(getTarget().getState());
  73         setCheckboxGroup(getTarget().getCheckboxGroup());
  74     }
  75 
  76     @Override
  77     public void itemStateChanged(final ItemEvent e) {
  78         // group.setSelectedCheckbox() will repaint the component
  79         // to let LWCheckboxPeer correctly handle it we should call it
  80         // after the current event is processed
  81         SwingUtilities.invokeLater(new Runnable() {
  82             @Override
  83             public void run() {
  84                 boolean postEvent = true;


 125     @Override
 126     public void setLabel(final String label) {
 127         synchronized (getDelegateLock()) {
 128             getDelegate().setText(label);
 129         }
 130     }
 131 
 132     @Override
 133     public void setState(final boolean state) {
 134         synchronized (getDelegateLock()) {
 135             getDelegate().setSelected(state);
 136         }
 137         repaintPeer();
 138     }
 139 
 140     @Override
 141     public boolean isFocusable() {
 142         return true;
 143     }
 144 
 145     @SuppressWarnings("serial")// Safe: outer class is non-serializable.
 146     final class CheckboxDelegate extends JComponent {
 147 
 148         private final JCheckBox cb;
 149         private final JRadioButton rb;
 150 
 151         CheckboxDelegate() {
 152             super();
 153             cb = new JCheckBox() {
 154                 @Override
 155                 public boolean hasFocus() {
 156                     return getTarget().hasFocus();
 157                 }
 158             };
 159             rb = new JRadioButton() {
 160                 @Override
 161                 public boolean hasFocus() {
 162                     return getTarget().hasFocus();
 163                 }
 164             };
 165             setLayout(null);