001: package net.xoetrope.xui;
002:
003: import java.awt.BorderLayout;
004: import java.awt.FlowLayout;
005: import java.awt.LayoutManager;
006: import java.awt.Container;
007: import java.awt.CardLayout;
008: import java.awt.GridLayout;
009: import java.awt.GridBagLayout;
010: import java.util.Hashtable;
011:
012: /**
013: * A helper class for working with layout managers. This class provides mappings
014: * between the names of layout styles and constraints and the corresponding
015: * Java constants.
016: * <p>Copyright (c) Xoetrope Ltd., 2002-2003</p>
017: * <p>License: see license.txt</p>
018: * $Revision: 1.8 $
019: */
020: public class XLayoutHelper {
021: public static final int LEFT = 0;
022: public static final int RIGHT = 1;
023: public static final int TOP = 2;
024: public static final int BOTTOM = 3;
025:
026: /**
027: * Sets a LayoutManager for the panel
028: * @param cont the container whose layout manager is being set or null to set the parent panel's layout manager
029: * @param type the layout manager as defined in the XLayoutHelper class
030: * @param style the style containing the layout manager attributes
031: */
032: public static LayoutManager addLayout(Container cont, int type) {
033: LayoutManager lm = null;
034: switch (type) {
035: case XPage.BORDER_LAYOUT:
036: lm = new BorderLayout();
037: break;
038: case XPage.FLOW_LAYOUT:
039: lm = new FlowLayout();
040: break;
041: case XPage.CARD_LAYOUT:
042: lm = new CardLayout();
043: break;
044: case XPage.GRID_LAYOUT:
045: lm = new GridLayout();
046: break;
047: case XPage.GRIDBAG_LAYOUT:
048: lm = new GridBagLayout();
049: break;
050: case XPage.NULL_LAYOUT:
051: default:
052: break;
053: }
054:
055: cont.setLayout(lm);
056: return lm;
057: }
058:
059: /**
060: * Sets a LayoutManager for the panel
061: * @param cont the container whose layout manager is being set or null to set the parent panel's layout manager
062: * @param type the layout manager as defined in the XLayoutHelper class
063: * @param style the style containing the layout manager attributes
064: */
065: public static LayoutManager addLayout(Container cont, int type,
066: Hashtable attribs) {
067: LayoutManager lm = addLayout(cont, type);
068:
069: if (attribs != null) {
070: switch (type) {
071: case XPage.BORDER_LAYOUT:
072: ((BorderLayout) lm).setHgap(getIntAttrib(attribs,
073: "hgap"));
074: ((BorderLayout) lm).setVgap(getIntAttrib(attribs,
075: "vgap"));
076: break;
077:
078: case XPage.FLOW_LAYOUT:
079: ((FlowLayout) lm)
080: .setHgap(getIntAttrib(attribs, "hgap"));
081: ((FlowLayout) lm)
082: .setVgap(getIntAttrib(attribs, "vgap"));
083: int alignment = getIntAttrib(attribs, "alignment");
084: ((FlowLayout) lm).setAlignment(alignment);
085: break;
086:
087: case XPage.CARD_LAYOUT:
088: ((CardLayout) lm)
089: .setHgap(getIntAttrib(attribs, "hgap"));
090: ((CardLayout) lm)
091: .setVgap(getIntAttrib(attribs, "vgap"));
092: break;
093:
094: case XPage.GRID_LAYOUT:
095: ((GridLayout) lm)
096: .setHgap(getIntAttrib(attribs, "hgap"));
097: ((GridLayout) lm)
098: .setVgap(getIntAttrib(attribs, "vgap"));
099: int rows = getIntAttrib(attribs, "rows");
100: int cols = getIntAttrib(attribs, "cols");
101: if ((rows > 0) || (cols > 0)) {
102: if (rows == 0) {
103: ((GridLayout) lm).setColumns(cols);
104: ((GridLayout) lm).setRows(rows);
105: } else {
106: ((GridLayout) lm).setRows(rows);
107: ((GridLayout) lm).setColumns(cols);
108: }
109: }
110: break;
111:
112: case XPage.GRIDBAG_LAYOUT:
113: break;
114:
115: default:
116: case XPage.NULL_LAYOUT:
117: break;
118: }
119: }
120:
121: if (type == XPage.BOX_LAYOUT) {
122: String orientation = (attribs != null ? (String) attribs
123: .get("isHorz") : "");
124: boolean isHorz = orientation.equals("true");
125: lm = new GridLayout(isHorz ? 0 : 1, isHorz ? 1 : 0);
126: cont.setLayout(lm);
127: }
128:
129: return lm;
130: }
131:
132: /**
133: * Gets a constraint object corresponding to a constraint name
134: * @param name the constraint name
135: * @return the constraint object
136: */
137: public int getAlignment(String name) {
138: int alignment = FlowLayout.CENTER;
139: name = name.toUpperCase();
140: if (name == null)
141: return alignment;
142: else if (name.compareTo("LEADING") == 0)
143: return 3;//FlowLayout.LEADING;
144: else if (name.compareTo("CENTER") == 0)
145: return FlowLayout.CENTER;
146: else if (name.compareTo("LEFT") == 0)
147: return FlowLayout.LEFT;
148: else if (name.compareTo("RIGHT") == 0)
149: return FlowLayout.RIGHT;
150: else if (name.compareTo("TRAILING") == 0)
151: return 4;//FlowLayout.TRAILING;
152:
153: return alignment;
154: }
155:
156: /**
157: * Get the layout type enumerated in XPage
158: * @param ls the layout style
159: * @return the type id
160: */
161: public static int getLayoutType(String ls) {
162: ls = ls.toUpperCase();
163: if (ls.compareTo("FLOW") == 0)
164: return XPage.FLOW_LAYOUT;
165: else if (ls.compareTo("BORDER") == 0)
166: return XPage.BORDER_LAYOUT;
167: else if (ls.compareTo("GRID") == 0)
168: return XPage.GRID_LAYOUT;
169: else if (ls.compareTo("GRIDBAG") == 0)
170: return XPage.GRIDBAG_LAYOUT;
171: else if (ls.compareTo("CARD") == 0)
172: return XPage.CARD_LAYOUT;
173: else if (ls.compareTo("BOX") == 0)
174: return XPage.BOX_LAYOUT;
175:
176: return XPage.NULL_LAYOUT;
177: }
178:
179: /**
180: * Gets a constraint object corresponding to a constraint name
181: * @param name the constraint name
182: * @return the constraint object
183: */
184: public Object getConstraint(String name) {
185: Object constraint = null;
186: name = name.toUpperCase();
187: if (name == null)
188: return constraint;
189: else if (name.compareTo("WEST") == 0)
190: return BorderLayout.WEST;
191: else if (name.compareTo("EAST") == 0)
192: return BorderLayout.EAST;
193: else if (name.compareTo("NORTH") == 0)
194: return BorderLayout.NORTH;
195: else if (name.compareTo("SOUTH") == 0)
196: return BorderLayout.SOUTH;
197: else if (name.compareTo("CENTER") == 0)
198: return BorderLayout.CENTER;
199: else if (name.compareTo("AFTER_LAST_LINE") == 0)
200: return "Last";//BorderLayout.AFTER_LAST_LINE;
201: else if (name.compareTo("AFTER_LINE_ENDS") == 0)
202: return "After";//BorderLayout.AFTER_LINE_ENDS;
203: else if (name.compareTo("BEFORE_FIRST_LINE") == 0)
204: return "First";//BorderLayout.BEFORE_FIRST_LINE;
205: else if (name.compareTo("BEFORE_LINE_BEGINS") == 0)
206: return "Before";//BorderLayout.BEFORE_LINE_BEGINS;
207:
208: return constraint;
209: }
210:
211: /**
212: * Get an attribute as an int value
213: * @param attribs the attribs hashtable
214: * @param attrib the attribute key
215: * @return the integer value
216: */
217: protected static int getIntAttrib(Hashtable attribs, String attrib) {
218: try {
219: Object value = attribs.get(attrib);
220: if (value != null)
221: return new Integer((String) value).intValue();
222: } catch (NumberFormatException ex) {
223: }
224: return 0;
225: }
226: }
|