KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > LibraryVisibilitySection


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.plugin;
12 import java.util.Vector JavaDoc;
13
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jdt.core.IPackageFragment;
17 import org.eclipse.jdt.core.JavaCore;
18 import org.eclipse.jdt.ui.ISharedImages;
19 import org.eclipse.jdt.ui.JavaElementLabelProvider;
20 import org.eclipse.jdt.ui.JavaUI;
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.action.Separator;
24 import org.eclipse.jface.viewers.ILabelProvider;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredContentProvider;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.ITableLabelProvider;
29 import org.eclipse.jface.viewers.LabelProvider;
30 import org.eclipse.jface.viewers.TableViewer;
31 import org.eclipse.jface.viewers.ViewerComparator;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.pde.core.IModelChangedEvent;
34 import org.eclipse.pde.core.plugin.IPluginLibrary;
35 import org.eclipse.pde.core.plugin.IPluginModelBase;
36 import org.eclipse.pde.internal.ui.PDEUIMessages;
37 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
38 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
39 import org.eclipse.pde.internal.ui.editor.TableSection;
40 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
41 import org.eclipse.pde.internal.ui.parts.EditableTablePart;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.dnd.Clipboard;
44 import org.eclipse.swt.events.SelectionAdapter;
45 import org.eclipse.swt.events.SelectionEvent;
46 import org.eclipse.swt.graphics.Image;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.layout.GridLayout;
49 import org.eclipse.swt.widgets.Button;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.ui.actions.ActionFactory;
52 import org.eclipse.ui.forms.IFormPart;
53 import org.eclipse.ui.forms.IPartSelectionListener;
54 import org.eclipse.ui.forms.widgets.FormToolkit;
55 import org.eclipse.ui.forms.widgets.Section;
56
57 public class LibraryVisibilitySection extends TableSection
58         implements IPartSelectionListener {
59     
60     private static int ADD_INDEX = 0;
61     private static int REMOVE_INDEX = 1;
62     
63     private Button fFullExportButton;
64     private Button fSelectedExportButton;
65     private IPluginLibrary fCurrentLibrary;
66     private Composite fPackageExportContainer;
67     private TableViewer fPackageExportViewer;
68     private Action fAddAction;
69     private Action fRemoveAction;
70     
71     class TableContentProvider extends DefaultContentProvider
72             implements IStructuredContentProvider {
73         public Object JavaDoc[] getElements(Object JavaDoc parent) {
74             if (parent instanceof IPluginLibrary) {
75                 String JavaDoc[] filters = ((IPluginLibrary) parent).getContentFilters();
76                 return filters == null ? new Object JavaDoc[0] : filters;
77             }
78             return new Object JavaDoc[0];
79         }
80     }
81     class TableLabelProvider extends LabelProvider
82             implements ITableLabelProvider {
83         
84         public String JavaDoc getColumnText(Object JavaDoc obj, int index) {
85             return obj.toString();
86         }
87         public Image getColumnImage(Object JavaDoc obj, int index) {
88             return JavaUI.getSharedImages().getImage(
89                     ISharedImages.IMG_OBJS_PACKAGE);
90         }
91     }
92     
93     public LibraryVisibilitySection(PDEFormPage formPage, Composite parent) {
94         super(formPage, parent, Section.DESCRIPTION, new String JavaDoc[]{
95                 PDEUIMessages.ManifestEditor_ExportSection_add,
96                 PDEUIMessages.ManifestEditor_ExportSection_remove});
97         getSection().setText(PDEUIMessages.ManifestEditor_ExportSection_title);
98         getSection().setDescription(PDEUIMessages.ManifestEditor_ExportSection_desc);
99         fHandleDefaultButton = false;
100     }
101     
102     public void createClient(Section section, FormToolkit toolkit) {
103         Composite container = toolkit.createComposite(section);
104         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 1));
105         
106         String JavaDoc label = PDEUIMessages.ManifestEditor_ExportSection_fullExport;
107         fFullExportButton = toolkit.createButton(container, label, SWT.RADIO);
108         fFullExportButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
109         fFullExportButton.addSelectionListener(new SelectionAdapter() {
110             public void widgetSelected(SelectionEvent e) {
111                 try {
112                     if (fCurrentLibrary != null)
113                         fCurrentLibrary.setExported(fFullExportButton.getSelection());
114                     getTablePart().setButtonEnabled(0, !fFullExportButton.getSelection());
115                     getTablePart().setButtonEnabled(1, false);
116                 } catch (CoreException e1) {
117                 }
118             }
119         });
120         
121         label = PDEUIMessages.ManifestEditor_ExportSection_selectedExport;
122         fSelectedExportButton = toolkit.createButton(container, label, SWT.RADIO);
123         fSelectedExportButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
124
125         createPackageViewer(container, toolkit);
126         update(null);
127         makeActions();
128         
129         IPluginModelBase model = (IPluginModelBase) getPage().getModel();
130         model.addModelChangedListener(this);
131         
132         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
133         section.setLayoutData(new GridData(GridData.FILL_BOTH));
134         section.setClient(container);
135     }
136     
137     private void makeActions() {
138         fAddAction = new Action(PDEUIMessages.ManifestEditor_ExportSection_add) {
139             public void run() {
140                 handleAdd();
141             }
142         };
143         fAddAction.setEnabled(isEditable());
144         
145         fRemoveAction = new Action(PDEUIMessages.ManifestEditor_ExportSection_remove) {
146             public void run() {
147                 handleRemove();
148             }
149         };
150         fRemoveAction.setEnabled(isEditable());
151     }
152
153     private void createPackageViewer(Composite parent, FormToolkit toolkit) {
154         fPackageExportContainer = toolkit.createComposite(parent);
155         fPackageExportContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
156         GridLayout layout = new GridLayout();
157         layout.marginWidth = 0;
158         layout.marginHeight = 0;
159         layout.numColumns = 2;
160         fPackageExportContainer.setLayout(layout);
161         fPackageExportContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
162
163         EditableTablePart tablePart = getTablePart();
164         tablePart.setEditable(getPage().getModel().isEditable());
165         createViewerPartControl(fPackageExportContainer, SWT.FULL_SELECTION, 2, toolkit);
166         fPackageExportViewer = tablePart.getTableViewer();
167         fPackageExportViewer.setContentProvider(new TableContentProvider());
168         fPackageExportViewer.setLabelProvider(new TableLabelProvider());
169         fPackageExportViewer.setComparator(new ViewerComparator());
170         toolkit.paintBordersFor(parent);
171     }
172     
173     protected void selectionChanged(IStructuredSelection selection) {
174         Object JavaDoc item = selection.getFirstElement();
175         getTablePart().setButtonEnabled(1, item != null);
176     }
177     
178     protected void buttonSelected(int index) {
179         if (index == ADD_INDEX)
180             handleAdd();
181         else if (index == REMOVE_INDEX)
182             handleRemove();
183     }
184     
185     /* (non-Javadoc)
186      * @see org.eclipse.pde.internal.ui.editor.PDESection#doGlobalAction(java.lang.String)
187      */

188     public boolean doGlobalAction(String JavaDoc actionId) {
189         
190         if (!isEditable()) { return false; }
191         
192         if (actionId.equals(ActionFactory.DELETE.getId())) {
193             handleRemove();
194             return true;
195         }
196         return false;
197     }
198     
199     public void dispose() {
200         IPluginModelBase model = (IPluginModelBase) getPage().getModel();
201         if (model!=null)
202             model.removeModelChangedListener(this);
203         super.dispose();
204     }
205     
206     protected void fillContextMenu(IMenuManager manager) {
207         if (fSelectedExportButton.isEnabled() && fSelectedExportButton.getSelection()) {
208             manager.add(fAddAction);
209             manager.add(new Separator());
210         }
211         if (!fPackageExportViewer.getSelection().isEmpty()) {
212             manager.add(fRemoveAction);
213         }
214         // Copy, cut, and paste operations not supported for plug-ins that do
215
// not have a MANIFEST.MF
216
getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager, false);
217     }
218     
219     private void handleAdd() {
220         IPluginModelBase model = (IPluginModelBase) getPage().getModel();
221         IProject project = model.getUnderlyingResource().getProject();
222         try {
223             if (project.hasNature(JavaCore.NATURE_ID)) {
224                 String JavaDoc[] names;
225                 names = fCurrentLibrary.getContentFilters();
226                 Vector JavaDoc existing = new Vector JavaDoc();
227                 if (names != null) {
228                     for (int i = 0; i < names.length; i++) {
229                         existing.add(names[i]);
230                     }
231                 }
232                 ILabelProvider labelProvider = new JavaElementLabelProvider();
233                 PackageSelectionDialog dialog = new PackageSelectionDialog(
234                         fPackageExportViewer.getTable().getShell(),
235                         labelProvider, JavaCore.create(project), existing, true);
236                 if (dialog.open() == Window.OK) {
237                     Object JavaDoc[] elements = dialog.getResult();
238                     for (int i = 0; i < elements.length; i++) {
239                         IPackageFragment fragment = (IPackageFragment) elements[i];
240                         fCurrentLibrary.addContentFilter(fragment.getElementName());
241                     }
242                 }
243                 labelProvider.dispose();
244             }
245         } catch (CoreException e) {
246         }
247     }
248     
249     private void handleRemove() {
250         IStructuredSelection ssel = (IStructuredSelection) fPackageExportViewer.getSelection();
251         Object JavaDoc[] items = ssel.toArray();
252         try {
253             for (int i = 0; i < items.length; i++) {
254                 fCurrentLibrary.removeContentFilter(items[i].toString());
255             }
256         } catch (CoreException e) {
257         }
258     }
259     
260     public void modelChanged(IModelChangedEvent e) {
261         if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
262             if (fCurrentLibrary!=null)
263                 update(null);
264             markStale();
265             return;
266         }
267         refresh();
268     }
269
270     public void refresh() {
271         update(fCurrentLibrary);
272         super.refresh();
273     }
274
275     public void selectionChanged(IFormPart source, ISelection selection) {
276         if (selection == null || selection.isEmpty())
277             update(null);
278         IStructuredSelection ssel = (IStructuredSelection) selection;
279         boolean enable = ssel.size() == 1;
280         setEnabled(enable);
281         if (enable && ssel.getFirstElement() instanceof IPluginLibrary)
282             update((IPluginLibrary) ssel.getFirstElement());
283     }
284     
285     private void setEnabled(boolean b) {
286         fFullExportButton.setEnabled(b);
287         fSelectedExportButton.setEnabled(b);
288         fPackageExportViewer.getTable().setEnabled(b);
289         if (!b) {
290             getTablePart().setButtonEnabled(ADD_INDEX, false);
291             getTablePart().setButtonEnabled(REMOVE_INDEX, false);
292         }
293     }
294
295     private void update(IPluginLibrary library) {
296         fCurrentLibrary = library;
297         if (library == null) {
298             fFullExportButton.setEnabled(false);
299             fFullExportButton.setSelection(false);
300             fSelectedExportButton.setEnabled(false);
301             fSelectedExportButton.setSelection(false);
302             fPackageExportViewer.setInput(new Object JavaDoc[0]);
303             getTablePart().setButtonEnabled(ADD_INDEX, false);
304             getTablePart().setButtonEnabled(REMOVE_INDEX, false);
305         } else {
306             fFullExportButton.setEnabled(isEditable());
307             fSelectedExportButton.setEnabled(isEditable());
308             fFullExportButton.setSelection(library.isFullyExported());
309             fSelectedExportButton.setSelection(!library.isFullyExported());
310             fPackageExportViewer.setInput(library);
311             getTablePart().setButtonEnabled(REMOVE_INDEX, false);
312             getTablePart().setButtonEnabled(ADD_INDEX, isEditable() && fSelectedExportButton.getSelection());
313         }
314     }
315     
316     /* (non-Javadoc)
317      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(org.eclipse.swt.dnd.Clipboard)
318      */

319     public boolean canPaste(Clipboard clipboard) {
320         // Paste not supported for plug-ins that do not have a MANIFEST.MF
321
return false;
322     }
323     
324 }
325
Popular Tags