001: /**
002: * Caption: Zaval Java Resource Editor
003: * $Revision: 0.37 $
004: * $Date: 2002/03/28 9:24:42 $
005: *
006: * @author: Victor Krapivin
007: * @version: 1.3
008: *
009: * Zaval JRC Editor is a visual editor which allows you to manipulate
010: * localization strings for all Java based software with appropriate
011: * support embedded.
012: *
013: * For more info on this product read Zaval Java Resource Editor User's Guide
014: * (It comes within this package).
015: * The latest product version is always available from the product's homepage:
016: * http://www.zaval.org/products/jrc-editor/
017: * and from the SourceForge:
018: * http://sourceforge.net/projects/zaval0002/
019: *
020: * Contacts:
021: * Support : support@zaval.org
022: * Change Requests : change-request@zaval.org
023: * Feedback : feedback@zaval.org
024: * Other : info@zaval.org
025: *
026: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
027: *
028: * This program is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU General Public License
030: * (version 2) as published by the Free Software Foundation.
031: *
032: * This program is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
035: * GNU General Public License for more details.
036: *
037: * You should have received a copy of the GNU General Public License
038: * along with this program; if not, write to the Free Software
039: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
040: *
041: */package org.zaval.awt;
042:
043: import java.awt.*;
044: import java.util.*;
045:
046: public class ExGridLayout implements LayoutManager {
047: private static int hackIE401 = -1;
048:
049: protected Hashtable comptable;
050: protected GridBagConstraints defaultConstraints;
051:
052: protected int startx = 0;
053: protected int starty = 0;
054: protected int cols = 0, rows = 0;
055:
056: protected int[] widths;
057: protected int[] heights;
058:
059: public static int MAX_X = 2560;
060: public static int MAX_Y = 2560;
061:
062: public static int MIN_W = 1; // empty cell has a size {1,1}
063: public static int MIN_H = 1;
064:
065: protected GridBagConstraints[] bags = null;
066: protected Dimension[] sizes = null;
067:
068: protected double max_wx = 10.0;
069: protected double max_wy = 10.0;
070:
071: public void setMaxExpanded(double wx, double wy) {
072: max_wx = wx;
073: max_wy = wy;
074: }
075:
076: public void setLimits(int a, int b) {
077: MAX_X = Math.max(a, MAX_X);
078: MAX_Y = Math.max(b, MAX_Y);
079: widths = new int[MAX_X + 1];
080: heights = new int[MAX_Y + 1];
081: }
082:
083: public ExGridLayout() {
084: widths = new int[MAX_X + 1];
085: heights = new int[MAX_Y + 1];
086: comptable = new Hashtable();
087: defaultConstraints = new GridBagConstraints();
088: initFix();
089: }
090:
091: public ExGridLayout(int realw, int realh) {
092: widths = new int[MAX_X + 1];
093: heights = new int[MAX_Y + 1];
094: comptable = new Hashtable();
095: defaultConstraints = new GridBagConstraints();
096: initFix();
097: }
098:
099: public void setConstraints(Component comp,
100: GridBagConstraints constraints) {
101: bags = null;
102: sizes = null;
103: comptable.put(comp, constraints.clone());
104: }
105:
106: public GridBagConstraints getConstraints(Component comp) {
107: GridBagConstraints constraints = (GridBagConstraints) comptable
108: .get(comp);
109: return constraints;
110: }
111:
112: public void updateComponent(Container owner, Component old,
113: Component nw) {
114: GridBagConstraints gs = getConstraints(old);
115: if (comptable.containsKey(old)) {
116: comptable.remove(old);
117: comptable.put(nw, gs);
118: owner.remove(old);
119: owner.add(nw);
120: if (bags == null)
121: return;
122: arrange(owner, nw);
123: }
124: }
125:
126: public Point location(int xx, int yy) {
127: Point loc = new Point(0, 0);
128: int x = startx, y = starty, i;
129: for (i = 0; i < cols; ++i) {
130: x += widths[i];
131: if (x > xx)
132: break;
133: }
134: loc.x = i;
135: x = starty;
136: for (i = 0; i < rows; ++i) {
137: x += heights[i];
138: if (x > yy)
139: break;
140: }
141: loc.y = i;
142: return loc;
143: }
144:
145: private void initFix() {
146: if (hackIE401 == -1) {
147: String jver = "1.0.2.";
148: String jven = "Sun";
149: try {
150: jver = System.getProperty("java.version");
151: } catch (Throwable t) {
152: }
153: try {
154: jven = System.getProperty("java.vendor");
155: } catch (Throwable t) {
156: }
157:
158: //System.err.print("Vendor: "+jven+", JDK version: "+jver);
159: if (!jver.startsWith("1.0") && jven.startsWith("Microsoft"))
160: hackIE401 = 1;
161: else
162: hackIE401 = 0;
163: //if(hackIE401==1) System.err.println(", hack is needed");
164: //else System.err.println();
165: }
166: }
167:
168: private Dimension getSize(Container parent, boolean pref) {
169: int i;
170: makeInfo(pref);
171: Insets z = parent.insets();
172: int x = z.left + z.right, y = z.top + z.bottom;
173: for (i = 0; i < cols; ++i)
174: x += widths[i];
175: for (i = 0; i < rows; ++i)
176: y += heights[i];
177: return new Dimension(x, y);
178: }
179:
180: private void makeInfo(boolean pref) {
181: int i = comptable.size(), j, k;
182:
183: if (bags != null && bags.length == i)
184: return;
185:
186: bags = new GridBagConstraints[i];
187: sizes = new Dimension[i];
188:
189: Component comps;
190:
191: i = 0;
192: Enumeration en = comptable.keys();
193: while (en.hasMoreElements()) {
194: comps = (Component) en.nextElement();
195: GridBagConstraints gb = (GridBagConstraints) comptable
196: .get(comps);
197: sizes[i] = pref ? comps.preferredSize() : comps
198: .minimumSize();
199: sizes[i].width += gb.insets.right + gb.insets.left;
200: sizes[i].height += gb.insets.top + gb.insets.bottom;
201: bags[i++] = gb;
202: k = gb.gridx + gb.gridwidth;
203: // if(k>MAX_X){
204: // System.err.println("X_over: "+comps+","+sizes[i-1]+","+k+
205: // gb.gridx+","+gb.gridwidth);
206: // k=MAX_X;
207: // }
208: cols = Math.max(cols, k);
209: k = gb.gridy + gb.gridheight;
210: // if(k>MAX_Y){
211: // System.err.println("Y_over: "+comps+","+sizes[i-1]+","+k+
212: // gb.gridy+","+gb.gridheight);
213: // k=MAX_Y;
214: // }
215: rows = Math.max(rows, k);
216: }
217: // System.err.println("Cols="+cols+", rows="+rows);
218:
219: for (i = 0; i < cols; ++i)
220: widths[i] = MIN_W;
221: for (i = 0; i < rows; ++i)
222: heights[i] = MIN_H;
223:
224: for (i = 0; i < bags.length; ++i) {
225: for (j = 0; j < bags[i].gridwidth; ++j) {
226: k = bags[i].gridx + j;
227: widths[k] = Math.max(
228: sizes[i].width / bags[i].gridwidth, widths[k]);
229: }
230: for (j = 0; j < bags[i].gridheight; ++j) {
231: k = bags[i].gridy + j;
232: heights[k] = Math.max(sizes[i].height
233: / bags[i].gridheight, heights[k]);
234: }
235: }
236: }
237:
238: private void arrange(Container parent) {
239: arrange(parent, null);
240: }
241:
242: private void arrange(Container parent, Component modf) {
243: makeInfo(true);
244: Component[] comps = parent.getComponents();
245: Rectangle r = parent.bounds();
246: Insets pz = parent.insets();
247: int x, y, w, h;
248: int i, j;
249:
250: Dimension d = getSize(parent, true);
251: //d.width -= pz.left+pz.right;
252: //d.height -= pz.top+pz.bottom;
253:
254: if (r.width < 5 || r.height < 5)
255: return; // too small control area
256:
257: double fx = (double) (r.width ) / d.width;
258: double fy = (double) (r.height) / d.height;
259:
260: if (fx > max_wx)
261: fx = max_wx;
262: if (fy > max_wy)
263: fy = max_wy;
264:
265: for (i = 0; i < comps.length; ++i) {
266: GridBagConstraints gb = (GridBagConstraints) comptable
267: .get(comps[i]);
268: if (gb == null) {
269: comps[i].hide();
270: continue;
271: }
272: if (modf != null && modf != comps[i])
273: continue;
274: x = y = 0;
275: w = h = 0;
276: for (j = 0; j < gb.gridx; ++j)
277: x += widths[j];
278: for (; j < gb.gridx + gb.gridwidth; ++j)
279: w += widths[j];
280: for (j = 0; j < gb.gridy; ++j)
281: y += heights[j];
282: for (; j < gb.gridy + gb.gridheight; ++j)
283: h += heights[j];
284:
285: x += gb.insets.left;
286: y += gb.insets.top;
287: w -= gb.insets.right + gb.insets.left;
288: h -= gb.insets.bottom + gb.insets.top;
289: if (w <= 0 || h <= 0) {
290: // comps[i].hide();
291: continue;
292: }
293:
294: Dimension z = comps[i].preferredSize();
295:
296: if (gb.fill == gb.HORIZONTAL) {
297: h = z.height;
298: z.width = w;
299: } else if (gb.fill == gb.VERTICAL) {
300: w = z.width;
301: z.height = h;
302: } else if (gb.fill == gb.BOTH) {
303: z.width = w;
304: z.height = h;
305: }
306: // if(gb.fill==gb.NONE){
307: // w=z.width;
308: // h=z.height;
309: // }
310:
311: if (w > z.width) {
312: int wrap = w - z.width;
313: if (gb.anchor == gb.CENTER) {
314: x += wrap / 2;
315: w = z.width;
316: }
317: if (gb.anchor == gb.EAST || gb.anchor == gb.NORTHEAST
318: || gb.anchor == gb.SOUTHEAST) {
319: x += wrap;
320: w = z.width;
321: }
322: }
323:
324: if (h > z.height) {
325: int wrap = h - z.height;
326: if (gb.anchor == gb.CENTER) {
327: y += wrap / 2;
328: h = z.height;
329: } else if (gb.anchor == gb.SOUTH
330: || gb.anchor == gb.SOUTHWEST
331: || gb.anchor == gb.SOUTHEAST) {
332: y += wrap;
333: h = z.height;
334: }
335: }
336:
337: h = (int) (fy * h + 0.5);
338: w = (int) (fx * w + 0.5);
339:
340: // if(comps[i].isVisible()) comps[i].show();
341: comps[i].resize(w, h);
342: comps[i].move((int) (x * fx) + pz.left, (int) (y * fy)
343: + pz.top);
344: // IE 4.01 bugfix
345: if (hackIE401 == 1 && !comps[i].isVisible()) {
346: comps[i].show();
347: comps[i].hide();
348: }
349: }
350: }
351:
352: // -----------------------------------
353:
354: public void addLayoutComponent(String name, Component comp) {
355: }
356:
357: public void removeLayoutComponent(Component comp) {
358: }
359:
360: public Dimension preferredLayoutSize(Container parent) {
361: return getSize(parent, true);
362: }
363:
364: public Dimension minimumLayoutSize(Container parent) {
365: return getSize(parent, false);
366: }
367:
368: public void layoutContainer(Container parent) {
369: // System.err.println("Arrange: "+parent);
370: boolean isVis = parent.isVisible();
371: if (isVis)
372: parent.hide();
373: try {
374: arrange(parent);
375: // System.err.println("Arranged "+parent);
376: } finally {
377: if (isVis) {
378: parent.show();
379: parent.repaint();
380: }
381: }
382: }
383:
384: public String toString() {
385: return getClass().getName();
386: }
387: }
|