KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.dialogs.PropertyPage;
28 import org.eclipse.ui.internal.ide.Category;
29 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
30 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
31 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
32 import org.eclipse.ui.internal.ide.misc.ProjectCapabilitySelectionGroup;
33 import org.eclipse.ui.internal.ide.registry.Capability;
34 import org.eclipse.ui.internal.ide.registry.CapabilityRegistry;
35
36 /**
37  * A property page for IProject resources to view and edit the
38  * capabilities assigned to the project.
39  */

40 public class ProjectCapabilityPropertyPage extends PropertyPage {
41     /**
42      * The wizard dialog width
43      */

44     private static final int SIZING_WIZARD_WIDTH = 500;
45
46     /**
47      * The wizard dialog height
48      */

49     private static final int SIZING_WIZARD_HEIGHT = 500;
50
51     private IProject project;
52
53     private ProjectCapabilitySelectionGroup capabilityGroup;
54
55     /**
56      * Creates a new ProjectCapabilityPropertyPage.
57      */

58     public ProjectCapabilityPropertyPage() {
59         super();
60     }
61
62     /* (non-Javadoc)
63      * Method declared on PreferencePage
64      */

65     protected Control createContents(Composite parent) {
66         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
67                 IIDEHelpContextIds.PROJECT_CAPABILITY_PROPERTY_PAGE);
68         noDefaultAndApplyButton();
69         CapabilityRegistry reg = IDEWorkbenchPlugin.getDefault()
70                 .getCapabilityRegistry();
71
72         String JavaDoc instructions;
73         if (reg.hasCapabilities())
74             instructions = IDEWorkbenchMessages.ProjectCapabilityPropertyPage_chooseCapabilities;
75         else
76             instructions = IDEWorkbenchMessages.ProjectCapabilityPropertyPage_noCapabilities;
77         Label label = new Label(parent, SWT.LEFT);
78         label.setFont(parent.getFont());
79         label.setText(instructions);
80
81         Capability[] caps = reg.getProjectCapabilities(getProject());
82         Capability[] disabledCaps = reg
83                 .getProjectDisabledCapabilities(getProject());
84         Category[] cats = new Category[0];
85         capabilityGroup = new ProjectCapabilitySelectionGroup(cats, caps,
86                 disabledCaps, reg);
87         return capabilityGroup.createContents(parent);
88     }
89
90     /**
91      * Returns the project which this property page applies to
92      *
93      * @return IProject the project for this property page
94      */

95     /* package */IProject getProject() {
96         if (project == null)
97             project = (IProject) getElement().getAdapter(IResource.class);
98
99         return project;
100     }
101
102     /* (non-Javadoc)
103      * Method declared on PreferencePage
104      */

105     public boolean performOk() {
106         // Avoid doing any work if no changes were made.
107
if (!capabilityGroup.getCapabilitiesModified())
108             return true;
109
110         // Validate the requested changes are ok
111
CapabilityRegistry reg = IDEWorkbenchPlugin.getDefault()
112                 .getCapabilityRegistry();
113         Capability[] caps = capabilityGroup.getSelectedCapabilities();
114         IStatus status = reg.validateCapabilities(caps);
115         if (!status.isOK()) {
116             ErrorDialog
117                     .openError(
118                             getShell(),
119                             IDEWorkbenchMessages.ProjectCapabilityPropertyPage_errorTitle,
120                             IDEWorkbenchMessages.ProjectCapabilityPropertyPage_invalidSelection,
121                             status);
122             return true;
123         }
124
125         // Get the current set of nature ids on the project
126
String JavaDoc[] natureIds;
127         try {
128             natureIds = getProject().getDescription().getNatureIds();
129             natureIds = getProject().getWorkspace().sortNatureSet(natureIds);
130         } catch (CoreException e) {
131             ErrorDialog
132                     .openError(
133                             getShell(),
134                             IDEWorkbenchMessages.ProjectCapabilityPropertyPage_errorTitle,
135                             IDEWorkbenchMessages.ProjectCapabilityPropertyPage_internalError,
136                             e.getStatus());
137             return true;
138         }
139
140         // Keep only the nature ids whose capability is selected
141
ArrayList JavaDoc keepIds = new ArrayList JavaDoc();
142         ArrayList JavaDoc removeCaps = new ArrayList JavaDoc();
143         for (int i = 0; i < natureIds.length; i++) {
144             boolean isRemoved = true;
145             String JavaDoc id = natureIds[i];
146             for (int j = 0; j < caps.length; j++) {
147                 if (id.equals(caps[j].getNatureId())) {
148                     keepIds.add(id);
149                     isRemoved = false;
150                     break;
151                 }
152             }
153             if (isRemoved)
154                 removeCaps.add(reg.getCapabilityForNature(id));
155         }
156
157         // Collect the capabilities to add
158
ArrayList JavaDoc newCaps = new ArrayList JavaDoc();
159         for (int i = 0; i < caps.length; i++) {
160             boolean isNew = true;
161             Capability cap = caps[i];
162             for (int j = 0; j < natureIds.length; j++) {
163                 if (natureIds[j].equals(cap.getNatureId())) {
164                     isNew = false;
165                     break;
166                 }
167             }
168             if (isNew)
169                 newCaps.add(cap);
170         }
171
172         // Launch the step wizard if needed
173
if (newCaps.size() > 0 || removeCaps.size() > 0) {
174             Capability[] newCapabilities = new Capability[newCaps.size()];
175             newCaps.toArray(newCapabilities);
176
177             Capability[] removeCapabilities = new Capability[removeCaps.size()];
178             removeCaps.toArray(removeCapabilities);
179
180             UpdateProjectCapabilityWizard wizard = new UpdateProjectCapabilityWizard(
181                     getProject(), newCapabilities, removeCapabilities);
182
183             MultiStepWizardDialog dialog = new MultiStepWizardDialog(
184                     getShell(), wizard);
185             dialog.create();
186             dialog.getShell().setSize(
187                     Math
188                             .max(SIZING_WIZARD_WIDTH, dialog.getShell()
189                                     .getSize().x), SIZING_WIZARD_HEIGHT);
190             PlatformUI.getWorkbench().getHelpSystem().setHelp(
191                     dialog.getShell(),
192                     IIDEHelpContextIds.UPDATE_CAPABILITY_WIZARD);
193             dialog.open();
194         }
195
196         return true;
197     }
198 }
199
Popular Tags