Frames | No Frames |
1: /* SwingCheckboxPeer.java -- A Swing based peer for AWT checkboxes 2: Copyright (C) 2007 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: package gnu.java.awt.peer.swing; 39: 40: import java.awt.Button; 41: import java.awt.Checkbox; 42: import java.awt.CheckboxGroup; 43: import java.awt.Container; 44: import java.awt.Graphics; 45: import java.awt.Image; 46: import java.awt.Label; 47: import java.awt.Point; 48: import java.awt.event.ActionEvent; 49: import java.awt.event.ActionListener; 50: import java.awt.event.FocusEvent; 51: import java.awt.event.ItemEvent; 52: import java.awt.event.ItemListener; 53: import java.awt.event.KeyEvent; 54: import java.awt.event.MouseEvent; 55: import java.awt.peer.CheckboxPeer; 56: 57: import javax.swing.JCheckBox; 58: import javax.swing.JComponent; 59: import javax.swing.JLabel; 60: import javax.swing.JToggleButton; 61: 62: /** 63: * A CheckboxPeer implementation that is backed by the Swing JCheckBox. 64: */ 65: public class SwingCheckboxPeer extends SwingComponentPeer implements 66: CheckboxPeer { 67: 68: /** 69: * A spezialized Swing checkbox used to paint the checkbox for the 70: * AWT checkbox. 71: */ 72: private class SwingCheckbox 73: extends JCheckBox 74: implements SwingComponent 75: { 76: Checkbox checkbox; 77: 78: SwingCheckbox(Checkbox checkbox) 79: { 80: this.checkbox = checkbox; 81: } 82: 83: /** 84: * Returns this checkbox. 85: * 86: * @return <code>this</code> 87: */ 88: public JComponent getJComponent() 89: { 90: return this; 91: } 92: 93: /** 94: * Handles mouse events by forwarding it to 95: * <code>processMouseEvent()</code>. 96: * 97: * @param ev the mouse event 98: */ 99: public void handleMouseEvent(MouseEvent ev) 100: { 101: ev.setSource(this); 102: processMouseEvent(ev); 103: } 104: 105: /** 106: * Handles mouse motion events by forwarding it to 107: * <code>processMouseMotionEvent()</code>. 108: * 109: * @param ev the mouse motion event 110: */ 111: public void handleMouseMotionEvent(MouseEvent ev) 112: { 113: ev.setSource(this); 114: processMouseMotionEvent(ev); 115: } 116: 117: /** 118: * Handles key events by forwarding it to <code>processKeyEvent()</code>. 119: * 120: * @param ev the mouse event 121: */ 122: public void handleKeyEvent(KeyEvent ev) 123: { 124: ev.setSource(this); 125: processKeyEvent(ev); 126: } 127: 128: /** 129: * Handles focus events by forwarding it to 130: * <code>processFocusEvent()</code>. 131: * 132: * @param ev the Focus event 133: */ 134: public void handleFocusEvent(FocusEvent ev) 135: { 136: processFocusEvent(ev); 137: } 138: 139: /** 140: * Overridden so that this method returns the correct value even without a 141: * peer. 142: * 143: * @return the screen location of the button 144: */ 145: public Point getLocationOnScreen() 146: { 147: return SwingCheckboxPeer.this.getLocationOnScreen(); 148: } 149: 150: /** 151: * Overridden so that the isShowing method returns the correct value 152: * for the swing button, even if it has no peer on its own. 153: * 154: * @return <code>true</code> if the button is currently showing, 155: * <code>false</code> otherwise 156: */ 157: public boolean isShowing() 158: { 159: boolean retVal = false; 160: if (checkbox != null) 161: retVal = checkbox.isShowing(); 162: return retVal; 163: } 164: 165: /** 166: * Overridden, so that the Swing button can create an Image without its 167: * own peer. 168: * 169: * @param w the width of the image 170: * @param h the height of the image 171: * 172: * @return an image 173: */ 174: public Image createImage(int w, int h) 175: { 176: return SwingCheckboxPeer.this.createImage(w, h); 177: } 178: 179: public Graphics getGraphics() 180: { 181: return SwingCheckboxPeer.this.getGraphics(); 182: } 183: 184: public Container getParent() 185: { 186: Container par = null; 187: if (checkbox != null) 188: par = checkbox.getParent(); 189: return par; 190: } 191: 192: public void requestFocus() { 193: SwingCheckboxPeer.this.requestFocus(awtComponent, false, true, 0); 194: } 195: 196: public boolean requestFocus(boolean temporary) { 197: return SwingCheckboxPeer.this.requestFocus(awtComponent, temporary, 198: true, 0); 199: } 200: } 201: 202: /** 203: * Listens for ActionEvents on the Swing button and triggers corresponding 204: * ActionEvents on the AWT button. 205: */ 206: class SwingCheckboxListener implements ItemListener 207: { 208: Checkbox awtCheckbox; 209: 210: SwingCheckboxListener(Checkbox checkbox) 211: { 212: awtCheckbox = checkbox; 213: } 214: 215: /** 216: * Receives notification when an action was performend on the button. 217: * 218: * @param event the action event 219: */ 220: public void itemStateChanged(ItemEvent event) 221: { 222: awtCheckbox.setState(event.getStateChange()==ItemEvent.SELECTED); 223: ItemListener[] l = awtCheckbox.getItemListeners(); 224: if (l.length == 0) 225: return; 226: ItemEvent ev = new ItemEvent(awtCheckbox, ItemEvent.ITEM_STATE_CHANGED, 227: awtCheckbox, event.getStateChange()); 228: for (int i = 0; i < l.length; ++i) 229: l[i].itemStateChanged(ev); 230: } 231: } 232: 233: /** 234: * Creates a new SwingCheckboxPeer instance. 235: */ 236: public SwingCheckboxPeer(Checkbox checkbox) 237: { 238: SwingCheckbox swingCheckbox = new SwingCheckbox(checkbox); 239: swingCheckbox.addItemListener(new SwingCheckboxListener(checkbox)); 240: 241: init(checkbox, swingCheckbox); 242: setLabel(checkbox.getLabel()); 243: setState(checkbox.getState()); 244: } 245: 246: public void setCheckboxGroup(CheckboxGroup group) 247: { 248: // TODO: Implement this. 249: } 250: 251: public void setLabel(String label) 252: { 253: ((JToggleButton) swingComponent).setText(label); 254: } 255: 256: public void setState(boolean state) 257: { 258: ((JToggleButton) swingComponent).setSelected(state); 259: } 260: 261: }