Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: db1dcb425f86e6b0c542e2940a94f1885fa9fa72 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*******************************************************************************
 * Copyright (c) 2004, 2005 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.jsf.facesconfig.ui.section;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.jst.jsf.common.ui.internal.dialogfield.DialogField;
import org.eclipse.jst.jsf.common.ui.internal.dialogfield.IDialogFieldApplyListener;
import org.eclipse.jst.jsf.common.ui.internal.dialogfield.RadiosDialogField;
import org.eclipse.jst.jsf.facesconfig.emf.FacesConfigPackage;
import org.eclipse.jst.jsf.facesconfig.emf.ManagedBeanType;
import org.eclipse.jst.jsf.facesconfig.ui.EditorMessages;
import org.eclipse.jst.jsf.facesconfig.ui.EditorPlugin;
import org.eclipse.jst.jsf.facesconfig.ui.IFacesConfigConstants;
import org.eclipse.jst.jsf.facesconfig.ui.page.IFacesConfigPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.PageBook;

/**
 * This section handles managed bean's initialization, including general class
 * type, map and list type managed bean.
 * 
 * @author Xiao-guang Zhang, sfshi
 */
public class InitializationSection extends AbstractFacesConfigSection {
	private static final String MANAGEDBEAN_GENERAL_CLASS = EditorMessages.InitializationSection_ClassType_General;

	private static final String MANAGEDBEAN_MAP = EditorMessages.InitializationSection_ClassType_Map;

	private static final String MANAGEDBEAN_LIST = EditorMessages.InitializationSection_ClassType_List;

	/** property initiliazation container composite */
	private Composite propertySection;

	/** property initiliazation DialogFieldGroup */
	private ManagedPropertyEditGroup managedPropertyGroup;

	/** map entries initiliazation container composite */
	private Composite mapSection;

	/** map entries initiliazation DialogFieldGroup */
	private MapEntriesEditGroup mapEntryGroup;

	/** list entries initiliazation container composite */
	private Composite listSection;

	/** list entries initiliazation DialogFieldGroup */
	private ListEntriesEditGroup listEntryGroup;

	/** the PageBook control for three kinds of managed bean */
	private PageBook pageBook;

	/** RadiosDialogField for three kinds of managed bean */
	private RadiosDialogField beanTypeField;

	/** current pages selection */
	private int currentPageIndex = 0;

	/**
	 * @param parent 
	 * @param managedForm
	 * @param page 
	 * @param toolkit
	 */
	public InitializationSection(Composite parent, IManagedForm managedForm,
			IFacesConfigPage page, FormToolkit toolkit) {
		super(parent, managedForm, page, toolkit, null, null); //$NON-NLS-1$
		getSection().setText(EditorMessages.InitializationSection_Name); //$NON-NLS-1$
		getSection().setDescription(
				EditorMessages.InitializationSection_Description); //$NON-NLS-1$

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.managedbean.sections.BaseSectionPart#createClient(org.eclipse.swt.widgets.Composite,
	 *      org.eclipse.ui.forms.widgets.FormToolkit)
	 */
	protected void createContents(Composite container, FormToolkit toolkit) {
		int numberOfColumns = 3;
		GridLayout gl = new GridLayout(numberOfColumns, false);
		gl.horizontalSpacing = 0;
		gl.marginWidth = 0;
		container.setLayout(gl);
		toolkit.paintBordersFor(container);

		createBeanTypeEntry(container, toolkit, numberOfColumns);
		createInitializationEntry(container, toolkit, numberOfColumns);
	}

	/**
	 * create the bean type selection section
	 * 
	 * @param container
	 * @param toolkit
	 */
	private void createBeanTypeEntry(Composite container, FormToolkit toolkit,
			int numberOfColumns) {
		beanTypeField = new RadiosDialogField();
		String[] items = { MANAGEDBEAN_GENERAL_CLASS, MANAGEDBEAN_MAP,
				MANAGEDBEAN_LIST };
		beanTypeField
				.setLabelText(EditorMessages.InitializationSection_ClassType);
		beanTypeField.setItems(items);
		beanTypeField.doFillIntoGrid(toolkit, container, numberOfColumns);
		beanTypeField
				.setDialogFieldApplyListener(new IDialogFieldApplyListener() {
					public void dialogFieldApplied(DialogField field) {
						RadiosDialogField beanTypeField1 = (RadiosDialogField) field;

						boolean bChangedSuccess = false;
						if (beanTypeField1.getSelectedIndex() == 0) {
							bChangedSuccess = switchPage(
									IFacesConfigConstants.MANAGED_PROPERTY,
									false);
						} else if (beanTypeField1.getSelectedIndex() == 1) {
							bChangedSuccess = switchPage(
									IFacesConfigConstants.MAP_ENTRIES, false);
						} else if (beanTypeField1.getSelectedIndex() == 2) {
							bChangedSuccess = switchPage(
									IFacesConfigConstants.LIST_ENTRIES, false);
						}

						if (bChangedSuccess) {
							currentPageIndex = beanTypeField1.getSelectedIndex();
						} else {
							beanTypeField1.setSelectedIndexWithoutUpdate(-1);
							beanTypeField1
									.setSelectedIndexWithoutUpdate(currentPageIndex);
						}
					}
				});
	}

	/**
	 * switch to general property, map-entries, or list-entries group If
	 * bForceClear equals to true, the others page will be cleared without user
	 * confirmation. if bForceClear equals to false, the confirmation dialog
	 * will pop up and let user determine whether this page is cleared or not.
	 * 
	 * @param pageID -
	 *            IFacesConfigConstants.MANAGED_PROPERTY,
	 *            MAP_ENTRIES,LIST_ENTRIES
	 * @param bForceClearOthers -
	 *            Force to clear other pages.
	 */
	private boolean switchPage(String pageID, boolean bForceClearOthers) {
		if (pageID == IFacesConfigConstants.MANAGED_PROPERTY) {
			if (clearPage(IFacesConfigConstants.MAP_ENTRIES, bForceClearOthers)
					&& clearPage(IFacesConfigConstants.LIST_ENTRIES,
							bForceClearOthers)) {
				managedPropertyGroup.refreshAll();
				pageBook.showPage(propertySection);
				// FIXME: there should be some other methods to resize the whole
				// section.
				InitializationSection.this.getSection().setExpanded(false);
				InitializationSection.this.getSection().setExpanded(true);
				return true;
			}
		} else if (pageID == IFacesConfigConstants.MAP_ENTRIES) {
			if (clearPage(IFacesConfigConstants.MANAGED_PROPERTY,
					bForceClearOthers)
					&& clearPage(IFacesConfigConstants.LIST_ENTRIES,
							bForceClearOthers)) {
				mapEntryGroup.refreshAll();
				pageBook.showPage(mapSection);
				// FIXME: there should be some other methods to resize the whole
				// section.
				InitializationSection.this.getSection().setExpanded(false);
				InitializationSection.this.getSection().setExpanded(true);
				return true;
			}
		} else if (pageID == IFacesConfigConstants.LIST_ENTRIES) {
			if (clearPage(IFacesConfigConstants.MANAGED_PROPERTY,
					bForceClearOthers)
					&& clearPage(IFacesConfigConstants.MAP_ENTRIES,
							bForceClearOthers)) {
				listEntryGroup.refreshAll();
				pageBook.showPage(listSection);
				// FIXME: there should be some other methods to resize the whole
				// section.
				InitializationSection.this.getSection().setExpanded(false);
				InitializationSection.this.getSection().setExpanded(true);
				return true;
			}
		}
		return false;
	}

	/**
	 * clear one specified page according to pageID if bForceClear equals to
	 * false, the confirmation dialog will pop up and let user determine whether
	 * this page is cleared or not.
	 * 
	 * @param pageID -
	 *            IFacesConfigConstants.MANAGED_PROPERTY,
	 *            MAP_ENTRIES,LIST_ENTRIES
	 * @param bForceClear -
	 *            force to clear current page.
	 * @return
	 */
	private boolean clearPage(String pageID, boolean bForceClear) {
		ManagedBeanType managedBean = (ManagedBeanType) getInput();

		if (pageID == IFacesConfigConstants.MANAGED_PROPERTY) {
			if (managedBean.getManagedProperty().size() == 0) {
				return true;
			}

			if (bForceClear
					|| EditorPlugin
							.getAlerts()
							.confirm(
									"ManagedBeanInitializationSection.Remove.Title",
									"ManagedBeanInitializationSection.RemoveManagedProperty")) {
				Command cmd = SetCommand.create(this.getEditingDomain(),
						managedBean, FacesConfigPackage.eINSTANCE
								.getManagedBeanType_ManagedProperty(),
						SetCommand.UNSET_VALUE);
				if (cmd.canExecute()) {
					getEditingDomain().getCommandStack().execute(cmd);
					return true;
				}

				return false;
			}
		} else if (pageID == IFacesConfigConstants.MAP_ENTRIES) {
			if (managedBean.getMapEntries() == null) {
				return true;
			}

			if (bForceClear
					|| EditorPlugin
							.getAlerts()
							.confirm(
									"ManagedBeanInitializationSection.Remove.Title",
									"ManagedBeanInitializationSection.RemoveMapEntries")) {
				Command cmd = SetCommand.create(this.getEditingDomain(),
						managedBean, FacesConfigPackage.eINSTANCE
								.getManagedBeanType_MapEntries(),
						SetCommand.UNSET_VALUE);
				if (cmd.canExecute()) {
					getEditingDomain().getCommandStack().execute(cmd);
					return true;
				}
			}
		} else if (pageID == IFacesConfigConstants.LIST_ENTRIES) {
			if (managedBean.getListEntries() == null) {
				return true;
			}

			if (bForceClear
					|| EditorPlugin
							.getAlerts()
							.confirm(
									"ManagedBeanInitializationSection.Remove.Title",
									"ManagedBeanInitializationSection.RemoveListEntries")) {
				Command cmd = SetCommand.create(this.getEditingDomain(),
						managedBean, FacesConfigPackage.eINSTANCE
								.getManagedBeanType_ListEntries(),
						SetCommand.UNSET_VALUE);
				if (cmd.canExecute()) {
					getEditingDomain().getCommandStack().execute(cmd);
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * create property initialization section
	 * 
	 * @param container
	 * @param toolkit
	 */
	private void createInitializationEntry(Composite container,
			FormToolkit toolkit, int numberOfColumns) {
		pageBook = new PageBook(container, SWT.NONE);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = numberOfColumns;
		pageBook.setLayoutData(gd);
		toolkit.paintBordersFor(pageBook);

		managedPropertyGroup = new ManagedPropertyEditGroup(this);
		managedPropertyGroup.initialize();
		managedPropertyGroup.addSelectionChangedListener(this);
		
		mapEntryGroup = new MapEntriesEditGroup(this);
		mapEntryGroup.initialize();
		mapEntryGroup.addSelectionChangedListener(this);
		
		listEntryGroup = new ListEntriesEditGroup(this);
		listEntryGroup.initialize();
		listEntryGroup.addSelectionChangedListener(this);
		
		propertySection = toolkit.createComposite(pageBook);
		GridLayout gl = new GridLayout();
		gl.horizontalSpacing = 0;
		gl.marginWidth = 0;
		propertySection.setLayout(gl);
		gd = new GridData(GridData.FILL_BOTH);
		propertySection.setLayoutData(gd);
		managedPropertyGroup.layoutDialogFields(toolkit, propertySection);
		
		mapSection = toolkit.createComposite(pageBook);
		mapEntryGroup.layoutDialogFields(toolkit, mapSection);
		
		listSection = toolkit.createComposite(pageBook);
		listEntryGroup.layoutDialogFields(toolkit, listSection);
		
		pageBook.showPage(propertySection);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.managedbean.sections.ISSESection#setInput(java.lang.Object)
	 */
	public void setInput(Object newInput) {
		super.setInput(newInput);
		refreshAll();
	}

	public void refreshAll() {
		if (getInput() instanceof ManagedBeanType) {
			ManagedBeanType managedBean = (ManagedBeanType) getInput();
			managedPropertyGroup.setInput(managedBean);
			mapEntryGroup.setInput(managedBean);
			listEntryGroup.setInput(managedBean);
			if (managedBean.getManagedProperty().size() > 0) {
				beanTypeField.setSelectedIndexWithoutUpdate(-1);
				beanTypeField.setSelectedIndex(0);
			} else if (managedBean.getMapEntries() != null) {
				beanTypeField.setSelectedIndexWithoutUpdate(-1);
				beanTypeField.setSelectedIndex(1);
			} else if (managedBean.getListEntries() != null) {
				beanTypeField.setSelectedIndexWithoutUpdate(-1);
				beanTypeField.setSelectedIndex(2);
			} else {
				beanTypeField.setSelectedIndexWithoutUpdate(-1);
				beanTypeField.setSelectedIndex(0);
			}
		}
	}
}

Back to the top