Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1e355d9393ac4879df02d39621a8700f30a33461 (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
/*******************************************************************************
 * 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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jst.jsf.facesconfig.ui.EditorMessages;
import org.eclipse.jst.jsf.facesconfig.ui.page.IFacesConfigPage;
import org.eclipse.jst.jsf.facesconfig.ui.page.OthersPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * @author Zhi-peng Zhang
 * @version
 */
/**
 * The base class for the Sections in Others Page.
 */
public abstract class OthersPageBaseSection extends AbstractFacesConfigSection {

	protected TableViewer tableViewer;

	protected Button removeButton;

	/**
	 * 
	 * @param componentClass
	 * @param parent
	 * @param managedForm
	 * @param page
	 * @param toolkit
	 * @param helpContextId
	 * @param helpTooltip
	 */
	public OthersPageBaseSection(Composite parent, IManagedForm managedForm,
			IFacesConfigPage page, FormToolkit toolkit, String helpContextId,
			String helpTooltip) {
		super(parent, managedForm, page, toolkit, helpContextId, helpTooltip);
	}

	/**
	 * 
	 * @param componentClass
	 * @param parent
	 * @param managedForm
	 * @param page
	 * @param toolkit
	 */
	public OthersPageBaseSection(Composite parent, IManagedForm managedForm,
			IFacesConfigPage page, FormToolkit toolkit) {
		this(parent, managedForm, page, toolkit, null, null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.jsf.facesconfig.ui.section.AbstractFacesConfigSection#createContents(org.eclipse.swt.widgets.Composite,
	 *      org.eclipse.ui.forms.widgets.FormToolkit)
	 */
	protected void createContents(Composite container, FormToolkit toolkit) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginWidth = layout.marginHeight = 5;
		container.setLayout(layout);
		createViewer(container, toolkit);
		createOperationSection(container, toolkit);
	}

	/**
	 * create TableViewer for this section. sub-class may override it to return
	 * a new type tableViewer. for example CheckboxTableViewer.
	 * 
	 * @param parent
	 * @return
	 */
	protected TableViewer createTableViewer(Composite parent) {
		return new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL
				| SWT.BORDER);
	}

	/**
	 * Config the viewer, such as set a filter and so on. Sub classes should
	 * override this method to add filter.
	 * 
	 * @param tableViewer1
	 */
	protected abstract void configTableViewer(TableViewer tableViewer1);

	/**
	 * 
	 * @param parent
	 * @param toolkit
	 */
	protected void createViewer(Composite parent, FormToolkit toolkit) {
		Composite tableContainer = toolkit.createComposite(parent);
		toolkit.paintBordersFor(tableContainer);

		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.heightHint = 200;
		tableContainer.setLayoutData(gd);
		tableContainer.setLayout(new GridLayout());

		tableViewer = createTableViewer(tableContainer);
		tableViewer.getControl()
				.setLayoutData(new GridData(GridData.FILL_BOTH));
		tableViewer.setContentProvider(new AdapterFactoryContentProvider(
				getAdapterFactory()));
		tableViewer.setLabelProvider(new AdapterFactoryLabelProvider(
				getAdapterFactory()));
		configTableViewer(tableViewer);

		tableViewer.addSelectionChangedListener(this);
	}

	/**
	 * 
	 * @param parent
	 * @param toolkit
	 */
	protected void createOperationSection(Composite parent, FormToolkit toolkit) {
		Composite operationContainer = toolkit.createComposite(parent);
		operationContainer.setLayoutData(new GridData(GridData.FILL_VERTICAL));

		operationContainer.setLayout(new GridLayout());

		Button addButton = toolkit.createButton(operationContainer,
				EditorMessages.UI_Button_Add, SWT.PUSH);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL
				| GridData.VERTICAL_ALIGN_BEGINNING);
		gd.grabExcessHorizontalSpace = false;
		addButton.setLayoutData(gd);

		addButton.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				addButtonSelected(e);
			}

			public void widgetDefaultSelected(SelectionEvent e) {
				addButtonSelected(e);
			}
		});

		removeButton = toolkit.createButton(operationContainer,
				EditorMessages.UI_Button_Remove, SWT.PUSH);

		removeButton.setEnabled(true);
		gd = new GridData(GridData.FILL_HORIZONTAL
				| GridData.VERTICAL_ALIGN_BEGINNING);
		gd.grabExcessHorizontalSpace = false;
		removeButton.setLayoutData(gd);
		removeButton.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				removeButtonSelected(e);
			}

			public void widgetDefaultSelected(SelectionEvent e) {
				removeButtonSelected(e);
			}
		});
	}


	abstract void addButtonSelected(SelectionEvent e);

	protected void removeButtonSelected(SelectionEvent e) {
		IStructuredSelection ssel = StructuredSelection.EMPTY;
		ISelection selection = getSelection();
		if (selection instanceof IStructuredSelection) {
			ssel = (IStructuredSelection) selection;
		}

		if (!ssel.isEmpty()) {
			List commands = new ArrayList(ssel.size());
			for (Iterator iter = ssel.iterator(); iter.hasNext();) {
				EObject element = (EObject) iter.next();
				if (element.eContainer().eContents().size() == 1) {
					// if the parent only have this one child, then remove it
					// together.
					element = element.eContainer();
				}
				Command command = RemoveCommand.create(getEditingDomain(),
						element);
				commands.add(command);

			}

			CompoundCommand command = new CompoundCommand(commands);
			if (command.canExecute()) {
				getEditingDomain().getCommandStack().execute(command);
			}
		}
	}

	public ISelection getSelection() {
		return tableViewer.getSelection();
	}

	protected void updateButtons() {
		if (!removeButton.isDisposed()) {
			IStructuredSelection ssel = (IStructuredSelection) getSelection();
			removeButton.setEnabled(!ssel.isEmpty());
		}
	}

	public void refresh() {
		super.refresh();
		tableViewer.refresh();
		updateButtons();
	}

	public void setSelection(ISelection selection) {
		tableViewer.setSelection(selection);
	}

	/**
	 * @return Returns the tableViewer.
	 */
	public TableViewer getTableViewer() {
		return tableViewer;
	}

	public void refreshAll() {
		setViewerInput(getInput());
		updateButtons();
	}

	/**
	 * set the structuredViewer's input
	 * 
	 * @param input
	 */
	abstract protected void setViewerInput(Object input);

	public void expansionStateChanged(boolean expanded) {
		if (expanded) {
			if (tableViewer.getInput() == null) {
				refreshAll();
			}
			tableViewer.setSelection(tableViewer.getSelection());
		} else {
			tableViewer.setSelection(null);
		}
		super.expansionStateChanged(expanded);
	}

	protected void expansionStateChanging(boolean expanding) {
		if (!expanding) {
			GridData gd = new GridData(GridData.FILL_HORIZONTAL);
			this.getSection().setLayoutData(gd);
		} else {
			((OthersPage) getPage()).closeOtherSections(this);

			GridData gd = new GridData(GridData.FILL_BOTH);
			this.getSection().setLayoutData(gd);
		}

		super.expansionStateChanging(expanding);
	}

	public void selectionChanged(SelectionChangedEvent event) {
		super.selectionChanged(event);
		updateButtons();
	}
}

Back to the top