1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 import org.eclipse.core.resources.mapping.*; 17 import org.eclipse.jface.action.*; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.swt.widgets.*; 20 import org.eclipse.team.core.mapping.*; 21 import org.eclipse.team.internal.ui.*; 22 import org.eclipse.team.ui.TeamImages; 23 import org.eclipse.team.ui.TeamUI; 24 import org.eclipse.team.ui.mapping.ITeamContentProviderDescriptor; 25 import org.eclipse.team.ui.mapping.ITeamContentProviderManager; 26 import org.eclipse.team.ui.synchronize.*; 27 28 public class ModelSelectionDropDownAction extends Action implements ISynchronizationScopeChangeListener { 29 30 private final ISynchronizePageConfiguration configuration; 31 private MenuManager menuManager; 32 private Action showAllAction; 33 private org.eclipse.jface.util.IPropertyChangeListener listener; 34 private MenuCreator menuCreator; 35 private Action showAllFlatAction; 36 37 private class MenuCreator implements IMenuCreator { 38 public void dispose() { 39 if(menuManager != null) { 40 menuManager.dispose(); 41 menuManager = null; 42 } 43 } 44 public Menu getMenu(Control parent) { 45 Menu fMenu = null; 46 if (menuManager == null) { 47 menuManager = new MenuManager(); 48 fMenu = menuManager.createContextMenu(parent); 49 menuManager.add(showAllAction); 50 showAllAction.setChecked(getActiveProviderId().equals(ModelSynchronizeParticipant.ALL_MODEL_PROVIDERS_VISIBLE)); 51 showAllFlatAction.setChecked(isFlatEnabled()); 52 ModelProvider[] modelProviders = getEnabledModelProviders(); 53 if (modelProviders.length > 0) 54 menuManager.add(new Separator()); 55 addModelsToMenu(modelProviders); 56 menuManager.add(new Separator()); 57 menuManager.add(showAllFlatAction); 58 59 menuManager.update(true); 60 } else { 61 fMenu = menuManager.getMenu(); 62 } 63 return fMenu; 64 } 65 66 public Menu getMenu(Menu parent) { 67 return null; 68 } 69 } 70 71 public ModelSelectionDropDownAction(ISynchronizePageConfiguration configuration) { 72 Utils.initAction(this, "action.pickModels."); this.configuration = configuration; 74 listener = new org.eclipse.jface.util.IPropertyChangeListener() { 75 public void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event) { 76 if (event.getProperty() == ModelSynchronizeParticipant.P_VISIBLE_MODEL_PROVIDER) { 77 update(); 78 } 79 if (event.getProperty().equals(ITeamContentProviderManager.PROP_ENABLED_MODEL_PROVIDERS)) { 80 rebuildMenu(); 81 } 82 } 83 }; 84 this.configuration.addPropertyChangeListener(listener); 85 TeamUI.getTeamContentProviderManager().addPropertyChangeListener(listener); 86 getSynchronizationContext().getScope().addScopeChangeListener(this); 87 showAllAction = new Action(TeamUIMessages.ModelSelectionDropDownAction_0, IAction.AS_RADIO_BUTTON) { 88 public void run() { 89 ModelSelectionDropDownAction.this.configuration.setProperty( 90 ModelSynchronizeParticipant.P_VISIBLE_MODEL_PROVIDER, 91 ModelSynchronizeParticipant.ALL_MODEL_PROVIDERS_VISIBLE); 92 } 93 }; 94 showAllFlatAction = new Action(TeamUIMessages.ModelSelectionDropDownAction_2, IAction.AS_CHECK_BOX) { 97 public void run() { 98 boolean checked = showAllFlatAction.isChecked(); 99 ModelSelectionDropDownAction.this.configuration.setProperty( 100 ITeamContentProviderManager.PROP_PAGE_LAYOUT, 101 checked ? ITeamContentProviderManager.FLAT_LAYOUT : ITeamContentProviderManager.TREE_LAYOUT); 102 } 103 }; 104 showAllFlatAction.setImageDescriptor(TeamImages.getImageDescriptor(ITeamUIImages.IMG_FLAT)); 105 menuCreator = new MenuCreator(); 107 setMenuCreator(menuCreator); 108 update(); 109 } 110 111 private ISynchronizationContext getSynchronizationContext() { 112 return (ISynchronizationContext)configuration.getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT); 113 } 114 115 public void dispose() { 116 if (menuCreator != null) 117 menuCreator.dispose(); 118 getSynchronizationContext().getScope().removeScopeChangeListener(this); 119 configuration.removePropertyChangeListener(listener); 120 TeamUI.getTeamContentProviderManager().removePropertyChangeListener(listener); 121 } 122 123 private ModelProvider[] getEnabledModelProviders() { 124 Set result = new HashSet (); 125 ModelProvider[] providers = ((ModelSynchronizeParticipant)configuration.getParticipant()).getEnabledModelProviders(); 126 providers = ModelMergeOperation.sortByExtension(providers); 127 for (int i = 0; i < providers.length; i++) { 128 ModelProvider provider = providers[i]; 129 ITeamContentProviderDescriptor desc = TeamUI.getTeamContentProviderManager().getDescriptor(provider.getId()); 130 if (desc != null && desc.isEnabled()) { 131 result.add(provider); 132 } 133 } 134 return (ModelProvider[]) result.toArray(new ModelProvider[result.size()]); 135 } 136 137 private void addModelsToMenu(ModelProvider[] modelProviders) { 138 String id = getActiveProviderId(); 139 for (int i = 0; i < modelProviders.length; i++) { 140 ModelProvider provider = modelProviders[i]; 141 Action action = new ShowModelProviderAction(configuration, provider); 142 action.setChecked(provider.getDescriptor().getId().equals(id)); 143 menuManager.add(action); 144 } 145 } 146 147 private String getActiveProviderId() { 148 String id = (String )configuration.getProperty(ModelSynchronizeParticipant.P_VISIBLE_MODEL_PROVIDER); 149 if (id == null) 150 id = ModelSynchronizeParticipant.ALL_MODEL_PROVIDERS_VISIBLE; 151 return id; 152 } 153 154 private ModelProvider getNextProvider() { 155 ModelProvider[] providers = getSynchronizationContext().getScope().getModelProviders(); 156 if (providers.length == 0) 157 return null; 158 providers = ModelOperation.sortByExtension(providers); 159 String id = getActiveProviderId(); 160 int index = 0; 161 if (id != null) { 162 for (int i = 0; i < providers.length; i++) { 163 ModelProvider provider = providers[i]; 164 if (provider.getDescriptor().getId().equals(id)) { 165 index = i + 1; 166 break; 167 } 168 } 169 if (index == providers.length) 170 index = 0; 171 } 172 return providers[index]; 173 } 174 175 public void update() { 176 ModelProvider next = getNextProvider(); 177 if (next == null) return; 178 String text = NLS.bind(TeamUIMessages.ModelSelectionDropDownAction_1, next.getDescriptor().getLabel()); 179 setToolTipText(text); 180 setText(text); 181 if (menuManager != null) { 182 showAllAction.setChecked(getActiveProviderId().equals(ModelSynchronizeParticipant.ALL_MODEL_PROVIDERS_VISIBLE)); 183 showAllFlatAction.setChecked(isFlatEnabled()); 184 IContributionItem[] items = menuManager.getItems(); 185 for (int i = 0; i < items.length; i++) { 186 IContributionItem item = items[i]; 187 if (item instanceof ActionContributionItem) { 188 ActionContributionItem aci = (ActionContributionItem) item; 189 IAction a = aci.getAction(); 190 if (a instanceof ShowModelProviderAction) { 191 ShowModelProviderAction action = (ShowModelProviderAction) a; 192 action.setChecked(action.getProviderId().equals(getActiveProviderId())); 193 } 194 } 195 } 196 } 197 } 199 200 private boolean isFlatEnabled() { 201 String p = (String )configuration.getProperty(ITeamContentProviderManager.PROP_PAGE_LAYOUT); 202 return p != null && p.equals(ITeamContentProviderManager.FLAT_LAYOUT); 203 } 204 205 208 public void run() { 209 ModelProvider next = getNextProvider(); 210 if (next == null) return; 211 Action action = new ShowModelProviderAction(configuration, next); 212 action.run(); 213 } 214 215 218 public void scopeChanged(ISynchronizationScope scope, ResourceMapping[] newMappings, ResourceTraversal[] newTraversals) { 219 if (newMappings.length > 0) { 220 rebuildMenu(); 221 } 222 } 223 224 private void rebuildMenu() { 225 Display display = TeamUIPlugin.getStandardDisplay(); 226 display.asyncExec(new Runnable () { 227 public void run() { 228 if(menuManager != null) { 229 menuManager.dispose(); 230 menuManager = null; 231 } 232 update(); 233 } 234 }); 235 } 236 } 237 | Popular Tags |