Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 005d21d856ce18acfc6dbc1e0c989df1c2191d7b (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
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
/*******************************************************************************
 * Copyright (c) 2006 Oracle Corporation.
 * 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:  Oracle
 *******************************************************************************/
package org.eclipse.jst.jsf.core.internal.jsflibraryconfig;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.jst.jsf.core.internal.Messages;

/**
 * Model for the custom control <b>JSFLibraryConfigControl</b>.
 * A JSFLibraryConfigModel object is initialized from a source and updated with 
 * selected implementation and component libraries when selections are changed.
 * 
 * @author Justin Chen - Oracle
 */
public class JSFLibraryConfigModel {
	final private JSFLibraryConfiglModelSource data;
	final private JSFLibraryRegistryUtil jsfLibReg;
	private List colJSFImplLib;
	private List colJSFCompLib;	
	
	/**
	 * Private constructor.  
	 * @param data
	 */
	private JSFLibraryConfigModel(JSFLibraryConfiglModelSource data) {
		this.data = data;
		this.jsfLibReg = JSFLibraryRegistryUtil.getInstance();
	}
		
	/**
	 * Return JSF implementation libraries.
	 * 
	 * This list is initialized from JSF library registry and updated with persistent configuration data.  
	 * 
	 * @return List
	 * @see org.eclipse.jst.jsf.ui.properties.IJSFLibraryDecoratorProvider#getProjectJSFImplementationLibraries()
	 */
	public List getJSFImplementationLibraries() {
		if (colJSFImplLib == null) {
			/* To initialze an implementation library list from registry 
			 * and then update the list with saved implementation library.
			 */
			colJSFImplLib = jsfLibReg.getJSFImplementationLibraries();			
			JSFLibraryReference targetLib = data.getJSFImplementationLibrary();
			if (targetLib == null) {
				// no saved implementation, get default implementation library
				targetLib = jsfLibReg.getDefaultJSFImplementationLibrary();
			}
			if (targetLib != null) {
				JSFLibraryReference srcLib = jsfLibReg.getJSFLibraryReferencebyID(targetLib.getID());				
				if (srcLib != null) {
					srcLib.setSelected(true);
					srcLib.setToBeDeployed(targetLib.isCheckedToBeDeployed());				
				}
			}
		} 
		return colJSFImplLib;
	}

	/**
	 * Return JSF component libraries.
	 * 
	 * This list is initialized from JSF library registry and updated with persistent 
	 * configuration data.  
	 * 
	 * @return List
	 * @see org.eclipse.jst.jsf.ui.properties.IJSFLibraryDecoratorProvider#getProjectJSFComponentLibraries()
	 */	
	public List getJSFComponentLibraries() {
		if (colJSFCompLib == null) {
			/* To initialize a component library list from registry and then 
			 * update list with saved component libraries.
			 */
			colJSFCompLib = jsfLibReg.getJSFComponentLibraries();			
			Iterator it = data.getJSFComponentLibraries().iterator();
			JSFLibraryReference targetItem = null;
			JSFLibraryReference srcItem = null;
			while (it.hasNext()) {
				targetItem = (JSFLibraryReference) it.next();
				srcItem = jsfLibReg.getJSFLibraryReferencebyID(targetItem.getID());
				if (srcItem != null) {
					srcItem.setSelected(true);
					srcItem.setToBeDeployed(targetItem.isCheckedToBeDeployed());
				}
			}
		}
		return colJSFCompLib;
	}

	/**
	 * Return the selected JSF implementation library currently.
	 * A null is returned if none is selected.
	 * 
	 * @return JSFLibraryReference 
	 */
	public JSFLibraryReference getCurrentJSFImplementationLibrarySelection() {
		Iterator it = getJSFImplementationLibraries().iterator();
		JSFLibraryReference crtItem = null;
		while (it.hasNext()) {
			crtItem = (JSFLibraryReference) it.next();
			if (crtItem.isSelected()) {
				return crtItem;
			}
		}		
		return null;
	}
	
	/**
	 * Return the selected JSF component libraries currently.
	 * An empty list is returned when no component libraries are selected.
	 * 
	 * @return list List
	 */
	public List getCurrentJSFComponentLibrarySelection() {
		List list = new ArrayList();

		Iterator it = getJSFComponentLibraries().iterator();
		JSFLibraryReference crtItem = null;
		while (it.hasNext()) {
			crtItem = (JSFLibraryReference) it.next();
			if (crtItem.isSelected()) {
				list.add(crtItem);
			}
		}
		return list;
	}	
	
	/**
	 * Returned a saved implementation library which was persisted as 
	 * DialogSettings or as project properties.
	 * 
	 * @return JSFLibraryReference
	 */
	public JSFLibraryReference getSavedJSFImplementationLibrary() {
		return data.getJSFImplementationLibrary();
	}

	/**
	 * Returned saved component libraries which were persisted as 
	 * DialogSettings or project persistent properties.
	 *  
	 * @return List
	 */
	public List getSavedJSFComponentLibraries() {
		return data.getJSFComponentLibraries();
	}
	
	/**
	 * Update the selected JSF implementation library.
	 * 
	 * Note: The library parameter won't be not added into the collection 
	 * if it does not exist already. 
	 * 
	 * @param library JSFLibraryReference
	 */
	public void setCurrentJSFImplementationLibrarySelection(final JSFLibraryReference library) {
		if (library != null) {			
			Iterator it = getJSFImplementationLibraries().iterator();
			JSFLibraryReference crtjsflib = null;
			while (it.hasNext()) {
				crtjsflib = (JSFLibraryReference) it.next();
				if (crtjsflib.getID().equals(library.getID())) {
					crtjsflib.setSelected(true);
					crtjsflib.setToBeDeployed(library.isCheckedToBeDeployed());
				} else {
					crtjsflib.setSelected(false);
				}
			}				
		}
	}

	/**
	 * Update the JSF library component libraries selection.
	 * 
	 * @param libraries List
	 */
	public void setCurrentJSFComponentLibrarySelection(final List libraries) {
		if (libraries != null) {
			/* Reset all item in component library list to unselect first.
			 * Then, update each item in cmponent libraries to the provided list.
			 */
			setJSFLibrariesSelection(getJSFComponentLibraries(), false);
	
			Iterator it = libraries.iterator();
			JSFLibraryReference crtItem;
			JSFLibraryReference srcItem = null;
			while (it.hasNext()) {
				crtItem = (JSFLibraryReference) it.next();
				srcItem = jsfLibReg.getJSFLibraryReferencebyID(crtItem.getID());
				
				if (srcItem != null) {
					srcItem.setSelected(true);
					srcItem.setToBeDeployed(crtItem.isCheckedToBeDeployed());
				}
			}		
		}
	}
		
	/**
	 * To save current configuration of implementation and component libraries 
	 * as project properties.
	 * 
	 * @param project IProject
	 */
	public void saveData(final IProject project) {
		// Instantiate one to make sure it is for a project.
		JSFLibraryConfigProjectData data_ = new JSFLibraryConfigProjectData(project);
		List implLibs = new ArrayList();
		implLibs.add(getCurrentJSFImplementationLibrarySelection());
		data_.saveData(implLibs, getCurrentJSFComponentLibrarySelection());
	}	
	
	/**
	 * Set selection state to given state to each libray in the collection.
	 * 
	 * @param libs List
	 * @param state boolean
	 */
	private void setJSFLibrariesSelection(final List libs, final boolean state) {
		Iterator it = libs.iterator();
		JSFLibraryReference crtjsflib;
		while (it.hasNext()) {
			crtjsflib = (JSFLibraryReference) it.next();
			crtjsflib.setSelected(state);
		}		
	}	
	
	/**
	 * Factory class to create new JSFLibraryConfigModel instances
	 */
	public static final class JSFLibraryConfigModelFactory {
	    /** 
	     * To create a new instance of JSFLibraryConfigModel object.  
		 * A NullPointerException is raised if source is null.
		 * 
		 * @param source JSFLibraryConfiglModelSource 
		 * @return JSFLibraryConfigModel 
		 */
		public static JSFLibraryConfigModel createInstance(final JSFLibraryConfiglModelSource source) {
			if (source == null) {
				throw new NullPointerException(Messages.JSFLibraryConfigModel_Null_Data_Source);
			}
			return new JSFLibraryConfigModel(source);
		}
	}
	
}

Back to the top