001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.soa.mapper.common.util;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024:
025: import java.awt.Dimension;
026: import java.awt.Image;
027: import java.awt.Point;
028: import java.awt.Toolkit;
029: import java.awt.Window;
030: import java.net.URL;
031: import javax.swing.Icon;
032: import javax.swing.ImageIcon;
033:
034: /**
035: * a swing utility class</p>
036: *
037: * @author JoneLin
038: * @created December 3, 2002
039: */
040: public class SbynSwingUtil {
041: /**
042: * Description of the Field
043: */
044: protected static SbynSwingUtil util = new SbynSwingUtil();
045:
046: /**
047: * Constructor for the SbynSwingUtil object
048: */
049: protected SbynSwingUtil() {
050: }
051:
052: /**
053: * Get an URL from url string.
054: *
055: * @param url url string to be converted to URL.
056: * @param aClass - a class
057: * @return a URL.
058: */
059: public static URL getURL(String url, Class aClass) {
060: try {
061: return aClass.getResource(url).openConnection().getURL();
062: } catch (Exception e) {
063: System.err.println("Error getting " + url);
064: return null;
065: }
066: }
067:
068: /**
069: * Creates URL from a url string.
070: *
071: * @param url url string to be converted to URL.
072: * @return a URL.
073: */
074: public static URL getURL(String url) {
075: return getURL(url, util.getClass());
076: }
077:
078: /**
079: * Creates an icon from the given url.
080: *
081: * @param url the url representing an image file, either .jpg or
082: * .gif
083: * @return the icon
084: */
085: public static Icon getIcon(String url) {
086: return getIcon(url, util.getClass());
087: }
088:
089: /**
090: * Creates an icon from the given url.
091: *
092: * @param url the url representing an image file, either .jpg or
093: * .gif
094: * @param aClass - a class
095: * @return Icon the icon
096: */
097: public static Icon getIcon(String url, Class aClass) {
098: try {
099: return new ImageIcon(aClass.getResource(url)
100: .openConnection().getURL());
101: } catch (Exception e) {
102: System.err.println("Error getting " + url);
103: return null;
104: }
105: }
106:
107: /**
108: * Creates an icon from the given url.
109: *
110: * @param url the url representing an image file, either .jpg or
111: * .gif
112: * @return Icon the icon
113: */
114: public static Icon getIcon(URL url) {
115: ImageIcon icon = null;
116: try {
117: icon = new ImageIcon(url);
118: } catch (Exception io) {
119: io.printStackTrace();
120: }
121: return icon;
122: }
123:
124: /**
125: * Center the given window on the screen.
126: *
127: * @param w the window
128: */
129: public static void centerOnScreen(Window w) {
130: Dimension mScreenSize = Toolkit.getDefaultToolkit()
131: .getScreenSize();
132:
133: Dimension mFrameSize = w.getSize();
134: Point mFrameLocation = new Point(mScreenSize.width / 2
135: - mFrameSize.width / 2, mScreenSize.height / 2
136: - mFrameSize.height / 2);
137: w.setLocation(mFrameLocation);
138: }
139:
140: /**
141: * Set window size to cover the screen.
142: *
143: * @param w the window
144: */
145: public static void setToScreenSize(Window w) {
146: Dimension mScreenSize = Toolkit.getDefaultToolkit()
147: .getScreenSize();
148: w.setSize(mScreenSize);
149: }
150:
151: /**
152: * Creates an image from the given url.
153: *
154: * @param url the url representing an image file, either .jpg or
155: * .gif
156: * @return Image the image
157: */
158: public static Image getImage(String url) {
159: return getImage(url, util.getClass());
160: }
161:
162: /**
163: * Creates an image from the given url.
164: *
165: * @param url the url representing an image file, either .jpg or
166: * .gif
167: * @param aClass class resource
168: * @return Image
169: */
170: public static Image getImage(String url, Class aClass) {
171: try {
172: ImageIcon icon = new ImageIcon(aClass.getResource(url)
173: .openConnection().getURL());
174: return icon.getImage();
175: } catch (Exception e) {
176: return null;
177: }
178: }
179:
180: /**
181: * returns the point with shortest distance to the target point
182: *
183: * @param p1 point to be tested
184: * @param p2 point to be tested
185: * @param target the target point to be tested against
186: * @return the point with shortest distance to the target
187: */
188: public static Point minDistance(Point p1, Point p2, Point target) {
189: double p1Dist = calculateDistance(p1.x, p1.y, target.x,
190: target.y);
191: double p2Dist = calculateDistance(p2.x, p2.y, target.x,
192: target.y);
193: if (p1Dist < p2Dist) {
194: return p1;
195: } else {
196: return p2;
197: }
198: }
199:
200: /**
201: * returns the distance between 2 points
202: *
203: * @param ax x coordinates for point 1
204: * @param ay y coordiates for point 1
205: * @param bx x coordinate for point 2
206: * @param by y coordinate for point 3
207: * @return the distance between 2 points
208: */
209: public static double calculateDistance(int ax, int ay, int bx,
210: int by) {
211: double sum = (bx - ax) * (bx - ax) + (by - ay) * (by - ay);
212: return Math.sqrt(sum);
213: }
214:
215: /**
216: * create a color based on the hex string (example: "ff9f00") if
217: * value is null, return null;
218: *
219: * @param value Description of the Parameter
220: * @return Description of the Return Value
221: */
222: public static Color createColor(String value) {
223: Color retColor = null;
224: try {
225: if ((value != null) && (value.length() > 0)) {
226: int pixel = Integer.parseInt(value, 16
227: /*
228: * radix
229: */
230: );
231: retColor = new Color(pixel);
232: }
233: } catch (NumberFormatException ex) {
234: // may be "white", "black"?
235: // try lookup?
236: // or even egate standard color name such as "egate.icon.background"
237: return null;
238: }
239: return retColor;
240: }
241:
242: /**
243: * Description of the Method
244: *
245: * @param component Description of the Parameter
246: * @param x Description of the Parameter
247: * @param y Description of the Parameter
248: * @return Description of the Return Value
249: */
250: public static boolean componentContainsPos(Component component,
251: int x, int y) {
252: return componentContainsPos(component, x, y, false);
253: }
254:
255: /**
256: * Description of the Method
257: *
258: * @param component Description of the Parameter
259: * @param x Description of the Parameter
260: * @param y Description of the Parameter
261: * @param reduceLowerBound Description of the Parameter
262: * @return Description of the Return Value
263: */
264: public static boolean componentContainsPos(Component component,
265: int x, int y, boolean reduceLowerBound) {
266:
267: int xx = component.getX();
268: int yy = component.getY();
269: if (reduceLowerBound) {
270: xx = xx - component.getWidth();
271: yy = yy - component.getHeight();
272: }
273:
274: int xxRight = component.getX();
275: int yyBottom = component.getY();
276: if (!reduceLowerBound) {
277: xxRight = xxRight + component.getWidth();
278:
279: }
280: yyBottom = yyBottom + component.getHeight();
281: if (xx <= x && x <= xxRight && yy <= y && y <= yyBottom) {
282: return true;
283: }
284: return false;
285: }
286:
287: }
|