Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7e12cf2622523f185d29e9c9a0a1f6890834d9f0 (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
/*******************************************************************************
 * Copyright (c) 2007, 2010 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.equinox.internal.p2.ui.admin.dialogs;

import org.eclipse.equinox.p2.metadata.MetadataFactory;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;

import java.util.Collection;
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.p2.metadata.*;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * An IUImplementationGroup is a reusable UI component that displays and edits the 
 * implementation-oriented properties of an IU. It can be used in 
 * different dialogs that manipulate or define IU's.
 * 
 * @since 3.4
 */
public class IUImplementationGroup extends IUGroup {

	private Text id;
	private Text version;
	private Text namespace;
	private Text touchpointType;
	private List touchpointData;
	private List requiredCapabilities;
	private List providedCapabilities;

	public IUImplementationGroup(final Composite parent, IInstallableUnit iu, ModifyListener listener) {
		super(parent, iu, listener);
	}

	protected Composite createGroupComposite(Composite parent, ModifyListener listener) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		GridData data = new GridData();
		data.widthHint = 350;
		composite.setLayoutData(data);

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

		// Grid data for controls spanning both columns
		GridData gd2 = new GridData(GridData.FILL_HORIZONTAL);
		gd2.horizontalSpan = 2;

		// Grid data for lists grabbing vertical space
		GridData gdList = new GridData(GridData.FILL_HORIZONTAL);
		GC gc = new GC(parent);
		gc.setFont(JFaceResources.getDialogFont());
		FontMetrics fontMetrics = gc.getFontMetrics();
		gc.dispose();
		gdList.horizontalSpan = 2;
		gdList.heightHint = Dialog.convertHeightInCharsToPixels(fontMetrics, 5);

		boolean editable = iuElement == null && listener != null;

		Label label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.IUGroup_ID);
		id = new Text(composite, SWT.BORDER);
		id.setLayoutData(gd);
		if (editable) {
			id.addModifyListener(listener);
		} else {
			id.setEditable(false);
		}

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.IUGroup_Version);
		version = new Text(composite, SWT.BORDER);
		version.setLayoutData(gd);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.IUGroup_Namespace);
		namespace = new Text(composite, SWT.BORDER);
		namespace.setLayoutData(gd);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.IUGroup_TouchpointType);
		touchpointType = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		touchpointType.setLayoutData(gd);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.IUGroup_TouchpointData);
		label.setLayoutData(gd2);
		touchpointData = new List(composite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
		touchpointData.setLayoutData(gdList);
		createCopyMenu(touchpointData);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.IUGroup_RequiredCapabilities);
		label.setLayoutData(gd2);
		requiredCapabilities = new List(composite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
		requiredCapabilities.setLayoutData(gdList);
		createCopyMenu(requiredCapabilities);

		label = new Label(composite, SWT.NONE);
		label.setText(ProvAdminUIMessages.IUGroup_ProvidedCapabilities);
		label.setLayoutData(gd2);
		providedCapabilities = new List(composite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
		providedCapabilities.setLayoutData(gdList);
		createCopyMenu(providedCapabilities);

		if (editable) {
			id.addModifyListener(listener);
			version.addModifyListener(listener);
			namespace.addModifyListener(listener);
			touchpointType.addModifyListener(listener);
		} else {
			id.setEditable(false);
			version.setEditable(false);
			namespace.setEditable(false);
			touchpointType.setEditable(false);
		}
		initializeFields();
		return composite;
	}

	private void initializeFields() {
		IInstallableUnit iu = getIU();
		if (iu == null) {
			return;
		}
		id.setText(iu.getId());
		version.setText(iu.getVersion().toString());

		String value = iu.getProperty(IInstallableUnit.NAMESPACE_IU_ID);
		if (value != null) {
			namespace.setText(value);
		}
		ITouchpointType type = iu.getTouchpointType();
		if (type != null) {
			touchpointType.setText(type.getId());
		}
		Collection<ITouchpointData> data = iu.getTouchpointData();
		String[] items = new String[data.size()];
		int i = 0;
		for (ITouchpointData td : data) {
			items[i++] = td.toString();
		}
		touchpointData.setItems(items);

		Collection<IRequirement> reqs = iu.getRequirements();
		items = new String[reqs.size()];
		i = 0;
		for (IRequirement req : reqs) {
			items[i++] = req.toString();
		}
		requiredCapabilities.setItems(items);
		Collection<IProvidedCapability> prov = iu.getProvidedCapabilities();
		items = new String[prov.size()];
		i = 0;
		for (IProvidedCapability capability : prov) {
			items[i++] = capability.toString();
		}
		providedCapabilities.setItems(items);
	}

	public void updateIU() {
		// If it's not an InstallableUnit it is not editable
		if (iuElement == null || iuElement instanceof IInstallableUnit) {
			InstallableUnitDescription unit = new InstallableUnitDescription();
			unit.setId(id.getText().trim());
			unit.setVersion(Version.create(version.getText().trim()));
			unit.setProperty(IInstallableUnit.NAMESPACE_IU_ID, namespace.getText().trim());
			// TODO this is bogus because we don't let user provide a touchpoint type version
			unit.setTouchpointType(MetadataFactory.createTouchpointType(touchpointType.getText().trim(), Version.create("1.0.0"))); //$NON-NLS-1$
			iuElement = MetadataFactory.createInstallableUnit(unit);
		}
	}

	/**
	 * 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.IUGroup_IU_ID_Required, 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 createCopyMenu(final List list) {
		Menu copyMenu = new Menu(list);
		MenuItem copyItem = new MenuItem(copyMenu, SWT.NONE);
		copyItem.addSelectionListener(new SelectionListener() {
			/*
			 * @see SelectionListener.widgetSelected (SelectionEvent)
			 */
			public void widgetSelected(SelectionEvent e) {
				copySelectionsToClipboard(list);
			}

			/*
			 * @see SelectionListener.widgetDefaultSelected(SelectionEvent)
			 */
			public void widgetDefaultSelected(SelectionEvent e) {
				copySelectionsToClipboard(list);
			}
		});
		copyItem.setText(JFaceResources.getString("copy")); //$NON-NLS-1$
		list.setMenu(copyMenu);
	}

	void copySelectionsToClipboard(List list) {
		StringBuffer buffer = new StringBuffer();
		String[] selections = list.getSelection();
		for (int i = 0; i < selections.length; i++) {
			buffer.append(selections[i]);
			buffer.append("\n"); //$NON-NLS-1$
		}
		Clipboard clipboard = new Clipboard(list.getDisplay());
		clipboard.setContents(new Object[] {buffer.toString()}, new Transfer[] {TextTransfer.getInstance()});
		clipboard.dispose();

	}

}

Back to the top