001: /*
002: ** Tim Endres' utilities package.
003: ** Copyright (c) 1997 by Tim Endres
004: **
005: ** This program is free software.
006: **
007: ** You may redistribute it and/or modify it under the terms of the GNU
008: ** General Public License as published by the Free Software Foundation.
009: ** Version 2 of the license should be included with this distribution in
010: ** the file LICENSE, as well as License.html. If the license is not
011: ** included with this distribution, you may find a copy at the FSF web
012: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
013: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
014: **
015: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
016: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
017: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
018: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
019: ** REDISTRIBUTION OF THIS SOFTWARE.
020: **
021: */
022:
023: package com.ice.util;
024:
025: import java.awt.*;
026: import java.awt.event.*;
027: import java.awt.image.*;
028: import java.net.URL;
029: import java.io.IOException;
030: import java.util.*;
031:
032: /**
033: * This is a class that contains useful utility functions related
034: * to the Java AWT package.
035: */
036:
037: public class AWTUtilities {
038: static public Point centerDialogInParent(Dialog dialog,
039: Component parent) {
040: Point parLoc = parent.getLocationOnScreen();
041:
042: Dimension parSz = parent.getSize();
043: Dimension dlgSz = dialog.getSize();
044:
045: int x = parLoc.x + (parSz.width - dlgSz.width) / 2;
046: int y = parLoc.y + (parSz.height - dlgSz.height) / 3;
047:
048: return new Point(x, y);
049: }
050:
051: static public Point computeDialogLocation(Dialog dialog, int w,
052: int h) {
053: Dimension scrnSz = dialog.getToolkit().getScreenSize();
054:
055: int x = (scrnSz.width - w) / 2;
056: int y = (scrnSz.height - h) / 3;
057:
058: return new Point(x, y);
059: }
060:
061: static public Point computeDialogLocation(Dialog dialog) {
062: Dimension dlgSz = dialog.getSize();
063: Dimension scrnSz = dialog.getToolkit().getScreenSize();
064:
065: int x = (scrnSz.width - dlgSz.width) / 2;
066: int y = (scrnSz.height - dlgSz.height) / 3;
067:
068: return new Point(x, y);
069: }
070:
071: static public Point computeDialogLocation(Dialog dialog,
072: Component rel) {
073: Dimension dlgSz = dialog.getSize();
074: Dimension scrnSz = dialog.getToolkit().getScreenSize();
075:
076: int x = (scrnSz.width - dlgSz.width) / 2;
077: int y = (scrnSz.height - dlgSz.height) / 3;
078:
079: if (rel != null) {
080: Dimension relSz = rel.getSize();
081: Point loc = rel.getLocationOnScreen();
082:
083: x = loc.x + ((relSz.width - dlgSz.width) / 2);
084:
085: y = loc.y + ((relSz.height - dlgSz.height) / 2);
086: }
087:
088: if (x < 0)
089: x = 0;
090: if (y < 0)
091: y = 0;
092:
093: return new Point(x, y);
094: }
095:
096: static public void constrain(Container container,
097: Component component, int fill, int anchor, int gx, int gy,
098: int gw, int gh, double wx, double wy) {
099: GridBagConstraints c = new GridBagConstraints();
100:
101: c.fill = fill;
102: c.anchor = anchor;
103: c.gridx = gx;
104: c.gridy = gy;
105: c.gridwidth = gw;
106: c.gridheight = gh;
107: c.weightx = wx;
108: c.weighty = wy;
109:
110: ((GridBagLayout) container.getLayout()).setConstraints(
111: component, c);
112:
113: container.add(component);
114: }
115:
116: static public void constrain(Container container,
117: Component component, int fill, int anchor, int gx, int gy,
118: int gw, int gh, double wx, double wy, Insets inset) {
119: GridBagConstraints c = new GridBagConstraints();
120:
121: c.fill = fill;
122: c.anchor = anchor;
123: c.gridx = gx;
124: c.gridy = gy;
125: c.gridwidth = gw;
126: c.gridheight = gh;
127: c.weightx = wx;
128: c.weighty = wy;
129: c.insets = inset;
130:
131: ((GridBagLayout) container.getLayout()).setConstraints(
132: component, c);
133:
134: container.add(component);
135: }
136:
137: static public void constrain(Container container,
138: Component component, int fill, int anchor, int gx, int gy,
139: int gw, int gh, double wx, double wy, int ipadx, int ipady) {
140: GridBagConstraints c = new GridBagConstraints();
141:
142: c.fill = fill;
143: c.anchor = anchor;
144: c.gridx = gx;
145: c.gridy = gy;
146: c.gridwidth = gw;
147: c.gridheight = gh;
148: c.weightx = wx;
149: c.weighty = wy;
150: c.ipadx = ipadx;
151: c.ipady = ipady;
152:
153: ((GridBagLayout) container.getLayout()).setConstraints(
154: component, c);
155:
156: container.add(component);
157: }
158:
159: static public Font getFont(String fontName) {
160: StringTokenizer toker = new StringTokenizer(fontName, "-",
161: false);
162:
163: String sName = "Helvetica";
164: String sStyle = "plain";
165: String sSize = "12";
166:
167: int numTokes = toker.countTokens();
168: boolean isok = true;
169:
170: try {
171: if (numTokes > 0) {
172: sName = toker.nextToken();
173:
174: if (numTokes == 2) {
175: sSize = toker.nextToken();
176: } else if (numTokes == 3) {
177: sStyle = toker.nextToken();
178: sSize = toker.nextToken();
179: }
180: }
181: } catch (Exception ex) {
182: System.err.println("Bad font specification '" + fontName
183: + "' - " + ex.getMessage());
184: return null;
185: }
186:
187: int style = (sStyle.equalsIgnoreCase("plain")) ? Font.PLAIN
188: : ((sStyle.equalsIgnoreCase("bold")) ? Font.BOLD
189: : ((sStyle.equalsIgnoreCase("italic")) ? Font.ITALIC
190: : (Font.BOLD + Font.ITALIC)));
191:
192: int size = Integer.parseInt(sSize);
193:
194: return new Font(sName, style, size);
195: }
196:
197: // The subtlety in getResource() is that it uses the
198: // Class loader of the class used to get the rousource.
199: // This means that if you want to load a resource from
200: // your JAR file, then you better use a class in the
201: // JAR file.
202:
203: public static Image getImageResource(String name)
204: throws java.io.IOException {
205: return AWTUtilities.getImageResource(AWTUtilities.class, name);
206: }
207:
208: public static Image getImageResource(Class base, String name)
209: throws java.io.IOException {
210: Image result = null;
211:
212: URL imageURL = base.getResource(name);
213:
214: if (imageURL != null) {
215: Toolkit tk = Toolkit.getDefaultToolkit();
216:
217: result = tk.createImage((ImageProducer) imageURL
218: .getContent());
219: }
220:
221: return result;
222: }
223:
224: public static Image getSystemImageResource(String name)
225: throws java.io.IOException {
226: Image result = null;
227:
228: URL imageURL = ClassLoader.getSystemResource(name);
229: if (imageURL != null) {
230: Toolkit tk = Toolkit.getDefaultToolkit();
231:
232: result = tk.createImage((ImageProducer) imageURL
233: .getContent());
234: }
235:
236: return result;
237: }
238:
239: }
|