1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.compare.structuremergeviewer.IDiffContainer; 17 import org.eclipse.compare.structuremergeviewer.IDiffElement; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.jface.action.*; 20 import org.eclipse.jface.dialogs.IDialogSettings; 21 import org.eclipse.jface.resource.ImageDescriptor; 22 import org.eclipse.jface.viewers.Viewer; 23 import org.eclipse.jface.viewers.ViewerSorter; 24 import org.eclipse.team.core.synchronize.*; 25 import org.eclipse.team.internal.ui.*; 26 import org.eclipse.team.ui.TeamImages; 27 import org.eclipse.team.ui.synchronize.*; 28 29 32 public class FlatModelProvider extends SynchronizeModelProvider { 33 34 public static class FlatModelProviderDescriptor implements ISynchronizeModelProviderDescriptor { 35 public static final String ID = TeamUIPlugin.ID + ".modelprovider_flat"; public String getId() { 37 return ID; 38 } 39 public String getName() { 40 return TeamUIMessages.FlatModelProvider_0; 41 } 42 public ImageDescriptor getImageDescriptor() { 43 return TeamImages.getImageDescriptor(ITeamUIImages.IMG_FLAT); 44 } 45 } 46 private static final FlatModelProviderDescriptor flatDescriptor = new FlatModelProviderDescriptor(); 47 48 private static final String P_LAST_RESOURCESORT = TeamUIPlugin.ID + ".P_LAST_RESOURCE_SORT"; 50 private int sortCriteria = FlatSorter.PATH; 51 52 56 public static class FullPathSyncInfoElement extends SyncInfoModelElement { 57 public FullPathSyncInfoElement(IDiffContainer parent, SyncInfo info) { 58 super(parent, info); 59 } 60 public String getName() { 61 IResource resource = getResource(); 62 return resource.getName() + " - " + resource.getFullPath().toString(); } 64 } 65 66 70 public class FlatSorter extends ViewerSorter { 71 72 private int resourceCriteria; 73 74 public final static int NAME = 1; 76 public final static int PATH = 2; 77 public final static int PARENT_NAME = 3; 78 79 public FlatSorter(int resourceCriteria) { 80 this.resourceCriteria = resourceCriteria; 81 } 82 83 protected int classComparison(Object element) { 84 if (element instanceof FullPathSyncInfoElement) { 85 return 0; 86 } 87 return 1; 88 } 89 90 protected int compareClass(Object element1, Object element2) { 91 return classComparison(element1) - classComparison(element2); 92 } 93 94 protected int compareNames(String s1, String s2) { 95 return collator.compare(s1, s2); 96 } 97 98 101 public int compare(Viewer viewer, Object o1, Object o2) { 102 103 if (o1 instanceof FullPathSyncInfoElement && o2 instanceof FullPathSyncInfoElement) { 104 IResource r1 = ((FullPathSyncInfoElement)o1).getResource(); 105 IResource r2 = ((FullPathSyncInfoElement)o2).getResource(); 106 if(resourceCriteria == NAME) 107 return compareNames(r1.getName(), r2.getName()); 108 else if(resourceCriteria == PATH) 109 return compareNames(r1.getFullPath().toString(), r2.getFullPath().toString()); 110 else if(resourceCriteria == PARENT_NAME) 111 return compareNames(r1.getParent().getName(), r2.getParent().getName()); 112 else return 0; 113 } else if (o1 instanceof ISynchronizeModelElement) 114 return 1; 115 else if (o2 instanceof ISynchronizeModelElement) 116 return -1; 117 118 return 0; 119 } 120 121 public int getResourceCriteria() { 122 return resourceCriteria; 123 } 124 } 125 126 129 private class ToggleSortOrderAction extends Action { 130 private int criteria; 131 protected ToggleSortOrderAction(String name, int criteria) { 132 super(name, IAction.AS_RADIO_BUTTON); 133 this.criteria = criteria; 134 update(); 135 } 136 137 public void run() { 138 if (isChecked() && sortCriteria != criteria) { 139 sortCriteria = criteria; 140 String key = getSettingsKey(); 141 IDialogSettings pageSettings = getConfiguration().getSite().getPageSettings(); 142 if(pageSettings != null) { 143 pageSettings.put(key, criteria); 144 } 145 update(); 146 FlatModelProvider.this.firePropertyChange(P_VIEWER_SORTER, null, null); 147 } 148 } 149 150 public void update() { 151 setChecked(sortCriteria == criteria); 152 } 153 154 protected String getSettingsKey() { 155 return P_LAST_RESOURCESORT; 156 } 157 } 158 159 162 public class FlatActionGroup extends SynchronizePageActionGroup { 163 private MenuManager sortByResource; 164 public void initialize(ISynchronizePageConfiguration configuration) { 165 super.initialize(configuration); 166 sortByResource = new MenuManager(TeamUIMessages.FlatModelProvider_6); 167 168 appendToGroup( 169 ISynchronizePageConfiguration.P_CONTEXT_MENU, 170 ISynchronizePageConfiguration.SORT_GROUP, 171 sortByResource); 172 173 FlatModelProvider.this.initialize(configuration); 175 176 sortByResource.add( new ToggleSortOrderAction(TeamUIMessages.FlatModelProvider_8, FlatSorter.PATH)); 177 sortByResource.add(new ToggleSortOrderAction(TeamUIMessages.FlatModelProvider_7, FlatSorter.NAME)); 178 sortByResource.add(new ToggleSortOrderAction(TeamUIMessages.FlatModelProvider_9, FlatSorter.PARENT_NAME)); 179 } 180 181 184 public void dispose() { 185 sortByResource.dispose(); 186 sortByResource.removeAll(); 187 super.dispose(); 188 } 189 } 190 191 public FlatModelProvider(ISynchronizePageConfiguration configuration, SyncInfoSet set) { 192 super(configuration, set); 193 initialize(configuration); 194 } 195 196 public FlatModelProvider(AbstractSynchronizeModelProvider parentProvider, ISynchronizeModelElement modelRoot, ISynchronizePageConfiguration configuration, SyncInfoSet set) { 197 super(parentProvider, modelRoot, configuration, set); 198 initialize(configuration); 199 } 200 201 private void initialize(ISynchronizePageConfiguration configuration) { 202 try { 203 IDialogSettings pageSettings = getConfiguration().getSite().getPageSettings(); 204 if(pageSettings != null) { 205 sortCriteria = pageSettings.getInt(P_LAST_RESOURCESORT); 206 } 207 } catch(NumberFormatException e) { 208 } 210 switch (sortCriteria) { 211 case FlatSorter.PATH: 212 case FlatSorter.NAME: 213 case FlatSorter.PARENT_NAME: 214 break; 215 default: 216 sortCriteria = FlatSorter.PATH; 217 break; 218 } 219 } 220 221 224 protected SynchronizePageActionGroup createActionGroup() { 225 return new FlatActionGroup(); 226 } 227 228 231 public ViewerSorter getViewerSorter() { 232 return new FlatSorter(sortCriteria); 233 } 234 235 238 protected IDiffElement[] buildModelObjects(ISynchronizeModelElement node) { 239 if (node == getModelRoot()); 240 SyncInfo[] infos = getSyncInfoSet().getSyncInfos(); 241 List result = new ArrayList (); 242 for (int i = 0; i < infos.length; i++) { 243 SyncInfo info = infos[i]; 244 result.add(createModelObject(node, info)); 245 } 246 return (IDiffElement[]) result.toArray(new IDiffElement[result.size()]); 247 } 248 249 252 protected void handleResourceAdditions(ISyncInfoTreeChangeEvent event) { 253 addResources(event.getAddedResources()); 254 } 255 256 259 protected void handleResourceRemovals(ISyncInfoTreeChangeEvent event) { 260 IResource[] resources = event.getRemovedResources(); 261 removeFromViewer(resources); 262 } 263 264 267 public ISynchronizeModelProviderDescriptor getDescriptor() { 268 return flatDescriptor; 269 } 270 271 protected void addResource(SyncInfo info) { 272 ISynchronizeModelElement node = getModelObject(info.getLocal()); 274 if (node != null) { 275 removeFromViewer(info.getLocal()); 278 } 279 createModelObject(getModelRoot(), info); 280 } 281 282 protected ISynchronizeModelElement createModelObject(ISynchronizeModelElement parent, SyncInfo info) { 283 SynchronizeModelElement newNode = new FullPathSyncInfoElement(parent, info); 284 addToViewer(newNode); 285 return newNode; 286 } 287 } 288 | Popular Tags |