1 56 package org.objectstyle.cayenne.modeler.util; 57 58 import java.util.Comparator ; 59 60 import org.objectstyle.cayenne.access.DataNode; 61 import org.objectstyle.cayenne.map.Attribute; 62 import org.objectstyle.cayenne.map.DataMap; 63 import org.objectstyle.cayenne.map.DbEntity; 64 import org.objectstyle.cayenne.map.Entity; 65 import org.objectstyle.cayenne.map.ObjEntity; 66 import org.objectstyle.cayenne.map.Procedure; 67 import org.objectstyle.cayenne.map.Relationship; 68 import org.objectstyle.cayenne.query.Query; 69 70 76 public class Comparators { 77 78 private static final Comparator dataDomainChildrenComparator = new DataDomainChildrenComparator(); 79 80 private static final Comparator dataMapChildrenComparator = new DataMapChildrenComparator(); 81 82 private static final Comparator entityChildrenComparator = new EntityChildrenComparator(); 83 84 private static final Comparator namedObjectComparator = new NamedObjectComparator(); 85 86 91 public static Comparator getDataDomainChildrenComparator() { 92 return dataDomainChildrenComparator; 93 } 94 95 101 public static Comparator getDataMapChildrenComparator() { 102 return dataMapChildrenComparator; 103 } 104 105 111 public static Comparator getEntityChildrenComparator() { 112 return entityChildrenComparator; 113 } 114 115 118 public static Comparator getNamedObjectComparator() { 119 return namedObjectComparator; 120 } 121 122 static class NamedObjectComparator implements Comparator { 123 124 public int compare(Object o1, Object o2) { 125 126 String name1 = ModelerUtil.getObjectName(o1); 127 String name2 = ModelerUtil.getObjectName(o2); 128 129 if (name1 == null) { 130 return (name2 != null) ? -1 : 0; 131 } 132 else if (name2 == null) { 133 return 1; 134 } 135 else { 136 return name1.compareTo(name2); 137 } 138 } 139 } 140 141 final static class DataDomainChildrenComparator extends NamedObjectComparator { 142 143 public int compare(Object o1, Object o2) { 144 int delta = getClassWeight(o1) - getClassWeight(o2); 145 if (delta != 0) { 146 return delta; 147 } 148 else { 149 return super.compare(o1, o2); 150 } 151 } 152 153 private static int getClassWeight(Object o) { 154 if (o instanceof DataMap) { 155 return 1; 156 } 157 else if (o instanceof DataNode) { 158 return 2; 159 } 160 else { 161 return Integer.MAX_VALUE; 163 } 164 } 165 } 166 167 final static class DataMapChildrenComparator extends NamedObjectComparator { 168 169 public int compare(Object o1, Object o2) { 170 int delta = getClassWeight(o1) - getClassWeight(o2); 171 if (delta != 0) { 172 return delta; 173 } 174 else { 175 return super.compare(o1, o2); 176 } 177 } 178 179 private static int getClassWeight(Object o) { 180 if (o instanceof DataMap) { 181 return 1; 182 } 183 else if (o instanceof ObjEntity) { 184 return 2; 185 } 186 else if (o instanceof DbEntity) { 187 return 3; 188 } 189 else if (o instanceof Procedure) { 190 return 4; 191 } 192 else if (o instanceof Query) { 193 return 5; 194 } 195 else { 196 return Integer.MAX_VALUE; 198 } 199 } 200 } 201 202 final static class EntityChildrenComparator extends NamedObjectComparator { 203 204 public int compare(Object o1, Object o2) { 205 int delta = getClassWeight(o1) - getClassWeight(o2); 206 if (delta != 0) { 207 return delta; 208 } 209 else { 210 return super.compare(o1, o2); 211 } 212 } 213 214 private static int getClassWeight(Object o) { 215 if (o instanceof Entity) { 216 return 1; 217 } 218 else if (o instanceof Attribute) { 219 return 2; 220 } 221 else if (o instanceof Relationship) { 222 return 3; 223 } 224 else { 225 return Integer.MAX_VALUE; 227 } 228 } 229 } 230 } | Popular Tags |