KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > ReferenceWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.feature;
12
13 import java.lang.reflect.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.operation.*;
18 import org.eclipse.jface.viewers.*;
19 import org.eclipse.jface.wizard.*;
20 import org.eclipse.pde.core.plugin.*;
21 import org.eclipse.pde.internal.core.*;
22 import org.eclipse.pde.internal.core.ifeature.*;
23 import org.eclipse.pde.internal.ui.*;
24 import org.eclipse.pde.internal.ui.elements.*;
25 import org.eclipse.pde.internal.ui.parts.*;
26 import org.eclipse.pde.internal.ui.wizards.*;
27 import org.eclipse.swt.*;
28 import org.eclipse.swt.layout.*;
29 import org.eclipse.swt.widgets.*;
30 import org.eclipse.ui.*;
31 import org.eclipse.ui.forms.widgets.*;
32
33 public abstract class ReferenceWizardPage extends WizardPage {
34     public static final String JavaDoc KEY_PLUGINS =
35         "FeatureEditor.PluginSection.new.label"; //$NON-NLS-1$
36
protected IFeatureModel model;
37     private TablePart checkboxTablePart;
38     private CheckboxTableViewer pluginViewer;
39     private boolean includeExternal;
40
41     class PluginContentProvider
42         extends DefaultContentProvider
43         implements IStructuredContentProvider {
44         public Object JavaDoc[] getElements(Object JavaDoc parent) {
45             return getChoices();
46         }
47     }
48     
49     class TablePart extends WizardCheckboxTablePart {
50         public TablePart() {
51             super(PDEPlugin.getResourceString(KEY_PLUGINS));
52         }
53         public void updateCounter(int count) {
54             super.updateCounter(count);
55             setPageComplete(count>0);
56         }
57         protected StructuredViewer createStructuredViewer(
58             Composite parent,
59             int style,
60             FormToolkit toolkit) {
61             StructuredViewer viewer =
62                 super.createStructuredViewer(parent, style, toolkit);
63             viewer.setSorter(ListUtil.PLUGIN_SORTER);
64             return viewer;
65         }
66     }
67
68     public ReferenceWizardPage(IFeatureModel model, boolean includeExternal) {
69         super("newFeaturePluginPage"); //$NON-NLS-1$
70
this.model = model;
71         setPageComplete(false);
72         checkboxTablePart = new TablePart();
73         PDEPlugin.getDefault().getLabelProvider().connect(this);
74         this.includeExternal = includeExternal;
75     }
76     
77     public void dispose() {
78         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
79         super.dispose();
80     }
81
82     public void createControl(Composite parent) {
83         Composite container = new Composite(parent, SWT.NULL);
84         GridLayout layout = new GridLayout();
85         layout.numColumns = 2;
86         container.setLayout(layout);
87         
88         createPluginList(container);
89         initialize();
90         setControl(container);
91         Dialog.applyDialogFont(container);
92     }
93
94     protected void createPluginList(Composite parent) {
95         checkboxTablePart.createControl(parent);
96         pluginViewer = checkboxTablePart.getTableViewer();
97         pluginViewer.setContentProvider(new PluginContentProvider());
98         pluginViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
99         pluginViewer.addFilter(new ViewerFilter() {
100             public boolean select(Viewer v, Object JavaDoc parent, Object JavaDoc object) {
101                 if (object instanceof IPluginModelBase) {
102                     IPluginModelBase model = (IPluginModelBase) object;
103                     return !isOnTheList(model);
104                 }
105                 return true;
106             }
107         });
108         pluginViewer.setSorter(ListUtil.PLUGIN_SORTER);
109         GridData gd = (GridData)checkboxTablePart.getControl().getLayoutData();
110         gd.heightHint = 300;
111     }
112     
113     protected abstract boolean isOnTheList(IPluginModelBase candidate);
114
115     public void init(IWorkbench workbench) {
116     }
117
118     private void initialize() {
119         pluginViewer.setInput(model.getFeature());
120         checkboxTablePart.setSelection(new Object JavaDoc[0]);
121     }
122     
123     private Object JavaDoc[] getAllChoices() {
124         PluginModelManager pmng = PDECore.getDefault().getModelManager();
125         return pmng.getPlugins();
126     }
127
128     private Object JavaDoc[] getChoices() {
129         if (includeExternal) return getAllChoices();
130         WorkspaceModelManager mng = PDECore.getDefault().getWorkspaceModelManager();
131         IPluginModel[] plugins = mng.getPluginModels();
132         IFragmentModel[] fragments = mng.getFragmentModels();
133
134         Object JavaDoc[] choices = new Object JavaDoc[plugins.length + fragments.length];
135         System.arraycopy(plugins, 0, choices, 0, plugins.length);
136         System.arraycopy(fragments, 0, choices, plugins.length, fragments.length);
137         return choices;
138     }
139
140     public boolean finish() {
141         final Object JavaDoc [] candidates = checkboxTablePart.getSelection();
142         IRunnableWithProgress op = new IRunnableWithProgress() {
143             public void run(IProgressMonitor monitor) throws InvocationTargetException {
144                 try {
145                     doAdd(candidates, monitor);
146                 } catch (CoreException e) {
147                     throw new InvocationTargetException(e);
148                 }
149             }
150         };
151         try {
152             getContainer().run(false, false, op);
153         } catch (InterruptedException JavaDoc e) {
154             return false;
155         } catch (InvocationTargetException e) {
156             PDEPlugin.logException(e);
157             return false;
158         }
159         return true;
160     }
161
162     protected abstract void doAdd(Object JavaDoc [] candidates, IProgressMonitor monitor) throws CoreException;
163 }
164
Popular Tags