KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > MatchSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.plugin;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.pde.core.IBaseModel;
17 import org.eclipse.pde.core.IModel;
18 import org.eclipse.pde.core.IModelChangeProvider;
19 import org.eclipse.pde.core.IModelChangedEvent;
20 import org.eclipse.pde.core.plugin.IMatchRules;
21 import org.eclipse.pde.core.plugin.IPluginImport;
22 import org.eclipse.pde.core.plugin.IPluginReference;
23 import org.eclipse.pde.internal.core.plugin.ImportObject;
24 import org.eclipse.pde.internal.ui.PDEPlugin;
25 import org.eclipse.pde.internal.ui.PDEUIMessages;
26 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
27 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
28 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
29 import org.eclipse.pde.internal.ui.editor.PDESection;
30 import org.eclipse.pde.internal.ui.parts.ComboPart;
31 import org.eclipse.pde.internal.ui.parts.FormEntry;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.ui.forms.IFormColors;
40 import org.eclipse.ui.forms.IFormPart;
41 import org.eclipse.ui.forms.IPartSelectionListener;
42 import org.eclipse.ui.forms.widgets.FormToolkit;
43 import org.eclipse.ui.forms.widgets.Section;
44
45 public class MatchSection extends PDESection implements IPartSelectionListener {
46     
47     private Button fReexportButton;
48     private Button fOptionalButton;
49     
50     private FormEntry fVersionText;
51
52     private ComboPart fMatchCombo;
53     protected IPluginReference fCurrentImport;
54     
55     private boolean fBlockChanges = false;
56     private boolean fAddReexport = true;
57
58     public MatchSection(PDEFormPage formPage, Composite parent, boolean addReexport) {
59         super(formPage, parent, Section.DESCRIPTION);
60         fAddReexport = addReexport;
61         createClient(getSection(), formPage.getEditor().getToolkit());
62     }
63     
64     public void commit(boolean onSave) {
65         if (isDirty() == false)
66             return;
67         if (fCurrentImport != null && fVersionText.getText().isEnabled()) {
68             fVersionText.commit();
69             String JavaDoc value = fVersionText.getValue();
70             int match = IMatchRules.NONE;
71             if (value != null && value.length() > 0) {
72                 applyVersion(value);
73                 match = getMatch();
74             }
75             applyMatch(match);
76         }
77         super.commit(onSave);
78     }
79     
80     public void cancelEdit() {
81         fVersionText.cancelEdit();
82         super.cancelEdit();
83     }
84     
85     public void createClient(Section section, FormToolkit toolkit) {
86         Composite container = toolkit.createComposite(section);
87         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
88         if (fAddReexport) {
89             createOptionalButton(toolkit, container);
90             createReexportButton(toolkit, container);
91         }
92         
93         fVersionText = new FormEntry(container, toolkit, PDEUIMessages.ManifestEditor_MatchSection_version, null, false);
94         fVersionText.setFormEntryListener(new FormEntryAdapter(this, getPage().getEditor().getEditorSite().getActionBars()) {
95             public void textValueChanged(FormEntry text) {
96                 applyVersion(text.getValue());
97             }
98             public void textDirty(FormEntry text) {
99                 if (fBlockChanges)
100                     return;
101                 markDirty();
102                 fBlockChanges = true;
103                 resetMatchCombo(fCurrentImport);
104                 fBlockChanges = false;
105             }
106         });
107         
108         Label matchLabel = toolkit.createLabel(container, PDEUIMessages.ManifestEditor_PluginSpecSection_versionMatch);
109         matchLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
110
111         fMatchCombo = new ComboPart();
112         fMatchCombo.createControl(container, toolkit, SWT.READ_ONLY);
113         fMatchCombo.add(""); //$NON-NLS-1$
114
fMatchCombo.add(PDEUIMessages.ManifestEditor_MatchSection_equivalent);
115         fMatchCombo.add(PDEUIMessages.ManifestEditor_MatchSection_compatible);
116         fMatchCombo.add(PDEUIMessages.ManifestEditor_MatchSection_perfect);
117         fMatchCombo.add(PDEUIMessages.ManifestEditor_MatchSection_greater);
118         fMatchCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
119         fMatchCombo.addSelectionListener(new SelectionAdapter() {
120             public void widgetSelected(SelectionEvent e) {
121                 if (!fBlockChanges) {
122                     applyMatch(fMatchCombo.getSelectionIndex());
123                 }
124             }
125         });
126         toolkit.paintBordersFor(container);
127         initialize();
128         update((IPluginReference) null);
129         
130         section.setClient(container);
131         section.setText(PDEUIMessages.MatchSection_title);
132         section.setDescription(PDEUIMessages.MatchSection_desc);
133         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
134         section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL|GridData.VERTICAL_ALIGN_BEGINNING));
135     }
136     
137     private void createReexportButton(FormToolkit toolkit, Composite container) {
138         fReexportButton = toolkit.createButton(container, PDEUIMessages.ManifestEditor_MatchSection_reexport, SWT.CHECK);
139         fReexportButton.addSelectionListener(new SelectionAdapter() {
140             public void widgetSelected(SelectionEvent e) {
141                 if (!fBlockChanges && fCurrentImport instanceof IPluginImport) {
142                     try {
143                         IPluginImport iimport = (IPluginImport) fCurrentImport;
144                         iimport.setReexported(fReexportButton.getSelection());
145                     } catch (CoreException ex) {
146                         PDEPlugin.logException(ex);
147                     }
148                 }
149             }
150         });
151         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
152         gd.horizontalSpan = 2;
153         fReexportButton.setLayoutData(gd);
154     }
155     
156     private void createOptionalButton(FormToolkit toolkit, Composite container) {
157         fOptionalButton = toolkit.createButton(container, PDEUIMessages.ManifestEditor_MatchSection_optional, SWT.CHECK);
158         fOptionalButton.addSelectionListener(new SelectionAdapter() {
159             public void widgetSelected(SelectionEvent e) {
160                 if (fBlockChanges)
161                     return;
162                 if (!fBlockChanges && fCurrentImport instanceof IPluginImport) {
163                     try {
164                         IPluginImport iimport = (IPluginImport) fCurrentImport;
165                         iimport.setOptional(fOptionalButton.getSelection());
166                     } catch (CoreException ex) {
167                         PDEPlugin.logException(ex);
168                     }
169                 }
170             }
171         });
172         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
173         gd.horizontalSpan = 2;
174         fOptionalButton.setLayoutData(gd);
175     }
176     
177     private void applyVersion(String JavaDoc version) {
178         try {
179             if (fCurrentImport != null) {
180                 fCurrentImport.setVersion(version);
181             }
182         } catch (CoreException ex) {
183             PDEPlugin.logException(ex);
184         }
185     }
186     
187     private void applyMatch(int match) {
188         try {
189             if (fCurrentImport != null) {
190                 fCurrentImport.setMatch(match);
191             }
192         } catch (CoreException ex) {
193             PDEPlugin.logException(ex);
194         }
195     }
196     
197     private int getMatch() {
198         return fMatchCombo.getSelectionIndex();
199     }
200     
201     public void dispose() {
202         IModel model = (IModel) getPage().getModel();
203         if (model instanceof IModelChangeProvider)
204             ((IModelChangeProvider) model).removeModelChangedListener(this);
205         super.dispose();
206     }
207     
208     private void initialize() {
209         IBaseModel model = getPage().getModel();
210         if (model instanceof IModelChangeProvider)
211             ((IModelChangeProvider) model).addModelChangedListener(this);
212     }
213     
214     public void modelChanged(IModelChangedEvent e) {
215         if (e.getChangeType() == IModelChangedEvent.REMOVE) {
216             Object JavaDoc obj = e.getChangedObjects()[0];
217             if (obj.equals(fCurrentImport)) {
218                 update((IPluginReference) null);
219             }
220         } else if (e.getChangeType() == IModelChangedEvent.CHANGE) {
221             Object JavaDoc object = e.getChangedObjects()[0];
222             if (object.equals(fCurrentImport)) {
223                 update(fCurrentImport);
224             }
225         }
226     }
227     
228     public void selectionChanged(IFormPart part, ISelection selection) {
229         IStructuredSelection ssel = (IStructuredSelection)selection;
230         if (ssel.size()==1) {
231             Object JavaDoc changeObject = ((IStructuredSelection) selection).getFirstElement();
232             IPluginReference input = null;
233             if (changeObject instanceof ImportObject)
234                 input = ((ImportObject) changeObject).getImport();
235             else if (changeObject instanceof IPluginReference)
236                 input = (IPluginReference) changeObject;
237             update(input);
238         } else {
239             update(null);
240         }
241     }
242     
243     private void resetMatchCombo(IPluginReference iimport) {
244         fMatchCombo.getControl().setEnabled(isEditable() && fVersionText.getText().getText().length() > 0);
245         setMatchCombo(iimport);
246     }
247     
248     private void setMatchCombo(IPluginReference iimport) {
249         fMatchCombo.select(iimport != null ? iimport.getMatch() : IMatchRules.NONE);
250     }
251     
252     protected void update(IPluginReference iimport) {
253         fBlockChanges = true;
254         if (iimport == null) {
255             if (fAddReexport) {
256                 fOptionalButton.setSelection(false);
257                 fOptionalButton.setEnabled(false);
258                 fReexportButton.setSelection(false);
259                 fReexportButton.setEnabled(false);
260             }
261             fVersionText.setValue(null, true);
262             fVersionText.setEditable(false);
263             fMatchCombo.getControl().setEnabled(false);
264             fMatchCombo.setText(""); //$NON-NLS-1$
265
fCurrentImport = null;
266             fBlockChanges = false;
267             return;
268         }
269         
270         if (fCurrentImport != null && !iimport.equals(fCurrentImport)
271                 && isEditable()) {
272             commit(false);
273         }
274         
275         fCurrentImport = iimport;
276         if (fCurrentImport instanceof IPluginImport) {
277             IPluginImport pimport = (IPluginImport) fCurrentImport;
278             fOptionalButton.setEnabled(isEditable());
279             fOptionalButton.setSelection(pimport.isOptional());
280             fReexportButton.setEnabled(isEditable());
281             fReexportButton.setSelection(pimport.isReexported());
282         }
283         fVersionText.setEditable(isEditable());
284         fVersionText.setValue(fCurrentImport.getVersion());
285         resetMatchCombo(fCurrentImport);
286         fBlockChanges = false;
287     }
288 }
289
Popular Tags