KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > views > dependencies > DependenciesViewPageContentProvider


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.dependencies;
12
13 import org.eclipse.jface.viewers.StructuredViewer;
14 import org.eclipse.jface.viewers.Viewer;
15 import org.eclipse.osgi.service.resolver.BundleDescription;
16 import org.eclipse.pde.core.plugin.IPluginModelBase;
17 import org.eclipse.pde.core.plugin.ModelEntry;
18 import org.eclipse.pde.internal.core.IPluginModelListener;
19 import org.eclipse.pde.internal.core.PDECore;
20 import org.eclipse.pde.internal.core.PluginModelDelta;
21 import org.eclipse.pde.internal.core.PluginModelManager;
22 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
23
24 public class DependenciesViewPageContentProvider extends DefaultContentProvider
25         implements IPluginModelListener {
26     private PluginModelManager fPluginManager;
27
28     private DependenciesView fView;
29
30     private StructuredViewer fViewer;
31
32     /**
33      * Constructor.
34      */

35     public DependenciesViewPageContentProvider(DependenciesView view) {
36         this.fView = view;
37         fPluginManager = PDECore.getDefault().getModelManager();
38         attachModelListener();
39     }
40     
41     public void attachModelListener() {
42         fPluginManager.addPluginModelListener(this);
43     }
44     
45     public void removeModelListener() {
46         fPluginManager.removePluginModelListener(this);
47     }
48
49     public void dispose() {
50         removeModelListener();
51     }
52
53     private void handleModifiedModels(ModelEntry[] modified) {
54         Object JavaDoc input = fViewer.getInput();
55         if (input instanceof IPluginModelBase) {
56             BundleDescription desc = ((IPluginModelBase)input).getBundleDescription();
57             String JavaDoc inputID = (desc != null) ? desc.getSymbolicName() : ((IPluginModelBase)input).getPluginBase().getId();
58
59             for (int i = 0; i < modified.length; i++) {
60                 ModelEntry entry = modified[i];
61                 if (entry.getId().equals(inputID)) {
62                     // if we find a matching id to our current input, check to see if the input still exists
63
if (modelExists(entry, (IPluginModelBase)input))
64                         fView.updateTitle(input);
65                     else
66                     // if input model does not exist, clear view
67
fView.openTo(null);
68                     return;
69                 }
70             }
71         }
72     }
73     
74     private boolean modelExists(ModelEntry entry, IPluginModelBase input) {
75         IPluginModelBase[][] entries = new IPluginModelBase[][] {entry.getExternalModels(), entry.getWorkspaceModels()};
76         for (int i = 0; i < 2; i++) {
77             for (int j = 0; j < entries[i].length; j++) {
78                 if (entries[i][j].equals(input))
79                     return true;
80             }
81         }
82         return false;
83     }
84
85     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
86         fView.updateTitle(newInput);
87         this.fViewer = (StructuredViewer) viewer;
88     }
89
90     public void modelsChanged(final PluginModelDelta delta) {
91         if (fViewer == null || fViewer.getControl().isDisposed())
92             return;
93
94         fViewer.getControl().getDisplay().asyncExec(new Runnable JavaDoc() {
95             public void run() {
96                 int kind = delta.getKind();
97                 if (fViewer.getControl().isDisposed())
98                     return;
99                 try {
100                     if ((kind & PluginModelDelta.REMOVED) != 0) {
101                         // called when all instances of a Bundle-SymbolicName are all removed
102
handleModifiedModels(delta.getRemovedEntries());
103                     }
104                     if ((kind & PluginModelDelta.CHANGED) != 0) {
105                         // called when a plug-in is changed (possibly the input)
106
// AND when the model for the ModelEntry changes (new bundle with existing id/remove bundle with 2 instances with same id)
107
handleModifiedModels(delta.getChangedEntries());
108                     }
109                     if ((kind & PluginModelDelta.ADDED) != 0) {
110                         // when user modifies Bundle-SymbolicName, a ModelEntry is created for the new name. In this case, if the input matches
111
// the modified model, we need to update the title.
112
handleModifiedModels(delta.getAddedEntries());
113                     }
114                 } finally {
115                     // no matter what, refresh the viewer since bundles might un/resolve with changes
116
fViewer.refresh();
117                 }
118             }
119         });
120     }
121
122     /**
123      * @return Returns the fPluginManager.
124      */

125     protected PluginModelManager getPluginManager() {
126         return fPluginManager;
127     }
128 }
129
Popular Tags