1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 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 32 public abstract class SynchronizeModelManager extends SynchronizePageActionGroup { 33 34 private static final String P_LAST_PROVIDER = TeamUIPlugin.ID + ".P_LAST_MODELPROVIDER"; 36 private ISynchronizeModelProvider modelProvider; 37 private List toggleModelProviderActions; 38 private ISynchronizePageConfiguration configuration; 39 private TreeViewerAdvisor advisor; 40 41 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 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"); this.configuration = configuration; 78 configuration.addActionContribution(this); 79 } 80 81 84 public void setViewerAdvisor(TreeViewerAdvisor advisor) { 85 this.advisor = advisor; 86 } 87 88 92 protected abstract ISynchronizeModelProviderDescriptor[] getSupportedModelProviders(); 93 94 99 protected abstract ISynchronizeModelProvider createModelProvider(String id); 100 101 105 public ISynchronizeModelProvider getActiveModelProvider() { 106 return modelProvider; 107 } 108 109 protected String getDefaultProviderId() { 110 String 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 CompressedFoldersModelProvider.CompressedFolderModelProviderDescriptor.ID; 119 } 120 121 128 protected String getSelectedProviderId() { 129 ISynchronizeModelProvider provider = getActiveModelProvider(); 130 if (provider != null) { 131 return provider.getDescriptor().getId(); 132 } 133 return getDefaultProviderId(); 134 } 135 136 143 protected void setInput(String 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 157 protected void saveProviderSettings(String id) { 158 IDialogSettings pageSettings = getConfiguration().getSite().getPageSettings(); 159 if(pageSettings != null) { 160 pageSettings.put(P_LAST_PROVIDER, id); 161 } 162 } 163 164 167 protected void setInput() { 168 configuration.setProperty(SynchronizePageConfiguration.P_MODEL, modelProvider.getModelRoot()); 169 if(advisor != null) 170 advisor.setInput(modelProvider); 171 } 172 173 179 public ISynchronizeModelElement getModelRoot() { 180 if (modelProvider != null && modelProvider instanceof SynchronizeModelProvider) { 181 ((SynchronizeModelProvider)modelProvider).waitUntilDone(new IProgressMonitor() { 182 public void beginTask(String 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 name) { 194 } 195 public void subTask(String 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 210 public void initialize(ISynchronizePageConfiguration configuration) { 211 super.initialize(configuration); 212 ISynchronizeModelProviderDescriptor[] providers = getSupportedModelProviders(); 213 if (providers.length > 1) { 215 toggleModelProviderActions = new ArrayList (); 216 for (int i = 0; i < providers.length; i++) { 217 final ISynchronizeModelProviderDescriptor provider = providers[i]; 218 toggleModelProviderActions.add(new ToggleModelProviderAction(provider)); 219 } 220 } 221 if(modelProvider == null) { 224 String defaultProviderId = getDefaultProviderId(); 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 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())); 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 groupId, IContributionManager menu) { 256 for (Iterator 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 268 public void dispose() { 269 if(modelProvider != null) { 270 modelProvider.dispose(); 271 } 272 super.dispose(); 273 } 274 275 279 public ISynchronizePageConfiguration getConfiguration() { 280 return configuration; 281 } 282 283 287 protected SyncInfoSet getSyncInfoSet() { 288 return (SyncInfoSet)getConfiguration().getProperty(ISynchronizePageConfiguration.P_SYNC_INFO_SET); 289 } 290 } 291 | Popular Tags |