summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/features/FeatureFrameWindow.java
blob: f17980688d20ae6edd796ddd605335f7349a07e5 (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
/* *************************************************************************
 
                               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/license.txt. Use of this product might 
   require purchasing a commercial license from IT Mill Ltd. For guidelines 
   on usage, see license/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.demo.features;

import java.util.HashMap;
import java.util.List;

import com.itmill.toolkit.ui.*;
import com.itmill.toolkit.ui.Button.ClickEvent;

public class FeatureFrameWindow
	extends Feature
	implements Button.ClickListener {

	private Button addButton = new Button("Add to application", this, "addWin");
	private Button removeButton =
		new Button("Remove from application", this, "delWin");
	private FrameWindow demoWindow;
	private HashMap windowToFramesetMap = new HashMap();
	private int count = 0;

	protected Component getDemoComponent() {
		OrderedLayout l = new OrderedLayout();
		demoWindow = new FrameWindow("Feature Test Window");
		demoWindow.getFrameset().newFrame(
			createFrame(demoWindow.getFrameset()));

		// Example panel
		Panel show = new Panel("Test Window Control");
		((OrderedLayout) show.getLayout()).setOrientation(
			OrderedLayout.ORIENTATION_HORIZONTAL);
		show.addComponent(addButton);
		show.addComponent(removeButton);
		updateWinStatus();
		l.addComponent(show);

		// Properties
		PropertyPanel p = new PropertyPanel(demoWindow);
		p.dependsOn(addButton);
		p.dependsOn(removeButton);
		Form ap =
			p.createBeanPropertySet(
				new String[] { "width", "height", "name", "border", "theme" });
		ap.replaceWithSelect(
			"border",
			new Object[] {
				new Integer(Window.BORDER_DEFAULT),
				new Integer(Window.BORDER_NONE),
				new Integer(Window.BORDER_MINIMAL)},
			new Object[] { "Default", "None", "Minimal" });

		p.addProperties("FrameWindow Properties", ap);
		l.addComponent(p);

		return l;
	}

	protected String getDescriptionXHTML() {
		return "<p>This component implements a window that contains a hierarchical set of frames. "
			+ "Each frame can contain a web-page, window or a set of frames that divides the space "
			+ "horizontally or vertically.</p>";
	}

	protected String getExampleSrc() {
		return "FrameWindow f = new FrameWindow(\"Frame example\");\n"
			+ "f.getFrameset().newFrame(window);\n"
			+ "f.getFrameset().newFrame(resource,\"targetName\");\n";
	}

	protected String getImage() {
		return "framewindow.jpg";
	}

	protected String getTitle() {
		return "FrameWindow";
	}

	public void addWin() {
		getApplication().addWindow(demoWindow);
		updateWinStatus();
	}

	public void delWin() {
		getApplication().removeWindow(demoWindow);
		updateWinStatus();
	}

	private void updateWinStatus() {
		if (demoWindow.getApplication() == null) {
			addButton.setEnabled(true);
			removeButton.setEnabled(false);
		} else {
			addButton.setEnabled(false);
			removeButton.setEnabled(true);
		}
	}

	public void buttonClick(ClickEvent event) {

		if (event.getButton().getCaption().equals("Remove")) {
			Window w = event.getButton().getWindow();
			FrameWindow.Frameset fs =
				(FrameWindow.Frameset) windowToFramesetMap.get(w);
			if (fs == demoWindow.getFrameset() && fs.size() <= 1) {
				// Do not remove the last frame	
			} else if (fs.size() > 1) {
				fs.removeFrame(fs.getFrame(w.getName()));
				windowToFramesetMap.remove(w);
			} else {
				FrameWindow.Frameset p = fs.getParentFrameset();
				if (p != demoWindow.getFrameset() || p.size() > 1)
					p.removeFrame(fs);
				if (p.size() == 0)
					p.newFrame(createFrame(p));
			}
		}

		if (event.getButton().getCaption().equals("Split")) {
			Window w = event.getButton().getWindow();
			FrameWindow.Frameset fs =
				(FrameWindow.Frameset) windowToFramesetMap.get(w);
			int index = 0;
			List l = fs.getFrames();
			while (index < l.size() && fs.getFrame(index).getWindow() != w)
				index++;
			fs.removeFrame(fs.getFrame(w.getName()));
			windowToFramesetMap.remove(w);
			if (index > fs.size())
				index = fs.size();
			fs = fs.newFrameset((Math.random() > 0.5), index);
			for (int i = 2 + (int) (Math.random() * 2.0); i > 0; i--)
				fs.newFrame(createFrame(fs));
		}
	}

	private Window createFrame(FrameWindow.Frameset fs) {
		Window w = new Window();
		w.addComponent(
			new Label("<b>Frame: " + (++count) + "</b>", Label.CONTENT_UIDL));
		w.addComponent(new Button("Split", this));
		w.addComponent(new Button("Remove", this));
		windowToFramesetMap.put(w, fs);
		return w;
	}
}