KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > plugin > FragmentContentPage


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
12 package org.eclipse.pde.internal.ui.wizards.plugin;
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.window.Window;
17 import org.eclipse.pde.core.plugin.IPlugin;
18 import org.eclipse.pde.core.plugin.IPluginModel;
19 import org.eclipse.pde.core.plugin.PluginRegistry;
20 import org.eclipse.pde.internal.core.util.VersionUtil;
21 import org.eclipse.pde.internal.ui.IHelpContextIds;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.pde.internal.ui.parts.PluginVersionPart;
24 import org.eclipse.pde.internal.ui.util.SWTUtil;
25 import org.eclipse.pde.internal.ui.wizards.IProjectProvider;
26 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.BusyIndicator;
29 import org.eclipse.swt.custom.StackLayout;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Combo;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Group;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swt.widgets.Text;
43 import org.eclipse.ui.PlatformUI;
44 import org.osgi.framework.Version;
45
46 public class FragmentContentPage extends ContentPage {
47
48     private Text fPluginIdText_newV;
49     private Text fPluginIdText_oldV;
50     private Text fPluginVersion;
51     private Combo fMatchCombo;
52     private boolean fNewVersion;
53     private Composite fNotebook;
54     private StackLayout fNotebookLayout;
55     private Composite fOldComp;
56     private Composite fNewComp;
57     private PluginVersionPart fVersionPart;
58
59     protected ModifyListener listener = new ModifyListener() {
60         public void modifyText(ModifyEvent e) {
61             validatePage();
62         }
63     };
64     
65     public FragmentContentPage(String JavaDoc pageName, IProjectProvider provider,
66             NewProjectCreationPage page, AbstractFieldData data) {
67         super(pageName, provider, page, data);
68         setTitle(PDEUIMessages.ContentPage_ftitle);
69         setDescription(PDEUIMessages.ContentPage_fdesc);
70         updateVersion(false);
71         fVersionPart = new PluginVersionPart(fNewVersion);
72     }
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.pde.internal.ui.wizards.plugin.ContentPage#createControl(org.eclipse.swt.widgets.Composite)
76      */

77     public void createControl(Composite parent) {
78         Composite container = new Composite(parent, SWT.NONE);
79         GridLayout layout = new GridLayout();
80         layout.verticalSpacing = 15;
81         container.setLayout(layout);
82         
83         createFragmentPropertiesGroup(container);
84         createParentPluginGroup(container);
85
86         Dialog.applyDialogFont(container);
87         setControl(container);
88         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.NEW_FRAGMENT_REQUIRED_DATA);
89     }
90
91     public void createFragmentPropertiesGroup(Composite container) {
92         Group propertiesGroup = new Group(container, SWT.NONE);
93         propertiesGroup.setLayout(new GridLayout(2, false));
94         propertiesGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
95         propertiesGroup.setText(PDEUIMessages.ContentPage_fGroup);
96
97         Label label = new Label(propertiesGroup, SWT.NONE);
98         label.setText(PDEUIMessages.ContentPage_fid);
99         fIdText = createText(propertiesGroup, propertiesListener);
100
101         label = new Label(propertiesGroup, SWT.NONE);
102         label.setText(PDEUIMessages.ContentPage_fversion);
103         fVersionText = createText(propertiesGroup, propertiesListener);
104
105         label = new Label(propertiesGroup, SWT.NONE);
106         label.setText(PDEUIMessages.ContentPage_fname);
107         fNameText = createText(propertiesGroup, propertiesListener);
108
109         label = new Label(propertiesGroup, SWT.NONE);
110         label.setText(PDEUIMessages.ContentPage_fprovider);
111         fProviderText = createText(propertiesGroup, propertiesListener);
112
113         fLibraryLabel = new Label(propertiesGroup, SWT.NONE);
114         fLibraryLabel.setText(PDEUIMessages.ProjectStructurePage_library);
115         fLibraryText = createText(propertiesGroup, propertiesListener);
116     }
117
118     private void createParentPluginGroup(Composite container) {
119         Group parentGroup = new Group(container, SWT.NONE);
120         parentGroup.setLayout(new GridLayout(2, false));
121         parentGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
122         parentGroup.setText(PDEUIMessages.ContentPage_parentPluginGroup);
123         
124         fNotebook = new Composite(parentGroup, SWT.NONE);
125         GridData gd = new GridData(GridData.FILL_BOTH);
126         gd.horizontalSpan = 2;
127         fNotebook.setLayoutData(gd);
128         fNotebookLayout = new StackLayout();
129         fNotebook.setLayout(fNotebookLayout);
130
131         fNewComp = createNewVersionComp(fNotebook);
132         fOldComp = createOldVersionComp(fNotebook);
133         fNotebookLayout.topControl = fNewVersion ? fNewComp : fOldComp;
134         
135     }
136
137     private Composite createNewVersionComp(Composite notebook) {
138         Composite comp = new Composite(notebook, SWT.NONE);
139         GridLayout layout = new GridLayout(3, false);
140         layout.marginHeight = layout.marginWidth = 0;
141         comp.setLayout(layout);
142         
143         Label label = new Label(comp, SWT.NONE);
144         label.setText(PDEUIMessages.FragmentContentPage_pid);
145         fPluginIdText_newV = createPluginIdContainer(comp, true, 2);
146         
147         fVersionPart.createVersionFields(comp, false, true);
148         fVersionPart.addListeners(listener, listener);
149         return comp;
150     }
151
152     private Composite createOldVersionComp(Composite notebook) {
153         Composite comp = new Composite(notebook, SWT.NONE);
154         GridLayout layout = new GridLayout(2, false);
155         layout.marginHeight = layout.marginWidth = 0;
156         comp.setLayout(layout);
157         
158         Label label = new Label(comp, SWT.NONE);
159         label.setText(PDEUIMessages.FragmentContentPage_pid);
160         
161         Composite container = new Composite(comp, SWT.NONE);
162         layout = new GridLayout(2, false);
163         layout.marginHeight = layout.marginWidth = 0;
164         container.setLayout(layout);
165         container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
166
167         fPluginIdText_oldV = createPluginIdContainer(container, false, 1);
168         
169         label = new Label(comp, SWT.NONE);
170         label.setText(PDEUIMessages.FragmentContentPage_pversion);
171         fPluginVersion = createText(comp, listener);
172         
173         label = new Label(comp, SWT.NONE);
174         label.setText(PDEUIMessages.ContentPage_matchRule);
175         
176         fMatchCombo = new Combo(comp, SWT.READ_ONLY | SWT.BORDER);
177         fMatchCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
178         fMatchCombo.setItems(new String JavaDoc[]{"", //$NON-NLS-1$
179
PDEUIMessages.ManifestEditor_MatchSection_equivalent,
180                 PDEUIMessages.ManifestEditor_MatchSection_compatible,
181                 PDEUIMessages.ManifestEditor_MatchSection_perfect,
182                 PDEUIMessages.ManifestEditor_MatchSection_greater});
183         fMatchCombo.setText(fMatchCombo.getItem(0));
184         return comp;
185     }
186
187     private Text createPluginIdContainer(Composite parent, final boolean validateRange, int span) {
188         final Text pluginText = createText(parent, listener);
189
190         Button browse = new Button(parent, SWT.PUSH);
191         browse.setText(PDEUIMessages.ContentPage_browse);
192         browse.setLayoutData(new GridData());
193         browse.addSelectionListener(new SelectionAdapter() {
194             public void widgetSelected(SelectionEvent e) {
195                 BusyIndicator.showWhile(pluginText.getDisplay(), new Runnable JavaDoc() {
196                     public void run() {
197                         PluginSelectionDialog dialog = new PluginSelectionDialog(
198                                 pluginText.getShell(), false, false);
199                         dialog.create();
200                         if (dialog.open() == Window.OK) {
201                             IPluginModel model = (IPluginModel) dialog.getFirstResult();
202                             IPlugin plugin = model.getPlugin();
203                             String JavaDoc version = computeInitialPluginVersion(plugin.getVersion());
204                             if (validateRange) {
205                                 fVersionPart.setVersion(version);
206                                 fVersionPart.preloadFields();
207                             } else {
208                                 fPluginVersion.setText(version);
209                             }
210                             pluginText.setText(plugin.getId());
211                         }
212                     }
213                 });
214             }
215         });
216         SWTUtil.setButtonDimensionHint(browse);
217         return pluginText;
218     }
219     
220     private String JavaDoc computeInitialPluginVersion(String JavaDoc pluginVersion) {
221         if (pluginVersion != null
222                 && VersionUtil.validateVersion(pluginVersion).isOK()) {
223             Version pvi = Version.parseVersion(pluginVersion);
224             return pvi.getMajor() + "." + pvi.getMinor() //$NON-NLS-1$
225
+ "." + pvi.getMicro(); //$NON-NLS-1$
226
}
227         
228         return pluginVersion;
229     }
230
231     public void updateData() {
232         super.updateData();
233         String JavaDoc version;
234         if (fNewVersion) {
235             version = fVersionPart.getVersion();
236             ((FragmentFieldData) fData).setPluginId(fPluginIdText_newV.getText().trim());
237         } else {
238             version = fPluginVersion.getText().trim();
239             ((FragmentFieldData) fData).setPluginId(fPluginIdText_oldV.getText().trim());
240             ((FragmentFieldData) fData).setMatch(fMatchCombo.getSelectionIndex());
241         }
242         ((FragmentFieldData) fData).setPluginVersion(version);
243     }
244     
245     /* (non-Javadoc)
246      * @see org.eclipse.pde.internal.ui.wizards.plugin.ContentPage#validatePage()
247      */

248     protected void validatePage() {
249         String JavaDoc errorMessage = validateProperties();
250         
251         if (errorMessage == null) {
252             String JavaDoc pluginID = fNewVersion ?
253                     fPluginIdText_newV.getText().trim() : fPluginIdText_oldV.getText().trim();
254             if (pluginID.length() == 0) {
255                 errorMessage = PDEUIMessages.ContentPage_nopid;
256             } else if (!(PluginRegistry.findModel(pluginID) instanceof IPluginModel)) {
257                 errorMessage = PDEUIMessages.ContentPage_pluginNotFound;
258             } else {
259                 if (fNewVersion) {
260                     IStatus status = fVersionPart.validateFullVersionRangeText(false);
261                     if (status.getSeverity() != IStatus.OK) {
262                         errorMessage = status.getMessage();
263                     }
264                 } else {
265                     errorMessage = validateVersion(fPluginVersion);
266                 }
267             }
268         }
269         if (fInitialized)
270             setErrorMessage(errorMessage);
271         setPageComplete(errorMessage == null);
272     }
273     
274     /* (non-Javadoc)
275      * @see org.eclipse.pde.internal.ui.wizards.plugin.ContentPage#getNameFieldQualifier()
276      */

277     protected String JavaDoc getNameFieldQualifier() {
278         return PDEUIMessages.ContentPage_fragment;
279     }
280     
281     /* (non-Javadoc)
282      * @see org.eclipse.pde.internal.ui.wizards.plugin.ContentPage#setVisible(boolean)
283      */

284     public void setVisible(boolean visible) {
285         if (visible) {
286             fMainPage.updateData();
287             updateVersion(true);
288         }
289         super.setVisible(visible);
290     }
291     
292     private void updateVersion(boolean updateComposite) {
293         fNewVersion = Double.parseDouble(fData.getTargetVersion()) > 3.0;
294         if (updateComposite) {
295             Control oldPage = fNotebookLayout.topControl;
296             if (fNewVersion)
297                 fNotebookLayout.topControl = fNewComp;
298             else
299                 fNotebookLayout.topControl = fOldComp;
300             if (oldPage != fNotebookLayout.topControl)
301                 fNotebook.layout();
302         }
303     }
304 }
305
Popular Tags