001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: //
025: // package
026: //
027: package jacareto.toolkit.awt;
028:
029: import java.awt.Component;
030: import java.awt.Container;
031: import java.awt.Dimension;
032: import java.awt.Insets;
033: import java.awt.LayoutManager;
034:
035: //JAVA
036: import java.io.Serializable;
037:
038: import java.util.StringTokenizer;
039: import java.util.Vector;
040:
041: /**
042: * Layout Manager, der es ermöglicht, die Position und den Platz den eine Komponente beansprucht
043: * relativ zum vorhandenen Platz anzugeben. Die Angaben werden in Prozent gemacht,
044: * Positionsangaben erfolgen relativ zur linken oberen Ecke. <br>
045: * <pre>
046: * z.B.
047: * setLayout(new RatioLayout());
048: * add("0,0;.45,.5", new Button("OK")); //0% nach rechts verschoben, 0% nach unten verschoben; 45% des verfügbaren Platzes in vertikaler Richtung, 50% des verfügbaren Platzes, in horizontaler Richtung
049: * add(".75,.75", new Button("QUIT")); //75% nach rechts verschoben, 75% nach unten verschoben; benutze die bevorzugte Größe der Komponente
050: * </pre>
051: */
052: public class RatioLayout implements LayoutManager, Serializable {
053: //ATTRIBUTE
054:
055: /** Speichert die gesetzten Werte für jedes Objekt. */
056: Vector ratios = new Vector(10);
057:
058: /** Speichert die vom Layout Manager verwalteten Objekte. */
059: Vector components = new Vector(10);
060:
061: //METHODEN
062:
063: /**
064: * Fügt eine Komponenete zum Layout hinzu.
065: * <pre>
066: * Vorbedingung : comp != null && r != null
067: * Nachbedingung: components.indexOf(comp) != -1
068: * </pre>
069: *
070: * @param r Verhalten der Komponente(siehe Klassenkopf)
071: * @param comp Komponente, die eingefügt werden soll
072: */
073: public void addLayoutComponent(String r, Component comp) {
074: ratios.addElement(r);
075: components.addElement(comp);
076: }
077:
078: /**
079: * Entfernt eine Komponente aus dem Layout
080: * <pre>
081: * Vorbedingung : comp != null
082: * Nachbedingung: components.indexOf(comp) == -1
083: * </pre>
084: *
085: * @param comp Zu entfernende Komponente
086: */
087: public void removeLayoutComponent(Component comp) {
088: int i = components.indexOf(comp);
089:
090: if (i != -1) {
091: ratios.removeElementAt(i);
092: components.removeElementAt(i);
093: }
094: }
095:
096: /**
097: * Aendert das Layout einer Komponente
098: * <pre>
099: * Vorbedingung : comp != null
100: * Nachbedingung: components.indexOf(comp) == -1
101: * </pre>
102: *
103: * @param comp Zu aendernde Komponente
104: * @param newLayout DOCUMENT ME!
105: */
106: public void changeLayoutComponent(Component comp, String newLayout) {
107: int i = components.indexOf(comp);
108:
109: if (i != -1) {
110: ratios.removeElementAt(i);
111: ratios.insertElementAt(newLayout, i);
112: }
113: }
114:
115: /**
116: * Bevorzugte Größe des Containers.
117: * <pre>
118: * Vorbedingung : target != null
119: * Nachbedingung: true
120: * </pre>
121: *
122: * @param target Der betroffene Kontainer
123: *
124: * @return DOCUMENT ME!
125: */
126: public Dimension preferredLayoutSize(Container target) {
127: return target.getSize();
128: }
129:
130: /**
131: * Minimale Größe des Kontainers.
132: * <pre>
133: * Vorbedingung : target != null
134: * Nachbedingung: true
135: * </pre>
136: *
137: * @param target Der betroffene Kontainer
138: *
139: * @return DOCUMENT ME!
140: */
141: public Dimension minimumLayoutSize(Container target) {
142: return target.getSize();
143: }
144:
145: /**
146: * Platziert die Komponenten in dem Container entsprechend der gemachten Angaben.
147: * <pre>
148: * Vorbedingung : target != null
149: * Nachbedingung: true
150: * </pre>
151: *
152: * @param target Der betroffene Kontainer
153: */
154: public void layoutContainer(Container target) {
155: Insets insets = target.getInsets();
156: int ncomponents = components.size();
157:
158: Dimension d = target.getSize();
159: d.width -= (insets.left + insets.right);
160: d.height -= (insets.top + insets.bottom);
161:
162: for (int i = 0; i < ncomponents; i++) {
163: Component comp = target.getComponent(i);
164:
165: if (ratios.size() > i) {
166: StringTokenizer st = new StringTokenizer(
167: (String) ratios.elementAt(i), ", \t;");
168: float rx = Float.valueOf(st.nextToken()).floatValue();
169: float ry = Float.valueOf(st.nextToken()).floatValue();
170: float rw = 0;
171: float rh = 0;
172: int w;
173: int h;
174:
175: if (st.hasMoreElements()) { // get width, height if they exist
176: rw = Float.valueOf(st.nextToken()).floatValue();
177: rh = Float.valueOf(st.nextToken()).floatValue();
178: w = (int) (d.width * rw);
179: h = (int) (d.height * rh);
180: } else {
181: Dimension compDim = comp.getPreferredSize();
182: w = compDim.width;
183: h = compDim.height;
184: }
185:
186: int x = (int) (d.width * rx);
187: int y = (int) (d.height * ry);
188: comp.setBounds(x + insets.left, y + insets.top, w, h);
189: }
190: }
191: }
192: }
|