001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.plugin;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.jface.viewers.ISelection;
014: import org.eclipse.jface.viewers.IStructuredSelection;
015: import org.eclipse.pde.core.IBaseModel;
016: import org.eclipse.pde.core.IModel;
017: import org.eclipse.pde.core.IModelChangeProvider;
018: import org.eclipse.pde.core.IModelChangedEvent;
019: import org.eclipse.pde.core.plugin.IMatchRules;
020: import org.eclipse.pde.core.plugin.IPluginImport;
021: import org.eclipse.pde.core.plugin.IPluginReference;
022: import org.eclipse.pde.internal.core.plugin.ImportObject;
023: import org.eclipse.pde.internal.ui.PDEPlugin;
024: import org.eclipse.pde.internal.ui.PDEUIMessages;
025: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
026: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
027: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
028: import org.eclipse.pde.internal.ui.editor.PDESection;
029: import org.eclipse.pde.internal.ui.parts.ComboPart;
030: import org.eclipse.pde.internal.ui.parts.FormEntry;
031: import org.eclipse.swt.SWT;
032: import org.eclipse.swt.events.SelectionAdapter;
033: import org.eclipse.swt.events.SelectionEvent;
034: import org.eclipse.swt.layout.GridData;
035: import org.eclipse.swt.widgets.Button;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Label;
038: import org.eclipse.ui.forms.IFormColors;
039: import org.eclipse.ui.forms.IFormPart;
040: import org.eclipse.ui.forms.IPartSelectionListener;
041: import org.eclipse.ui.forms.widgets.FormToolkit;
042: import org.eclipse.ui.forms.widgets.Section;
043:
044: public class MatchSection extends PDESection implements
045: IPartSelectionListener {
046:
047: private Button fReexportButton;
048: private Button fOptionalButton;
049:
050: private FormEntry fVersionText;
051:
052: private ComboPart fMatchCombo;
053: protected IPluginReference fCurrentImport;
054:
055: private boolean fBlockChanges = false;
056: private boolean fAddReexport = true;
057:
058: public MatchSection(PDEFormPage formPage, Composite parent,
059: boolean addReexport) {
060: super (formPage, parent, Section.DESCRIPTION);
061: fAddReexport = addReexport;
062: createClient(getSection(), formPage.getEditor().getToolkit());
063: }
064:
065: public void commit(boolean onSave) {
066: if (isDirty() == false)
067: return;
068: if (fCurrentImport != null
069: && fVersionText.getText().isEnabled()) {
070: fVersionText.commit();
071: String value = fVersionText.getValue();
072: int match = IMatchRules.NONE;
073: if (value != null && value.length() > 0) {
074: applyVersion(value);
075: match = getMatch();
076: }
077: applyMatch(match);
078: }
079: super .commit(onSave);
080: }
081:
082: public void cancelEdit() {
083: fVersionText.cancelEdit();
084: super .cancelEdit();
085: }
086:
087: public void createClient(Section section, FormToolkit toolkit) {
088: Composite container = toolkit.createComposite(section);
089: container.setLayout(FormLayoutFactory
090: .createSectionClientGridLayout(false, 2));
091: if (fAddReexport) {
092: createOptionalButton(toolkit, container);
093: createReexportButton(toolkit, container);
094: }
095:
096: fVersionText = new FormEntry(container, toolkit,
097: PDEUIMessages.ManifestEditor_MatchSection_version,
098: null, false);
099: fVersionText.setFormEntryListener(new FormEntryAdapter(this ,
100: getPage().getEditor().getEditorSite().getActionBars()) {
101: public void textValueChanged(FormEntry text) {
102: applyVersion(text.getValue());
103: }
104:
105: public void textDirty(FormEntry text) {
106: if (fBlockChanges)
107: return;
108: markDirty();
109: fBlockChanges = true;
110: resetMatchCombo(fCurrentImport);
111: fBlockChanges = false;
112: }
113: });
114:
115: Label matchLabel = toolkit
116: .createLabel(
117: container,
118: PDEUIMessages.ManifestEditor_PluginSpecSection_versionMatch);
119: matchLabel.setForeground(toolkit.getColors().getColor(
120: IFormColors.TITLE));
121:
122: fMatchCombo = new ComboPart();
123: fMatchCombo.createControl(container, toolkit, SWT.READ_ONLY);
124: fMatchCombo.add(""); //$NON-NLS-1$
125: fMatchCombo
126: .add(PDEUIMessages.ManifestEditor_MatchSection_equivalent);
127: fMatchCombo
128: .add(PDEUIMessages.ManifestEditor_MatchSection_compatible);
129: fMatchCombo
130: .add(PDEUIMessages.ManifestEditor_MatchSection_perfect);
131: fMatchCombo
132: .add(PDEUIMessages.ManifestEditor_MatchSection_greater);
133: fMatchCombo.getControl().setLayoutData(
134: new GridData(GridData.FILL_HORIZONTAL));
135: fMatchCombo.addSelectionListener(new SelectionAdapter() {
136: public void widgetSelected(SelectionEvent e) {
137: if (!fBlockChanges) {
138: applyMatch(fMatchCombo.getSelectionIndex());
139: }
140: }
141: });
142: toolkit.paintBordersFor(container);
143: initialize();
144: update((IPluginReference) null);
145:
146: section.setClient(container);
147: section.setText(PDEUIMessages.MatchSection_title);
148: section.setDescription(PDEUIMessages.MatchSection_desc);
149: section.setLayout(FormLayoutFactory.createClearGridLayout(
150: false, 1));
151: section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
152: | GridData.VERTICAL_ALIGN_BEGINNING));
153: }
154:
155: private void createReexportButton(FormToolkit toolkit,
156: Composite container) {
157: fReexportButton = toolkit.createButton(container,
158: PDEUIMessages.ManifestEditor_MatchSection_reexport,
159: SWT.CHECK);
160: fReexportButton.addSelectionListener(new SelectionAdapter() {
161: public void widgetSelected(SelectionEvent e) {
162: if (!fBlockChanges
163: && fCurrentImport instanceof IPluginImport) {
164: try {
165: IPluginImport iimport = (IPluginImport) fCurrentImport;
166: iimport.setReexported(fReexportButton
167: .getSelection());
168: } catch (CoreException ex) {
169: PDEPlugin.logException(ex);
170: }
171: }
172: }
173: });
174: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
175: gd.horizontalSpan = 2;
176: fReexportButton.setLayoutData(gd);
177: }
178:
179: private void createOptionalButton(FormToolkit toolkit,
180: Composite container) {
181: fOptionalButton = toolkit.createButton(container,
182: PDEUIMessages.ManifestEditor_MatchSection_optional,
183: SWT.CHECK);
184: fOptionalButton.addSelectionListener(new SelectionAdapter() {
185: public void widgetSelected(SelectionEvent e) {
186: if (fBlockChanges)
187: return;
188: if (!fBlockChanges
189: && fCurrentImport instanceof IPluginImport) {
190: try {
191: IPluginImport iimport = (IPluginImport) fCurrentImport;
192: iimport.setOptional(fOptionalButton
193: .getSelection());
194: } catch (CoreException ex) {
195: PDEPlugin.logException(ex);
196: }
197: }
198: }
199: });
200: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
201: gd.horizontalSpan = 2;
202: fOptionalButton.setLayoutData(gd);
203: }
204:
205: private void applyVersion(String version) {
206: try {
207: if (fCurrentImport != null) {
208: fCurrentImport.setVersion(version);
209: }
210: } catch (CoreException ex) {
211: PDEPlugin.logException(ex);
212: }
213: }
214:
215: private void applyMatch(int match) {
216: try {
217: if (fCurrentImport != null) {
218: fCurrentImport.setMatch(match);
219: }
220: } catch (CoreException ex) {
221: PDEPlugin.logException(ex);
222: }
223: }
224:
225: private int getMatch() {
226: return fMatchCombo.getSelectionIndex();
227: }
228:
229: public void dispose() {
230: IModel model = (IModel) getPage().getModel();
231: if (model instanceof IModelChangeProvider)
232: ((IModelChangeProvider) model)
233: .removeModelChangedListener(this );
234: super .dispose();
235: }
236:
237: private void initialize() {
238: IBaseModel model = getPage().getModel();
239: if (model instanceof IModelChangeProvider)
240: ((IModelChangeProvider) model)
241: .addModelChangedListener(this );
242: }
243:
244: public void modelChanged(IModelChangedEvent e) {
245: if (e.getChangeType() == IModelChangedEvent.REMOVE) {
246: Object obj = e.getChangedObjects()[0];
247: if (obj.equals(fCurrentImport)) {
248: update((IPluginReference) null);
249: }
250: } else if (e.getChangeType() == IModelChangedEvent.CHANGE) {
251: Object object = e.getChangedObjects()[0];
252: if (object.equals(fCurrentImport)) {
253: update(fCurrentImport);
254: }
255: }
256: }
257:
258: public void selectionChanged(IFormPart part, ISelection selection) {
259: IStructuredSelection ssel = (IStructuredSelection) selection;
260: if (ssel.size() == 1) {
261: Object changeObject = ((IStructuredSelection) selection)
262: .getFirstElement();
263: IPluginReference input = null;
264: if (changeObject instanceof ImportObject)
265: input = ((ImportObject) changeObject).getImport();
266: else if (changeObject instanceof IPluginReference)
267: input = (IPluginReference) changeObject;
268: update(input);
269: } else {
270: update(null);
271: }
272: }
273:
274: private void resetMatchCombo(IPluginReference iimport) {
275: fMatchCombo.getControl()
276: .setEnabled(
277: isEditable()
278: && fVersionText.getText().getText()
279: .length() > 0);
280: setMatchCombo(iimport);
281: }
282:
283: private void setMatchCombo(IPluginReference iimport) {
284: fMatchCombo.select(iimport != null ? iimport.getMatch()
285: : IMatchRules.NONE);
286: }
287:
288: protected void update(IPluginReference iimport) {
289: fBlockChanges = true;
290: if (iimport == null) {
291: if (fAddReexport) {
292: fOptionalButton.setSelection(false);
293: fOptionalButton.setEnabled(false);
294: fReexportButton.setSelection(false);
295: fReexportButton.setEnabled(false);
296: }
297: fVersionText.setValue(null, true);
298: fVersionText.setEditable(false);
299: fMatchCombo.getControl().setEnabled(false);
300: fMatchCombo.setText(""); //$NON-NLS-1$
301: fCurrentImport = null;
302: fBlockChanges = false;
303: return;
304: }
305:
306: if (fCurrentImport != null && !iimport.equals(fCurrentImport)
307: && isEditable()) {
308: commit(false);
309: }
310:
311: fCurrentImport = iimport;
312: if (fCurrentImport instanceof IPluginImport) {
313: IPluginImport pimport = (IPluginImport) fCurrentImport;
314: fOptionalButton.setEnabled(isEditable());
315: fOptionalButton.setSelection(pimport.isOptional());
316: fReexportButton.setEnabled(isEditable());
317: fReexportButton.setSelection(pimport.isReexported());
318: }
319: fVersionText.setEditable(isEditable());
320: fVersionText.setValue(fCurrentImport.getVersion());
321: resetMatchCombo(fCurrentImport);
322: fBlockChanges = false;
323: }
324: }
|