KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.editor.plugin;
12
13 import org.eclipse.pde.core.IBaseModel;
14 import org.eclipse.pde.core.IModel;
15 import org.eclipse.pde.core.IModelChangedEvent;
16 import org.eclipse.pde.core.IModelChangedListener;
17 import org.eclipse.pde.core.ModelChangedEvent;
18 import org.eclipse.pde.core.plugin.ISharedExtensionsModel;
19 import org.eclipse.pde.internal.core.IModelChangeProviderExtension;
20 import org.eclipse.pde.internal.core.IModelChangedListenerFilter;
21 import org.eclipse.pde.internal.core.bundle.BundleFragmentModel;
22 import org.eclipse.pde.internal.core.bundle.BundlePluginModel;
23 import org.eclipse.pde.internal.core.bundle.BundlePluginModelBase;
24 import org.eclipse.pde.internal.core.ibundle.IBundleModel;
25 import org.eclipse.pde.internal.ui.editor.FormOutlinePage;
26 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
27 import org.eclipse.pde.internal.ui.editor.build.BuildInputContext;
28 import org.eclipse.pde.internal.ui.editor.context.InputContext;
29 import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
30 import org.eclipse.ui.forms.IFormPart;
31
32
33 public class PluginInputContextManager extends InputContextManager {
34     private BundlePluginModelBase bmodel;
35     /**
36      *
37      */

38     public PluginInputContextManager(PDEFormEditor editor) {
39         super(editor);
40     }
41
42     public IBaseModel getAggregateModel() {
43         if (bmodel!=null)
44             return bmodel;
45         return findPluginModel();
46     }
47     
48     public IModel getPluginModel() {
49         if (bmodel!=null)
50             return bmodel.getExtensionsModel();
51         return findPluginModel();
52     }
53     
54     protected void fireContextChange(InputContext context, boolean added) {
55         super.fireContextChange(context, added);
56         if (context.getId().equals(BundleInputContext.CONTEXT_ID)) {
57             if (added)// bundle arriving
58
bundleAdded(context);
59             else
60             // bundle going away
61
bundleRemoved(context);
62         }
63         else if (context.getId().equals(BuildInputContext.CONTEXT_ID)) {
64             if (added)
65                 buildAdded(context);
66             else
67                 buildRemoved(context);
68         }
69         else if (context.getId().equals(PluginInputContext.CONTEXT_ID)) {
70             if (added)
71                 pluginAdded(context);
72             else
73                 pluginRemoved(context);
74         }
75     }
76     private void bundleAdded(InputContext bundleContext) {
77         IBundleModel model = (IBundleModel)bundleContext.getModel();
78         if (model.isFragmentModel())
79             bmodel = new BundleFragmentModel();
80         else
81             bmodel = new BundlePluginModel();
82         bmodel.setBundleModel(model);
83         syncExtensions();
84     }
85     
86     private void syncExtensions() {
87         IModel emodel = findPluginModel();
88         if (emodel!=null && emodel instanceof ISharedExtensionsModel) {
89             bmodel.setExtensionsModel((ISharedExtensionsModel)emodel);
90             transferListeners(emodel, bmodel);
91         }
92         else
93             bmodel.setExtensionsModel(null);
94     }
95     
96     private IModel findPluginModel() {
97         InputContext pcontext = findContext(PluginInputContext.CONTEXT_ID);
98         return (pcontext != null) ? (IModel)pcontext.getModel() : null;
99     }
100
101     private void bundleRemoved(InputContext bundleContext) {
102         if (bmodel!=null) {
103             BundlePluginModelBase preserved = bmodel;
104             bmodel = null;
105             IModel emodel = findPluginModel();
106             if (emodel!=null)
107                 transferListeners(preserved, emodel);
108         }
109     }
110     
111     private void transferListeners(IModel source, IModel target) {
112         if (source instanceof IModelChangeProviderExtension &&
113                 target instanceof IModelChangeProviderExtension) {
114             IModelChangeProviderExtension smodel = (IModelChangeProviderExtension)source;
115             IModelChangeProviderExtension tmodel = (IModelChangeProviderExtension)target;
116             // first fire one last event to all the listeners to
117
// refresh
118
smodel.fireModelChanged(new ModelChangedEvent(smodel, IModelChangedEvent.WORLD_CHANGED, null, null));
119             // now pass the listener to the target model
120
smodel.transferListenersTo(tmodel, new IModelChangedListenerFilter() {
121                 public boolean accept(IModelChangedListener listener) {
122                     if (listener instanceof IFormPart ||
123                             listener instanceof FormOutlinePage)
124                         return true;
125                     return false;
126                 }
127             });
128         }
129     }
130     
131     private void pluginAdded(InputContext pluginContext) {
132         if (bmodel!=null)
133             syncExtensions();
134     }
135     private void pluginRemoved(InputContext pluginContext) {
136         if (bmodel!=null)
137             syncExtensions();
138     }
139     private void buildAdded(InputContext buildContext) {
140     }
141     private void buildRemoved(InputContext buildContext) {
142     }
143 }
144
Popular Tags