View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: AWTUtilities.java,v 1.7 2002/09/13 17:04:38 ludovicc Exp $ 37 */ 38 package org.scopemvc.view.awt; 39 40 41 import java.awt.Component; 42 import java.awt.Dimension; 43 import java.awt.Point; 44 import java.awt.Rectangle; 45 import java.awt.Toolkit; 46 import java.awt.Window; 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 import org.scopemvc.core.View; 50 import org.scopemvc.util.Debug; 51 52 /*** 53 * <P> 54 * 55 * Utilities dependent on Java AWT and Scope core packages only. </P> 56 * 57 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 58 * @created 05 September 2002 59 * @version $Revision: 1.7 $ $Date: 2002/09/13 17:04:38 $ 60 */ 61 public final class AWTUtilities { 62 63 private static final Log LOG = LogFactory.getLog(AWTUtilities.class); 64 65 private AWTUtilities() { } 66 67 /*** 68 * Find the next View from a Component upwards in the java.awt.Component 69 * containment hierarchy that has a parent Controller that it can issue 70 * Controls to. 71 * 72 * @param inComponent The Component to find a Controller for 73 * @return The parent View bound to a non-null Controller 74 */ 75 public static View findControlIssuer(Component inComponent) { 76 Component parent = inComponent; 77 while (parent != null) { 78 if (parent instanceof View) { 79 if (((View) parent).getController() != null) { 80 return (View) parent; 81 } 82 } 83 parent = parent.getParent(); 84 } 85 if (LOG.isInfoEnabled()) { 86 LOG.warn("findControlIssuer: no Control Issuer for component: " + inComponent); 87 } 88 return null; 89 } 90 91 92 // --------------------------------- Window positioning 93 94 /*** 95 * Center a window on the screen 96 * 97 * @param inWindow The Window to center 98 */ 99 public static void centreOnScreen(Window inWindow) { 100 if (inWindow == null) { 101 throw new IllegalArgumentException(); 102 } 103 104 Rectangle centredBounds = getCenteredBounds(inWindow.getSize()); 105 inWindow.setLocation(centredBounds.x, centredBounds.y); 106 } 107 108 109 /*** 110 * Center a window on a parent Window if showing, or if not assume position 111 * of parent is (0, 0) 112 * 113 * @param inParent The parent Window used as reference if it's visible 114 * @param inWindow The Window to center 115 */ 116 public static void centreOnWindow(Window inParent, Window inWindow) { 117 if (inParent == null) { 118 throw new IllegalArgumentException("Can't centre on a null parent."); 119 } 120 if (inWindow == null) { 121 throw new IllegalArgumentException("Can't centre a null Window."); 122 } 123 124 Dimension parentSize = inParent.getSize(); 125 Dimension windowSize = inWindow.getSize(); 126 Point parentLocation = new Point(0, 0); 127 if (inParent.isShowing()) { 128 parentLocation = inParent.getLocationOnScreen(); 129 } 130 131 int x = parentLocation.x + parentSize.width / 2 - windowSize.width / 2; 132 int y = parentLocation.y + parentSize.height / 2 - windowSize.height / 2; 133 134 inWindow.setLocation(x, y); 135 } 136 137 138 /*** 139 * Make sure window fits on the screeen 140 * 141 * @param inWindow The Window to fit on the screen 142 */ 143 public static void fitOnScreen(Window inWindow) { 144 Dimension fullScreenSize = Toolkit.getDefaultToolkit().getScreenSize(); 145 Dimension screenSize = new Dimension(fullScreenSize.width, fullScreenSize.height); 146 Dimension windowSize = inWindow.getSize(); 147 Point windowLocation = inWindow.getLocation(); 148 149 if (LOG.isDebugEnabled()) { 150 LOG.debug("fitOnScreen: screenSize: " + screenSize); 151 LOG.debug("fitOnScreen: windowSize: " + windowSize); 152 LOG.debug("fitOnScreen: windowLocation: " + windowLocation); 153 } 154 155 Dimension newWindowSize = new Dimension(windowSize); 156 Point newWindowLocation = new Point(windowLocation); 157 158 if (windowLocation.x < 0) { 159 newWindowLocation.x = 0; 160 } 161 162 if (windowLocation.y < 0) { 163 newWindowLocation.y = 0; 164 } 165 166 if (newWindowLocation.x + windowSize.width > screenSize.width) { 167 if (LOG.isDebugEnabled()) { 168 LOG.debug("fitOnScreen: too wide"); 169 } 170 // First try to reposition window 171 newWindowLocation.x = windowLocation.x - (windowLocation.x + windowSize.width - screenSize.width); 172 if (LOG.isDebugEnabled()) { 173 LOG.debug("fitOnScreen: reposition: location: " + newWindowLocation); 174 } 175 // Then resize it 176 if (newWindowLocation.x < 0) { 177 newWindowSize.width += newWindowLocation.x; 178 newWindowLocation.x = 0; 179 if (LOG.isDebugEnabled()) { 180 LOG.debug("fitOnScreen: reposition: size: " + newWindowSize); 181 } 182 } 183 } 184 185 if (newWindowLocation.y + windowSize.height > screenSize.height) { 186 // First try to reposition window 187 newWindowLocation.y = windowLocation.y - (windowLocation.y + windowSize.height - screenSize.height); 188 // Then resize it 189 if (newWindowLocation.y < 0) { 190 newWindowSize.height += newWindowLocation.y; 191 newWindowLocation.y = 0; 192 } 193 } 194 195 if (LOG.isDebugEnabled()) { 196 LOG.debug("fitOnScreen: newWindowSize: " + newWindowSize); 197 LOG.debug("fitOnScreen: newWindowLocation: " + newWindowLocation); 198 } 199 200 inWindow.setSize(newWindowSize); 201 inWindow.setLocation(newWindowLocation); 202 } 203 204 205 private static Rectangle getCenteredBounds(Dimension inWindowSize) { 206 if (Debug.ON) { 207 Debug.assertTrue(inWindowSize != null); 208 } 209 210 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 211 212 int x = screenSize.width / 2 - inWindowSize.width / 2; 213 int y = screenSize.height / 2 - inWindowSize.height / 2; 214 215 return new Rectangle(x, y, inWindowSize.width, inWindowSize.height); 216 } 217 } 218

This page was automatically generated by Maven