Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d31917871b2a1d53d96450c5f57224f5f07f975f (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
/*******************************************************************************
 *  Copyright (c) 2010, 2012 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.pde.internal.ui.editor.feature;

import org.eclipse.pde.internal.core.ifeature.IFeatureModel;

import java.util.ArrayList;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.*;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.jface.window.Window;
import org.eclipse.pde.core.IEditable;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.ifeature.*;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.eclipse.pde.internal.ui.dialogs.FeatureSelectionDialog;
import org.eclipse.pde.internal.ui.editor.*;
import org.eclipse.pde.internal.ui.editor.context.XMLDocumentSetupParticpant;
import org.eclipse.pde.internal.ui.editor.text.XMLConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.forms.IFormColors;
import org.eclipse.ui.forms.widgets.*;

/**
 * Provides the UI for the License Agreement section of the Information page in the Feature Editor.
 * There are two radio buttons which allow the user to choose between setting a license in text
 * or to point at a feature which specifies the licensing.
 * 
 * @since 3.7
 * @see InfoSection
 * @see FeatureEditor
 */
public class LicenseFeatureSection extends PDESection {

	private Text fLicenseFeatureIDText;
	private Button fLicenseButton;
	private Text fLicenseFeatureVersionText;
	private Text fUrlText;
	private SourceViewer fSourceViewer;
	private SourceViewerConfiguration fSourceConfiguration;
	private IDocument fDocument;
	private boolean fIgnoreChange;
	private Button fSharedLicenseButton;
	private Button fLocalLicenseButton;

	public LicenseFeatureSection(PDEFormPage page, Composite parent, XMLConfiguration fSourceConfiguration) {
		super(page, parent, ExpandableComposite.NO_TITLE, false);
		this.fSourceConfiguration = fSourceConfiguration;
		fDocument = new Document();
		new XMLDocumentSetupParticpant().setup(fDocument);
		createClient(getSection(), page.getManagedForm().getToolkit());
	}

	/**
	 * Creates the UI in the given section using the provided toolkit
	 * @param toolkit
	 * @param parent
	 */
	public void createClient(Section section, FormToolkit toolkit) {
		Composite page = toolkit.createComposite(section);
		final StackLayout stackLayout = new StackLayout();

		GridLayout layout = FormLayoutFactory.createClearGridLayout(false, 2);
		layout.horizontalSpacing = 8;
		page.setLayout(layout);
		fSharedLicenseButton = toolkit.createButton(page, PDEUIMessages.FeatureEditor_licenseFeatureSection_sharedButton, SWT.RADIO);
		fLocalLicenseButton = toolkit.createButton(page, PDEUIMessages.FeatureEditor_licenseFeatureSection_localButton, SWT.RADIO);

		GridData gd = new GridData();
		gd.horizontalIndent = 5;
		fLocalLicenseButton.setLayoutData(gd);

		final Composite sectionsComposite = toolkit.createComposite(page);
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 2;
		sectionsComposite.setLayoutData(gd);
		sectionsComposite.setLayout(stackLayout);

		// Shared Section

		final Composite licenseFeatureComposite = toolkit.createComposite(sectionsComposite);
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 2;
		licenseFeatureComposite.setLayoutData(gd);

		layout = new GridLayout();
		layout.numColumns = 3;
		layout.marginWidth = 2;
		layout.marginHeight = 5;
		layout.verticalSpacing = 8;
		licenseFeatureComposite.setLayout(layout);

		Label label = toolkit.createLabel(licenseFeatureComposite, PDEUIMessages.FeatureEditor_licenseFeatureSection_featureID);
		label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));

		fLicenseFeatureIDText = toolkit.createText(licenseFeatureComposite, null, SWT.SINGLE);
		fLicenseFeatureIDText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				infoModified();
			}
		});

		fLicenseFeatureIDText.setEditable(true);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		fLicenseFeatureIDText.setLayoutData(gd);

		fLicenseButton = toolkit.createButton(licenseFeatureComposite, PDEUIMessages.FeatureEditor_licenseFeatureSection_browse, SWT.PUSH);
		fLicenseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				handleSelect();
			}
		});

		label = toolkit.createLabel(licenseFeatureComposite, PDEUIMessages.FeatureEditor_licenseFeatureSection_featureVersion);
		label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));

		fLicenseFeatureVersionText = toolkit.createText(licenseFeatureComposite, null, SWT.SINGLE);
		fLicenseFeatureVersionText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				infoModified();
			}
		});

		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		fLicenseFeatureVersionText.setLayoutData(gd);

		// Local Section

		final Composite localLicenseComposite = toolkit.createComposite(sectionsComposite);
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 2;
		localLicenseComposite.setLayoutData(gd);

		layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginWidth = 2;
		layout.marginHeight = 5;
		layout.verticalSpacing = 8;
		localLicenseComposite.setLayout(layout);

		label = toolkit.createLabel(localLicenseComposite, PDEUIMessages.FeatureEditor_InfoSection_url);
		label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));

		fUrlText = toolkit.createText(localLicenseComposite, null, SWT.SINGLE);
		fUrlText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				infoModified();
			}
		});
		gd = new GridData(GridData.FILL_HORIZONTAL);
		fUrlText.setLayoutData(gd);

		label = toolkit.createLabel(localLicenseComposite, PDEUIMessages.FeatureEditor_InfoSection_text);
		label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
		gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
		label.setLayoutData(gd);

		int styles = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL;
		fSourceViewer = new SourceViewer(localLicenseComposite, null, styles);
		fSourceViewer.configure(fSourceConfiguration);
		fSourceViewer.setDocument(fDocument);
		StyledText styledText = fSourceViewer.getTextWidget();
		styledText.setFont(JFaceResources.getTextFont());
		styledText.setMenu(getPage().getPDEEditor().getContextMenu());
		styledText.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
		fDocument.addDocumentListener(new IDocumentListener() {
			public void documentChanged(DocumentEvent e) {
				infoModified();
			}

			public void documentAboutToBeChanged(DocumentEvent e) {
			}
		});

		if (SWT.getPlatform().equals("motif") == false) { //$NON-NLS-1$
			toolkit.paintBordersFor(localLicenseComposite);
		}

		gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
		gd.widthHint = 50;
		gd.heightHint = 50;
		styledText.setLayoutData(gd);

		fSharedLicenseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (((Button) e.widget).getSelection()) {
					stackLayout.topControl = licenseFeatureComposite;
					sectionsComposite.layout();
				}
			}
		});
		fLocalLicenseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (((Button) e.widget).getSelection()) {
					stackLayout.topControl = localLicenseComposite;
					sectionsComposite.layout();
				}
			}
		});

		section.setClient(page);

		IFeatureModel model = (IFeatureModel) getPage().getModel();
		IFeature feature = model.getFeature();
		if (feature.getLicenseFeatureID() == null || feature.getLicenseFeatureID().length() == 0) {
			fLocalLicenseButton.setSelection(true);
			fSharedLicenseButton.setSelection(false);
			stackLayout.topControl = localLicenseComposite;
		} else {
			fLocalLicenseButton.setSelection(false);
			fSharedLicenseButton.setSelection(true);
			stackLayout.topControl = licenseFeatureComposite;
		}
		model.addModelChangedListener(this);

		toolkit.paintBordersFor(licenseFeatureComposite);

	}

	private void handleSelect() {
		BusyIndicator.showWhile(fLicenseFeatureIDText.getDisplay(), new Runnable() {
			public void run() {
				IFeatureModel[] allModels = PDECore.getDefault().getFeatureModelManager().getModels();
				ArrayList<IFeatureModel> newModels = new ArrayList<IFeatureModel>();
				for (int i = 0; i < allModels.length; i++) {
					if (canAdd(allModels[i]))
						newModels.add(allModels[i]);
				}
				IFeatureModel[] candidateModels = newModels.toArray(new IFeatureModel[newModels.size()]);
				FeatureSelectionDialog dialog = new FeatureSelectionDialog(fLicenseFeatureIDText.getShell(), candidateModels, false);
				if (dialog.open() == Window.OK) {
					Object[] models = dialog.getResult();
					doSelect((IFeatureModel) models[0]);
				}
			}

			private void doSelect(IFeatureModel licenseFeatureModel) {
				IFeature licenseFeature = licenseFeatureModel.getFeature();
				fLicenseFeatureIDText.setText(licenseFeature.getId());
				fLicenseFeatureVersionText.setText(licenseFeature.getVersion());
			}

			private boolean canAdd(IFeatureModel candidate) {
				IFeatureModel model = (IFeatureModel) getPage().getModel();
				IFeature feature = model.getFeature();
				String id = feature.getId();
				String candidateID = candidate.getFeature().getId();
				return !candidateID.equals(id) && !candidateID.equals(fLicenseFeatureIDText.getText());
			}
		});
	}

	private void infoModified() {
		IFeatureModel featureModel = (IFeatureModel) getPage().getModel();

		if (fLicenseFeatureIDText.getText().length() == 0 && fLicenseFeatureVersionText.getText().length() > 0) {
			fIgnoreChange = true;
			fLicenseFeatureVersionText.setText(""); //$NON-NLS-1$
			fIgnoreChange = false;
		}
		if (!fIgnoreChange && featureModel instanceof IEditable) {
			((IEditable) featureModel).setDirty(true);
			markDirty();
		}
	}

	public void refresh() {
		fIgnoreChange = true;
		IFeatureModel model = (IFeatureModel) getPage().getModel();
		IFeature feature = model.getFeature();
		fLicenseFeatureIDText.setText(feature.getLicenseFeatureID());
		fLicenseFeatureVersionText.setText(feature.getLicenseFeatureVersion());
		IFeatureInfo info = feature.getFeatureInfo(IFeature.INFO_LICENSE);
		String url = null;
		String description = null;
		if (info != null) {
			url = info.getURL();
			description = info.getDescription();
		}
		fUrlText.setText(url != null ? url : ""); //$NON-NLS-1$
		fDocument.set(description != null ? description : ""); //$NON-NLS-1$
		super.refresh();
		fIgnoreChange = false;
	}

	public void commit(boolean onSave) {
		IFeatureModel featureModel = (IFeatureModel) getPage().getModel();
		IFeature feature = featureModel.getFeature();

		if (fSharedLicenseButton.getSelection()) {
			feature.setLicenseFeatureID(fLicenseFeatureIDText.getText());
			feature.setLicenseFeatureVersion(fLicenseFeatureVersionText.getText());
		} else {
			feature.setLicenseFeatureID(""); //$NON-NLS-1$
			feature.setLicenseFeatureVersion(""); //$NON-NLS-1$
			fIgnoreChange = true;
			fLicenseFeatureIDText.setText(""); //$NON-NLS-1$
			fLicenseFeatureVersionText.setText(""); //$NON-NLS-1$
			fIgnoreChange = false;
		}

		String url = fUrlText.getText();
		String description = fDocument.get();

		try {
			IFeatureInfo targetInfo = feature.getFeatureInfo(2);
			if (targetInfo == null) {
				targetInfo = featureModel.getFactory().createInfo(2);
				feature.setFeatureInfo(targetInfo, 2);
			}
			targetInfo.setURL(url);
			targetInfo.setDescription(description);
		} catch (CoreException e) {
		}
		super.commit(onSave);
	}
}

Back to the top