KickJava   Java API By Example, From Geeks To Geeks.

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


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.plugin;
12 import java.util.*;
13
14 import org.eclipse.core.resources.*;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jdt.core.*;
17 import org.eclipse.jdt.ui.*;
18 import org.eclipse.jface.action.*;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.osgi.util.*;
21 import org.eclipse.pde.core.*;
22 import org.eclipse.pde.core.plugin.*;
23 import org.eclipse.pde.internal.core.*;
24 import org.eclipse.pde.internal.core.ibundle.*;
25 import org.eclipse.pde.internal.ui.*;
26 import org.eclipse.pde.internal.ui.editor.*;
27 import org.eclipse.pde.internal.ui.editor.context.*;
28 import org.eclipse.pde.internal.ui.elements.*;
29 import org.eclipse.swt.*;
30 import org.eclipse.swt.events.*;
31 import org.eclipse.swt.graphics.*;
32 import org.eclipse.swt.layout.*;
33 import org.eclipse.swt.widgets.*;
34 import org.eclipse.ui.actions.*;
35 import org.eclipse.ui.forms.events.*;
36 import org.eclipse.ui.forms.widgets.*;
37 import org.eclipse.ui.part.PageBook;
38 import org.osgi.framework.*;
39
40 public class PluginActivationSection extends TableSection
41         implements
42             IModelChangedListener, IInputContextListener {
43     private PageBook topBook;
44     private Composite topContainer;
45     private Composite blankContainer;
46     private TableViewer fExceptionsTableViewer;
47     private Button fDoActivateButton;
48     private Button fDoNotActivateButton;
49     private Font fBoldFont;
50     private static final String JavaDoc ECLIPSE_AUTOSTART = PDEPlugin.getResourceString("PluginActivationSection.autostart"); //$NON-NLS-1$
51

52     class TableContentProvider extends DefaultContentProvider
53             implements
54                 IStructuredContentProvider {
55         public Object JavaDoc[] getElements(Object JavaDoc parent) {
56             return getExceptions();
57         }
58     }
59     class TableLabelProvider extends LabelProvider
60             implements
61                 ITableLabelProvider {
62         public String JavaDoc getColumnText(Object JavaDoc obj, int index) {
63             return obj.toString();
64         }
65         public Image getColumnImage(Object JavaDoc obj, int index) {
66             return JavaUI.getSharedImages().getImage(
67                     ISharedImages.IMG_OBJS_PACKAGE);
68         }
69     }
70     public PluginActivationSection(PDEFormPage page, Composite parent) {
71         super(
72                 page,
73                 parent,
74                 Section.DESCRIPTION,
75                 new String JavaDoc[]{
76                         PDEPlugin
77                                 .getResourceString("ManifestEditor.OSGiSection.add"), //$NON-NLS-1$
78
PDEPlugin
79                                 .getResourceString("ManifestEditor.OSGiSection.remove")}); //$NON-NLS-1$
80
getSection().setText(PDEPlugin.getResourceString("PluginActivationSection.title")); //$NON-NLS-1$
81
getSection()
82                 .setDescription(
83                         PDEPlugin.getResourceString("PluginActivationSection.desc")); //$NON-NLS-1$
84
}
85     private void update() {
86         fDoActivateButton.setEnabled(isEditable());
87         fDoActivateButton.setSelection(isAutoStart());
88         fDoNotActivateButton.setSelection(!isAutoStart());
89         fDoNotActivateButton.setEnabled(isEditable());
90         enableButtons();
91     }
92
93     private boolean isAutoStart() {
94         ManifestElement element = getManifestElement();
95         return (element == null) ? true : !"false".equals(element.getValue()); //$NON-NLS-1$
96
}
97
98     private String JavaDoc[] getExceptions() {
99         ManifestElement element = getManifestElement();
100         if (element == null)
101             return new String JavaDoc[0];
102
103         String JavaDoc exceptions = element.getAttribute("exceptions"); //$NON-NLS-1$
104
if (exceptions == null)
105             return new String JavaDoc[0];
106
107         ArrayList tokens = new ArrayList();
108         StringTokenizer tok = new StringTokenizer(exceptions, ","); //$NON-NLS-1$
109
while (tok.hasMoreTokens())
110             tokens.add(tok.nextToken().trim());
111         return (String JavaDoc[]) tokens.toArray(new String JavaDoc[tokens.size()]);
112     }
113
114     private ManifestElement getManifestElement() {
115         IBundleModel model = getBundleModel();
116         if (model != null) {
117             String JavaDoc value = model.getBundle().getHeader(ECLIPSE_AUTOSTART);
118             if (value != null) {
119                 try {
120                     ManifestElement[] elements = ManifestElement.parseHeader(
121                             ECLIPSE_AUTOSTART, value);
122                     if (elements != null && elements.length > 0)
123                         return elements[0];
124                 } catch (BundleException e) {
125                 }
126             }
127         }
128         return null;
129     }
130     /*
131      * (non-Javadoc)
132      *
133      * @see org.eclipse.pde.internal.ui.neweditor.PDESection#isEditable()
134      */

135     public boolean isEditable() {
136         return getPage().getModel().isEditable()
137                 && getBundleModel() != null;
138     }
139     /*
140      * (non-Javadoc)
141      *
142      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
143      */

144     public void dispose() {
145         InputContextManager contextManager = getPage().getPDEEditor().getContextManager();
146         if (contextManager!=null)
147             contextManager.removeInputContextListener(this);
148         IBundleModel model = getBundleModel();
149         if (model != null)
150             model.removeModelChangedListener(this);
151         fBoldFont.dispose();
152         super.dispose();
153     }
154
155     public boolean doGlobalAction(String JavaDoc actionId) {
156         if (actionId.equals(ActionFactory.DELETE.getId())) {
157             handleRemove();
158             return true;
159         }
160         return false;
161     }
162
163     protected void fillContextMenu(IMenuManager manager) {
164         getPage().getPDEEditor().getContributor().contextMenuAboutToShow(
165                 manager);
166     }
167
168     /*
169      * (non-Javadoc)
170      *
171      * @see org.eclipse.pde.core.IModelChangedListener#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
172      */

173     public void modelChanged(IModelChangedEvent event) {
174         if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
175             markStale();
176             return;
177         }
178         if (event.getChangedProperty().equals(ECLIPSE_AUTOSTART)) {
179             refresh();
180         }
181     }
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
187      */

188     public void refresh() {
189         fExceptionsTableViewer.refresh();
190         fDoActivateButton.setSelection(isAutoStart());
191         fDoNotActivateButton.setSelection(!isAutoStart());
192         super.refresh();
193     }
194
195     private void initializeFonts() {
196         FontData[] fontData = getSection().getFont().getFontData();
197         FontData data;
198         if (fontData.length > 0)
199             data = fontData[0];
200         else
201             data = new FontData();
202         data.setStyle(SWT.BOLD);
203         fBoldFont = new Font(getSection().getDisplay(), fontData);
204     }
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.eclipse.pde.internal.ui.neweditor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section,
209      * org.eclipse.ui.forms.widgets.FormToolkit)
210      */

211     protected void createClient(Section section, FormToolkit toolkit) {
212         initializeFonts();
213
214         Composite mainContainer = toolkit.createComposite(section);
215         GridLayout layout = new GridLayout();
216         layout.marginHeight = layout.marginWidth = 0;
217         layout.verticalSpacing = 5;
218         mainContainer.setLayout(layout);
219         mainContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
220
221         /*
222          * create new manifest part
223          */

224         if (getPage().getPDEEditor().getAggregateModel().isEditable()) {
225             topBook = new PageBook(mainContainer, SWT.NULL);
226             //create a blank container that will be used
227
// to hide the text and the link when not needed
228
blankContainer = toolkit.createComposite(topBook);
229             blankContainer.setLayout(new GridLayout());
230             Label label = toolkit.createLabel(blankContainer, null);
231             GridData gd = new GridData();
232             gd.heightHint = 1;
233             gd.widthHint = 1;
234             label.setLayoutData(gd);
235
236             topContainer = toolkit.createComposite(topBook);
237             layout = new GridLayout();
238             layout.marginHeight = layout.marginWidth = 2;
239             layout.numColumns = 2;
240             layout.makeColumnsEqualWidth = false;
241             topContainer.setLayout(layout);
242             toolkit
243                     .createLabel(
244                             topContainer,
245                             PDEPlugin.getResourceString("PluginActivationSection.manifestRequired")); //$NON-NLS-1$
246
Hyperlink manifestLink = toolkit.createHyperlink(topContainer,
247                     PDEPlugin.getResourceString("PluginActivationSection.createManifest"), SWT.NULL); //$NON-NLS-1$
248
manifestLink.addHyperlinkListener(new IHyperlinkListener() {
249                 public void linkActivated(HyperlinkEvent e) {
250                     try {
251                         getPage().getEditor().doSave(null);
252                         IPluginModelBase model = (IPluginModelBase) getPage()
253                                 .getPDEEditor().getAggregateModel();
254                         PDEPluginConverter.convertToOSGIFormat(model
255                                 .getUnderlyingResource().getProject(), model
256                                 .isFragmentModel()
257                                 ? "fragment.xml" //$NON-NLS-1$
258
: "plugin.xml", new NullProgressMonitor()); //$NON-NLS-1$
259
} catch (CoreException e1) {
260                     }
261                 }
262                 public void linkExited(HyperlinkEvent e) {
263                 }
264                 public void linkEntered(HyperlinkEvent e) {
265                 }
266             });
267             bundleModeChanged(getContextId().equals(BundleInputContext.CONTEXT_ID));
268         }
269
270         /*
271          * Bottom parts (Activation Rule & Exceptions)
272          */

273         Composite bottomContainer = toolkit.createComposite(mainContainer);
274         layout = new GridLayout();
275         layout.makeColumnsEqualWidth = true;
276         layout.marginHeight = layout.marginWidth = 0;
277         layout.numColumns = 2;
278         bottomContainer.setLayout(layout);
279         bottomContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
280         /*
281          * Activation rule part
282          */

283         Composite ruleContainer = toolkit.createComposite(bottomContainer);
284         layout = new GridLayout();
285         layout.marginHeight = layout.marginWidth = 2;
286         ruleContainer.setLayout(layout);
287         ruleContainer.setLayoutData(new GridData(
288                 GridData.VERTICAL_ALIGN_BEGINNING));
289
290         Label activateLabel = toolkit.createLabel(ruleContainer,
291                 PDEPlugin.getResourceString("PluginActivationSection.rule")); //$NON-NLS-1$
292
activateLabel.setFont(fBoldFont);
293
294         fDoActivateButton = toolkit.createButton(ruleContainer,
295                 PDEPlugin.getResourceString("PluginActivationSection.activate"), SWT.RADIO); //$NON-NLS-1$
296

297         GridData gd = new GridData();
298         gd.horizontalIndent = 5;
299         fDoActivateButton.setLayoutData(gd);
300         fDoActivateButton.addSelectionListener(new SelectionAdapter() {
301             public void widgetSelected(SelectionEvent e) {
302                 writeHeader();
303             }
304         });
305         /*
306          * auto-activate should be set to true by default with empty exceptions
307          * package list
308          */

309         fDoNotActivateButton = toolkit.createButton(ruleContainer,
310                 PDEPlugin.getResourceString("PluginActivationSection.noActivate"), SWT.RADIO); //$NON-NLS-1$
311
gd = new GridData();
312         gd.horizontalIndent = 5;
313         fDoNotActivateButton.setLayoutData(gd);
314         fDoNotActivateButton.addSelectionListener(new SelectionAdapter() {
315             public void widgetSelected(SelectionEvent e) {
316                 writeHeader();
317             }
318         });
319         /*
320          * Exceptions part
321          */

322         Composite exceptionsContainer = toolkit
323                 .createComposite(bottomContainer);
324         layout = new GridLayout();
325         layout.marginWidth = layout.marginHeight = 2;
326         layout.verticalSpacing = 3;
327         exceptionsContainer.setLayout(layout);
328         exceptionsContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
329
330         Label exceptionLabel = toolkit.createLabel(exceptionsContainer,
331                 PDEPlugin.getResourceString("PluginActivationSection.exception.title")); //$NON-NLS-1$
332
exceptionLabel.setFont(fBoldFont);
333         exceptionLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
334         Label label = toolkit
335                 .createLabel(
336                         exceptionsContainer,
337                         PDEPlugin.getResourceString("PluginActivationSection.exception.desc"), //$NON-NLS-1$
338
SWT.WRAP);
339         gd = new GridData(GridData.FILL_HORIZONTAL);
340         gd.widthHint = 225;
341         label.setLayoutData(gd);
342
343         Composite exceptionsPkgContainer = toolkit
344                 .createComposite(exceptionsContainer);
345         layout = new GridLayout();
346         layout.marginWidth = layout.marginHeight = 0;
347         layout.numColumns = 2;
348         exceptionsPkgContainer.setLayout(layout);
349         exceptionsPkgContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
350
351         createViewerPartControl(exceptionsPkgContainer, SWT.FULL_SELECTION, 2,
352                 toolkit);
353         fExceptionsTableViewer = getTablePart().getTableViewer();
354         fExceptionsTableViewer.setContentProvider(new TableContentProvider());
355         fExceptionsTableViewer.setLabelProvider(new TableLabelProvider());
356         fExceptionsTableViewer.setInput(getBundleModel());
357
358         toolkit.paintBordersFor(exceptionsContainer);
359         section.setClient(mainContainer);
360         IBundleModel model = getBundleModel();
361         if (model != null)
362             model.addModelChangedListener(this);
363         InputContextManager contextManager = getPage().getPDEEditor().getContextManager();
364         if (contextManager!=null)
365             contextManager.addInputContextListener(this);
366         update();
367     }
368     protected void enableButtons() {
369         getTablePart().setButtonEnabled(0, isEditable());
370         getTablePart().setButtonEnabled(1, false);
371     }
372     protected void buttonSelected(int index) {
373         if (index == 0)
374             handleAdd();
375         else if (index == 1)
376             handleRemove();
377     }
378     private void handleAdd() {
379         IPluginModelBase model = (IPluginModelBase) getPage().getModel();
380         IProject project = model.getUnderlyingResource().getProject();
381         try {
382             if (project.hasNature(JavaCore.NATURE_ID)) {
383                 TableItem[] existingPackages = fExceptionsTableViewer
384                         .getTable().getItems();
385                 Vector existing = new Vector();
386                 for (int i = 0; i < existingPackages.length; i++) {
387                     existing.add(existingPackages[i].getText());
388                 }
389                 ILabelProvider labelProvider = new JavaElementLabelProvider();
390                 PackageSelectionDialog dialog = new PackageSelectionDialog(
391                         fExceptionsTableViewer.getTable().getShell(),
392                         labelProvider, JavaCore.create(project), existing);
393                 if (dialog.open() == PackageSelectionDialog.OK) {
394                     Object JavaDoc[] elements = dialog.getResult();
395                     for (int i = 0; i < elements.length; i++) {
396                         fExceptionsTableViewer
397                                 .add(((IPackageFragment) elements[i])
398                                         .getElementName());
399                     }
400                     writeHeader();
401                 }
402                 labelProvider.dispose();
403             }
404         } catch (CoreException e) {
405         }
406     }
407
408     private void writeHeader() {
409         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
410         buffer.append(fDoActivateButton.getSelection() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
411
TableItem[] items = fExceptionsTableViewer.getTable().getItems();
412         if (items.length > 0)
413             buffer.append(";exceptions=\""); //$NON-NLS-1$
414
for (int i = 0; i < items.length; i++) {
415             if (i > 0)
416                 buffer.append(" "); //$NON-NLS-1$
417
buffer.append(items[i].getData().toString());
418             if (i < items.length - 1)
419                 buffer.append("," + System.getProperty("line.separator")); //$NON-NLS-1$ //$NON-NLS-2$
420
}
421         if (items.length > 0)
422             buffer.append("\""); //$NON-NLS-1$
423
getBundleModel().getBundle().setHeader(ECLIPSE_AUTOSTART,
424                 buffer.toString());
425     }
426
427     private void handleRemove() {
428         IStructuredSelection ssel = (IStructuredSelection) fExceptionsTableViewer
429                 .getSelection();
430         Object JavaDoc[] items = ssel.toArray();
431         for (int i = 0; i < items.length; i++) {
432             fExceptionsTableViewer.remove(items[i]);
433         }
434         writeHeader();
435     }
436     /*
437      * (non-Javadoc)
438      *
439      * @see org.eclipse.pde.internal.ui.neweditor.TableSection#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
440      */

441     protected void selectionChanged(IStructuredSelection selection) {
442         getTablePart().setButtonEnabled(1,
443                 selection != null && !selection.isEmpty());
444     }
445
446     public String JavaDoc getContextId() {
447         if (getPluginBase() instanceof IBundlePluginBase)
448             return BundleInputContext.CONTEXT_ID;
449         return PluginInputContext.CONTEXT_ID;
450     }
451     private IPluginBase getPluginBase() {
452         IBaseModel model = getPage().getPDEEditor().getAggregateModel();
453         return ((IPluginModelBase) model).getPluginBase();
454     }
455
456     private IBundleModel getBundleModel() {
457         InputContext context = getPage().getPDEEditor().getContextManager()
458                 .findContext(BundleInputContext.CONTEXT_ID);
459         return context != null ? (IBundleModel) context.getModel() : null;
460     }
461
462     /* (non-Javadoc)
463      * @see org.eclipse.pde.internal.ui.editor.context.IInputContextListener#contextAdded(org.eclipse.pde.internal.ui.editor.context.InputContext)
464      */

465     public void contextAdded(InputContext context) {
466         if (!context.getId().equals(BundleInputContext.CONTEXT_ID))
467             return;
468         // bundle added - remove the text for manifest creation
469
// and enable controls
470
bundleModeChanged(true);
471     }
472     /* (non-Javadoc)
473      * @see org.eclipse.pde.internal.ui.editor.context.IInputContextListener#contextRemoved(org.eclipse.pde.internal.ui.editor.context.InputContext)
474      */

475     public void contextRemoved(InputContext context) {
476         if (!context.getId().equals(BundleInputContext.CONTEXT_ID))
477             return;
478         // bundle removed - add the text for manifest creation
479
// and disable controls
480
bundleModeChanged(false);
481     }
482     private void bundleModeChanged(boolean added) {
483         if (added && getPage().getModel().isEditable()) {
484             topBook.showPage(blankContainer);
485         }
486         else {
487             topBook.showPage(topContainer);
488         }
489         if (fDoActivateButton!=null) {
490             update();
491             topBook.getParent().layout();
492             getManagedForm().reflow(true);
493             fExceptionsTableViewer.setInput(getBundleModel());
494         }
495     }
496     /* (non-Javadoc)
497      * @see org.eclipse.pde.internal.ui.editor.context.IInputContextListener#monitoredFileAdded(org.eclipse.core.resources.IFile)
498      */

499     public void monitoredFileAdded(IFile monitoredFile) {
500     }
501     /* (non-Javadoc)
502      * @see org.eclipse.pde.internal.ui.editor.context.IInputContextListener#monitoredFileRemoved(org.eclipse.core.resources.IFile)
503      */

504     public boolean monitoredFileRemoved(IFile monitoredFile) {
505         return false;
506     }
507 }
508
Popular Tags