001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.ui;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030:
031: import java.util.*;
032:
033: import javax.swing.*;
034:
035: /**
036: *
037: *
038: * @author $author$
039: * @version $Revision: 1.16 $
040: */
041: public class UIUtil implements SwingConstants {
042: /**
043: * Parse a string in the format of <code>[character]</code> to create an
044: * Integer that may be used for an action.
045: *
046: * @param character mnemonic string
047: * @return mnemonic
048: */
049: public static Integer parseMnemonicString(String string) {
050: try {
051: return new Integer(string);
052: } catch (Throwable t) {
053: return new Integer(-1);
054: }
055: }
056:
057: /**
058: * Parse a string in the format of [ALT+|CTRL+|SHIFT+]<keycode> to
059: * create a keystroke. This can be used to define accelerators from
060: * resource bundles
061: *
062: * @param string accelerator string
063: * @return keystroke
064: */
065: public static KeyStroke parseAcceleratorString(String string) {
066: if ((string == null) || string.equals("")) {
067: return null;
068: }
069:
070: StringTokenizer t = new StringTokenizer(string, "+");
071: int mod = 0;
072: int key = -1;
073:
074: while (t.hasMoreTokens()) {
075: String x = t.nextToken();
076:
077: if (x.equalsIgnoreCase("ctrl")) {
078: mod += KeyEvent.CTRL_MASK;
079: } else if (x.equalsIgnoreCase("shift")) {
080: mod += KeyEvent.SHIFT_MASK;
081: } else if (x.equalsIgnoreCase("alt")) {
082: mod += KeyEvent.ALT_MASK;
083: } else {
084: try {
085: java.lang.reflect.Field f = KeyEvent.class
086: .getField(x);
087: key = f.getInt(null);
088: } catch (Throwable ex) {
089: ex.printStackTrace();
090: }
091: }
092: }
093:
094: if (key != -1) {
095: KeyStroke ks = KeyStroke.getKeyStroke(key, mod);
096:
097: return ks;
098: }
099:
100: return null;
101: }
102:
103: /**
104: *
105: *
106: * @param parent
107: * @param componentToAdd
108: * @param constraints
109: * @param pos
110: *
111: * @throws IllegalArgumentException
112: */
113: public static void jGridBagAdd(JComponent parent,
114: Component componentToAdd, GridBagConstraints constraints,
115: int pos) {
116: if (!(parent.getLayout() instanceof GridBagLayout)) {
117: throw new IllegalArgumentException(
118: "parent must have a GridBagLayout");
119: }
120:
121: //
122: GridBagLayout layout = (GridBagLayout) parent.getLayout();
123:
124: //
125: constraints.gridwidth = pos;
126: layout.setConstraints(componentToAdd, constraints);
127: parent.add(componentToAdd);
128: }
129:
130: /**
131: *
132: *
133: * @param p
134: * @param c
135: */
136: public static void positionComponent(int p, Component c) {
137: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
138:
139: switch (p) {
140: case NORTH_WEST:
141: c.setLocation(0, 0);
142:
143: break;
144:
145: case NORTH:
146: c.setLocation((d.width - c.getSize().width) / 2, 0);
147:
148: break;
149:
150: case NORTH_EAST:
151: c.setLocation((d.width - c.getSize().width), 0);
152:
153: break;
154:
155: case WEST:
156: c.setLocation(0, (d.height - c.getSize().height) / 2);
157:
158: break;
159:
160: case SOUTH_WEST:
161: c.setLocation(0, (d.height - c.getSize().height));
162:
163: break;
164:
165: case EAST:
166: c.setLocation(d.width - c.getSize().width, (d.height - c
167: .getSize().height) / 2);
168:
169: break;
170:
171: case SOUTH_EAST:
172: c.setLocation((d.width - c.getSize().width), (d.height - c
173: .getSize().height) - 30);
174:
175: break;
176:
177: case CENTER:
178: c.setLocation((d.width - c.getSize().width) / 2,
179: (d.height - c.getSize().height) / 2);
180:
181: break;
182: }
183: }
184: }
|