1 11 12 package org.eclipse.ui.internal.keys; 13 14 import java.util.Comparator ; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.ui.keys.ModifierKey; 18 19 26 class NativeModifierKeyComparator implements Comparator { 27 28 31 private final static int UNKNOWN_KEY = Integer.MAX_VALUE; 32 33 38 public int compare(Object left, Object right) { 39 ModifierKey modifierKeyLeft = (ModifierKey) left; 40 ModifierKey modifierKeyRight = (ModifierKey) right; 41 int modifierKeyLeftRank = rank(modifierKeyLeft); 42 int modifierKeyRightRank = rank(modifierKeyRight); 43 44 if (modifierKeyLeftRank != modifierKeyRightRank) { 45 return modifierKeyLeftRank - modifierKeyRightRank; 46 } else { 47 return modifierKeyLeft.compareTo(modifierKeyRight); 48 } 49 } 50 51 59 private int rank(ModifierKey modifierKey) { 60 String platform = SWT.getPlatform(); 61 62 if ("win32".equals(platform)) { return rankWindows(modifierKey); 64 } 65 66 if ("gtk".equals(platform)) { return rankGNOME(modifierKey); 69 } 70 71 if ("carbon".equals(platform)) { return rankMacOSX(modifierKey); 73 } 74 75 if ("motif".equals(platform)) { return rankGNOME(modifierKey); 78 } 79 80 return UNKNOWN_KEY; 81 } 82 83 92 private final int rankGNOME(ModifierKey modifierKey) { 93 if (ModifierKey.SHIFT.equals(modifierKey)) { 94 return 0; 95 } 96 97 if (ModifierKey.CTRL.equals(modifierKey)) { 98 return 1; 99 } 100 101 if (ModifierKey.ALT.equals(modifierKey)) { 102 return 2; 103 } 104 105 return UNKNOWN_KEY; 106 107 } 108 109 118 134 143 private final int rankMacOSX(ModifierKey modifierKey) { 144 if (ModifierKey.SHIFT.equals(modifierKey)) { 145 return 0; 146 } 147 148 if (ModifierKey.CTRL.equals(modifierKey)) { 149 return 1; 150 } 151 152 if (ModifierKey.ALT.equals(modifierKey)) { 153 return 2; 154 } 155 156 if (ModifierKey.COMMAND.equals(modifierKey)) { 157 return 3; 158 } 159 160 return UNKNOWN_KEY; 161 } 162 163 172 private final int rankWindows(ModifierKey modifierKey) { 173 if (ModifierKey.CTRL.equals(modifierKey)) { 174 return 0; 175 } 176 177 if (ModifierKey.ALT.equals(modifierKey)) { 178 return 1; 179 } 180 181 if (ModifierKey.SHIFT.equals(modifierKey)) { 182 return 2; 183 } 184 185 return UNKNOWN_KEY; 186 } 187 } 188 | Popular Tags |