KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > exports > BaseExportWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.wizards.exports;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.jdt.core.IJavaProject;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.dialogs.IDialogSettings;
22 import org.eclipse.jface.dialogs.IMessageProvider;
23 import org.eclipse.jface.viewers.IStructuredContentProvider;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.TableViewer;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.pde.core.IModel;
28 import org.eclipse.pde.internal.ui.PDEPlugin;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
31 import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart;
32 import org.eclipse.pde.internal.ui.wizards.ListUtil;
33 import org.eclipse.swt.SWT;
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.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.TabFolder;
40 import org.eclipse.swt.widgets.TabItem;
41 import org.eclipse.ui.IWorkingSet;
42 import org.eclipse.ui.IWorkingSetManager;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
45
46 public abstract class BaseExportWizardPage extends AbstractExportWizardPage {
47     
48     protected ExportPart fExportPart;
49     private IStructuredSelection fSelection;
50     protected ExportDestinationTab fDestinationTab;
51     protected ExportOptionsTab fOptionsTab;
52     protected JARSigningTab fJARSiginingTab;
53     protected TabFolder fTabFolder;
54
55     class ExportListProvider extends DefaultContentProvider implements
56             IStructuredContentProvider {
57         public Object JavaDoc[] getElements(Object JavaDoc parent) {
58             return getListElements();
59         }
60     }
61
62     class ExportPart extends WizardCheckboxTablePart {
63         public ExportPart(String JavaDoc label, String JavaDoc[] buttonLabels) {
64             super(label, buttonLabels);
65         }
66
67         public void updateCounter(int count) {
68             super.updateCounter(count);
69             pageChanged();
70         }
71
72         protected void buttonSelected(Button button, int index) {
73             switch (index) {
74             case 0:
75                 handleSelectAll(true);
76                 break;
77             case 1:
78                 handleSelectAll(false);
79                 break;
80             case 2:
81                 handleWorkingSets();
82             }
83         }
84     }
85
86     public BaseExportWizardPage(IStructuredSelection selection, String JavaDoc name, String JavaDoc choiceLabel) {
87         super(name);
88         fSelection = selection;
89         fExportPart =
90             new ExportPart(
91                 choiceLabel,
92                 new String JavaDoc[] {
93                     PDEUIMessages.WizardCheckboxTablePart_selectAll,
94                     PDEUIMessages.WizardCheckboxTablePart_deselectAll,
95                     PDEUIMessages.ExportWizard_workingSet });
96         setDescription(PDEUIMessages.ExportWizard_Plugin_description);
97     }
98     
99     public void createControl(Composite parent) {
100         Composite container = new Composite(parent, SWT.NULL);
101         GridLayout layout = new GridLayout();
102         layout.verticalSpacing = 10;
103         container.setLayout(layout);
104         
105         createViewer(container);
106         
107         fTabFolder = new TabFolder(container, SWT.NONE);
108         fTabFolder.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
109         createTabs(fTabFolder, getDialogSettings());
110         
111         initializeViewer();
112         if (getErrorMessage() != null) {
113             setMessage(getErrorMessage());
114             setErrorMessage(null);
115         }
116         setControl(container);
117         hookHelpContext(container);
118         Dialog.applyDialogFont(container);
119     }
120     
121     protected void createTabs(TabFolder folder, IDialogSettings settings) {
122         createDestinationTab(folder);
123         fDestinationTab.initialize(settings);
124         
125         createOptionsTab(folder);
126         fOptionsTab.initialize(settings);
127
128         if (fOptionsTab.useJARFormat()) {
129             createJARSigningTab(folder);
130             fJARSiginingTab.initialize(settings);
131         }
132     }
133     
134     protected void createDestinationTab(TabFolder folder) {
135         fDestinationTab = new ExportDestinationTab(this);
136         TabItem item = new TabItem(folder, SWT.NONE);
137         item.setControl(fDestinationTab.createControl(folder));
138         item.setText(PDEUIMessages.ExportWizard_destination);
139     }
140     
141     protected void createOptionsTab(TabFolder folder) {
142         fOptionsTab = new ExportOptionsTab(this);
143         TabItem item = new TabItem(folder, SWT.NONE);
144         item.setControl(fOptionsTab.createControl(folder));
145         item.setText(PDEUIMessages.ExportWizard_options);
146     }
147     
148     protected void createJARSigningTab(TabFolder folder) {
149         fJARSiginingTab = new JARSigningTab(this);
150         TabItem item = new TabItem(folder, SWT.NONE);
151         item.setControl(fJARSiginingTab.createControl(folder));
152         item.setText(PDEUIMessages.AdvancedPluginExportPage_signJar);
153     }
154    
155     protected void createViewer(Composite parent) {
156         Composite composite = new Composite(parent, SWT.NONE);
157         GridLayout layout = new GridLayout();
158         layout.numColumns = 3;
159         layout.marginHeight = 0;
160         layout.marginWidth = 0;
161         composite.setLayout(layout);
162         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
163         
164         fExportPart.createControl(composite);
165         GridData gd = (GridData) fExportPart.getControl().getLayoutData();
166         gd.heightHint = 125;
167         gd.widthHint = 150;
168         gd.horizontalSpan = 2;
169
170         TableViewer viewer = fExportPart.getTableViewer();
171         viewer.setContentProvider(new ExportListProvider());
172         viewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
173         viewer.setComparator(ListUtil.PLUGIN_COMPARATOR);
174         fExportPart.getTableViewer().setInput(getInput());
175     }
176     
177     protected abstract Object JavaDoc getInput();
178     
179     protected void initializeViewer() {
180         Object JavaDoc[] elems = fSelection.toArray();
181         ArrayList JavaDoc checked = new ArrayList JavaDoc(elems.length);
182
183         for (int i = 0; i < elems.length; i++) {
184             Object JavaDoc elem = elems[i];
185             IProject project = null;
186
187             if (elem instanceof IFile) {
188                 IFile file = (IFile) elem;
189                 project = file.getProject();
190             } else if (elem instanceof IProject) {
191                 project = (IProject) elem;
192             } else if (elem instanceof IJavaProject) {
193                 project = ((IJavaProject) elem).getProject();
194             }
195             if (project != null) {
196                 IModel model = findModelFor(project);
197                 if (model != null && !checked.contains(model)) {
198                     checked.add(model);
199                 }
200             }
201         }
202         fExportPart.setSelection(checked.toArray());
203         if (checked.size() > 0)
204             fExportPart.getTableViewer().reveal(checked.get(0));
205     }
206
207     private void handleWorkingSets() {
208         IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
209         IWorkingSetSelectionDialog dialog = manager.createWorkingSetSelectionDialog(getShell(), true);
210         if (dialog.open() == Window.OK) {
211             ArrayList JavaDoc models = new ArrayList JavaDoc();
212             IWorkingSet[] workingSets = dialog.getSelection();
213             for (int i = 0; i < workingSets.length; i++) {
214                 IAdaptable[] elements = workingSets[i].getElements();
215                 for (int j = 0; j < elements.length; j++) {
216                     IModel model = findModelFor(elements[j]);
217                     if (isValidModel(model)) {
218                         models.add(model);
219                     }
220                 }
221             }
222             fExportPart.setSelection(models.toArray());
223         }
224     }
225     
226     public Object JavaDoc[] getSelectedItems() {
227         return fExportPart.getSelection();
228     }
229     
230     protected void pageChanged() {
231         if (getMessage() != null)
232             setMessage(null);
233         if (fOptionsTab != null) {
234             String JavaDoc path = fOptionsTab.getAntBuildFileName();
235             String JavaDoc warningMessage = null;
236             if (path != null && path.length() > 0 && "build.xml".equals(new Path(path).lastSegment())) //$NON-NLS-1$
237
warningMessage = PDEUIMessages.ExportOptionsTab_antReservedMessage;
238             setMessage(warningMessage, IMessageProvider.WARNING);
239         }
240         String JavaDoc error = fExportPart.getSelectionCount() > 0 ? null
241                 : PDEUIMessages.ExportWizard_status_noselection;
242         if (error == null)
243             error = validateTabs();
244         setErrorMessage(error);
245         setPageComplete(error == null);
246     }
247     
248     protected String JavaDoc validateTabs() {
249         String JavaDoc message = fDestinationTab.validate();
250         if (message == null)
251             message = fOptionsTab.validate();
252         if (message == null && fTabFolder.getItemCount() > 2)
253             message = fJARSiginingTab.validate();
254         return message;
255     }
256
257     protected abstract void hookHelpContext(Control control);
258     
259     protected abstract boolean isValidModel(IModel model);
260     
261     public abstract Object JavaDoc[] getListElements();
262     
263     protected abstract IModel findModelFor(IAdaptable object);
264     
265     protected void saveSettings(IDialogSettings settings) {
266         fDestinationTab.saveSettings(settings);
267         fOptionsTab.saveSettings(settings);
268         if (fJARSiginingTab != null)
269             fJARSiginingTab.saveSettings(settings);
270     }
271
272     protected boolean doExportToDirectory() {
273         return fDestinationTab.doExportToDirectory();
274     }
275
276     protected String JavaDoc getFileName() {
277         return fDestinationTab.getFileName();
278     }
279
280     protected String JavaDoc getDestination() {
281         return fDestinationTab.getDestination();
282     }
283
284     protected boolean doExportSource() {
285         return fOptionsTab.doExportSource();
286     }
287
288     protected boolean useJARFormat() {
289         return fOptionsTab.useJARFormat();
290     }
291
292     protected boolean doGenerateAntFile() {
293         return fOptionsTab.doGenerateAntFile();
294     }
295
296     protected String JavaDoc getAntBuildFileName() {
297         return fOptionsTab.getAntBuildFileName();
298     }
299     
300     protected String JavaDoc getQualifier() {
301         return fOptionsTab.getQualifier();
302     }
303
304     protected String JavaDoc[] getSigningInfo() {
305         if (fJARSiginingTab == null || fTabFolder.getItemCount() < 3)
306             return null;
307         return fJARSiginingTab.getSigningInfo();
308     }
309
310     protected abstract void adjustAdvancedTabsVisibility();
311     
312     protected void adjustJARSigningTabVisibility() {
313         IDialogSettings settings = getDialogSettings();
314         if (useJARFormat()) {
315             if (fTabFolder.getItemCount() < 3) {
316                 createJARSigningTab(fTabFolder);
317                 fJARSiginingTab.initialize(settings);
318             }
319         } else {
320             if (fTabFolder.getItemCount() >= 3) {
321                 fJARSiginingTab.saveSettings(settings);
322                 fTabFolder.getItem(2).dispose();
323             }
324         }
325     }
326
327 }
328
Popular Tags