aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/ui/ExpandLayout.java
blob: 253edb81556179dd6381b6f665d4cf40da48b2b0 (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
package com.itmill.toolkit.ui;

import java.util.Iterator;

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

/**
 * TODO finish documentation
 * 
 * our layouts (except custom layout of course) don't currently work at all with
 * relative widths. This layout tries to cope with this issue.
 * 
 * basically this is ordered layout which has Sizeable interface 100 % height &
 * width by default
 * 
 * all contained components may also have Sizeable interfaces sizes
 * 
 * can be used to build flexible layout where some component gets all the space
 * other components don't use. Or just provide expanded container.
 * 
 */
public class ExpandLayout extends OrderedLayout implements Sizeable {

	private int height = 100;

	private int width = 100;

	private int widthUnit = UNITS_PERCENTAGE;

	private int heightUnit = UNITS_PERCENTAGE;

	private Component expanded;

	public ExpandLayout() {

	}

	public ExpandLayout(int orientation) {
		this();
		setOrientation(orientation);
	}

	/**
	 * @param c
	 *            Component which container will be maximized
	 */
	public void expand(Component c) {
		this.expanded = c;
		requestRepaint();
	}

	public String getTag() {
		return "expandlayout";
	}

	public void paintContent(PaintTarget target) throws PaintException {
		// Size
		if (getHeight() >= 0)
			target.addAttribute("height", "" + getHeight()
					+ Sizeable.UNIT_SYMBOLS[getHeightUnits()]);
		if (getWidth() >= 0)
			target.addAttribute("width", "" + getWidth()
					+ Sizeable.UNIT_SYMBOLS[getWidthUnits()]);

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

		// Adds all items in all the locations
		for (Iterator i = getComponentIterator(); i.hasNext();) {
			Component c = (Component) i.next();
			if (c != null) {
				target.startTag("cc");
				if (c == expanded)
					target.addAttribute("expanded", true);
				c.paint(target);
				target.endTag("cc");
			}
		}
	}

	public void addComponent(Component c, int index) {
		if (expanded == null) {
			expanded = c;
		}
		super.addComponent(c, index);
	}

	public void addComponent(Component c) {
		if (expanded == null) {
			expanded = c;
		}
		super.addComponent(c);
	}

	public void addComponentAsFirst(Component c) {
		if (expanded == null) {
			expanded = c;
		}
		super.addComponentAsFirst(c);
	}

	public void removeComponent(Component c) {
		super.removeComponent(c);
		if (c == expanded && this.getComponentIterator().hasNext())
			expanded = (Component) this.getComponentIterator().next();
		else
			expanded = null;
	}

	public void replaceComponent(Component oldComponent, Component newComponent) {
		super.replaceComponent(oldComponent, newComponent);
		if (oldComponent == expanded)
			expanded = newComponent;
	}

	public int getHeight() {
		return height;
	}

	public int getHeightUnits() {
		return heightUnit;
	}

	public int getWidth() {
		return width;
	}

	public int getWidthUnits() {
		return widthUnit;
	}

	public void setHeight(int height) {
		this.height = height;

	}

	public void setHeightUnits(int units) {
		this.heightUnit = units;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setWidthUnits(int units) {
		this.widthUnit = units;
	}
}