Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 605c0b25c2b8e01f1bd39b851736098bc01726a5 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007 Symbian Software Limited and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Bala Torati (Symbian) - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.ui.templateengine.pages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.core.templateengine.TemplateEngineUtil;
import org.eclipse.cdt.ui.templateengine.SimpleElementException;
import org.eclipse.cdt.ui.templateengine.uitree.InputUIElement;
import org.eclipse.cdt.ui.templateengine.uitree.SimpleUIElementGroup;
import org.eclipse.cdt.ui.templateengine.uitree.UIElement;


/**
 * The UIPagesProvider creates a Map of UIPages. The Map will have ID as key,
 * UIPage as value. The sequence of call to get Map of UIPages. 1.
 * clearOrderVector() for all PropertyGroup Elements. 2. getUIPages(...)
 * 
 */
public class UIPagesProvider {

	/**
	 * maintains the Page display order.
	 */
	private List/*<String>*/ orderVector;


	public UIPagesProvider() {
		orderVector = new ArrayList();
	}
	
	/**
	 * after getting this clear the Vector.
	 * 
	 * @return Vector
	 */
	public List/*<String>*/ getOrderVector() {
		return orderVector;
	}

	/**
	 * re-initialize the Vector.
	 */
	public void clearOrderVector() {
		orderVector = new ArrayList/*<String>*/();
	}

	/**
	 * This class has methods to return an HashMap of UIPages. The UIPages will
	 * correspond to UIElement group passed as parameter to this method. For a
	 * group UIElement, the children count is taken. An array of UIPage for the
	 * count is created. The same is initialized with UIPages.
	 * 
	 * @param uiElement
	 *            UIElement group root element. Which can be converted to a
	 *            UIPage.
	 * @param valueStore
	 * @return HashMap, UIPages corresonding to param aUIElement.
	 */
	public Map/*<String, UIWizardPage>*/ getWizardUIPages(UIElement uiElement, Map/*<String, String>*/ valueStore) {
		int childCount = 0;

		try {
			childCount = uiElement.getChildCount();
		} catch (SimpleElementException e) {
			TemplateEngineUtil.log(e);
		}

		// HashMap of UIPages
		HashMap/*<String, UIWizardPage>*/ pageMap = new HashMap/*<String, UIWizardPage>*/();

		// If uiElement contains other group elements as children.
		if (hasChildUIGroupElement(uiElement)) {

			for (int i = 0; i < childCount; i++) {
				try {
					pageMap.putAll(getWizardUIPages(uiElement.getChild(i), valueStore)); // recursion
				} catch (SimpleElementException e) {
					TemplateEngineUtil.log(e);
				}
			}
		}
		else {
			if ((hasChildUIElement(uiElement))) {
				String title = (String) uiElement.getAttributes().get(UIElement.TITLE);
				String description = (String) (uiElement.getAttributes()).get(UIElement.DESCRIPTION);
				UIWizardPage uiPage = new UIWizardPage(title, description, uiElement, valueStore);

				pageMap.put((uiElement.getAttributes()).get(UIElement.ID), uiPage);
				addToOrderVector((String) (uiElement.getAttributes()).get(UIElement.ID));
			}
		}
		return pageMap;
	}

	/**
	 * whether the given (node in UIElementTree) UIElement contains children of
	 * group type.
	 * 
	 * @param parent
	 * @return boolean, true if it does, false otherwise.
	 */
	public boolean hasChildUIGroupElement(UIElement parent) {
		boolean retVal = false;
		try {
			if (parent.getChildCount() > 0) {
				for (int i = 0; i < parent.getChildCount(); i++) {
					if (parent.getChild(i) instanceof SimpleUIElementGroup) {
						retVal = true;
						break;
					}
				}
			}
		} catch (SimpleElementException see) {
			retVal = false;
		}
		return retVal;
	}

	/**
	 * whether the given (node in UIElementTree) UIElement contains children of
	 * UIElement type.
	 * 
	 * @param parent
	 * @return boolean, true if it does, false otherwise.
	 */
	public boolean hasChildUIElement(UIElement parent) {
		boolean retVal = false;
		try {
			if (parent.getChildCount() > 0) {
				for (int i = 0; i < parent.getChildCount(); i++) {
					if (parent.getChild(i) instanceof InputUIElement) {
						retVal = true;
						break;
					}
				}
			}
		} catch (SimpleElementException see) {
			retVal = false;
		}
		return retVal;
	}

	/**
	 * If the order vector contains the page id return, do not add it to order
	 * vector. HashMap will not allow duplicate keys.
	 * 
	 * @param pageId
	 */
	private void addToOrderVector(String pageId) {
		String containerIds = null;
		for (int i = 0; i < orderVector.size(); i++) {
			containerIds = (String) orderVector.get(i);
			if (containerIds.equalsIgnoreCase(pageId))
				return;
		}
		orderVector.add(pageId);
	}
}

Back to the top