01: /*
02: * Copyright (C) 2004 TiongHiang Lee
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: * Email: thlee@onemindsoft.org
19: */
20:
21: package org.onemind.swingweb.client.awt.ui;
22:
23: import java.awt.*;
24: import org.onemind.commons.java.lang.ref.SoftHashMap;
25: import org.onemind.swingweb.client.dom.DomNode;
26:
27: public class UIHelper {
28:
29: private static SoftHashMap SIZES = new SoftHashMap();
30:
31: public static final Point getLocation(DomNode DomNode) {
32: String x = DomNode.getAttribute("x");
33: String y = DomNode.getAttribute("y");
34: if (!isEmpty(x) && !isEmpty(y)) {
35: return new Point(Integer.parseInt(x), Integer.parseInt(y));
36: } else {
37: return null;
38: }
39: }
40:
41: public static final Dimension getDimension(DomNode DomNode) {
42: String width = DomNode.getAttribute("width");
43: String height = DomNode.getAttribute("height");
44: if (!isEmpty(width) && !isEmpty(height)) {
45: return new Dimension(Integer.parseInt(width), Integer
46: .parseInt(height));
47: } else {
48: return null;
49: }
50: }
51:
52: public static boolean isEmpty(String str) {
53: return str == null || str.length() == 0;
54: }
55:
56: public static final void setSize(Component component,
57: DomNode DomNode) {
58: Dimension dim = getDimension(DomNode);
59: Dimension setSize = (Dimension) SIZES.get(component);
60: if (dim != null && (setSize == null || !setSize.equals(dim))) {
61: component.setSize(dim);
62: SIZES.put(component, dim);
63: }
64: }
65:
66: public static final void setLocation(Component component,
67: DomNode DomNode) {
68: Point loc = getLocation(DomNode);
69: if (loc != null) {
70: component.setLocation(loc);
71: }
72: }
73:
74: public static final void setBounds(Component child, DomNode e) {
75: int adjustmentY = 0;
76: if (child.getParent() instanceof Frame) {
77: if (((Frame) child.getParent()).getMenuBar() != null) {
78: adjustmentY = 50;
79: } else {
80: adjustmentY = 25;
81: }
82: }
83: Dimension dim = getDimension(e);
84: Point loc = getLocation(e);
85: /// System.out.println("Set bound of " + child + " to " + loc + " " + dim);
86: if (dim != null && loc != null) {
87: child.setBounds((int) loc.getX(), (int) loc.getY()
88: + adjustmentY, (int) dim.getWidth(), (int) dim
89: .getHeight());
90: }
91: }
92: }
|