KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > SynchronizeModelManager


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.team.internal.ui.synchronize;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jface.action.*;
20 import org.eclipse.jface.dialogs.IDialogSettings;
21 import org.eclipse.jface.util.IPropertyChangeListener;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.team.core.synchronize.SyncInfoSet;
25 import org.eclipse.team.internal.ui.*;
26 import org.eclipse.team.ui.synchronize.*;
27 import org.eclipse.ui.IActionBars;
28
29 /**
30  * Manages the models that can be displayed by a synchronize page
31  */

32 public abstract class SynchronizeModelManager extends SynchronizePageActionGroup {
33     
34     private static final String JavaDoc P_LAST_PROVIDER = TeamUIPlugin.ID + ".P_LAST_MODELPROVIDER"; //$NON-NLS-1$
35

36     private ISynchronizeModelProvider modelProvider;
37     private List JavaDoc toggleModelProviderActions;
38     private ISynchronizePageConfiguration configuration;
39     private TreeViewerAdvisor advisor;
40     
41     /**
42      * Action that allows changing the model providers supported by this advisor.
43      */

44     private class ToggleModelProviderAction extends Action implements IPropertyChangeListener {
45         private ISynchronizeModelProviderDescriptor descriptor;
46         protected ToggleModelProviderAction(ISynchronizeModelProviderDescriptor descriptor) {
47             super(descriptor.getName(), IAction.AS_RADIO_BUTTON);
48             setImageDescriptor(descriptor.getImageDescriptor());
49             setToolTipText(descriptor.getName());
50             this.descriptor = descriptor;
51             update();
52             configuration.addPropertyChangeListener(this);
53         }
54
55         public void run() {
56             if (!getSelectedProviderId().equals(descriptor.getId())) {
57                 setInput(descriptor.getId(), null);
58             }
59         }
60         
61         public void update() {
62             setChecked(getSelectedProviderId().equals(descriptor.getId()));
63         }
64
65         /* (non-Javadoc)
66          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
67          */

68         public void propertyChange(PropertyChangeEvent event) {
69             if (event.getProperty().equals(SynchronizePageConfiguration.P_MODEL)) {
70                 update();
71             }
72         }
73     }
74     
75     public SynchronizeModelManager(ISynchronizePageConfiguration configuration) {
76         Assert.isNotNull(configuration, "configuration cannot be null"); //$NON-NLS-1$
77
this.configuration = configuration;
78         configuration.addActionContribution(this);
79     }
80     
81     /**
82      * Initialize the model manager to be used with the provided advisor.
83      */

84     public void setViewerAdvisor(TreeViewerAdvisor advisor) {
85         this.advisor = advisor;
86     }
87     
88     /**
89      * Return the list of supported model providers for this advisor.
90      * @return the supported models
91      */

92     protected abstract ISynchronizeModelProviderDescriptor[] getSupportedModelProviders();
93     
94     /**
95      * Get the model provider that will be used to create the input
96      * for the adviser's viewer.
97      * @return the model provider
98      */

99     protected abstract ISynchronizeModelProvider createModelProvider(String JavaDoc id);
100     
101     /**
102      * Return the provider that is currently active.
103      * @return the provider that is currently active
104      */

105     public ISynchronizeModelProvider getActiveModelProvider() {
106         return modelProvider;
107     }
108     
109     protected String JavaDoc getDefaultProviderId() {
110         String JavaDoc defaultLayout = TeamUIPlugin.getPlugin().getPreferenceStore().getString(IPreferenceIds.SYNCVIEW_DEFAULT_LAYOUT);
111         if (defaultLayout.equals(IPreferenceIds.TREE_LAYOUT)) {
112             return HierarchicalModelProvider.HierarchicalModelProviderDescriptor.ID;
113         }
114         if (defaultLayout.equals(IPreferenceIds.FLAT_LAYOUT)) {
115             return FlatModelProvider.FlatModelProviderDescriptor.ID;
116         }
117         // Return compressed folder is the others were not a match
118
return CompressedFoldersModelProvider.CompressedFolderModelProviderDescriptor.ID;
119     }
120     
121     /**
122      * Return the id of the selected provider. By default, this is the
123      * id of the active provider. However, subclasses that use a composite
124      * may return an id that differs from that of the active provider
125      * and return an id of a sub-provider instead.
126      * @return the id of the selected provider
127      */

128     protected String JavaDoc getSelectedProviderId() {
129         ISynchronizeModelProvider provider = getActiveModelProvider();
130         if (provider != null) {
131             return provider.getDescriptor().getId();
132         }
133         return getDefaultProviderId();
134     }
135     
136     /**
137      * Replace the active provider with a provider for the given id.
138      * The new provider is created and initialized and assigned
139      * as the input of the viewer.
140      * @param id the id used to consfigure the new model provider
141      * @param monitor a progress monitor
142      */

143     protected void setInput(String JavaDoc id, IProgressMonitor monitor) {
144         if(modelProvider != null) {
145             modelProvider.saveState();
146             modelProvider.dispose();
147         }
148         modelProvider = createModelProvider(id);
149         saveProviderSettings(id);
150         modelProvider.prepareInput(monitor);
151         setInput();
152     }
153     
154     /**
155      * Save the settings for the currently active provider
156      */

157     protected void saveProviderSettings(String JavaDoc id) {
158         IDialogSettings pageSettings = getConfiguration().getSite().getPageSettings();
159         if(pageSettings != null) {
160             pageSettings.put(P_LAST_PROVIDER, id);
161         }
162     }
163
164     /**
165      * Set the input of the viewer to the root model element.
166      */

167     protected void setInput() {
168         configuration.setProperty(SynchronizePageConfiguration.P_MODEL, modelProvider.getModelRoot());
169         if(advisor != null)
170             advisor.setInput(modelProvider);
171     }
172     
173     /**
174      * Return the model root of the currently active model provider.
175      * This method will wait until the model is done updating.
176      * It is for use by test purposes only.
177      * @return the model root
178      */

179     public ISynchronizeModelElement getModelRoot() {
180         if (modelProvider != null && modelProvider instanceof SynchronizeModelProvider) {
181             ((SynchronizeModelProvider)modelProvider).waitUntilDone(new IProgressMonitor() {
182                 public void beginTask(String JavaDoc name, int totalWork) {
183                 }
184                 public void done() {
185                 }
186                 public void internalWorked(double work) {
187                 }
188                 public boolean isCanceled() {
189                     return false;
190                 }
191                 public void setCanceled(boolean value) {
192                 }
193                 public void setTaskName(String JavaDoc name) {
194                 }
195                 public void subTask(String JavaDoc name) {
196                 }
197                 public void worked(int work) {
198                     while (Display.getCurrent().readAndDispatch()) {}
199                 }
200             });
201             return modelProvider.getModelRoot();
202         } else {
203             return null;
204         }
205     }
206     
207     /* (non-Javadoc)
208      * @see org.eclipse.team.ui.synchronize.IActionContribution#initialize(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration)
209      */

210     public void initialize(ISynchronizePageConfiguration configuration) {
211         super.initialize(configuration);
212         ISynchronizeModelProviderDescriptor[] providers = getSupportedModelProviders();
213         // We only need switching of layouts if there is more than one model provider
214
if (providers.length > 1) {
215             toggleModelProviderActions = new ArrayList JavaDoc();
216             for (int i = 0; i < providers.length; i++) {
217                 final ISynchronizeModelProviderDescriptor provider = providers[i];
218                 toggleModelProviderActions.add(new ToggleModelProviderAction(provider));
219             }
220         }
221         // The input may of been set already. In that case, don't change it and
222
// simply assign it to the view.
223
if(modelProvider == null) {
224             String JavaDoc defaultProviderId = getDefaultProviderId(); /* use providers prefered */
225             IDialogSettings pageSettings = configuration.getSite().getPageSettings();
226             if(pageSettings != null && pageSettings.get(P_LAST_PROVIDER) != null) {
227                 defaultProviderId = pageSettings.get(P_LAST_PROVIDER);
228             }
229             setInput(defaultProviderId, null);
230         } else {
231             setInput();
232         }
233     }
234     
235     /* (non-Javadoc)
236      * @see org.eclipse.team.ui.synchronize.IActionContribution#setActionBars(org.eclipse.ui.IActionBars)
237      */

238     public void fillActionBars(IActionBars actionBars) {
239         if (toggleModelProviderActions == null) return;
240         IToolBarManager toolbar = actionBars.getToolBarManager();
241         IMenuManager menu = actionBars.getMenuManager();
242         IContributionItem group = findGroup(menu, ISynchronizePageConfiguration.LAYOUT_GROUP);
243         if(menu != null && group != null) {
244             MenuManager layout = new MenuManager(Utils.getString("action.layout.label", Policy.getActionBundle())); //$NON-NLS-1$
245
menu.appendToGroup(group.getId(), layout);
246             appendToMenu(null, layout);
247         } else if(toolbar != null) {
248             group = findGroup(toolbar, ISynchronizePageConfiguration.LAYOUT_GROUP);
249             if (group != null) {
250                 appendToMenu(group.getId(), toolbar);
251             }
252         }
253     }
254     
255     private void appendToMenu(String JavaDoc groupId, IContributionManager menu) {
256         for (Iterator JavaDoc iter = toggleModelProviderActions.iterator(); iter.hasNext();) {
257             if (groupId == null) {
258                 menu.add((Action) iter.next());
259             } else {
260                 menu.appendToGroup(groupId, (Action) iter.next());
261             }
262         }
263     }
264     
265     /* (non-Javadoc)
266      * @see org.eclipse.team.ui.synchronize.IActionContribution#dispose()
267      */

268     public void dispose() {
269         if(modelProvider != null) {
270             modelProvider.dispose();
271         }
272         super.dispose();
273     }
274     
275     /**
276      * Returns the configuration
277      * @return the configuration.
278      */

279     public ISynchronizePageConfiguration getConfiguration() {
280         return configuration;
281     }
282     
283     /**
284      * Return the sync info set that is contained in the configuration.
285      * @return the sync info set that is contained in the configuration
286      */

287     protected SyncInfoSet getSyncInfoSet() {
288         return (SyncInfoSet)getConfiguration().getProperty(ISynchronizePageConfiguration.P_SYNC_INFO_SET);
289     }
290 }
291
Popular Tags