KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > target > ImplicitDependenciesSection


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.editor.target;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.SelectionChangedEvent;
20 import org.eclipse.jface.viewers.TableViewer;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.jface.viewers.ViewerComparator;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.osgi.service.resolver.BundleDescription;
25 import org.eclipse.pde.core.IModelChangedEvent;
26 import org.eclipse.pde.core.plugin.IPluginModelBase;
27 import org.eclipse.pde.core.plugin.PluginRegistry;
28 import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo;
29 import org.eclipse.pde.internal.core.itarget.ITarget;
30 import org.eclipse.pde.internal.core.itarget.ITargetModel;
31 import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
32 import org.eclipse.pde.internal.ui.PDEPlugin;
33 import org.eclipse.pde.internal.ui.PDEUIMessages;
34 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
35 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
36 import org.eclipse.pde.internal.ui.editor.TableSection;
37 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor;
38 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
39 import org.eclipse.pde.internal.ui.parts.TablePart;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.ui.actions.ActionFactory;
44 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
45 import org.eclipse.ui.forms.widgets.FormToolkit;
46 import org.eclipse.ui.forms.widgets.Section;
47
48 public class ImplicitDependenciesSection extends TableSection {
49     
50     private TableViewer fViewer;
51     private static final int ADD_INDEX = 0;
52     private static final int REMOVE_INDEX = 1;
53     private static final int REMOVE_ALL_INDEX = 2;
54
55     public ImplicitDependenciesSection(PDEFormPage page, Composite parent) {
56         super(page, parent, Section.DESCRIPTION, new String JavaDoc[] {PDEUIMessages.ImplicitDependenicesSection_Add , PDEUIMessages.ImplicitDependenicesSection_Remove, PDEUIMessages.ImplicitDependenicesSection_RemoveAll});
57     }
58
59     protected void createClient(Section section, FormToolkit toolkit) {
60         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
61         section.setText(PDEUIMessages.ImplicitDependenicesSection_Title);
62         section.setDescription(PDEUIMessages.TargetImplicitPluginsTab_desc);
63         Composite container = toolkit.createComposite(section);
64         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
65         container.setLayoutData(new GridData(GridData.FILL_BOTH));
66         createViewerPartControl(container, SWT.MULTI, 2, toolkit);
67         fViewer = getTablePart().getTableViewer();
68         fViewer.setContentProvider(new DefaultTableProvider() {
69             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
70                 return getImplicitPluginsInfo().getPlugins();
71             }
72         });
73         fViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
74         fViewer.setComparator(new ViewerComparator() {
75             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
76                 ITargetPlugin p1 = (ITargetPlugin) e1;
77                 ITargetPlugin p2 = (ITargetPlugin) e2;
78                 return super.compare(viewer, p1.getId(), p2.getId());
79             }
80         });
81         fViewer.setInput(getTarget());
82         fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
83             public void selectionChanged(SelectionChangedEvent event) {
84                 updateButtons();
85             }
86         });
87         
88         toolkit.paintBordersFor(container);
89         section.setClient(container);
90         GridData gd = new GridData(GridData.FILL_BOTH);
91         section.setLayoutData(gd);
92         updateButtons();
93         getModel().addModelChangedListener(this);
94     }
95
96     public boolean doGlobalAction(String JavaDoc actionId) {
97         if (actionId.equals(ActionFactory.DELETE.getId())) {
98             handleRemove();
99             return true;
100         }
101         if (actionId.equals(ActionFactory.CUT.getId())) {
102             // delete here and let the editor transfer
103
// the selection to the clipboard
104
handleRemove();
105             return false;
106         }
107         if (actionId.equals(ActionFactory.PASTE.getId())) {
108             doPaste();
109             return true;
110         }
111         return false;
112     }
113     
114     private void updateButtons() {
115         TablePart part = getTablePart();
116         boolean empty = fViewer.getSelection().isEmpty();
117         part.setButtonEnabled(1, !empty);
118         boolean hasElements = fViewer.getTable().getItemCount() > 0;
119         part.setButtonEnabled(2, hasElements);
120     }
121     
122     protected void buttonSelected(int index) {
123         switch (index) {
124         case ADD_INDEX:
125             handleAdd();
126             break;
127         case REMOVE_INDEX:
128             handleRemove();
129             break;
130         case REMOVE_ALL_INDEX:
131             handleRemoveAll();
132         }
133     }
134     
135     protected void handleAdd() {
136         ElementListSelectionDialog dialog = new ElementListSelectionDialog(
137                 PDEPlugin.getActiveWorkbenchShell(),
138                 PDEPlugin.getDefault().getLabelProvider());
139         
140         dialog.setElements(getValidBundles());
141         dialog.setTitle(PDEUIMessages.PluginSelectionDialog_title);
142         dialog.setMessage(PDEUIMessages.PluginSelectionDialog_message);
143         dialog.setMultipleSelection(true);
144         if (dialog.open() == Window.OK) {
145             Object JavaDoc[] models = dialog.getResult();
146             ArrayList JavaDoc pluginsToAdd = new ArrayList JavaDoc();
147             ITargetModel model = getModel();
148             for (int i = 0; i < models.length; i++) {
149                 BundleDescription desc = (BundleDescription) models[i];
150                 ITargetPlugin plugin = model.getFactory().createPlugin();
151                 plugin.setId(desc.getSymbolicName());
152                 plugin.setModel(model);
153                 pluginsToAdd.add(plugin);
154             }
155             getImplicitPluginsInfo().addPlugins((ITargetPlugin[])pluginsToAdd.toArray(new ITargetPlugin[pluginsToAdd.size()]));
156             updateButtons();
157         }
158     }
159     
160     protected Object JavaDoc[] getValidBundles() {
161         ITargetPlugin[] plugins = getImplicitPluginsInfo().getPlugins();
162         Set JavaDoc currentPlugins = new HashSet JavaDoc((4/3) * plugins.length + 1);
163         for (int i = 0; i < plugins.length; i++) {
164             currentPlugins.add(plugins[i].getId());
165         }
166         
167         IPluginModelBase[] models = PluginRegistry.getActiveModels(false);
168         Set JavaDoc result = new HashSet JavaDoc((4/3) * models.length + 1);
169         for (int i = 0; i < models.length; i++) {
170             BundleDescription desc = models[i].getBundleDescription();
171             if (desc != null) {
172                 if (!currentPlugins.contains(desc.getSymbolicName()))
173                     result.add(desc);
174             }
175         }
176         return result.toArray();
177     }
178     
179     protected void handleRemove() {
180         Object JavaDoc[] src = ((IStructuredSelection)fViewer.getSelection()).toArray();
181         ITargetPlugin[] plugins = new ITargetPlugin[src.length];
182         System.arraycopy(src, 0, plugins, 0, src.length);
183         getImplicitPluginsInfo().removePlugins(plugins);
184         updateButtons();
185     }
186     
187     protected void handleRemoveAll() {
188         IImplicitDependenciesInfo info =getImplicitPluginsInfo();
189         info.removePlugins(info.getPlugins());
190         updateButtons();
191     }
192     
193     private IImplicitDependenciesInfo getImplicitPluginsInfo() {
194         IImplicitDependenciesInfo info = getTarget().getImplicitPluginsInfo();
195         if (info == null) {
196             info = getModel().getFactory().createImplicitPluginInfo();
197             getTarget().setImplicitPluginsInfo(info);
198         }
199         return info;
200     }
201     
202     private ITarget getTarget() {
203         return getModel().getTarget();
204     }
205     
206     private ITargetModel getModel() {
207         return (ITargetModel)getPage().getPDEEditor().getAggregateModel();
208     }
209     
210     /* (non-Javadoc)
211      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
212      */

213     public void modelChanged(IModelChangedEvent e) {
214         if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
215             handleModelEventWorldChanged(e);
216             return;
217         }
218         if (e.getChangeType() == IModelChangedEvent.CHANGE &&
219                 e.getChangedProperty().equals(IImplicitDependenciesInfo.P_IMPLICIT_PLUGINS)) {
220             ITargetPlugin[] plugins = (ITargetPlugin[])e.getOldValue();
221             for (int i = 0; i < plugins.length; i++)
222                 fViewer.remove(plugins[i]);
223             plugins = (ITargetPlugin[])e.getNewValue();
224             for (int i = 0; i < plugins.length; i++)
225                 fViewer.add(plugins[i]);
226         }
227     }
228     
229     /**
230      * @param event
231      */

232     private void handleModelEventWorldChanged(IModelChangedEvent event) {
233         // Reload input
234
fViewer.setInput(getTarget());
235         // Perform the refresh
236
refresh();
237     }
238     
239     /* (non-Javadoc)
240      * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
241      */

242     public void refresh() {
243         fViewer.refresh();
244         updateButtons();
245         super.refresh();
246     }
247     
248     /* (non-Javadoc)
249      * @see org.eclipse.pde.internal.ui.editor.TableSection#handleDoubleClick(org.eclipse.jface.viewers.IStructuredSelection)
250      */

251     protected void handleDoubleClick(IStructuredSelection selection) {
252         handleOpen(selection);
253     }
254     
255     private void handleOpen(IStructuredSelection selection) {
256         Object JavaDoc object = selection.getFirstElement();
257         ManifestEditor.openPluginEditor(((ITargetPlugin)object).getId());
258     }
259     
260     protected boolean canPaste(Object JavaDoc target, Object JavaDoc[] objects) {
261         for (int i = 0; i < objects.length; i++) {
262             if (!(objects[i] instanceof ITargetPlugin))
263                 return false;
264         }
265         return true;
266     }
267     
268     protected void doPaste(Object JavaDoc target, Object JavaDoc[] objects) {
269         for (int i = 0; i < objects.length; i++) {
270             if ( objects[i] instanceof ITargetPlugin )
271                 getImplicitPluginsInfo().addPlugin((ITargetPlugin)objects[i]);
272         }
273     }
274     
275     protected void selectionChanged(IStructuredSelection selection) {
276         getPage().getPDEEditor().setSelection(selection);
277     }
278     
279     public void dispose() {
280         ITargetModel model = getModel();
281         if (model != null)
282             model.removeModelChangedListener(this);
283     }
284     
285     protected boolean createCount() { return true; }
286 }
287
Popular Tags