001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.keys;
011:
012: import java.util.Comparator;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.ui.keys.ModifierKey;
016:
017: /**
018: * A comparator that sorts the modifier keys based on the native environment.
019: * Currently, this is only the windowing toolkit, but in the future it might
020: * expand to include the window manager.
021: *
022: * @since 3.0
023: */
024: class NativeModifierKeyComparator implements Comparator {
025:
026: /**
027: * The sort order value to use when the modifier key is not recognized.
028: */
029: private final static int UNKNOWN_KEY = Integer.MAX_VALUE;
030:
031: /*
032: * (non-Javadoc)
033: *
034: * @see java.lang.Comparable#compareTo(java.lang.Object)
035: */
036: public int compare(Object left, Object right) {
037: ModifierKey modifierKeyLeft = (ModifierKey) left;
038: ModifierKey modifierKeyRight = (ModifierKey) right;
039: int modifierKeyLeftRank = rank(modifierKeyLeft);
040: int modifierKeyRightRank = rank(modifierKeyRight);
041:
042: if (modifierKeyLeftRank != modifierKeyRightRank) {
043: return modifierKeyLeftRank - modifierKeyRightRank;
044: } else {
045: return modifierKeyLeft.compareTo(modifierKeyRight);
046: }
047: }
048:
049: /**
050: * Calculates a rank for a given modifier key.
051: *
052: * @param modifierKey
053: * The modifier key to rank; may be <code>null</code>.
054: * @return The rank of this modifier key. This is a non-negative number
055: * where a lower number suggests a higher rank.
056: */
057: private int rank(ModifierKey modifierKey) {
058: String platform = SWT.getPlatform();
059:
060: if ("win32".equals(platform)) { //$NON-NLS-1$
061: return rankWindows(modifierKey);
062: }
063:
064: if ("gtk".equals(platform)) { //$NON-NLS-1$
065: // TODO Do a look-up on window manager.
066: return rankGNOME(modifierKey);
067: }
068:
069: if ("carbon".equals(platform)) { //$NON-NLS-1$
070: return rankMacOSX(modifierKey);
071: }
072:
073: if ("motif".equals(platform)) { //$NON-NLS-1$
074: // TODO Do a look-up on window manager.
075: return rankGNOME(modifierKey);
076: }
077:
078: return UNKNOWN_KEY;
079: }
080:
081: /**
082: * Provides a ranking for the modifier key based on the modifier key
083: * ordering used in the GNOME window manager.
084: *
085: * @param modifierKey
086: * The modifier key to rank; may be <code>null</code>.
087: * @return The rank of this modifier key. This is a non-negative number
088: * where a lower number suggests a higher rank.
089: */
090: private final int rankGNOME(ModifierKey modifierKey) {
091: if (ModifierKey.SHIFT.equals(modifierKey)) {
092: return 0;
093: }
094:
095: if (ModifierKey.CTRL.equals(modifierKey)) {
096: return 1;
097: }
098:
099: if (ModifierKey.ALT.equals(modifierKey)) {
100: return 2;
101: }
102:
103: return UNKNOWN_KEY;
104:
105: }
106:
107: /**
108: * Provides a ranking for the modifier key based on the modifier key
109: * ordering used in the KDE window manager.
110: *
111: * @param modifierKey
112: * The modifier key to rank; may be <code>null</code>.
113: * @return The rank of this modifier key. This is a non-negative number
114: * where a lower number suggests a higher rank.
115: */
116: // private final int rankKDE(ModifierKey modifierKey) {
117: // if (ModifierKey.ALT.equals(modifierKey)) {
118: // return 0;
119: // }
120: //
121: // if (ModifierKey.CTRL.equals(modifierKey)) {
122: // return 1;
123: // }
124: //
125: // if (ModifierKey.SHIFT.equals(modifierKey)) {
126: // return 2;
127: // }
128: //
129: // return UNKNOWN_KEY;
130: // }
131: /**
132: * Provides a ranking for the modifier key based on the modifier key
133: * ordering used in the MacOS X operating system.
134: *
135: * @param modifierKey
136: * The modifier key to rank; may be <code>null</code>.
137: * @return The rank of this modifier key. This is a non-negative number
138: * where a lower number suggests a higher rank.
139: */
140: private final int rankMacOSX(ModifierKey modifierKey) {
141: if (ModifierKey.SHIFT.equals(modifierKey)) {
142: return 0;
143: }
144:
145: if (ModifierKey.CTRL.equals(modifierKey)) {
146: return 1;
147: }
148:
149: if (ModifierKey.ALT.equals(modifierKey)) {
150: return 2;
151: }
152:
153: if (ModifierKey.COMMAND.equals(modifierKey)) {
154: return 3;
155: }
156:
157: return UNKNOWN_KEY;
158: }
159:
160: /**
161: * Provides a ranking for the modifier key based on the modifier key
162: * ordering used in the Windows operating system.
163: *
164: * @param modifierKey
165: * The modifier key to rank; may be <code>null</code>.
166: * @return The rank of this modifier key. This is a non-negative number
167: * where a lower number suggests a higher rank.
168: */
169: private final int rankWindows(ModifierKey modifierKey) {
170: if (ModifierKey.CTRL.equals(modifierKey)) {
171: return 0;
172: }
173:
174: if (ModifierKey.ALT.equals(modifierKey)) {
175: return 1;
176: }
177:
178: if (ModifierKey.SHIFT.equals(modifierKey)) {
179: return 2;
180: }
181:
182: return UNKNOWN_KEY;
183: }
184: }
|