Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 08137448dd1b86cec277c004af3b2d561db09106 (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ptp.services.ui;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.window.Window;
import org.eclipse.ptp.services.core.IServiceConfiguration;
import org.eclipse.ptp.services.core.ProjectNotConfiguredException;
import org.eclipse.ptp.services.core.ServiceModelManager;
import org.eclipse.ptp.services.ui.dialogs.ServiceConfigurationSelectionDialog;
import org.eclipse.ptp.services.ui.messages.Messages;
import org.eclipse.ptp.services.ui.widgets.ServiceProviderConfigurationWidget;
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.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbenchPropertyPage;
import org.eclipse.ui.dialogs.PropertyPage;
import org.eclipse.ui.forms.widgets.SharedScrolledComposite;

/**
 * This class implements a project properties page which allows the user to
 * associate service configurations with the project
 * 
 * @author dave
 * 
 */
public class ServiceConfigurationPropertyPage extends PropertyPage implements
		IWorkbenchPropertyPage {
	
	private class ServiceScrolledComposite extends SharedScrolledComposite {

		public ServiceScrolledComposite(Composite parent, int style) {
			super(parent, style);
		}
		
	}
	
	/**
	 * Class to handle widget selection events
	 * 
	 * @author dave
	 * 
	 */
	private class EventHandler implements SelectionListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org
		 * .eclipse.swt.events.SelectionEvent)
		 */
		public void widgetDefaultSelected(SelectionEvent e) {
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
		 * .swt.events.SelectionEvent)
		 */
		public void widgetSelected(SelectionEvent e) {
			Object source;

			source = e.getSource();
			if (source == serviceConfigurationList) {
				showSelectedConfiguration();
			} else if (source == addButton) {
				addServiceConfiguration();
			} else if (source == removeButton) {
				removeServiceConfiguration();
			} else if (source == serviceModelWidget) {
				serviceModelPane.reflow(true);
			}
		}
	}

	/**
	 * Comparator class used to sort service configurations in ascending order
	 * by name
	 * 
	 * @author dave
	 * 
	 */
	private class ServiceConfigurationComparator implements Comparator<IServiceConfiguration> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(IServiceConfiguration o1, IServiceConfiguration o2) {
			return o1.getName().compareTo(o2.getName());
		}
	}

	private static int BUTTON_WIDTH = 85;

	private Button addButton;
	
	private IServiceConfiguration currentConfig;
	private Vector<IServiceConfiguration> deletedServiceConfigurations;
	private Vector<IServiceConfiguration> addedServiceConfigurations;
	private final EventHandler eventHandler = new EventHandler();
	private Composite propertiesPane;
	private Button removeButton;
	private ServiceConfigurationComparator serviceConfigurationComparator;
	private Table serviceConfigurationList;
	private ServiceScrolledComposite serviceModelPane;
	private ServiceProviderConfigurationWidget serviceModelWidget;

	/**
	 * Create the service configuration properties page
	 */
	public ServiceConfigurationPropertyPage() {
		super();
		serviceConfigurationComparator = new ServiceConfigurationComparator();
	}

	/**
	 * Delete service configurations when Ok or Apply button is pressed
	 * 
	 * @return Status from superclass indicating if Ok processing is to continue
	 */
	public boolean performOk() {
		deleteServiceConfigurations();
		addServiceConfigurations();
		serviceModelWidget.applyChangesToConfiguration();
		try {
			ServiceModelManager.getInstance().saveModelConfiguration();
		} catch (IOException e) {
			ServicesUIPlugin.getDefault().log(e);
		}
		return super.performOk();
	}

	/**
	 * Add selected service configurations to the set of service
	 * configurations known to the service model manager
	 */
	private void addServiceConfigurations() {
		if (addedServiceConfigurations != null)
			for (IServiceConfiguration configuration : addedServiceConfigurations)
				ServiceModelManager.getInstance().addConfiguration(
						getProject(), configuration);
	}

	/**
	 * Add a new service configuration to the list of service configurations
	 * used by this project
	 */
	private void addServiceConfiguration() {
		ServiceConfigurationSelectionDialog dialog;
		int status;
		Set<IServiceConfiguration> configs;

		// Display a dialog containing a list of available service
		// configurations
		try {
			configs = ServiceModelManager.getInstance().getConfigurations(
					getProject());
		} catch (ProjectNotConfiguredException e) {
			configs = new HashSet<IServiceConfiguration>();
		}
		dialog = new ServiceConfigurationSelectionDialog(getShell(), configs);
		status = dialog.open();
		if (status == Window.OK) {
			IServiceConfiguration config;
			TableItem item;

			config = dialog.getSelectedConfiguration();
			if (config != null) {
				item = new TableItem(serviceConfigurationList, 0);
				item.setData(config);
				item.setText(config.getName());
				// ServiceModelManager.getInstance().addConfiguration(
				// getProject(), config);
				if (addedServiceConfigurations == null)
					addedServiceConfigurations = new Vector<IServiceConfiguration>();
				addedServiceConfigurations.add(config);
			}
		}
	}

	/**
	 * Remove selected service configurations from the set of service
	 * configurations known to the service model manager
	 */
	private void deleteServiceConfigurations() {
		if (deletedServiceConfigurations != null) {
			for (IServiceConfiguration config : deletedServiceConfigurations) {
				ServiceModelManager.getInstance().removeConfiguration(
						getProject(), config);
			}
			deletedServiceConfigurations.clear();
		}
	}

	/**
	 * Get the project Object
	 * 
	 * @return The project
	 */
	private IProject getProject() {
		Object element = getElement();
		IProject project = null;
		if (element instanceof IProject) {
			project = (IProject) element;
		} else if (element instanceof IAdaptable) {
			project = (IProject) ((IAdaptable) element)
					.getAdapter(IProject.class);
		}
		return project;

	}

	/**
	 * Get the list of service configuration used by this project, sort by name
	 * and then populate the service configuration list.
	 */
	private void getProjectConfigurations() {
		IServiceConfiguration serviceConfigurations[];

		try {
			serviceConfigurations = ServiceModelManager.getInstance()
					.getConfigurations(getProject()).toArray(new IServiceConfiguration[0]);
			Arrays.sort(serviceConfigurations, serviceConfigurationComparator);
			for (Object config : serviceConfigurations) {
				TableItem item;

				item = new TableItem(serviceConfigurationList, 0);
				item.setData(config);
				item.setText(0, ((IServiceConfiguration) config).getName());
			}
		} catch (ProjectNotConfiguredException e) {
		}
	}

	/**
	 * Remove the selected service configuration from the project
	 */
	private void removeServiceConfiguration() {
		TableItem selection[];
		IServiceConfiguration selectedConfig;

		selection = serviceConfigurationList.getSelection();
		if (selection.length > 0) {
			selectedConfig = (IServiceConfiguration) selection[0].getData();
			if (deletedServiceConfigurations == null) {
				deletedServiceConfigurations = new Vector<IServiceConfiguration>();
			}
			// Selected service model is added to vector to be deleted during Ok
			// or Apply button processing
			deletedServiceConfigurations.add(selectedConfig);
			serviceConfigurationList.remove(serviceConfigurationList
					.getSelectionIndex());
			serviceModelWidget.setServiceConfiguration(null);
		}
	}

	/**
	 * Show the details for the selected service configuration
	 */
	private void showSelectedConfiguration() {
		TableItem selection[];
		IServiceConfiguration selectedConfig;

		selection = serviceConfigurationList.getSelection();
		if (selection.length > 0) {
			selectedConfig = (IServiceConfiguration) selection[0].getData();
			if (selectedConfig != currentConfig) {
				currentConfig = selectedConfig;
				
				Set<String> natures = Collections.emptySet();
				IProject project = (IProject) getElement().getAdapter(IProject.class);
				if(project != null) {
					try {
						natures = new HashSet<String>(Arrays.asList(project.getDescription().getNatureIds()));
					} catch (CoreException e) {
						ServicesUIPlugin.getDefault().log(e);
					}
				}
				serviceModelWidget.setServiceConfiguration(selectedConfig, natures);
				serviceModelWidget.setEnabled(true);
				serviceModelPane.reflow(true);
			}
		} else {
			currentConfig = null;
			serviceModelWidget.setServiceConfiguration(null);
			serviceModelWidget.setEnabled(false);
		}
	}

	@Override
	protected Control createContents(Composite parent) {
		// Create the properties pane, which will contain the service
		// configuration list
		// and the current service configuration
		propertiesPane = new Composite(parent, SWT.NONE);
		GridLayout propertiesPaneLayout = new GridLayout(1, true);
		propertiesPaneLayout.marginHeight = 0;
		propertiesPaneLayout.marginWidth = 0;
		propertiesPane.setLayout(propertiesPaneLayout);

		// Create the pane for the service configuration list and then create
		// the service configuration list
		Composite serviceConfigurationPane = new Composite(propertiesPane, SWT.NONE);
		GridLayout serviceConfigLayout = new GridLayout(2, false);
		serviceConfigurationPane.setLayout(serviceConfigLayout);
		serviceConfigurationPane.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label label = new Label(serviceConfigurationPane, SWT.NONE);
		label.setText(Messages.ServiceConfigurationPropertyPage_2);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 2;
		label.setLayoutData(data);

		serviceConfigurationList = new Table(serviceConfigurationPane, SWT.SINGLE|SWT.BORDER);
		serviceConfigurationList.setLayoutData(new GridData(GridData.FILL_BOTH));
		serviceConfigurationList.setLinesVisible(false);
		serviceConfigurationList.addSelectionListener(eventHandler);

		// Create the pane and buttons to add and remove service configurations
		Composite buttonPane = new Composite(serviceConfigurationPane, SWT.NONE);
		buttonPane.setLayout(new GridLayout(1, true));
		addButton = new Button(buttonPane, SWT.PUSH);
		addButton.setText(Messages.ServiceConfigurationPropertyPage_0);
		addButton.addSelectionListener(eventHandler);
		data = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		data.widthHint = BUTTON_WIDTH;
		addButton.setLayoutData(data);
		removeButton = new Button(buttonPane, SWT.PUSH);
		removeButton.setText(Messages.ServiceConfigurationPropertyPage_1);
		removeButton.addSelectionListener(eventHandler);
		data = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		data.widthHint = BUTTON_WIDTH;
		removeButton.setLayoutData(data);

		// Create the pane which will contain the current service model
		serviceModelPane = new ServiceScrolledComposite(propertiesPane, SWT.V_SCROLL|SWT.H_SCROLL);
		serviceModelPane.setExpandVertical(true);
		serviceModelPane.setExpandHorizontal(true);
		serviceModelPane.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		Composite serviceComp = new Composite(serviceModelPane, SWT.NONE);
		serviceComp.setLayout(new GridLayout(1, true));
		
		serviceModelWidget = new ServiceProviderConfigurationWidget(serviceComp, SWT.NONE);
		serviceModelWidget.addSelectionListener(eventHandler);
		serviceModelWidget.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		serviceModelPane.setContent(serviceComp);
		serviceModelPane.setMinSize(serviceComp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		// Fill in the list of service configurations currently used by this
		// project
		getProjectConfigurations();
		return propertiesPane;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
	 */
	@Override
	protected void performDefaults() {
		// clean up pending deletes
		if (deletedServiceConfigurations != null)
			deletedServiceConfigurations.removeAllElements();
		// remove any pending additions
		if (addedServiceConfigurations != null)
			addedServiceConfigurations.removeAllElements();
		currentConfig = null;
		serviceConfigurationList.removeAll(); // cleanup table
		getProjectConfigurations(); // reload table
		serviceModelWidget.setServiceConfiguration(null);
		super.performDefaults();
		setErrorMessage(null);
	}

}

Back to the top