001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.editor.plugin;
011:
012: import org.eclipse.pde.core.IBaseModel;
013: import org.eclipse.pde.core.IModel;
014: import org.eclipse.pde.core.IModelChangedEvent;
015: import org.eclipse.pde.core.IModelChangedListener;
016: import org.eclipse.pde.core.ModelChangedEvent;
017: import org.eclipse.pde.core.plugin.ISharedExtensionsModel;
018: import org.eclipse.pde.internal.core.IModelChangeProviderExtension;
019: import org.eclipse.pde.internal.core.IModelChangedListenerFilter;
020: import org.eclipse.pde.internal.core.bundle.BundleFragmentModel;
021: import org.eclipse.pde.internal.core.bundle.BundlePluginModel;
022: import org.eclipse.pde.internal.core.bundle.BundlePluginModelBase;
023: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
024: import org.eclipse.pde.internal.ui.editor.FormOutlinePage;
025: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
026: import org.eclipse.pde.internal.ui.editor.build.BuildInputContext;
027: import org.eclipse.pde.internal.ui.editor.context.InputContext;
028: import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
029: import org.eclipse.ui.forms.IFormPart;
030:
031: public class PluginInputContextManager extends InputContextManager {
032: private BundlePluginModelBase bmodel;
033:
034: /**
035: *
036: */
037: public PluginInputContextManager(PDEFormEditor editor) {
038: super (editor);
039: }
040:
041: public IBaseModel getAggregateModel() {
042: if (bmodel != null)
043: return bmodel;
044: return findPluginModel();
045: }
046:
047: public IModel getPluginModel() {
048: if (bmodel != null)
049: return bmodel.getExtensionsModel();
050: return findPluginModel();
051: }
052:
053: protected void fireContextChange(InputContext context, boolean added) {
054: super .fireContextChange(context, added);
055: if (context.getId().equals(BundleInputContext.CONTEXT_ID)) {
056: if (added)// bundle arriving
057: bundleAdded(context);
058: else
059: // bundle going away
060: bundleRemoved(context);
061: } else if (context.getId().equals(BuildInputContext.CONTEXT_ID)) {
062: if (added)
063: buildAdded(context);
064: else
065: buildRemoved(context);
066: } else if (context.getId()
067: .equals(PluginInputContext.CONTEXT_ID)) {
068: if (added)
069: pluginAdded(context);
070: else
071: pluginRemoved(context);
072: }
073: }
074:
075: private void bundleAdded(InputContext bundleContext) {
076: IBundleModel model = (IBundleModel) bundleContext.getModel();
077: if (model.isFragmentModel())
078: bmodel = new BundleFragmentModel();
079: else
080: bmodel = new BundlePluginModel();
081: bmodel.setBundleModel(model);
082: syncExtensions();
083: }
084:
085: private void syncExtensions() {
086: IModel emodel = findPluginModel();
087: if (emodel != null && emodel instanceof ISharedExtensionsModel) {
088: bmodel.setExtensionsModel((ISharedExtensionsModel) emodel);
089: transferListeners(emodel, bmodel);
090: } else
091: bmodel.setExtensionsModel(null);
092: }
093:
094: private IModel findPluginModel() {
095: InputContext pcontext = findContext(PluginInputContext.CONTEXT_ID);
096: return (pcontext != null) ? (IModel) pcontext.getModel() : null;
097: }
098:
099: private void bundleRemoved(InputContext bundleContext) {
100: if (bmodel != null) {
101: BundlePluginModelBase preserved = bmodel;
102: bmodel = null;
103: IModel emodel = findPluginModel();
104: if (emodel != null)
105: transferListeners(preserved, emodel);
106: }
107: }
108:
109: private void transferListeners(IModel source, IModel target) {
110: if (source instanceof IModelChangeProviderExtension
111: && target instanceof IModelChangeProviderExtension) {
112: IModelChangeProviderExtension smodel = (IModelChangeProviderExtension) source;
113: IModelChangeProviderExtension tmodel = (IModelChangeProviderExtension) target;
114: // first fire one last event to all the listeners to
115: // refresh
116: smodel.fireModelChanged(new ModelChangedEvent(smodel,
117: IModelChangedEvent.WORLD_CHANGED, null, null));
118: // now pass the listener to the target model
119: smodel.transferListenersTo(tmodel,
120: new IModelChangedListenerFilter() {
121: public boolean accept(
122: IModelChangedListener listener) {
123: if (listener instanceof IFormPart
124: || listener instanceof FormOutlinePage)
125: return true;
126: return false;
127: }
128: });
129: }
130: }
131:
132: private void pluginAdded(InputContext pluginContext) {
133: if (bmodel != null)
134: syncExtensions();
135: }
136:
137: private void pluginRemoved(InputContext pluginContext) {
138: if (bmodel != null)
139: syncExtensions();
140: }
141:
142: private void buildAdded(InputContext buildContext) {
143: }
144:
145: private void buildRemoved(InputContext buildContext) {
146: }
147: }
|