KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.jface.dialogs.ErrorDialog;
26 import org.eclipse.jface.dialogs.IDialogConstants;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.jface.operation.IRunnableWithProgress;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.jface.viewers.IContentProvider;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.viewers.LabelProvider;
34 import org.eclipse.jface.viewers.SelectionChangedEvent;
35 import org.eclipse.jface.viewers.StructuredSelection;
36 import org.eclipse.jface.viewers.TableViewer;
37 import org.eclipse.jface.wizard.IWizardContainer;
38 import org.eclipse.jface.wizard.IWizardPage;
39 import org.eclipse.jface.wizard.WizardDialog;
40 import org.eclipse.osgi.util.NLS;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.events.SelectionEvent;
43 import org.eclipse.swt.events.SelectionListener;
44 import org.eclipse.swt.graphics.Font;
45 import org.eclipse.swt.graphics.Image;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Button;
49 import org.eclipse.swt.widgets.Composite;
50 import org.eclipse.swt.widgets.Control;
51 import org.eclipse.swt.widgets.Label;
52 import org.eclipse.swt.widgets.Shell;
53 import org.eclipse.ui.ICapabilityUninstallWizard;
54 import org.eclipse.ui.PlatformUI;
55 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
56 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
57 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
58 import org.eclipse.ui.internal.ide.registry.Capability;
59 import org.eclipse.ui.internal.ide.registry.CapabilityRegistry;
60 import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog;
61 import org.eclipse.ui.model.WorkbenchContentProvider;
62
63 /**
64  * A property page for IProject resources to edit a single
65  * capability at a time.
66  */

67 public class ProjectCapabilityEditingPropertyPage extends
68         ProjectCapabilityPropertyPage {
69     private static final int SIZING_WIZARD_WIDTH = 500;
70
71     private static final int SIZING_WIZARD_HEIGHT = 500;
72
73     private TableViewer table;
74
75     private Button addButton;
76
77     private Button removeButton;
78
79     private ArrayList JavaDoc disabledCaps = new ArrayList JavaDoc();
80
81     private Capability selectedCap;
82
83     private CapabilityRegistry reg;
84
85     /**
86      * Creates a new ProjectCapabilityEditingPropertyPage.
87      */

88     public ProjectCapabilityEditingPropertyPage() {
89         super();
90     }
91
92     /* (non-Javadoc)
93      * Method declared on PreferencePage
94      */

95     protected Control createContents(Composite parent) {
96         Font font = parent.getFont();
97         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
98                 IIDEHelpContextIds.PROJECT_CAPABILITY_PROPERTY_PAGE);
99         noDefaultAndApplyButton();
100         reg = IDEWorkbenchPlugin.getDefault().getCapabilityRegistry();
101
102         Composite topComposite = new Composite(parent, SWT.NONE);
103         GridLayout layout = new GridLayout();
104         layout.marginHeight = 0;
105         layout.marginWidth = 0;
106         topComposite.setLayout(layout);
107         topComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
108
109         String JavaDoc instructions;
110         if (reg.hasCapabilities())
111             instructions = IDEWorkbenchMessages.ProjectCapabilityPropertyPage_chooseCapabilities;
112         else
113             instructions = IDEWorkbenchMessages.ProjectCapabilityPropertyPage_noCapabilities;
114         Label label = new Label(topComposite, SWT.LEFT);
115         label.setFont(font);
116         label.setText(instructions);
117
118         Capability[] caps = reg.getProjectDisabledCapabilities(getProject());
119         disabledCaps.addAll(Arrays.asList(caps));
120
121         Composite mainComposite = new Composite(topComposite, SWT.NONE);
122         layout = new GridLayout();
123         layout.numColumns = 2;
124         layout.marginHeight = 0;
125         layout.marginWidth = 0;
126         mainComposite.setLayout(layout);
127         mainComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
128
129         Composite capComposite = new Composite(mainComposite, SWT.NONE);
130         layout = new GridLayout();
131         layout.marginHeight = 0;
132         layout.marginWidth = 0;
133         capComposite.setLayout(layout);
134         capComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
135
136         label = new Label(capComposite, SWT.LEFT);
137         label.setFont(font);
138         label.setText(IDEWorkbenchMessages.ProjectCapabilitySelectionGroup_capabilities);
139
140         table = new TableViewer(capComposite, SWT.SINGLE | SWT.H_SCROLL
141                 | SWT.V_SCROLL | SWT.BORDER);
142         table.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
143         table.getTable().setFont(font);
144         table.setLabelProvider(new CapabilityLabelProvider());
145         table.setContentProvider(getContentProvider());
146         table.setInput(getProject());
147
148         Composite buttonComposite = new Composite(mainComposite, SWT.NONE);
149         layout = new GridLayout();
150         layout.marginHeight = 0;
151         layout.marginWidth = 0;
152         buttonComposite.setLayout(layout);
153         buttonComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
154
155         label = new Label(buttonComposite, SWT.LEFT);
156         label.setFont(font);
157         label.setText(""); //$NON-NLS-1$
158

159         addButton = new Button(buttonComposite, SWT.PUSH);
160         addButton.setText(IDEWorkbenchMessages.ProjectCapabilityEditingPropertyPage_add);
161         addButton.setEnabled(true);
162         addButton.addSelectionListener(new SelectionListener() {
163             public void widgetSelected(SelectionEvent e) {
164                 addCapability();
165             }
166
167             public void widgetDefaultSelected(SelectionEvent e) {
168             }
169         });
170         GridData data = new GridData();
171         data.horizontalAlignment = GridData.FILL;
172         int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
173         data.widthHint = Math.max(widthHint, addButton.computeSize(SWT.DEFAULT,
174                 SWT.DEFAULT, true).x);
175         addButton.setLayoutData(data);
176         addButton.setFont(font);
177
178         removeButton = new Button(buttonComposite, SWT.PUSH);
179         removeButton.setText(IDEWorkbenchMessages.ProjectCapabilityEditingPropertyPage_remove);
180         removeButton.setEnabled(false);
181         removeButton.addSelectionListener(new SelectionListener() {
182             public void widgetSelected(SelectionEvent e) {
183                 removeCapability(selectedCap);
184             }
185
186             public void widgetDefaultSelected(SelectionEvent e) {
187             }
188         });
189         data = new GridData();
190         data.horizontalAlignment = GridData.FILL;
191         widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
192         data.widthHint = Math.max(widthHint, removeButton.computeSize(
193                 SWT.DEFAULT, SWT.DEFAULT, true).x);
194         removeButton.setLayoutData(data);
195         removeButton.setFont(font);
196
197         table.addSelectionChangedListener(new ISelectionChangedListener() {
198             public void selectionChanged(SelectionChangedEvent event) {
199                 selectedCap = null;
200                 IStructuredSelection sel = (IStructuredSelection) event
201                         .getSelection();
202                 if (sel != null)
203                     selectedCap = (Capability) sel.getFirstElement();
204                 removeButton.setEnabled(selectedCap != null);
205             }
206         });
207
208         return topComposite;
209     }
210
211     /**
212      * Returns the content provider for the viewers
213      */

214     private IContentProvider getContentProvider() {
215         return new WorkbenchContentProvider() {
216             public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
217                 if (parentElement instanceof IProject)
218                     return reg.getProjectCapabilities((IProject) parentElement);
219                 else
220                     return null;
221             }
222         };
223     }
224
225     /**
226      * Returns whether the category is considered disabled
227      */

228     private boolean isDisabledCapability(Capability cap) {
229         return disabledCaps.contains(cap);
230     }
231
232     private void addCapability() {
233         ProjectCapabilitySimpleAddWizard wizard;
234         wizard = new ProjectCapabilitySimpleAddWizard(
235                 PlatformUI.getWorkbench(), StructuredSelection.EMPTY,
236                 getProject());
237         WizardDialog dialog = new WizardDialog(getShell(), wizard);
238         dialog.create();
239         dialog.getShell().setSize(
240                 Math.max(SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x),
241                 SIZING_WIZARD_HEIGHT);
242         PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(),
243                 IIDEHelpContextIds.UPDATE_CAPABILITY_WIZARD);
244         dialog.open();
245
246         table.refresh();
247     }
248
249     private void removeCapability(Capability cap) {
250         ArrayList JavaDoc results = new ArrayList JavaDoc();
251         results.addAll(Arrays.asList(reg.getProjectCapabilities(getProject())));
252         results.remove(cap);
253         Capability[] caps = new Capability[results.size()];
254         results.toArray(caps);
255
256         for (int i = 0; i < caps.length; i++) {
257             List JavaDoc prereqs = Arrays.asList(reg.getPrerequisiteIds(caps[i]));
258             if (prereqs.contains(cap.getId())) {
259                 MessageDialog
260                         .openWarning(
261                                 getShell(),
262                                 IDEWorkbenchMessages.ProjectCapabilityPropertyPage_errorTitle,
263                                 NLS.bind(IDEWorkbenchMessages.ProjectCapabilityPropertyPage_capabilityRequired, caps[i].getName()));
264                 return;
265             }
266         }
267
268         IStatus status = reg.validateCapabilities(caps);
269         if (!status.isOK()) {
270             ErrorDialog
271                     .openError(
272                             getShell(),
273                             IDEWorkbenchMessages.ProjectCapabilityPropertyPage_errorTitle,
274                             IDEWorkbenchMessages.ProjectCapabilityPropertyPage_invalidSelection,
275                             status);
276             return;
277         }
278
279         String JavaDoc[] natureIds = new String JavaDoc[1];
280         natureIds[0] = cap.getNatureId();
281
282         ICapabilityUninstallWizard wizard = cap.getUninstallWizard();
283         if (wizard == null)
284             wizard = new RemoveCapabilityWizard();
285         if (wizard != null) {
286             wizard.init(PlatformUI.getWorkbench(), StructuredSelection.EMPTY,
287                     getProject(), natureIds);
288             wizard.addPages();
289         }
290
291         if (wizard.getStartingPage() == null) {
292             wizard.setContainer(new StubContainer());
293             wizard.performFinish();
294             wizard.setContainer(null);
295         } else {
296             wizard = cap.getUninstallWizard();
297             if (wizard == null)
298                 wizard = new RemoveCapabilityWizard();
299             if (wizard != null)
300                 wizard.init(PlatformUI.getWorkbench(),
301                         StructuredSelection.EMPTY, getProject(), natureIds);
302             WizardDialog dialog = new WizardDialog(getShell(), wizard);
303             dialog.create();
304             dialog.getShell().setSize(
305                     Math
306                             .max(SIZING_WIZARD_WIDTH, dialog.getShell()
307                                     .getSize().x), SIZING_WIZARD_HEIGHT);
308             PlatformUI.getWorkbench().getHelpSystem().setHelp(
309                     dialog.getShell(),
310                     IIDEHelpContextIds.UPDATE_CAPABILITY_WIZARD);
311             dialog.open();
312         }
313
314         table.refresh();
315     }
316
317     /* (non-Javadoc)
318      * Method declared on PreferencePage
319      */

320     public boolean performOk() {
321         return true;
322     }
323
324     class CapabilityLabelProvider extends LabelProvider {
325         private Map JavaDoc imageTable;
326
327         public void dispose() {
328             if (imageTable != null) {
329                 Iterator JavaDoc it = imageTable.values().iterator();
330                 while (it.hasNext())
331                     ((Image) it.next()).dispose();
332                 imageTable = null;
333             }
334         }
335
336         public Image getImage(Object JavaDoc element) {
337             ImageDescriptor descriptor = ((Capability) element)
338                     .getIconDescriptor();
339             if (descriptor == null)
340                 return null;
341
342             //obtain the cached image corresponding to the descriptor
343
if (imageTable == null) {
344                 imageTable = new Hashtable JavaDoc(40);
345             }
346             Image image = (Image) imageTable.get(descriptor);
347             if (image == null) {
348                 image = descriptor.createImage();
349                 imageTable.put(descriptor, image);
350             }
351             return image;
352         }
353
354         public String JavaDoc getText(Object JavaDoc element) {
355             Capability cap = (Capability) element;
356             String JavaDoc text = cap.getName();
357             if (isDisabledCapability(cap))
358                 text = NLS.bind(IDEWorkbenchMessages.ProjectCapabilitySelectionGroup_disabledLabel, text);
359             return text;
360         }
361     }
362
363     class StubContainer implements IWizardContainer {
364         public IWizardPage getCurrentPage() {
365             return null;
366         }
367
368         public Shell getShell() {
369             return ProjectCapabilityEditingPropertyPage.this.getShell();
370         }
371
372         public void showPage(IWizardPage page) {
373         }
374
375         public void updateButtons() {
376         }
377
378         public void updateMessage() {
379         }
380
381         public void updateTitleBar() {
382         }
383
384         public void updateWindowTitle() {
385         }
386
387         public void run(boolean fork, boolean cancelable,
388                 IRunnableWithProgress runnable)
389                 throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
390             new ProgressMonitorJobsDialog(getShell()).run(fork, cancelable,
391                     runnable);
392         }
393     }
394 }
395
Popular Tags