KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > bindings > keys > formatting > EmacsKeyFormatter


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.jface.bindings.keys.formatting;
13
14 import java.util.ResourceBundle JavaDoc;
15
16 import org.eclipse.jface.bindings.keys.IKeyLookup;
17 import org.eclipse.jface.bindings.keys.KeyLookupFactory;
18 import org.eclipse.jface.bindings.keys.KeySequence;
19 import org.eclipse.jface.bindings.keys.KeyStroke;
20 import org.eclipse.jface.util.Util;
21
22 /**
23  * <p>
24  * A key formatter providing the Emacs-style accelerators using single letters
25  * to represent the modifier keys.
26  * </p>
27  *
28  * @since 3.1
29  */

30 public final class EmacsKeyFormatter extends AbstractKeyFormatter {
31
32     /**
33      * The resource bundle used by <code>format()</code> to translate formal
34      * string representations by locale.
35      */

36     private final static ResourceBundle JavaDoc RESOURCE_BUNDLE = ResourceBundle
37             .getBundle(EmacsKeyFormatter.class.getName());
38
39     /**
40      * Formats an individual key into a human readable format. This converts the
41      * key into a format similar to Xemacs.
42      *
43      * @param key
44      * The key to format; must not be <code>null</code>.
45      * @return The key formatted as a string; should not be <code>null</code>.
46      */

47     public String JavaDoc format(final int key) {
48         final IKeyLookup lookup = KeyLookupFactory.getDefault();
49         if (lookup.isModifierKey(key)) {
50             String JavaDoc formattedName = Util.translateString(RESOURCE_BUNDLE, lookup
51                     .formalNameLookup(key), null);
52             if (formattedName != null) {
53                 return formattedName;
54             }
55         }
56
57         return super.format(key).toLowerCase();
58     }
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see org.eclipse.jface.bindings.keys.AbstractKeyFormatter#getKeyDelimiter()
64      */

65     protected String JavaDoc getKeyDelimiter() {
66         return Util.translateString(RESOURCE_BUNDLE, KEY_DELIMITER_KEY,
67                 KeyStroke.KEY_DELIMITER);
68     }
69
70     /*
71      * (non-Javadoc)
72      *
73      * @see org.eclipse.jface.bindings.keys.AbstractKeyFormatter#getKeyStrokeDelimiter()
74      */

75     protected String JavaDoc getKeyStrokeDelimiter() {
76         return Util.translateString(RESOURCE_BUNDLE, KEY_STROKE_DELIMITER_KEY,
77                 KeySequence.KEY_STROKE_DELIMITER);
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see org.eclipse.jface.bindings.keys.AbstractKeyFormatter#sortModifierKeys(int)
84      */

85     protected int[] sortModifierKeys(int modifierKeys) {
86         final IKeyLookup lookup = KeyLookupFactory.getDefault();
87         final int[] sortedKeys = new int[4];
88         int index = 0;
89
90         if ((modifierKeys & lookup.getAlt()) != 0) {
91             sortedKeys[index++] = lookup.getAlt();
92         }
93         if ((modifierKeys & lookup.getCommand()) != 0) {
94             sortedKeys[index++] = lookup.getCommand();
95         }
96         if ((modifierKeys & lookup.getCtrl()) != 0) {
97             sortedKeys[index++] = lookup.getCtrl();
98         }
99         if ((modifierKeys & lookup.getShift()) != 0) {
100             sortedKeys[index++] = lookup.getShift();
101         }
102
103         return sortedKeys;
104     }
105 }
106
Popular Tags