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.Checkbox; 41: import java.awt.CheckboxGroup; 42: import java.awt.Container; 43: import java.awt.Graphics; 44: import java.awt.Image; 45: import java.awt.Point; 46: import java.awt.event.FocusEvent; 47: import java.awt.event.ItemEvent; 48: import java.awt.event.ItemListener; 49: import java.awt.event.KeyEvent; 50: import java.awt.event.MouseEvent; 51: import java.awt.peer.CheckboxPeer; 52: 53: import javax.swing.JCheckBox; 54: import javax.swing.JComponent; 55: import javax.swing.JToggleButton; 56: 57: /** 58: * A CheckboxPeer implementation that is backed by the Swing JCheckBox. 59: */ 60: public class SwingCheckboxPeer extends SwingComponentPeer implements 61: CheckboxPeer { 62: 63: /** 64: * A spezialized Swing checkbox used to paint the checkbox for the 65: * AWT checkbox. 66: */ 67: private class SwingCheckbox 68: extends JCheckBox 69: implements SwingComponent 70: { 71: Checkbox checkbox; 72: 73: SwingCheckbox(Checkbox checkbox) 74: { 75: this.checkbox = checkbox; 76: } 77: 78: /** 79: * Returns this checkbox. 80: * 81: * @return <code>this</code> 82: */ 83: public JComponent getJComponent() 84: { 85: return this; 86: } 87: 88: /** 89: * Handles mouse events by forwarding it to 90: * <code>processMouseEvent()</code>. 91: * 92: * @param ev the mouse event 93: */ 94: public void handleMouseEvent(MouseEvent ev) 95: { 96: ev.setSource(this); 97: processMouseEvent(ev); 98: } 99: 100: /** 101: * Handles mouse motion events by forwarding it to 102: * <code>processMouseMotionEvent()</code>. 103: * 104: * @param ev the mouse motion event 105: */ 106: public void handleMouseMotionEvent(MouseEvent ev) 107: { 108: ev.setSource(this); 109: processMouseMotionEvent(ev); 110: } 111: 112: /** 113: * Handles key events by forwarding it to <code>processKeyEvent()</code>. 114: * 115: * @param ev the mouse event 116: */ 117: public void handleKeyEvent(KeyEvent ev) 118: { 119: ev.setSource(this); 120: processKeyEvent(ev); 121: } 122: 123: /** 124: * Handles focus events by forwarding it to 125: * <code>processFocusEvent()</code>. 126: * 127: * @param ev the Focus event 128: */ 129: public void handleFocusEvent(FocusEvent ev) 130: { 131: processFocusEvent(ev); 132: } 133: 134: /** 135: * Overridden so that this method returns the correct value even without a 136: * peer. 137: * 138: * @return the screen location of the button 139: */ 140: public Point getLocationOnScreen() 141: { 142: return SwingCheckboxPeer.this.getLocationOnScreen(); 143: } 144: 145: /** 146: * Overridden so that the isShowing method returns the correct value 147: * for the swing button, even if it has no peer on its own. 148: * 149: * @return <code>true</code> if the button is currently showing, 150: * <code>false</code> otherwise 151: */ 152: public boolean isShowing() 153: { 154: boolean retVal = false; 155: if (checkbox != null) 156: retVal = checkbox.isShowing(); 157: return retVal; 158: } 159: 160: /** 161: * Overridden, so that the Swing button can create an Image without its 162: * own peer. 163: * 164: * @param w the width of the image 165: * @param h the height of the image 166: * 167: * @return an image 168: */ 169: public Image createImage(int w, int h) 170: { 171: return SwingCheckboxPeer.this.createImage(w, h); 172: } 173: 174: public Graphics getGraphics() 175: { 176: return SwingCheckboxPeer.this.getGraphics(); 177: } 178: 179: public Container getParent() 180: { 181: Container par = null; 182: if (checkbox != null) 183: par = checkbox.getParent(); 184: return par; 185: } 186: 187: public void requestFocus() { 188: SwingCheckboxPeer.this.requestFocus(awtComponent, false, true, 0); 189: } 190: 191: public boolean requestFocus(boolean temporary) { 192: return SwingCheckboxPeer.this.requestFocus(awtComponent, temporary, 193: true, 0); 194: } 195: } 196: 197: /** 198: * Listens for ActionEvents on the Swing button and triggers corresponding 199: * ActionEvents on the AWT button. 200: */ 201: class SwingCheckboxListener implements ItemListener 202: { 203: Checkbox awtCheckbox; 204: 205: SwingCheckboxListener(Checkbox checkbox) 206: { 207: awtCheckbox = checkbox; 208: } 209: 210: /** 211: * Receives notification when an action was performend on the button. 212: * 213: * @param event the action event 214: */ 215: public void itemStateChanged(ItemEvent event) 216: { 217: awtCheckbox.setState(event.getStateChange()==ItemEvent.SELECTED); 218: ItemListener[] l = awtCheckbox.getItemListeners(); 219: if (l.length == 0) 220: return; 221: ItemEvent ev = new ItemEvent(awtCheckbox, ItemEvent.ITEM_STATE_CHANGED, 222: awtCheckbox, event.getStateChange()); 223: for (int i = 0; i < l.length; ++i) 224: l[i].itemStateChanged(ev); 225: } 226: } 227: 228: /** 229: * Creates a new SwingCheckboxPeer instance. 230: */ 231: public SwingCheckboxPeer(Checkbox checkbox) 232: { 233: SwingCheckbox swingCheckbox = new SwingCheckbox(checkbox); 234: swingCheckbox.addItemListener(new SwingCheckboxListener(checkbox)); 235: 236: init(checkbox, swingCheckbox); 237: setLabel(checkbox.getLabel()); 238: setState(checkbox.getState()); 239: } 240: 241: public void setCheckboxGroup(CheckboxGroup group) 242: { 243: // TODO: Implement this. 244: } 245: 246: public void setLabel(String label) 247: { 248: ((JToggleButton) swingComponent).setText(label); 249: } 250: 251: public void setState(boolean state) 252: { 253: ((JToggleButton) swingComponent).setSelected(state); 254: } 255: 256: }