1 /******************************************************************************* 2 * Copyright (c) 2000, 2010 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.team.internal.ccvs.ui.repo; 15 16 17 import org.eclipse.jface.viewers.Viewer; 18 import org.eclipse.jface.viewers.ViewerComparator; 19 import org.eclipse.team.internal.ccvs.core.*; 20 import org.eclipse.team.internal.ccvs.ui.model.*; 21 import org.eclipse.ui.progress.PendingUpdateAdapter; 22 23 public class RepositoryComparator extends ViewerComparator { 24 25 /** 26 * Default sorting order, by label. 27 */ 28 public static final int ORDER_BY_LABEL = 0; 29 30 public static final int ORDER_BY_LOCATION = 1; 31 32 public static final int ORDER_BY_HOST = 2; 33 34 private int orderBy = ORDER_BY_LABEL; 35 36 private boolean ascending = true; 37 RepositoryComparator(int order, boolean ascending)38 public RepositoryComparator(int order, boolean ascending) { 39 super(); 40 this.orderBy = order; 41 this.ascending = ascending; 42 } 43 RepositoryComparator()44 public RepositoryComparator() { 45 super(); 46 } 47 getOrderBy()48 public int getOrderBy() { 49 return orderBy; 50 } 51 setOrder(int orderBy)52 public void setOrder(int orderBy) { 53 this.orderBy = orderBy; 54 } 55 setAscending(boolean ascending)56 public void setAscending(boolean ascending) { 57 this.ascending = ascending; 58 } 59 isAscending()60 public boolean isAscending() { 61 return ascending; 62 } 63 category(Object element)64 public int category(Object element) { 65 if (element instanceof ICVSRemoteFolder) { 66 if (((ICVSRemoteFolder)element).isDefinedModule()) { 67 return 7; 68 } 69 return 1; 70 } 71 if (element instanceof RemoteModule) { 72 ICVSRemoteResource resource = ((RemoteModule)element).getCVSResource(); 73 if (resource instanceof ICVSRemoteFolder) { 74 ICVSRemoteFolder folder = (ICVSRemoteFolder) resource; 75 if (folder.isDefinedModule()) { 76 return 7; 77 } 78 } 79 return 1; 80 } 81 if (element instanceof ICVSRemoteFile) { 82 return 2; 83 } 84 if (element instanceof CVSTagElement) { 85 CVSTagElement tagElement = (CVSTagElement)element; 86 if (tagElement.getTag().getType() == CVSTag.HEAD) { 87 return 0; 88 } else if (tagElement.getTag().getType() == CVSTag.BRANCH) { 89 return 4; 90 } else if (tagElement.getTag().getType() == CVSTag.VERSION) { 91 return 5; 92 } else if (tagElement.getTag().getType() == CVSTag.DATE){ 93 return 6; 94 }else{ 95 return 7; 96 } 97 } 98 if (element instanceof BranchCategory) { 99 return 4; 100 } 101 if (element instanceof VersionCategory) { 102 return 5; 103 } 104 if (element instanceof DateTagCategory){ 105 return 6; 106 } 107 if (element instanceof PendingUpdateAdapter) { 108 return 10000; 109 } 110 return 0; 111 } 112 compare(Viewer viewer, Object o1, Object o2)113 public int compare(Viewer viewer, Object o1, Object o2) { 114 int cat1 = category(o1); 115 int cat2 = category(o2); 116 if (cat1 != cat2) return cat1 - cat2; 117 118 if (o1 instanceof CVSTagElement && o2 instanceof CVSTagElement) { 119 CVSTag tag1 = ((CVSTagElement)o1).getTag(); 120 CVSTag tag2 = ((CVSTagElement)o2).getTag(); 121 if (tag1.getType() == CVSTag.BRANCH) { 122 return tag1.compareTo(tag2); 123 } else { 124 return -1 * tag1.compareTo(tag2); 125 } 126 } 127 128 // Sort versions in reverse alphabetical order 129 if (o1 instanceof ICVSRemoteFolder && o2 instanceof ICVSRemoteFolder) { 130 ICVSRemoteFolder f1 = (ICVSRemoteFolder)o1; 131 ICVSRemoteFolder f2 = (ICVSRemoteFolder)o2; 132 if (f1.getName().equals(f2.getName())) { 133 return compare(f1, f2); 134 } 135 } 136 137 if (o1 instanceof ICVSRepositoryLocation && o2 instanceof ICVSRepositoryLocation) { 138 return ((ICVSRepositoryLocation)o1).getLocation(false).compareTo(((ICVSRepositoryLocation)o2).getLocation(false)); 139 } 140 141 if (o1 instanceof RepositoryRoot && o2 instanceof RepositoryRoot) { 142 RepositoryRoot rr1 = (RepositoryRoot) o1; 143 RepositoryRoot rr2 = (RepositoryRoot) o2; 144 145 // use repository location strings to compare RepositoryRoots 146 ICVSRepositoryLocation rl1 = rr1.getRoot(); 147 ICVSRepositoryLocation rl2 = rr2.getRoot(); 148 149 int compareResult = 0; 150 switch (orderBy) { 151 case ORDER_BY_HOST: 152 compareResult = rl1.getHost().compareTo(rl2.getHost()); 153 if (compareResult != 0) 154 break; 155 case ORDER_BY_LOCATION: 156 compareResult = rl1.getLocation(false).compareTo( 157 rl2.getLocation(false)); 158 if (compareResult != 0) 159 break; 160 // add other cases here 161 case ORDER_BY_LABEL: 162 // for default order use super.compare (i.e. compare labels) 163 default: 164 compareResult = super.compare(viewer, o1, o2); 165 } 166 return ascending ? compareResult : -compareResult; 167 } 168 169 return super.compare(viewer, o1, o2); 170 } 171 172 /* 173 * Compare to remote folders whose names are the same. 174 */ compare(ICVSRemoteFolder f1, ICVSRemoteFolder f2)175 private int compare(ICVSRemoteFolder f1, ICVSRemoteFolder f2) { 176 CVSTag tag1 = f1.getTag(); 177 CVSTag tag2 = f2.getTag(); 178 if (tag1 == null) return 1; 179 if (tag2 == null) return -1; 180 return tag2.compareTo(tag1); 181 } 182 183 } 184 185