KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > views > plugins > PluginsView


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.views.plugins;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.resources.IStorage;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.jdt.core.IClassFile;
26 import org.eclipse.jdt.core.IJarEntryResource;
27 import org.eclipse.jdt.core.IPackageFragment;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.ui.JavaUI;
30 import org.eclipse.jdt.ui.actions.OpenAction;
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.jface.action.IMenuListener;
33 import org.eclipse.jface.action.IMenuManager;
34 import org.eclipse.jface.action.IToolBarManager;
35 import org.eclipse.jface.action.MenuManager;
36 import org.eclipse.jface.action.Separator;
37 import org.eclipse.jface.dialogs.IDialogSettings;
38 import org.eclipse.jface.resource.ImageDescriptor;
39 import org.eclipse.jface.util.IPropertyChangeListener;
40 import org.eclipse.jface.util.PropertyChangeEvent;
41 import org.eclipse.jface.viewers.DoubleClickEvent;
42 import org.eclipse.jface.viewers.IDoubleClickListener;
43 import org.eclipse.jface.viewers.ISelection;
44 import org.eclipse.jface.viewers.ISelectionChangedListener;
45 import org.eclipse.jface.viewers.IStructuredSelection;
46 import org.eclipse.jface.viewers.LabelProvider;
47 import org.eclipse.jface.viewers.SelectionChangedEvent;
48 import org.eclipse.jface.viewers.StructuredSelection;
49 import org.eclipse.jface.viewers.TreeViewer;
50 import org.eclipse.jface.viewers.Viewer;
51 import org.eclipse.jface.viewers.ViewerFilter;
52 import org.eclipse.osgi.util.NLS;
53 import org.eclipse.pde.core.plugin.IPluginModelBase;
54 import org.eclipse.pde.core.plugin.ModelEntry;
55 import org.eclipse.pde.core.plugin.PluginRegistry;
56 import org.eclipse.pde.internal.core.DependencyManager;
57 import org.eclipse.pde.internal.core.FileAdapter;
58 import org.eclipse.pde.internal.core.IPluginModelListener;
59 import org.eclipse.pde.internal.core.ModelFileAdapter;
60 import org.eclipse.pde.internal.core.PDECore;
61 import org.eclipse.pde.internal.core.PluginModelDelta;
62 import org.eclipse.pde.internal.core.PluginModelManager;
63 import org.eclipse.pde.internal.ui.IHelpContextIds;
64 import org.eclipse.pde.internal.ui.IPDEUIConstants;
65 import org.eclipse.pde.internal.ui.IPreferenceConstants;
66 import org.eclipse.pde.internal.ui.PDEPlugin;
67 import org.eclipse.pde.internal.ui.PDEPluginImages;
68 import org.eclipse.pde.internal.ui.PDEUIMessages;
69 import org.eclipse.pde.internal.ui.editor.JarEntryEditorInput;
70 import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
71 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor;
72 import org.eclipse.pde.internal.ui.refactoring.RenamePluginAction;
73 import org.eclipse.pde.internal.ui.views.dependencies.OpenPluginDependenciesAction;
74 import org.eclipse.pde.internal.ui.views.dependencies.OpenPluginReferencesAction;
75 import org.eclipse.pde.internal.ui.wizards.ListUtil;
76 import org.eclipse.swt.SWT;
77 import org.eclipse.swt.custom.BusyIndicator;
78 import org.eclipse.swt.dnd.Clipboard;
79 import org.eclipse.swt.dnd.DND;
80 import org.eclipse.swt.dnd.FileTransfer;
81 import org.eclipse.swt.dnd.Transfer;
82 import org.eclipse.swt.program.Program;
83 import org.eclipse.swt.widgets.Composite;
84 import org.eclipse.swt.widgets.Display;
85 import org.eclipse.swt.widgets.Menu;
86 import org.eclipse.ui.IActionBars;
87 import org.eclipse.ui.IEditorInput;
88 import org.eclipse.ui.IPageLayout;
89 import org.eclipse.ui.ISharedImages;
90 import org.eclipse.ui.IWorkbenchActionConstants;
91 import org.eclipse.ui.IWorkbenchPage;
92 import org.eclipse.ui.PartInitException;
93 import org.eclipse.ui.PlatformUI;
94 import org.eclipse.ui.actions.ActionContext;
95 import org.eclipse.ui.actions.ActionFactory;
96 import org.eclipse.ui.actions.ContributionItemFactory;
97 import org.eclipse.ui.keys.IBindingService;
98 import org.eclipse.ui.part.DrillDownAdapter;
99 import org.eclipse.ui.part.IShowInSource;
100 import org.eclipse.ui.part.IShowInTargetList;
101 import org.eclipse.ui.part.ShowInContext;
102 import org.eclipse.ui.part.ViewPart;
103
104 public class PluginsView extends ViewPart implements IPluginModelListener{
105     
106     private static final String JavaDoc DEFAULT_EDITOR_ID = "org.eclipse.ui.DefaultTextEditor"; //$NON-NLS-1$
107
private static final String JavaDoc HIDE_WRKSPC = "hideWorkspace"; //$NON-NLS-1$
108
private static final String JavaDoc HIDE_EXENABLED = "hideEnabledExternal"; //$NON-NLS-1$
109
private static final String JavaDoc SHOW_EXDISABLED = "showDisabledExternal"; //$NON-NLS-1$
110
private TreeViewer fTreeViewer;
111     private DrillDownAdapter fDrillDownAdapter;
112     private IPropertyChangeListener fPropertyListener;
113     private Action fOpenAction;
114     private Action fHideExtDisabledFilterAction;
115     private Action fHideExtEnabledFilterAction;
116     private Action fHideWorkspaceFilterAction;
117     private Action fOpenManifestAction;
118     private Action fOpenSchemaAction;
119     private Action fOpenSystemEditorAction;
120     private Action fOpenClassFileAction;
121     private Action fOpenTextEditorAction;
122     private Action fSelectDependentAction;
123     private Action fSelectInJavaSearchAction;
124     private Action fSelectAllAction;
125     private RenamePluginAction fRefactorAction;
126     private CollapseAllAction fCollapseAllAction;
127     private DisabledFilter fHideExtEnabledFilter = new DisabledFilter(true);
128     private DisabledFilter fHideExtDisabledFilter = new DisabledFilter(false);
129     private WorkspaceFilter fHideWorkspaceFilter = new WorkspaceFilter();
130     private JavaFilter fJavaFilter = new JavaFilter();
131     private CopyToClipboardAction fCopyAction;
132     private Clipboard fClipboard;
133     private PluginModelManager fManager;
134
135     class DisabledFilter extends ViewerFilter {
136         
137         boolean fEnabled;
138         DisabledFilter(boolean enabled) {
139             fEnabled = enabled;
140         }
141         public boolean select(Viewer v, Object JavaDoc parent, Object JavaDoc element) {
142             if (element instanceof IPluginModelBase) {
143                 IPluginModelBase model = (IPluginModelBase) element;
144                 return model.getUnderlyingResource() != null || model.isEnabled() != fEnabled;
145             }
146             return true;
147         }
148     }
149
150     class WorkspaceFilter extends ViewerFilter {
151         public boolean select(Viewer v, Object JavaDoc parent, Object JavaDoc element) {
152             if (element instanceof IPluginModelBase) {
153                 IPluginModelBase model = (IPluginModelBase) element;
154                 return model.getUnderlyingResource() == null;
155             }
156             return true;
157         }
158     }
159
160     class JavaFilter extends ViewerFilter {
161         public boolean select(Viewer v, Object JavaDoc parent, Object JavaDoc element) {
162             if (element instanceof IPackageFragment) {
163                 IPackageFragment packageFragment = (IPackageFragment) element;
164                 try {
165                     return packageFragment.hasChildren();
166                 } catch (JavaModelException e) {
167                     return false;
168                 }
169             }
170             return true;
171         }
172     }
173
174     class CollapseAllAction extends Action {
175         public CollapseAllAction() {
176             setText(PDEUIMessages.PluginsView_CollapseAllAction_label);
177             setDescription(PDEUIMessages.PluginsView_CollapseAllAction_description);
178             setToolTipText(PDEUIMessages.PluginsView_CollapseAllAction_tooltip);
179             setImageDescriptor(PDEPluginImages.DESC_COLLAPSE_ALL);
180         }
181
182         /*
183          * (non-Javadoc)
184          *
185          * @see org.eclipse.jface.action.Action#run()
186          */

187         public void run() {
188             fTreeViewer.collapseAll();
189         }
190     }
191     
192     /**
193      * Constructor for PluginsView.
194      */

195     public PluginsView() {
196         fPropertyListener = new IPropertyChangeListener() {
197             public void propertyChange(PropertyChangeEvent event) {
198                 String JavaDoc property = event.getProperty();
199                 if (property.equals(IPreferenceConstants.PROP_SHOW_OBJECTS)) {
200                     fTreeViewer.refresh();
201                 }
202             }
203         };
204     }
205
206     public void dispose() {
207         fManager.removePluginModelListener(this);
208         PDECore.getDefault().getSearchablePluginsManager().removePluginModelListener(this);
209         PDEPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(
210                 fPropertyListener);
211         if (fClipboard != null) {
212             fClipboard.dispose();
213             fClipboard = null;
214         }
215         super.dispose();
216     }
217
218     /*
219      * (non-Javadoc)
220      * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
221      */

222     public void createPartControl(Composite parent) {
223         fTreeViewer = new TreeViewer(parent, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
224         fDrillDownAdapter = new DrillDownAdapter(fTreeViewer);
225         fTreeViewer.setContentProvider(new PluginsContentProvider(this));
226         fTreeViewer.setLabelProvider(new PluginsLabelProvider());
227         fTreeViewer.setComparator(ListUtil.PLUGIN_COMPARATOR);
228         initDragAndDrop();
229         makeActions();
230         initFilters();
231         IActionBars actionBars = getViewSite().getActionBars();
232         contributeToActionBars(actionBars);
233         registerGlobalActions(actionBars);
234         hookContextMenu();
235         hookDoubleClickAction();
236         fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
237             public void selectionChanged(SelectionChangedEvent e) {
238                 handleSelectionChanged(e.getSelection());
239             }
240         });
241         PDECore.getDefault().getSearchablePluginsManager().addPluginModelListener(this);
242         fManager = PDECore.getDefault().getModelManager();
243         fTreeViewer.setInput(fManager);
244         fManager.addPluginModelListener(this);
245         updateContentDescription();
246         PDEPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(
247             fPropertyListener);
248         getViewSite().setSelectionProvider(fTreeViewer);
249
250         PlatformUI.getWorkbench().getHelpSystem().setHelp(
251             fTreeViewer.getControl(),
252             IHelpContextIds.PLUGINS_VIEW);
253     }
254
255     private void contributeToActionBars(IActionBars actionBars) {
256         contributeToLocalToolBar(actionBars.getToolBarManager());
257         contributeToDropDownMenu(actionBars.getMenuManager());
258     }
259
260     private void contributeToDropDownMenu(IMenuManager manager) {
261         manager.add(fHideWorkspaceFilterAction);
262         manager.add(fHideExtEnabledFilterAction);
263         manager.add(fHideExtDisabledFilterAction);
264     }
265
266     private void contributeToLocalToolBar(IToolBarManager manager) {
267         fDrillDownAdapter.addNavigationActions(manager);
268         manager.add(new Separator());
269         manager.add(fCollapseAllAction);
270     }
271     
272     private void registerGlobalActions(IActionBars actionBars) {
273         actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
274             fSelectAllAction);
275         actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(),
276             new Action() {
277                 public void run() {
278                     IStructuredSelection selection = (IStructuredSelection)fTreeViewer.getSelection();
279                     if (selection.size() == 1) {
280                         Object JavaDoc element = selection.getFirstElement();
281                         if (element instanceof IPluginModelBase) {
282                             fRefactorAction.setPlugin((IPluginModelBase)element);
283                             fRefactorAction.run();
284                             return;
285                         }
286                     }
287                     Display.getDefault().beep();
288                 }
289         });
290     }
291     private void makeActions() {
292         fClipboard = new Clipboard(fTreeViewer.getTree().getDisplay());
293         fOpenAction = new Action() {
294             public void run() {
295                 handleDoubleClick();
296             }
297         };
298         fOpenAction.setText(PDEUIMessages.PluginsView_open);
299         
300         fHideExtDisabledFilterAction = new Action() {
301             public void run() {
302                 boolean checked = fHideExtDisabledFilterAction.isChecked();
303                 if (checked)
304                     fTreeViewer.removeFilter(fHideExtDisabledFilter);
305                 else
306                     fTreeViewer.addFilter(fHideExtDisabledFilter);
307                 getSettings().put(SHOW_EXDISABLED, checked);
308                 updateContentDescription();
309             }
310         };
311         fHideExtDisabledFilterAction.setText(PDEUIMessages.PluginsView_showDisabled);
312         
313         fHideExtEnabledFilterAction = new Action() {
314             public void run() {
315                 boolean checked = fHideExtEnabledFilterAction.isChecked();
316                 if (checked)
317                     fTreeViewer.removeFilter(fHideExtEnabledFilter);
318                 else
319                     fTreeViewer.addFilter(fHideExtEnabledFilter);
320                 getSettings().put(HIDE_EXENABLED, !checked);
321                 updateContentDescription();
322             }
323         };
324         fHideExtEnabledFilterAction.setText(PDEUIMessages.PluginsView_showEnabled);
325         
326         fHideWorkspaceFilterAction = new Action() {
327             public void run() {
328                 boolean checked = fHideWorkspaceFilterAction.isChecked();
329                 if (checked)
330                     fTreeViewer.removeFilter(fHideWorkspaceFilter);
331                 else
332                     fTreeViewer.addFilter(fHideWorkspaceFilter);
333                 getSettings().put(HIDE_WRKSPC, !checked);
334                 updateContentDescription();
335             }
336         };
337         fHideWorkspaceFilterAction.setText(PDEUIMessages.PluginsView_showWorkspace);
338
339         fOpenTextEditorAction = new Action() {
340             public void run() {
341                 handleOpenTextEditor(getSelectedFile(), null);
342             }
343         };
344         fOpenTextEditorAction.setText(PDEUIMessages.PluginsView_textEditor);
345         fOpenTextEditorAction.setImageDescriptor(
346             PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
347                 ISharedImages.IMG_OBJ_FILE));
348
349         fOpenSystemEditorAction = new Action() {
350             public void run() {
351                 handleOpenSystemEditor(getSelectedFile());
352             }
353         };
354         fOpenSystemEditorAction.setText(PDEUIMessages.PluginsView_systemEditor);
355         
356         fOpenManifestAction = new Action() {
357             public void run() {
358                 handleOpenManifestEditor(getSelectedFile());
359             }
360         };
361         fOpenManifestAction.setText(PDEUIMessages.PluginsView_manifestEditor);
362         
363         fOpenSchemaAction = new Action() {
364             public void run() {
365                 handleOpenSchemaEditor(getSelectedFile());
366             }
367         };
368         fOpenSchemaAction.setText(PDEUIMessages.PluginsView_schemaEditor);
369
370         fCopyAction = new CopyToClipboardAction(fClipboard);
371         fCopyAction.setText(PDEUIMessages.PluginsView_copy);
372
373         fSelectDependentAction = new Action() {
374             public void run() {
375                 handleSelectDependencies();
376             }
377         };
378         fSelectDependentAction.setText(PDEUIMessages.PluginsView_dependentPlugins);
379         
380         fSelectInJavaSearchAction = new Action() {
381             public void run() {
382                 handleSelectInJavaSearch();
383             }
384         };
385         fSelectInJavaSearchAction.setText(PDEUIMessages.PluginsView_pluginsInJavaSearch);
386         
387         fSelectAllAction = new Action() {
388             public void run() {
389                 super.run();
390                 fTreeViewer.getTree().selectAll();
391             }
392         };
393         fSelectAllAction.setText(PDEUIMessages.PluginsView_SelectAllAction_label);
394         
395         fCollapseAllAction = new CollapseAllAction();
396
397         fOpenClassFileAction = new OpenAction(getViewSite());
398         
399         fRefactorAction = new RenamePluginAction();
400     }
401
402     private FileAdapter getSelectedFile() {
403         Object JavaDoc obj = getSelectedObject();
404         if (obj instanceof FileAdapter)
405             return (FileAdapter) obj;
406         return null;
407     }
408
409     private IPluginModelBase getEnclosingModel() {
410         Object JavaDoc obj = getSelectedObject();
411         if (obj == null)
412             return null;
413         if (obj instanceof IPluginModelBase)
414             return (IPluginModelBase) obj;
415         if (obj instanceof FileAdapter) {
416             FileAdapter file = (FileAdapter) obj;
417             if (file.isManifest()) {
418                 FileAdapter parent = file.getParent();
419                 if (parent instanceof ModelFileAdapter)
420                     return ((ModelFileAdapter) parent).getModel();
421             }
422         }
423         return null;
424     }
425
426     private Object JavaDoc getSelectedObject() {
427         IStructuredSelection selection =
428             (IStructuredSelection) fTreeViewer.getSelection();
429         if (selection.isEmpty() || selection.size() != 1)
430             return null;
431         return selection.getFirstElement();
432     }
433     private void fillContextMenu(IMenuManager manager) {
434         IStructuredSelection selection =
435             (IStructuredSelection) fTreeViewer.getSelection();
436
437         boolean allowRefactoring = false;
438         if (selection.size() == 1) {
439             Object JavaDoc sobj = selection.getFirstElement();
440             boolean addSeparator = false;
441             if (sobj instanceof IPluginModelBase) {
442                 IPluginModelBase model = (IPluginModelBase)sobj;
443                 File JavaDoc file = new File JavaDoc(model.getInstallLocation());
444                 if (file.isFile() || model.getUnderlyingResource() != null) {
445                     manager.add(fOpenAction);
446                 }
447                 if (model.getUnderlyingResource() != null)
448                     allowRefactoring = true;
449             }
450             if (sobj instanceof FileAdapter
451                 && ((FileAdapter) sobj).isDirectory() == false) {
452                 manager.add(fOpenAction);
453                 MenuManager openWithMenu = new MenuManager(PDEUIMessages.PluginsView_openWith);
454                 fillOpenWithMenu(openWithMenu, sobj);
455                 manager.add(openWithMenu);
456                 addSeparator = true;
457             }
458             if (isOpenableStorage(sobj)) {
459                 manager.add(fOpenAction);
460                 addSeparator = true;
461             }
462             if (sobj instanceof IClassFile) {
463                 manager.add(fOpenClassFileAction);
464                 addSeparator = true;
465             }
466             IPluginModelBase entry = getEnclosingModel();
467             if (entry != null) {
468                 Action action = new OpenPluginDependenciesAction(entry);
469                 action.setText(PDEUIMessages.PluginsView_openDependencies);
470                 action.setImageDescriptor(PDEPluginImages.DESC_CALLEES);
471                 manager.add(action);
472                 manager.add(new Separator());
473                 
474                 action = new OpenPluginReferencesAction(entry);
475                 action.setText(PDEUIMessages.SearchAction_references);
476                 action.setImageDescriptor(PDEPluginImages.DESC_CALLERS);
477                 manager.add(action);
478                 addSeparator = true;
479             }
480             if (addSeparator)
481                 manager.add(new Separator());
482         }
483         if (selection.size() > 0) {
484             if (isShowInApplicable()) {
485                 String JavaDoc showInLabel = PDEUIMessages.PluginsView_showIn;
486                 IBindingService bindingService = (IBindingService) PlatformUI
487                         .getWorkbench().getAdapter(IBindingService.class);
488                 if (bindingService != null) {
489                     String JavaDoc keyBinding = bindingService
490                             .getBestActiveBindingFormattedFor("org.eclipse.ui.navigate.showInQuickMenu"); //$NON-NLS-1$
491
if (keyBinding != null) {
492                         showInLabel += '\t' + keyBinding;
493                     }
494                 }
495                 IMenuManager showInMenu = new MenuManager(showInLabel);
496                 showInMenu.add(ContributionItemFactory.VIEWS_SHOW_IN
497                         .create(getViewSite().getWorkbenchWindow()));
498                         
499                 manager.add(showInMenu);
500                 manager.add(new Separator());
501             }
502             if (ImportActionGroup.canImport(selection)) {
503                 ImportActionGroup actionGroup = new ImportActionGroup();
504                 actionGroup.setContext(new ActionContext(selection));
505                 actionGroup.fillContextMenu(manager);
506                 manager.add(new Separator());
507             }
508             
509             JavaSearchActionGroup actionGroup =
510                 new JavaSearchActionGroup();
511             actionGroup.setContext(new ActionContext(selection));
512             actionGroup.fillContextMenu(manager);
513         }
514         fCopyAction.setSelection(selection);
515         manager.add(fCopyAction);
516         IMenuManager selectionMenu = new MenuManager(PDEUIMessages.PluginsView_select);
517         manager.add(selectionMenu);
518         if (selection.size() > 0)
519             selectionMenu.add(fSelectDependentAction);
520         selectionMenu.add(fSelectInJavaSearchAction);
521         selectionMenu.add(fSelectAllAction);
522         manager.add(new Separator());
523         if (allowRefactoring) {
524             fRefactorAction.setPlugin((IPluginModelBase)selection.getFirstElement());
525             manager.add(fRefactorAction);
526             manager.add(new Separator());
527         }
528         fDrillDownAdapter.addNavigationActions(manager);
529         manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
530     }
531     
532     public boolean isShowInApplicable() {
533         IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection();
534         if (selection.isEmpty())
535             return false;
536         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
537             Object JavaDoc obj = iter.next();
538             if (!(obj instanceof IPluginModelBase))
539                 return false;
540             if (((IPluginModelBase)obj).getUnderlyingResource() == null)
541                 return false;
542         }
543         return true;
544     }
545     
546     private void fillOpenWithMenu(IMenuManager manager, Object JavaDoc obj) {
547         FileAdapter adapter = (FileAdapter) obj;
548         String JavaDoc editorId = adapter.getEditorId();
549
550         String JavaDoc fileName = adapter.getFile().getName();
551         String JavaDoc lcFileName = fileName.toLowerCase(Locale.ENGLISH);
552         ImageDescriptor desc =
553             PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor(
554                 fileName);
555         if (lcFileName.equals("plugin.xml") //$NON-NLS-1$
556
|| lcFileName.equals("fragment.xml") //$NON-NLS-1$
557
|| lcFileName.equals("manifest.mf")) { //$NON-NLS-1$
558
fOpenManifestAction.setImageDescriptor(desc);
559             manager.add(fOpenManifestAction);
560             manager.add(new Separator());
561             fOpenManifestAction.setChecked(
562                 editorId != null
563                     && editorId.equals(IPDEUIConstants.MANIFEST_EDITOR_ID));
564         }
565         if (lcFileName.endsWith(".mxsd") || lcFileName.endsWith(".exsd")) { //$NON-NLS-1$ //$NON-NLS-2$
566
fOpenSchemaAction.setImageDescriptor(desc);
567             manager.add(fOpenSchemaAction);
568             manager.add(new Separator());
569             fOpenSchemaAction.setChecked(
570                 editorId != null
571                     && editorId.equals(IPDEUIConstants.SCHEMA_EDITOR_ID));
572         }
573         manager.add(fOpenTextEditorAction);
574         fOpenTextEditorAction.setChecked(
575             editorId == null || editorId.equals(DEFAULT_EDITOR_ID));
576         fOpenSystemEditorAction.setImageDescriptor(desc);
577         fOpenSystemEditorAction.setChecked(editorId != null && editorId.equals("@system")); //$NON-NLS-1$
578
manager.add(fOpenSystemEditorAction);
579     }
580
581     protected void initDragAndDrop() {
582         int ops = DND.DROP_COPY | DND.DROP_MOVE;
583         Transfer[] transfers = new Transfer[] { FileTransfer.getInstance()};
584         fTreeViewer.addDragSupport(
585             ops,
586             transfers,
587             new PluginsDragAdapter(fTreeViewer));
588     }
589
590     private IDialogSettings getSettings() {
591         IDialogSettings master = PDEPlugin.getDefault().getDialogSettings();
592         IDialogSettings section = master.getSection("pluginsView"); //$NON-NLS-1$
593
if (section == null) {
594             section = master.addNewSection("pluginsView"); //$NON-NLS-1$
595
}
596         return section;
597     }
598
599     private void initFilters() {
600         IDialogSettings settings = getSettings();
601         fTreeViewer.addFilter(fJavaFilter);
602         boolean hideWorkspace = settings.getBoolean(HIDE_WRKSPC);
603         boolean hideEnabledExternal = settings.getBoolean(HIDE_EXENABLED);
604         boolean hideDisabledExternal = !settings.getBoolean(SHOW_EXDISABLED);
605         if (hideWorkspace)
606             fTreeViewer.addFilter(fHideWorkspaceFilter);
607         if (hideEnabledExternal)
608             fTreeViewer.addFilter(fHideExtEnabledFilter);
609         if (hideDisabledExternal)
610             fTreeViewer.addFilter(fHideExtDisabledFilter);
611         fHideWorkspaceFilterAction.setChecked(!hideWorkspace);
612         fHideExtEnabledFilterAction.setChecked(!hideEnabledExternal);
613         fHideExtDisabledFilterAction.setChecked(!hideDisabledExternal);
614     }
615
616     private void hookContextMenu() {
617         MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
618
menuMgr.setRemoveAllWhenShown(true);
619         menuMgr.addMenuListener(new IMenuListener() {
620             public void menuAboutToShow(IMenuManager manager) {
621                 PluginsView.this.fillContextMenu(manager);
622             }
623         });
624         Menu menu = menuMgr.createContextMenu(fTreeViewer.getControl());
625         fTreeViewer.getControl().setMenu(menu);
626         getSite().registerContextMenu(menuMgr, fTreeViewer);
627     }
628
629     private void handleDoubleClick() {
630         Object JavaDoc obj = getSelectedObject();
631         if (obj instanceof IPluginModelBase) {
632             boolean expanded = false;
633             // only expand target models
634
if (((IPluginModelBase)obj).getUnderlyingResource() == null) {
635                 expanded = fTreeViewer.getExpandedState(obj);
636                 fTreeViewer.setExpandedState(obj, !expanded);
637             }
638             if (fTreeViewer.getExpandedState(obj) == expanded) {
639                 // not expandable, open editor
640
ManifestEditor.openPluginEditor((IPluginModelBase)obj);
641             }
642         } else if (obj instanceof FileAdapter) {
643             FileAdapter adapter = (FileAdapter) obj;
644             if (adapter.isDirectory()) {
645                 fTreeViewer.setExpandedState(
646                     adapter,
647                     !fTreeViewer.getExpandedState(adapter));
648                 return;
649             }
650             String JavaDoc editorId = adapter.getEditorId();
651             if (editorId != null && editorId.equals("@system")) //$NON-NLS-1$
652
handleOpenSystemEditor(adapter);
653             else
654                 handleOpenTextEditor(adapter, editorId);
655         } else if (obj instanceof IClassFile) {
656             fOpenClassFileAction.run();
657         } else if (isOpenableStorage(obj)) {
658             handleOpenStorage((IStorage) obj);
659         }
660     }
661
662     private static boolean isOpenableStorage(Object JavaDoc storage) {
663         if (storage instanceof IJarEntryResource)
664             return ((IJarEntryResource) storage).isFile();
665         return storage instanceof IStorage;
666     }
667     
668     private void handleOpenStorage(IStorage obj) {
669         IWorkbenchPage page = PDEPlugin.getActivePage();
670         IEditorInput input = new JarEntryEditorInput(obj);
671         try {
672             page.openEditor(input, DEFAULT_EDITOR_ID);
673         } catch (PartInitException e) {
674             PDEPlugin.logException(e);
675         }
676     }
677
678     private void handleSelectDependencies() {
679         IStructuredSelection selection =
680             (IStructuredSelection) fTreeViewer.getSelection();
681         if (selection.size() == 0)
682             return;
683         
684         IPluginModelBase[] models = new IPluginModelBase[selection.size()];
685         System.arraycopy(selection.toArray(), 0, models, 0, selection.size());
686         Set JavaDoc set = DependencyManager.getSelfandDependencies(models);
687         Object JavaDoc[] symbolicNames = set.toArray();
688         ArrayList JavaDoc result = new ArrayList JavaDoc(set.size());
689         for (int i = 0; i < symbolicNames.length; i++) {
690             IPluginModelBase model = PluginRegistry.findModel(symbolicNames[i].toString());
691             if (model != null)
692                 result.add(model);
693         }
694         fTreeViewer.setSelection(new StructuredSelection(result.toArray()));
695     }
696     
697     private void handleSelectInJavaSearch() {
698         PluginsContentProvider provider =
699             (PluginsContentProvider) fTreeViewer.getContentProvider();
700         Object JavaDoc[] elements = provider.getElements(fTreeViewer.getInput());
701         ArrayList JavaDoc result = new ArrayList JavaDoc();
702         for (int i = 0; i < elements.length; i++) {
703             Object JavaDoc element = elements[i];
704             if (element instanceof IPluginModelBase) {
705                 String JavaDoc id = ((IPluginModelBase) element).getPluginBase().getId();
706                 if (PDECore.getDefault().getSearchablePluginsManager().isInJavaSearch(id))
707                     result.add(element);
708             }
709         }
710         fTreeViewer.setSelection(new StructuredSelection(result.toArray()));
711     }
712
713     private void handleOpenTextEditor(FileAdapter adapter, String JavaDoc editorId) {
714         if (adapter == null)
715             return;
716         IWorkbenchPage page = PDEPlugin.getActivePage();
717         if (editorId == null) {
718             if (adapter.isManifest())
719                 editorId = IPDEUIConstants.MANIFEST_EDITOR_ID;
720             else if (adapter.isSchema())
721                 editorId = IPDEUIConstants.SCHEMA_EDITOR_ID;
722         }
723         try {
724             if (editorId == null || editorId.equals("@system")) //$NON-NLS-1$
725
editorId = DEFAULT_EDITOR_ID;
726             page.openEditor(
727                 new SystemFileEditorInput(adapter.getFile()),
728                 editorId);
729             adapter.setEditorId(editorId);
730         } catch (PartInitException e) {
731             PDEPlugin.logException(e);
732         }
733     }
734
735     private void handleOpenManifestEditor(FileAdapter adapter) {
736         handleOpenTextEditor(adapter, IPDEUIConstants.MANIFEST_EDITOR_ID);
737     }
738     
739     private void handleOpenSchemaEditor(FileAdapter adapter) {
740         handleOpenTextEditor(adapter, IPDEUIConstants.SCHEMA_EDITOR_ID);
741     }
742
743     private void handleOpenSystemEditor(FileAdapter adapter) {
744         if (adapter == null)
745             return;
746         File JavaDoc localFile = null;
747
748         try {
749             localFile = getLocalCopy(adapter.getFile());
750         } catch (IOException JavaDoc e) {
751             PDEPlugin.logException(e);
752             return;
753         } catch (CoreException e) {
754             PDEPlugin.logException(e);
755         }
756         // Start busy indicator.
757
final File JavaDoc file = localFile;
758         final boolean result[] = new boolean[1];
759         BusyIndicator
760             .showWhile(fTreeViewer.getTree().getDisplay(), new Runnable JavaDoc() {
761             public void run() {
762                 // Open file using shell.
763
String JavaDoc path = file.getAbsolutePath();
764                 result[0] = Program.launch(path);
765             }
766         });
767
768         // ShellExecute returns whether call was successful
769
if (!result[0]) {
770             PDEPlugin.logException(
771                 new PartInitException(
772                     NLS.bind(PDEUIMessages.PluginsView_unableToOpen, file.getName())));
773         } else {
774             adapter.setEditorId("@system"); //$NON-NLS-1$
775
}
776     }
777
778     private File JavaDoc getLocalCopy(File JavaDoc file) throws IOException JavaDoc, CoreException {
779         // create a tmp. copy of this file and make it
780
// read-only. This is to ensure that the original
781
// file belonging to the external plug-in directories
782
// will not be modified.
783
String JavaDoc fileName = file.getName();
784         String JavaDoc prefix;
785         String JavaDoc suffix = null;
786         int dotLoc = fileName.indexOf('.');
787         if (dotLoc != -1) {
788             prefix = fileName.substring(0, dotLoc);
789             suffix = fileName.substring(dotLoc);
790         } else {
791             prefix = fileName;
792         }
793
794         File JavaDoc tmpFile = File.createTempFile(prefix, suffix);
795         tmpFile.deleteOnExit();
796         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(tmpFile);
797         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
798         byte[] cbuffer = new byte[1024];
799         int read = 0;
800
801         while (read != -1) {
802             read = fis.read(cbuffer);
803             if (read != -1)
804                 fos.write(cbuffer, 0, read);
805         }
806         fos.flush();
807         fos.close();
808         fis.close();
809         tmpFile.setReadOnly();
810         return tmpFile;
811     }
812
813     private void handleSelectionChanged(ISelection selection) {
814         String JavaDoc text = ""; //$NON-NLS-1$
815
Object JavaDoc obj = getSelectedObject();
816         if (obj instanceof IPluginModelBase) {
817             text = ((IPluginModelBase) obj).getInstallLocation();
818         }
819         if (obj instanceof FileAdapter) {
820             text = ((FileAdapter) obj).getFile().getAbsolutePath();
821         }
822         getViewSite().getActionBars().getStatusLineManager().setMessage(text);
823     }
824     
825     private void hookDoubleClickAction() {
826         fTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
827             public void doubleClick(DoubleClickEvent event) {
828                 handleDoubleClick();
829             }
830         });
831     }
832
833     /*
834      * (non-Javadoc)
835      * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
836      */

837     public void setFocus() {
838         fTreeViewer.getTree().setFocus();
839     }
840     
841     void updateTitle(Object JavaDoc newInput) {
842         IConfigurationElement config = getConfigurationElement();
843         if (config == null)
844             return;
845         
846         if (newInput == null
847                 || newInput.equals(PDECore.getDefault().getModelManager())) {
848             updateContentDescription();
849             setTitleToolTip(getTitle());
850         } else {
851             String JavaDoc viewName = config.getAttribute("name"); //$NON-NLS-1$
852
String JavaDoc name = ((LabelProvider) fTreeViewer.getLabelProvider()).getText(newInput);
853             setContentDescription(viewName + ": " + name); //$NON-NLS-1$
854
setTitleToolTip(getInputPath(newInput));
855         }
856     }
857     private String JavaDoc getInputPath(Object JavaDoc input) {
858         if (input instanceof FileAdapter) {
859             return "file: " + ((FileAdapter) input).getFile().getAbsolutePath(); //$NON-NLS-1$
860
}
861         if (input instanceof IPluginModelBase) {
862             IPluginModelBase model = (IPluginModelBase)input;
863             return "plugin: " + model.getInstallLocation(); //$NON-NLS-1$
864
}
865         return ""; //$NON-NLS-1$
866
}
867     
868     private void updateContentDescription() {
869         String JavaDoc total = Integer.toString(PluginRegistry.getAllModels().length);
870         String JavaDoc visible = Integer.toString(fTreeViewer.getTree().getItemCount());
871         setContentDescription(NLS.bind(PDEUIMessages.PluginsView_description, visible, total));
872     }
873     
874     public void modelsChanged(final PluginModelDelta delta) {
875         if (fTreeViewer == null || fTreeViewer.getTree().isDisposed())
876             return;
877
878         fTreeViewer.getTree().getDisplay().asyncExec(new Runnable JavaDoc() {
879             public void run() {
880                 int kind = delta.getKind();
881                 if (fTreeViewer.getTree().isDisposed())
882                     return;
883                 if ((kind & PluginModelDelta.CHANGED) !=0 || (kind & PluginModelDelta.REMOVED) != 0) {
884                     // Don't know exactly what change -
885
// the safest way out is to refresh
886
fTreeViewer.refresh();
887                 } else if ((kind & PluginModelDelta.ADDED) != 0) {
888                     ModelEntry[] added = delta.getAddedEntries();
889                     for (int i = 0; i < added.length; i++) {
890                         IPluginModelBase[] models = getModels(added[i]);
891                         for (int j = 0; j < models.length; j++) {
892                             if (isVisible(models[j]))
893                                 fTreeViewer.add(fManager, models[j]);
894                         }
895                     }
896                 }
897                 updateContentDescription();
898             }
899         });
900     }
901     
902     private IPluginModelBase[] getModels(ModelEntry entry) {
903         return (entry.hasWorkspaceModels())
904                     ? entry.getWorkspaceModels() : entry.getExternalModels();
905     }
906     
907     private boolean isVisible(IPluginModelBase entry) {
908         ViewerFilter[] filters = fTreeViewer.getFilters();
909         for (int i = 0; i < filters.length; i++) {
910             if (!filters[i].select(fTreeViewer, fManager, entry))
911                 return false;
912         }
913         return true;
914     }
915
916     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
917         if(isShowInApplicable())
918         { if(adapter == IShowInSource.class && isShowInApplicable())
919             { return getShowInSource();
920             }
921             else if(adapter == IShowInTargetList.class)
922             { return getShowInTargetList();
923             }
924         }
925
926         return super.getAdapter(adapter);
927     }
928     
929     /**
930      * Returns the <code>IShowInSource</code> for this view.
931      * @return the <code>IShowInSource</code>
932      */

933     protected IShowInSource getShowInSource() {
934         return new IShowInSource() {
935             public ShowInContext getShowInContext() {
936                 ArrayList JavaDoc resourceList = new ArrayList JavaDoc();
937                 IStructuredSelection selection = (IStructuredSelection)fTreeViewer.getSelection();
938                 IStructuredSelection resources;
939                 if (selection.isEmpty())
940                 { resources = null;
941                 }
942                 else
943                 { for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
944                         Object JavaDoc obj = iter.next();
945                         if (obj instanceof IPluginModelBase) {
946                             resourceList.add(((IPluginModelBase)obj).getUnderlyingResource());
947                         }
948                     }
949                     resources = new StructuredSelection(resourceList);
950                 }
951                 
952                 return new ShowInContext(
953                     fTreeViewer.getInput(),
954                     resources);
955             }
956         };
957     }
958
959     /**
960      * Returns the <code>IShowInTargetList</code> for this view.
961      * @return the <code>IShowInTargetList</code>
962      */

963     protected IShowInTargetList getShowInTargetList() {
964         return new IShowInTargetList() {
965             public String JavaDoc[] getShowInTargetIds() {
966                 return new String JavaDoc[]
967                        {JavaUI.ID_PACKAGES, IPageLayout.ID_RES_NAV};
968             }
969         };
970     }
971 }
972
Popular Tags