summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/ui/OrderedLayout.java
blob: 0b7cc04a0900da18744669ef0fffbf5b141daf37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* *************************************************************************
 
 IT Mill Toolkit 

 Development of Browser User Interfaces Made Easy

 Copyright (C) 2000-2006 IT Mill Ltd
 
 *************************************************************************

 This product is distributed under commercial license that can be found
 from the product package on license.pdf. Use of this product might 
 require purchasing a commercial license from IT Mill Ltd. For guidelines 
 on usage, see licensing-guidelines.html

 *************************************************************************
 
 For more information, contact:
 
 IT Mill Ltd                           phone: +358 2 4802 7180
 Ruukinkatu 2-4                        fax:   +358 2 4802 7181
 20540, Turku                          email:  info@itmill.com
 Finland                               company www: www.itmill.com
 
 Primary source for information and releases: www.itmill.com

 ********************************************************************** */

package com.itmill.toolkit.ui;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;

/**
 * Ordered layout.
 * 
 * <code>OrderedLayout</code> is a component container, which shows the
 * subcomponents in the order of their addition in specified orientation.
 * 
 * @author IT Mill Ltd.
 * @version
 * @VERSION@
 * @since 3.0
 */
public class OrderedLayout extends AbstractLayout {

	/* Predefined orientations ***************************************** */

	/**
	 * Components are to be laid out vertically.
	 */
	public static int ORIENTATION_VERTICAL = 0;

	/**
	 * Components are to be laid out horizontally.
	 */
	public static int ORIENTATION_HORIZONTAL = 1;

	/**
	 * Custom layout slots containing the components.
	 */
	private LinkedList components = new LinkedList();

	/**
	 * Mapping from components to alignments (horizontal + vertical).
	 */
	private Map componentToAlignment = new HashMap();

	/**
	 * Contained component should be aligned horizontally to the left.
	 */
	public static final int ALIGNMENT_LEFT = 1;

	/**
	 * Contained component should be aligned horizontally to the right.
	 */
	public static final int ALIGNMENT_RIGHT = 2;

	/**
	 * Contained component should be aligned vertically to the top.
	 */
	public static final int ALIGNMENT_TOP = 4;

	/**
	 * Contained component should be aligned vertically to the bottom.
	 */
	public static final int ALIGNMENT_BOTTOM = 8;

	/**
	 * Contained component should be horizontally aligned to center.
	 */
	public static final int HORIZONTAL_ALIGNMENT_CENTER = 16;

	/**
	 * Contained component should be vertically aligned to center.
	 */
	public static final int VERTICAL_ALIGNMENT_CENTER = 32;

	/**
	 * Orientation of the layout.
	 */
	private int orientation;

	/**
	 * Is spacing between contained components enabled. Defaults to false.
	 */
	private boolean spacing = false;

	/**
	 * Creates a new ordered layout. The order of the layout is
	 * <code>ORIENTATION_VERTICAL</code>.
	 */
	public OrderedLayout() {
		orientation = ORIENTATION_VERTICAL;
	}

	/**
	 * Create a new ordered layout. The orientation of the layout is given as
	 * parameters.
	 * 
	 * @param orientation
	 *            the Orientation of the layout.
	 */
	public OrderedLayout(int orientation) {
		this.orientation = orientation;
	}

	/**
	 * Gets the component UIDL tag.
	 * 
	 * @return the Component UIDL tag as string.
	 */
	public String getTag() {
		return "orderedlayout";
	}

	/**
	 * Add a component into this container. The component is added to the right
	 * or under the previous component.
	 * 
	 * @param c
	 *            the component to be added.
	 */
	public void addComponent(Component c) {
		components.add(c);
		super.addComponent(c);
		requestRepaint();
	}

	/**
	 * Adds a component into this container. The component is added to the left
	 * or on top of the other components.
	 * 
	 * @param c
	 *            the component to be added.
	 */
	public void addComponentAsFirst(Component c) {
		components.addFirst(c);
		super.addComponent(c);
		requestRepaint();
	}

	/**
	 * Adds a component into indexed position in this container.
	 * 
	 * @param c
	 *            the component to be added.
	 * @param index
	 *            the Index of the component position. The components currently
	 *            in and after the position are shifted forwards.
	 */
	public void addComponent(Component c, int index) {
		components.add(index, c);
		super.addComponent(c);
		requestRepaint();
	}

	/**
	 * Removes the component from this container.
	 * 
	 * @param c
	 *            the component to be removed.
	 */
	public void removeComponent(Component c) {
		super.removeComponent(c);
		components.remove(c);
		componentToAlignment.remove(c);
		requestRepaint();
	}

	/**
	 * Gets the component container iterator for going trough all the components
	 * in the container.
	 * 
	 * @return the Iterator of the components inside the container.
	 */
	public Iterator getComponentIterator() {
		return components.iterator();
	}

	/**
	 * Paints the content of this component.
	 * 
	 * @param target
	 *            the Paint Event.
	 * @throws PaintException
	 *             if the paint operation failed.
	 */
	public void paintContent(PaintTarget target) throws PaintException {
		super.paintContent(target);

		// Adds the attributes: orientation
		// note that the default values (b/vertical) are omitted
		if (orientation == ORIENTATION_HORIZONTAL)
			target.addAttribute("orientation", "horizontal");

		if (this.spacing)
			target.addAttribute("spacing", this.spacing);

		// Store alignment info in this String
		String alignments = "";

		// Adds all items in all the locations
		for (Iterator i = components.iterator(); i.hasNext();) {
			Component c = (Component) i.next();
			if (c != null) {
				// Paint child component UIDL
				c.paint(target);

				// Get alignment info for component
				if (componentToAlignment.containsKey(c))
					alignments += ((Integer) componentToAlignment.get(c))
							.intValue();
				else
					// Default alignment is top-left
					alignments += (ALIGNMENT_TOP + ALIGNMENT_LEFT);
				if (i.hasNext())
					alignments += ",";
			}
		}

		// Add child component alignment info to layout tag
		target.addAttribute("alignments", alignments);
	}

	/**
	 * Gets the orientation of the container.
	 * 
	 * @return the Value of property orientation.
	 */
	public int getOrientation() {
		return this.orientation;
	}

	/**
	 * Set the orientation of the container.
	 * 
	 * @param orientation
	 *            the New value of property orientation.
	 */
	public void setOrientation(int orientation) {

		// Checks the validity of the argument
		if (orientation < ORIENTATION_VERTICAL
				|| orientation > ORIENTATION_HORIZONTAL)
			throw new IllegalArgumentException();

		this.orientation = orientation;
		requestRepaint();
	}

	/* Documented in superclass */
	public void replaceComponent(Component oldComponent, Component newComponent) {

		// Gets the locations
		int oldLocation = -1;
		int newLocation = -1;
		int location = 0;
		for (Iterator i = components.iterator(); i.hasNext();) {
			Component component = (Component) i.next();

			if (component == oldComponent)
				oldLocation = location;
			if (component == newComponent)
				newLocation = location;

			location++;
		}

		if (oldLocation == -1)
			addComponent(newComponent);
		else if (newLocation == -1) {
			removeComponent(oldComponent);
			addComponent(newComponent, oldLocation);
		} else {
			if (oldLocation > newLocation) {
				components.remove(oldComponent);
				components.add(newLocation, oldComponent);
				components.remove(newComponent);
				componentToAlignment.remove(newComponent);
				components.add(oldLocation, newComponent);
			} else {
				components.remove(newComponent);
				components.add(oldLocation, newComponent);
				components.remove(oldComponent);
				componentToAlignment.remove(oldComponent);
				components.add(newLocation, oldComponent);
			}

			requestRepaint();
		}
	}

	/**
	 * Set alignment for one contained component in this layout. Alignment is
	 * calculated as a bit mask of the two passed values.
	 * 
	 * @param childComponent
	 *            the component to align within it's layout cell.
	 * @param horizontalAlignment
	 *            the horizontal alignment for the child component (left,
	 *            center, right).
	 * @param verticalAlignment
	 *            the vertical alignment for the child component (top, center,
	 *            bottom).
	 */
	public void setComponentAlignment(Component childComponent,
			int horizontalAlignment, int verticalAlignment) {
		componentToAlignment.put(childComponent, new Integer(
				horizontalAlignment + verticalAlignment));
	}

	/**
	 * Enable spacing between child components within this layout.
	 * 
	 * <p>
	 * <strong>NOTE:</strong> This will only affect spaces between components,
	 * not also all around spacing of the layout (i.e. do not mix this with HTML
	 * Table elements cellspacing-attribute). Use {@link #setMargin(boolean)} to
	 * add extra space around the layout.
	 * </p>
	 * 
	 * @param enabled
	 */
	public void setSpacing(boolean enabled) {
		this.spacing = enabled;
	}
}