Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bad107b3c3df76ec5a46b5e7657fa9a36019a57c (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
/*******************************************************************************
 * Copyright (c) 2007 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
 *     Andrew Overholt <overholt@redhat.com> - Fix for Bug 197970  
 *        	[prov] unset Profile name causes exception bringing up profile properties
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.ui.admin.dialogs;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.ui.admin.ProvAdminUIActivator;
import org.eclipse.equinox.internal.p2.ui.admin.ProvAdminUIMessages;
import org.eclipse.equinox.internal.provisional.p2.engine.IProfile;
import org.eclipse.equinox.internal.provisional.p2.ui.ProfileFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * A ProfileGroup is a reusable UI component that displays the
 * properties of a profile.  It can be used to create a new profile.
 * . It can be used in different dialogs that manipulate
 * or define profiles.
 * 
 * @since 3.4
 * 
 */
public class ProfileGroup {

	Text id;
	Text location;
	Text cache;
	Text name;
	Text description;
	Text flavor;
	Text environments;
	Text nl;
	IProfile profile;

	public ProfileGroup(final Composite parent, IProfile profile, ModifyListener listener) {
		this.profile = profile;

		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		composite.setLayout(layout);
		GridData data = new GridData();
		data.widthHint = 350;
		composite.setLayoutData(data);

		// Grid data for most text controls
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;

		Label label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.ProfileGroup_ID);
		id = new Text(composite, SWT.BORDER);
		id.setLayoutData(gd);
		setEditable(id, profile == null, listener);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.ProfileGroup_InstallFolder);
		location = new Text(composite, SWT.BORDER);
		location.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		setEditable(location, profile == null, listener);

		Button locationButton = new Button(composite, SWT.PUSH);
		locationButton.setText(ProvAdminUIMessages.ProfileGroup_Browse);
		locationButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				DirectoryDialog dialog = new DirectoryDialog(parent.getShell(), SWT.APPLICATION_MODAL);
				dialog.setMessage(ProvAdminUIMessages.ProfileGroup_SelectProfileMessage);
				String dir = dialog.open();
				if (dir != null) {
					location.setText(dir);
				}
			}
		});
		setEditable(locationButton, profile == null, listener);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.ProfileGroup_Cache);
		cache = new Text(composite, SWT.BORDER);
		cache.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		setEditable(cache, profile == null, listener);

		locationButton = new Button(composite, SWT.PUSH);
		locationButton.setText(ProvAdminUIMessages.ProfileGroup_Browse2);
		locationButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				DirectoryDialog dialog = new DirectoryDialog(parent.getShell(), SWT.APPLICATION_MODAL);
				dialog.setMessage(ProvAdminUIMessages.ProfileGroup_SelectBundlePoolCache);
				String dir = dialog.open();
				if (dir != null) {
					cache.setText(dir);
				}
			}
		});
		setEditable(locationButton, profile == null, listener);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.ProfileGroup_Name);
		name = new Text(composite, SWT.BORDER);
		name.setLayoutData(gd);
		setEditable(name, profile == null, listener);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.ProfileGroup_Description);
		description = new Text(composite, SWT.BORDER);
		description.setLayoutData(gd);
		setEditable(description, profile == null, listener);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.ProfileGroup_Flavor);
		flavor = new Text(composite, SWT.BORDER);
		flavor.setLayoutData(gd);
		setEditable(flavor, profile == null, listener);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.ProfileGroup_Environments);
		environments = new Text(composite, SWT.BORDER);
		environments.setLayoutData(gd);
		setEditable(environments, profile == null, listener);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.ProfileGroup_NL);
		nl = new Text(composite, SWT.BORDER);
		nl.setLayoutData(gd);
		setEditable(nl, profile == null, listener);

		initializeFields();
	}

	private void initializeFields() {
		if (profile == null) {
			location.setText(ProfileFactory.getDefaultLocation());
			environments.setText(ProfileFactory.getDefaultEnvironments());
			nl.setText(ProfileFactory.getDefaultNL());
			flavor.setText(ProfileFactory.getDefaultFlavor());
		} else {
			String value = profile.getProfileId();
			// Should not happen, profiles must have an id, but just in case.
			if (value == null)
				value = ""; //$NON-NLS-1$
			id.setText(value);

			// The remaining values may be null
			value = profile.getProperty(IProfile.PROP_INSTALL_FOLDER);
			if (value != null) {
				location.setText(value);
			}
			value = profile.getProperty(IProfile.PROP_CACHE);
			if (value != null) {
				cache.setText(value);
			}

			value = profile.getProperty(IProfile.PROP_NAME);
			if (value != null) {
				name.setText(value);
			}
			value = profile.getProperty(IProfile.PROP_DESCRIPTION);
			if (value != null) {
				description.setText(value);
			}
			value = profile.getProperty(IProfile.PROP_FLAVOR);
			flavor.setText(value != null ? value : ProfileFactory.getDefaultFlavor());

			value = profile.getProperty(IProfile.PROP_ENVIRONMENTS);
			if (value != null) {
				environments.setText(value);
			}
			value = profile.getProperty(IProfile.PROP_NL);
			if (value != null) {
				nl.setText(value);
			}
		}
	}

	public Map getProfileProperties() {
		if (profile == null) {
			Map profileProperties = new HashMap();

			String value = location.getText().trim();
			if (value.length() > 0) {
				profileProperties.put(IProfile.PROP_INSTALL_FOLDER, value);
			}

			value = cache.getText().trim();
			if (value.length() > 0) {
				profileProperties.put(IProfile.PROP_CACHE, value);
			}
			value = name.getText().trim();
			if (value.length() > 0) {
				profileProperties.put(IProfile.PROP_NAME, value);
			}
			value = description.getText().trim();
			if (value.length() > 0) {
				profileProperties.put(IProfile.PROP_DESCRIPTION, value);
			}
			value = flavor.getText().trim();
			if (value.length() > 0) {
				profileProperties.put(IProfile.PROP_FLAVOR, value);
			}
			value = environments.getText().trim();
			if (value.length() > 0) {
				profileProperties.put(IProfile.PROP_ENVIRONMENTS, value);
			}
			value = nl.getText().trim();
			if (value.length() > 0) {
				profileProperties.put(IProfile.PROP_NL, value);
			}
			return profileProperties;
		}
		return profile.getProperties();
	}

	public Composite getComposite() {
		if (id == null) {
			return null;
		}
		return id.getParent();
	}

	public IProfile getProfile() {
		return profile;
	}

	public String getProfileId() {
		if (profile != null) {
			return profile.getProfileId();
		}
		return id.getText().trim();
	}

	/**
	 * Return a status indicating the validity of the profile info
	 * 
	 * @return a status indicating the validity of the profile info
	 */
	public IStatus verify() {
		if (id.getText().trim().length() == 0) {
			return new Status(IStatus.ERROR, ProvAdminUIActivator.PLUGIN_ID, 0, ProvAdminUIMessages.ProfileGroup_ProfileIDRequired, null);
		}
		if (location.getText().trim().length() == 0) {
			return new Status(IStatus.ERROR, ProvAdminUIActivator.PLUGIN_ID, 0, ProvAdminUIMessages.ProfileGroup_ProfileInstallFolderRequired, null);
		}

		// TODO what kind of validation do we perform for other properties?
		return new Status(IStatus.OK, ProvAdminUIActivator.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$

	}

	private void setEditable(Control control, boolean editable, ModifyListener listener) {
		if (control instanceof Text) {
			Text text = (Text) control;
			text.setEditable(editable);
			if (listener != null && editable)
				text.addModifyListener(listener);
		} else {
			control.setEnabled(editable);
		}
	}
}

Back to the top