KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > ProjectCapabilitySimpleSelectionPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font should be
11  * activated and used by other components.
12  *******************************************************************************/

13 package org.eclipse.ui.internal.ide.dialogs;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.jface.viewers.IContentProvider;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 import org.eclipse.jface.viewers.TreeViewer;
27 import org.eclipse.jface.wizard.IWizard;
28 import org.eclipse.jface.wizard.IWizardPage;
29 import org.eclipse.jface.wizard.WizardPage;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.custom.BusyIndicator;
33 import org.eclipse.swt.graphics.Font;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.ui.ICapabilityInstallWizard;
39 import org.eclipse.ui.IWorkbench;
40 import org.eclipse.ui.internal.ide.Category;
41 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
42 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
43 import org.eclipse.ui.internal.ide.registry.Capability;
44 import org.eclipse.ui.internal.ide.registry.CapabilityRegistry;
45 import org.eclipse.ui.model.WorkbenchContentProvider;
46 import org.eclipse.ui.model.WorkbenchLabelProvider;
47
48 public class ProjectCapabilitySimpleSelectionPage extends WizardPage {
49     private IWorkbench workbench;
50
51     private IStructuredSelection selection;
52
53     private IProject project;
54
55     private TreeViewer viewer;
56
57     private CapabilityRegistry reg;
58
59     private Capability chosenCapability;
60
61     private HashMap JavaDoc mapCapToWizard = new HashMap JavaDoc();
62
63     /**
64      * Creates an instance of this page.
65      */

66     public ProjectCapabilitySimpleSelectionPage(String JavaDoc pageName,
67             IWorkbench workbench, IStructuredSelection selection,
68             IProject project) {
69         super(pageName);
70         this.workbench = workbench;
71         this.selection = selection;
72         this.project = project;
73         this.reg = IDEWorkbenchPlugin.getDefault().getCapabilityRegistry();
74     }
75
76     /* (non-Javadoc)
77      * Method declared on IWizardPage
78      */

79     public boolean canFlipToNextPage() {
80         return isPageComplete();
81     }
82
83     /* (non-Javadoc)
84      * Method declared on IDialogPage
85      */

86     public void createControl(Composite parent) {
87         Font font = parent.getFont();
88         Composite topContainer = new Composite(parent, SWT.NONE);
89         topContainer.setLayout(new GridLayout());
90         topContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
91
92         Label label = new Label(topContainer, SWT.LEFT);
93         label.setText(IDEWorkbenchMessages.ProjectCapabilitySelectionGroup_capabilities);
94         GridData data = new GridData();
95         data.verticalAlignment = SWT.TOP;
96         label.setLayoutData(data);
97         label.setFont(font);
98
99         viewer = new TreeViewer(topContainer, SWT.SINGLE | SWT.H_SCROLL
100                 | SWT.V_SCROLL | SWT.BORDER);
101         viewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
102         viewer.getTree().setFont(font);
103         viewer.setLabelProvider(new WorkbenchLabelProvider());
104         viewer.setContentProvider(getContentProvider());
105         viewer.setInput(reg);
106
107         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
108             public void selectionChanged(SelectionChangedEvent event) {
109                 chosenCapability = null;
110                 if (event.getSelection() instanceof IStructuredSelection) {
111                     IStructuredSelection sel = (IStructuredSelection) event
112                             .getSelection();
113                     if (sel != null && !sel.isEmpty()) {
114                         Object JavaDoc result = sel.getFirstElement();
115                         if (result instanceof Capability) {
116                             chosenCapability = (Capability) result;
117                         }
118                     }
119                 }
120
121                 setPageComplete(validateChosenCapability());
122             }
123         });
124
125         setControl(topContainer);
126     }
127
128     /**
129      * Returns the content provider for the viewer
130      */

131     private IContentProvider getContentProvider() {
132         return new WorkbenchContentProvider() {
133             public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
134                 if (parentElement instanceof CapabilityRegistry) {
135                     ArrayList JavaDoc cats = reg.getUsedCategories();
136                     if (reg.getMiscCategory() != null)
137                         cats.add(reg.getMiscCategory());
138                     return cats.toArray();
139                 }
140                 if (parentElement instanceof Category)
141                     return ((Category) parentElement)
142                             .getChildren(parentElement);
143                 return null;
144             }
145
146             public boolean hasChildren(Object JavaDoc element) {
147                 if (element instanceof CapabilityRegistry)
148                     return true;
149                 if (element instanceof Category)
150                     return ((Category) element).hasElements();
151                 return false;
152             }
153         };
154     }
155
156     /* (non-Javadoc)
157      * Method declared on IWizardPage.
158      */

159     public IWizardPage getNextPage() {
160         if (chosenCapability == null)
161             return null;
162
163         final IWizard[] wizard = new IWizard[1];
164         wizard[0] = (IWizard) mapCapToWizard.get(chosenCapability);
165         if (wizard[0] == null) {
166             BusyIndicator.showWhile(getShell().getDisplay(), new Runnable JavaDoc() {
167                 public void run() {
168                     ICapabilityInstallWizard wiz;
169                     wiz = chosenCapability.getInstallWizard();
170                     if (wiz != null) {
171                         wiz.init(workbench, selection, project);
172                         wiz.addPages();
173                         mapCapToWizard.put(chosenCapability, wiz);
174                         wizard[0] = wiz;
175                     }
176                 }
177             });
178         }
179
180         if (wizard[0] == null)
181             return null;
182
183         IWizardPage page = wizard[0].getStartingPage();
184         wizard[0] = null;
185         return page;
186     }
187
188     private boolean validateChosenCapability() {
189         if (chosenCapability == null) {
190             setErrorMessage(null);
191             return false;
192         }
193
194         Capability[] caps = reg.getProjectCapabilities(project);
195         List JavaDoc existingCaps = Arrays.asList(caps);
196         if (existingCaps.contains(chosenCapability)) {
197             setErrorMessage(IDEWorkbenchMessages.ProjectCapabilitySimpleSelectionPage_capabilityExist);
198             return false;
199         }
200
201         String JavaDoc[] ids = reg.getPrerequisiteIds(chosenCapability);
202         Capability[] prereqs = reg.findCapabilities(ids);
203         for (int i = 0; i < prereqs.length; i++) {
204             if (prereqs[i] == null) {
205                 setErrorMessage(NLS.bind(IDEWorkbenchMessages.ProjectCapabilitySimpleSelectionPage_capabilityMissing, ids[i]));
206                 return false;
207             }
208             if (!existingCaps.contains(prereqs[i])) {
209                 setErrorMessage(NLS.bind(IDEWorkbenchMessages.ProjectCapabilitySimpleSelectionPage_capabilityRequired, prereqs[i].getName()));
210                 return false;
211             }
212         }
213
214         ids = reg.getMembershipSetIds(chosenCapability);
215         List JavaDoc idsList = Arrays.asList(ids);
216         for (int i = 0; i < caps.length; i++) {
217             String JavaDoc[] setIds = reg.getMembershipSetIds(caps[i]);
218             for (int j = 0; j < setIds.length; j++) {
219                 if (idsList.contains(setIds[j])) {
220                     setErrorMessage(NLS.bind(IDEWorkbenchMessages.ProjectCapabilitySimpleSelectionPage_capabilitySet, caps[i].getName()));
221                     return false;
222                 }
223             }
224         }
225
226         Capability[] newCaps = new Capability[caps.length + 1];
227         System.arraycopy(caps, 0, newCaps, 0, caps.length);
228         newCaps[caps.length] = chosenCapability;
229         IStatus status = reg.validateCapabilities(newCaps);
230         if (!status.isOK()) {
231             setErrorMessage(status.getMessage());
232             return false;
233         }
234
235         setErrorMessage(null);
236         return true;
237     }
238 }
239
Popular Tags