Frames | No Frames |
1: /* SwingContainerPeer.java -- A Swing based peer for AWT containers 2: Copyright (C) 2006 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.Component; 41: import java.awt.Container; 42: import java.awt.Graphics; 43: import java.awt.Insets; 44: import java.awt.Shape; 45: import java.awt.event.MouseEvent; 46: import java.awt.peer.ComponentPeer; 47: import java.awt.peer.ContainerPeer; 48: 49: /** 50: * A peer for Container to be used with the Swing based AWT peers. 51: * 52: * @author Roman Kennke (kennke@aicas.com) 53: */ 54: public class SwingContainerPeer 55: extends SwingComponentPeer 56: implements ContainerPeer 57: { 58: 59: /** 60: * Creates a new SwingContainerPeer. 61: * 62: * @param awtCont 63: */ 64: public SwingContainerPeer(Component awtCont) 65: { 66: init(awtCont, null); 67: } 68: 69: /** 70: * Returns the insets of the container. 71: * 72: * This is implemented to return the insets of the Swing container. 73: * 74: * @return the insets of the container 75: */ 76: public Insets insets() 77: { 78: Insets retVal; 79: if (swingComponent != null) 80: retVal = swingComponent.getJComponent().getInsets(); 81: else 82: retVal = new Insets(0, 0, 0, 0); 83: return retVal; 84: } 85: 86: /** 87: * Returns the insets of the container. 88: * 89: * This is implemented to return the insets of the Swing container. 90: * 91: * @return the insets of the container 92: */ 93: public Insets getInsets() 94: { 95: Insets retVal; 96: if (swingComponent != null) 97: retVal = swingComponent.getJComponent().getInsets(); 98: else 99: retVal = new Insets(0, 0, 0, 0); 100: return retVal; 101: } 102: 103: /** 104: * Called before the validation of this containers begins. 105: */ 106: public void beginValidate() 107: { 108: // Nothing to do here. 109: } 110: 111: /** 112: * Called after the validation of this containers ended. 113: */ 114: public void endValidate() 115: { 116: // Nothing to do here. 117: } 118: 119: /** 120: * Called before the layout of this containers begins. 121: */ 122: public void beginLayout() 123: { 124: // Nothing to do here. 125: } 126: 127: /** 128: * Called after the layout of this containers ended. 129: */ 130: public void endLayout() 131: { 132: // Nothing to do here. 133: } 134: 135: /** 136: * Returns <code>false</code> unconditionally. This method is not used at 137: * the moment. 138: * 139: * @return <code>false</code> 140: */ 141: public boolean isPaintPending() 142: { 143: return false; 144: } 145: 146: /** 147: * Returns <code>false</code> unconditionally. This method is not used at 148: * the moment. 149: * 150: * @return <code>false</code> 151: */ 152: public boolean isRestackSupported() 153: { 154: return false; 155: } 156: 157: /** 158: * This method is not used at the moment. 159: */ 160: public void cancelPendingPaint(int x, int y, int width, int height) 161: { 162: // Nothing to do here. 163: } 164: 165: /** 166: * This method is not used at the moment. 167: */ 168: public void restack() 169: { 170: // Nothing to do here. 171: } 172: 173: /** 174: * Triggers painting of a component. This calls peerPaint on all the child 175: * components of this container. 176: * 177: * @param g the graphics context to paint to 178: */ 179: protected void peerPaint(Graphics g) 180: { 181: Container c = (Container) awtComponent; 182: Component[] children = c.getComponents(); 183: for (int i = children.length - 1; i >= 0; --i) 184: { 185: Component child = children[i]; 186: ComponentPeer peer = child.getPeer(); 187: boolean translated = false; 188: boolean clipped = false; 189: Shape oldClip = g.getClip(); 190: try 191: { 192: g.translate(child.getX(), child.getY()); 193: translated = true; 194: g.setClip(0, 0, child.getWidth(), child.getHeight()); 195: clipped = true; 196: if (peer instanceof SwingComponentPeer) 197: ((SwingComponentPeer) peer).peerPaint(g); 198: } 199: finally 200: { 201: if (translated) 202: g.translate(- child.getX(), - child.getY()); 203: if (clipped) 204: g.setClip(oldClip); 205: } 206: } 207: } 208: 209: /** 210: * Handles mouse events by dispatching it to the correct component. 211: * 212: * @param ev the mouse event 213: */ 214: protected void handleMouseEvent(MouseEvent ev) 215: { 216: Component comp = awtComponent.getComponentAt(ev.getPoint()); 217: if(comp == null) 218: comp = awtComponent; 219: if (comp != null) 220: { 221: ComponentPeer peer = comp.getPeer(); 222: if (awtComponent != comp && !comp.isLightweight() && peer instanceof SwingComponentPeer) 223: { 224: ev.translatePoint(comp.getX(), comp.getY()); 225: ev.setSource(comp); 226: ((SwingComponentPeer) peer).handleMouseEvent(ev); 227: } 228: } 229: } 230: 231: /** 232: * Handles mouse events by dispatching it to the correct component. 233: * 234: * @param ev the mouse event 235: */ 236: protected void handleMouseMotionEvent(MouseEvent ev) 237: { 238: Component comp = awtComponent.getComponentAt(ev.getPoint()); 239: if (comp != null) 240: { 241: ComponentPeer peer = comp.getPeer(); 242: if (awtComponent != comp && !comp.isLightweight() && peer instanceof SwingComponentPeer) 243: { 244: ev.translatePoint(comp.getX(), comp.getY()); 245: ((SwingComponentPeer) peer).handleMouseMotionEvent(ev); 246: } 247: } 248: } 249: }