KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > keys > NativeModifierKeyComparator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.keys;
13
14 import java.util.Comparator JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.ui.keys.ModifierKey;
18
19 /**
20  * A comparator that sorts the modifier keys based on the native environment.
21  * Currently, this is only the windowing toolkit, but in the future it might
22  * expand to include the window manager.
23  *
24  * @since 3.0
25  */

26 class NativeModifierKeyComparator implements Comparator JavaDoc {
27
28     /**
29      * The sort order value to use when the modifier key is not recognized.
30      */

31     private final static int UNKNOWN_KEY = Integer.MAX_VALUE;
32
33     /*
34      * (non-Javadoc)
35      *
36      * @see java.lang.Comparable#compareTo(java.lang.Object)
37      */

38     public int compare(Object JavaDoc left, Object JavaDoc 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     /**
52      * Calculates a rank for a given modifier key.
53      *
54      * @param modifierKey
55      * The modifier key to rank; may be <code>null</code>.
56      * @return The rank of this modifier key. This is a non-negative number
57      * where a lower number suggests a higher rank.
58      */

59     private int rank(ModifierKey modifierKey) {
60         String JavaDoc platform = SWT.getPlatform();
61
62         if ("win32".equals(platform)) { //$NON-NLS-1$
63
return rankWindows(modifierKey);
64         }
65
66         if ("gtk".equals(platform)) { //$NON-NLS-1$
67
// TODO Do a look-up on window manager.
68
return rankGNOME(modifierKey);
69         }
70
71         if ("carbon".equals(platform)) { //$NON-NLS-1$
72
return rankMacOSX(modifierKey);
73         }
74
75         if ("motif".equals(platform)) { //$NON-NLS-1$
76
// TODO Do a look-up on window manager.
77
return rankGNOME(modifierKey);
78         }
79
80         return UNKNOWN_KEY;
81     }
82
83     /**
84      * Provides a ranking for the modifier key based on the modifier key
85      * ordering used in the GNOME window manager.
86      *
87      * @param modifierKey
88      * The modifier key to rank; may be <code>null</code>.
89      * @return The rank of this modifier key. This is a non-negative number
90      * where a lower number suggests a higher rank.
91      */

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     /**
110      * Provides a ranking for the modifier key based on the modifier key
111      * ordering used in the KDE window manager.
112      *
113      * @param modifierKey
114      * The modifier key to rank; may be <code>null</code>.
115      * @return The rank of this modifier key. This is a non-negative number
116      * where a lower number suggests a higher rank.
117      */

118 // private final int rankKDE(ModifierKey modifierKey) {
119
// if (ModifierKey.ALT.equals(modifierKey)) {
120
// return 0;
121
// }
122
//
123
// if (ModifierKey.CTRL.equals(modifierKey)) {
124
// return 1;
125
// }
126
//
127
// if (ModifierKey.SHIFT.equals(modifierKey)) {
128
// return 2;
129
// }
130
//
131
// return UNKNOWN_KEY;
132
// }
133

134     /**
135      * Provides a ranking for the modifier key based on the modifier key
136      * ordering used in the MacOS X operating system.
137      *
138      * @param modifierKey
139      * The modifier key to rank; may be <code>null</code>.
140      * @return The rank of this modifier key. This is a non-negative number
141      * where a lower number suggests a higher rank.
142      */

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     /**
164      * Provides a ranking for the modifier key based on the modifier key
165      * ordering used in the Windows operating system.
166      *
167      * @param modifierKey
168      * The modifier key to rank; may be <code>null</code>.
169      * @return The rank of this modifier key. This is a non-negative number
170      * where a lower number suggests a higher rank.
171      */

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