1 /*
2  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details ( see the LICENSE file ).
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 package org.gudy.azureus2.ui.swt.shells;
20 
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.StackLayout;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Shell;
37 import org.gudy.azureus2.core3.util.Debug;
38 import org.gudy.azureus2.ui.swt.Utils;
39 import org.gudy.azureus2.ui.swt.components.shell.ShellFactory;
40 
41 import com.aelitis.azureus.ui.swt.utils.FontUtils;
42 
43 abstract public class MultipageWizard
44 {
45 
46 	private Shell shell;
47 
48 	private int shellStyle;
49 
50 	private Composite topPanel;
51 
52 	private Composite contentPanel;
53 
54 	private Label titleLabel;
55 
56 	private Label descriptionLabel;
57 
58 	/**
59 	 * A map of pageID(String)/<code>IWizardPage</code>; using LinkedHashMap since the order the pages are inserted is important
60 	 */
61 	private Map pages = new LinkedHashMap();
62 
63 	private StackLayout contentStackLayout;
64 
65 	private IWizardPage currentPage;
66 
67 	private IWizardPage previousPage;
68 
69 	private List initializedPages = new ArrayList();
70 
createPages()71 	public abstract void createPages();
72 
init()73 	private void init() {
74 
75 		shell = ShellFactory.createMainShell(shellStyle);
76 
77 		createControls();
78 		createPages();
79 
80 	}
81 
createControls()82 	private void createControls() {
83 		GridLayout gLayout = new GridLayout();
84 		gLayout.marginHeight = 0;
85 		gLayout.marginWidth = 0;
86 		gLayout.verticalSpacing = 0;
87 		shell.setLayout(gLayout);
88 		Utils.setShellIcon(shell);
89 
90 		topPanel = new Composite(shell, SWT.NONE);
91 		Utils.setLayoutData(topPanel, new GridData(SWT.FILL, SWT.TOP, true, false));
92 		GridLayout gLayout1 = new GridLayout();
93 		gLayout1.marginBottom = 10;
94 		topPanel.setLayout(gLayout1);
95 		topPanel.setBackground(shell.getDisplay().getSystemColor(
96 				SWT.COLOR_LIST_BACKGROUND));
97 		topPanel.setBackgroundMode(SWT.INHERIT_FORCE);
98 
99 		Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
100 		Utils.setLayoutData(separator1, new GridData(SWT.FILL, SWT.CENTER, true, false));
101 
102 		contentPanel = new Composite(shell, SWT.NONE);
103 		Utils.setLayoutData(contentPanel, new GridData(SWT.FILL, SWT.FILL, true, true));
104 		contentStackLayout = new StackLayout();
105 		contentPanel.setLayout(contentStackLayout);
106 
107 		titleLabel = new Label(topPanel, SWT.NONE);
108 		Utils.setLayoutData(titleLabel, new GridData(SWT.FILL, SWT.TOP, true, false));
109 		FontUtils.setFontHeight(titleLabel, 16, SWT.NORMAL);
110 
111 		descriptionLabel = new Label(topPanel, SWT.WRAP);
112 		GridData gData = new GridData(SWT.FILL, SWT.FILL, true, true);
113 		gData.horizontalIndent = 10;
114 		Utils.setLayoutData(descriptionLabel, gData);
115 
116 		shell.layout(true, true);
117 
118 	}
119 
fullScreen(boolean isFullScreen)120 	public void fullScreen(boolean isFullScreen) {
121 		topPanel.setVisible(false == isFullScreen);
122 		((GridData) topPanel.getLayoutData()).exclude = isFullScreen;
123 		shell.layout(true, true);
124 	}
125 
addPage(IWizardPage page)126 	public boolean addPage(IWizardPage page) {
127 		if (null == page) {
128 			return false;
129 		}
130 		if (true == pages.containsKey(page.getPageID())) {
131 			Debug.out("MultipageWizard:: a page with this ID already exists ID:"
132 					+ page.getPageID());
133 			return false;
134 		}
135 
136 		pages.put(page.getPageID(), page);
137 
138 		if (true == page.isInitOnStartup()) {
139 			page.createControls(contentPanel);
140 			initializedPages.add(page.getPageID());
141 		}
142 		return true;
143 	}
144 
isFirstPage(String pageID)145 	public boolean isFirstPage(String pageID) {
146 		if (false == pages.isEmpty()) {
147 			return pageID.equals(((IWizardPage) pages.values().iterator().next()).getPageID());
148 		}
149 		return false;
150 	}
151 
isLastPage(String pageID)152 	public boolean isLastPage(String pageID) {
153 		if (false == pages.isEmpty()) {
154 			IWizardPage page = null;
155 			for (Iterator iterator = pages.values().iterator(); iterator.hasNext();) {
156 				page = (IWizardPage) iterator.next();
157 			}
158 
159 			if (null != page) {
160 				return page.getPageID().equals(pageID);
161 			}
162 		}
163 		return false;
164 	}
165 
removePage(IWizardPage page)166 	public boolean removePage(IWizardPage page) {
167 		if (null == page) {
168 			return false;
169 		}
170 
171 		if (false == pages.containsKey(page.getPageID())) {
172 			Debug.out("MultipageWizard:: a page with this ID is not found ID:"
173 					+ page.getPageID());
174 			return false;
175 		}
176 		pages.remove(page.getPageID());
177 		page.performDispose();
178 		return true;
179 	}
180 
showPage(String pageID)181 	public void showPage(String pageID) {
182 		if (false == pages.containsKey(pageID)) {
183 			Debug.out("MultipageWizard:: a page with this ID is not found ID:"
184 					+ pageID);
185 			return;
186 		}
187 
188 		IWizardPage page = (IWizardPage) pages.get(pageID);
189 
190 		if (null != currentPage) {
191 			if (true == currentPage.getPageID().equals(page.getPageID())) {
192 				return;
193 			}
194 			currentPage.performAboutToBeHidden();
195 		}
196 
197 		/*
198 		 * Initializing the page if not done already
199 		 */
200 		if (false == initializedPages.contains(page.getPageID())) {
201 			page.createControls(contentPanel);
202 			initializedPages.add(page.getPageID());
203 		}
204 
205 		page.performAboutToBeShown();
206 
207 		previousPage = currentPage;
208 		currentPage = page;
209 		contentStackLayout.topControl = page.getControl();
210 
211 		update();
212 
213 		contentPanel.layout(true);
214 	}
215 
open()216 	public void open() {
217 		/*
218 		 * Show the first page
219 		 */
220 		if (false == pages.isEmpty()) {
221 			IWizardPage page = (IWizardPage) pages.values().iterator().next();
222 			showPage(page.getPageID());
223 		}
224 
225 		shell.open();
226 	}
227 
update()228 	private void update() {
229 		if (null != currentPage) {
230 			setText(currentPage.getWindowTitle());
231 			setTitle(currentPage.getTitle());
232 			setDescription(currentPage.getDesciption());
233 		}
234 	}
235 
setTitle(String title)236 	public void setTitle(String title) {
237 		titleLabel.setText(title + "");
238 	}
239 
setDescription(String description)240 	public void setDescription(String description) {
241 		descriptionLabel.setText(description + "");
242 	}
243 
244 	/**
245 	 * Return the <code>IWizardPage</code> with the given id; returns <code>null</code> if page is not found
246 	 * @param pageID
247 	 * @return
248 	 */
getPage(String pageID)249 	public IWizardPage getPage(String pageID) {
250 		if (false == pages.containsKey(pageID)) {
251 			Debug.out("MultipageWizard:: a Page with this ID is not found ID:"
252 					+ pageID);
253 			return null;
254 		}
255 
256 		return (IWizardPage) pages.get(pageID);
257 	}
258 
performCancel()259 	public void performCancel() {
260 		close();
261 	}
262 
performNext()263 	public void performNext() {
264 		if (true == pages.isEmpty()) {
265 			return;
266 		}
267 
268 		if (null == currentPage) {
269 			IWizardPage page = (IWizardPage) pages.values().iterator().next();
270 			showPage(page.getPageID());
271 		} else {
272 			boolean foundCurrent = false;
273 			for (Iterator iterator = pages.values().iterator(); iterator.hasNext();) {
274 				IWizardPage page = (IWizardPage) iterator.next();
275 				if (true == foundCurrent) {
276 					showPage(page.getPageID());
277 					return;
278 				}
279 
280 				if (page.getPageID().equals(currentPage.getPageID())) {
281 					foundCurrent = true;
282 				}
283 			}
284 
285 			if (false == foundCurrent) {
286 				Debug.out("MultipageWizard:: there is no more page to go to");
287 			}
288 
289 		}
290 	}
291 
performBack()292 	public void performBack() {
293 
294 		if (null != previousPage) {
295 			showPage(previousPage.getPageID());
296 		}
297 	}
298 
299 	/* ===========================================
300 		 * Below are just some convenience delegations
301 		 * =========================================== */
getShell()302 	public Shell getShell() {
303 		return shell;
304 	}
305 
close()306 	public void close() {
307 		shell.close();
308 	}
309 
getData(String key)310 	public Object getData(String key) {
311 		return shell.getData(key);
312 	}
313 
getLocation()314 	public Point getLocation() {
315 		return shell.getLocation();
316 	}
317 
getSize()318 	public Point getSize() {
319 		return shell.getSize();
320 	}
321 
getText()322 	public String getText() {
323 		return shell.getText();
324 	}
325 
getToolTipText()326 	public String getToolTipText() {
327 		return shell.getToolTipText();
328 	}
329 
setBounds(int x, int y, int width, int height)330 	public void setBounds(int x, int y, int width, int height) {
331 		shell.setBounds(x, y, width, height);
332 	}
333 
setData(String key, Object value)334 	public void setData(String key, Object value) {
335 		shell.setData(key, value);
336 	}
337 
setLocation(int x, int y)338 	public void setLocation(int x, int y) {
339 		shell.setLocation(x, y);
340 	}
341 
setSize(int width, int height)342 	public void setSize(int width, int height) {
343 		shell.setSize(width, height);
344 	}
345 
setText(String string)346 	public void setText(String string) {
347 		shell.setText(string);
348 	}
349 
setToolTipText(String string)350 	public void setToolTipText(String string) {
351 		shell.setToolTipText(string);
352 	}
353 
setVisible(boolean visible)354 	public void setVisible(boolean visible) {
355 		shell.setVisible(visible);
356 	}
357 
getImage()358 	public Image getImage() {
359 		return shell.getImage();
360 	}
361 
setImage(Image image)362 	public void setImage(Image image) {
363 		shell.setImage(image);
364 	}
365 
366 }
367