001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.views.dependencies;
011:
012: import org.eclipse.jface.viewers.StructuredViewer;
013: import org.eclipse.jface.viewers.Viewer;
014: import org.eclipse.osgi.service.resolver.BundleDescription;
015: import org.eclipse.pde.core.plugin.IPluginModelBase;
016: import org.eclipse.pde.core.plugin.ModelEntry;
017: import org.eclipse.pde.internal.core.IPluginModelListener;
018: import org.eclipse.pde.internal.core.PDECore;
019: import org.eclipse.pde.internal.core.PluginModelDelta;
020: import org.eclipse.pde.internal.core.PluginModelManager;
021: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
022:
023: public class DependenciesViewPageContentProvider extends
024: DefaultContentProvider implements IPluginModelListener {
025: private PluginModelManager fPluginManager;
026:
027: private DependenciesView fView;
028:
029: private StructuredViewer fViewer;
030:
031: /**
032: * Constructor.
033: */
034: public DependenciesViewPageContentProvider(DependenciesView view) {
035: this .fView = view;
036: fPluginManager = PDECore.getDefault().getModelManager();
037: attachModelListener();
038: }
039:
040: public void attachModelListener() {
041: fPluginManager.addPluginModelListener(this );
042: }
043:
044: public void removeModelListener() {
045: fPluginManager.removePluginModelListener(this );
046: }
047:
048: public void dispose() {
049: removeModelListener();
050: }
051:
052: private void handleModifiedModels(ModelEntry[] modified) {
053: Object input = fViewer.getInput();
054: if (input instanceof IPluginModelBase) {
055: BundleDescription desc = ((IPluginModelBase) input)
056: .getBundleDescription();
057: String inputID = (desc != null) ? desc.getSymbolicName()
058: : ((IPluginModelBase) input).getPluginBase()
059: .getId();
060:
061: for (int i = 0; i < modified.length; i++) {
062: ModelEntry entry = modified[i];
063: if (entry.getId().equals(inputID)) {
064: // if we find a matching id to our current input, check to see if the input still exists
065: if (modelExists(entry, (IPluginModelBase) input))
066: fView.updateTitle(input);
067: else
068: // if input model does not exist, clear view
069: fView.openTo(null);
070: return;
071: }
072: }
073: }
074: }
075:
076: private boolean modelExists(ModelEntry entry, IPluginModelBase input) {
077: IPluginModelBase[][] entries = new IPluginModelBase[][] {
078: entry.getExternalModels(), entry.getWorkspaceModels() };
079: for (int i = 0; i < 2; i++) {
080: for (int j = 0; j < entries[i].length; j++) {
081: if (entries[i][j].equals(input))
082: return true;
083: }
084: }
085: return false;
086: }
087:
088: public void inputChanged(Viewer viewer, Object oldInput,
089: Object newInput) {
090: fView.updateTitle(newInput);
091: this .fViewer = (StructuredViewer) viewer;
092: }
093:
094: public void modelsChanged(final PluginModelDelta delta) {
095: if (fViewer == null || fViewer.getControl().isDisposed())
096: return;
097:
098: fViewer.getControl().getDisplay().asyncExec(new Runnable() {
099: public void run() {
100: int kind = delta.getKind();
101: if (fViewer.getControl().isDisposed())
102: return;
103: try {
104: if ((kind & PluginModelDelta.REMOVED) != 0) {
105: // called when all instances of a Bundle-SymbolicName are all removed
106: handleModifiedModels(delta.getRemovedEntries());
107: }
108: if ((kind & PluginModelDelta.CHANGED) != 0) {
109: // called when a plug-in is changed (possibly the input)
110: // AND when the model for the ModelEntry changes (new bundle with existing id/remove bundle with 2 instances with same id)
111: handleModifiedModels(delta.getChangedEntries());
112: }
113: if ((kind & PluginModelDelta.ADDED) != 0) {
114: // when user modifies Bundle-SymbolicName, a ModelEntry is created for the new name. In this case, if the input matches
115: // the modified model, we need to update the title.
116: handleModifiedModels(delta.getAddedEntries());
117: }
118: } finally {
119: // no matter what, refresh the viewer since bundles might un/resolve with changes
120: fViewer.refresh();
121: }
122: }
123: });
124: }
125:
126: /**
127: * @return Returns the fPluginManager.
128: */
129: protected PluginModelManager getPluginManager() {
130: return fPluginManager;
131: }
132: }
|