KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.SortedSet JavaDoc;
20 import java.util.TreeSet JavaDoc;
21
22 import org.eclipse.ui.keys.KeySequence;
23 import org.eclipse.ui.keys.KeyStroke;
24 import org.eclipse.ui.keys.ModifierKey;
25 import org.eclipse.ui.keys.NaturalKey;
26
27 /**
28  * A key formatter providing a special compact format for displaying key
29  * bindings.
30  *
31  * @since 3.0
32  */

33 public class CompactKeyFormatter extends NativeKeyFormatter {
34
35     /*
36      * (non-Javadoc)
37      *
38      * @see org.eclipse.ui.keys.KeyFormatter#format(org.eclipse.ui.keys.KeySequence)
39      */

40     public String JavaDoc format(KeySequence keySequence) {
41         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
42
43         List JavaDoc keyStrokes = keySequence.getKeyStrokes();
44         KeyStroke[] keyStrokeArray = (KeyStroke[]) keyStrokes
45                 .toArray(new KeyStroke[keyStrokes.size()]);
46         Set JavaDoc previousModifierKeys = Collections.EMPTY_SET;
47         List JavaDoc naturalKeys = new ArrayList JavaDoc();
48         for (int i = 0; i < keyStrokeArray.length; i++) {
49             KeyStroke keyStroke = keyStrokeArray[i];
50             Set JavaDoc currentModifierKeys = keyStroke.getModifierKeys();
51
52             if (!previousModifierKeys.equals(currentModifierKeys)) {
53                 // End the old sequence fragment.
54
if (i > 0) {
55                     stringBuffer.append(formatKeyStrokes(previousModifierKeys,
56                             naturalKeys));
57                     stringBuffer.append(getKeyStrokeDelimiter());
58                 }
59
60                 // Start a new one.
61
previousModifierKeys = currentModifierKeys;
62                 naturalKeys.clear();
63
64             }
65
66             naturalKeys.add(keyStroke.getNaturalKey());
67         }
68
69         stringBuffer
70                 .append(formatKeyStrokes(previousModifierKeys, naturalKeys));
71
72         return stringBuffer.toString();
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see org.eclipse.ui.keys.KeyFormatter#formatKeyStroke(org.eclipse.ui.keys.KeyStroke)
79      */

80     public String JavaDoc formatKeyStrokes(Set JavaDoc modifierKeys, List JavaDoc naturalKeys) {
81         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
82         String JavaDoc keyDelimiter = getKeyDelimiter();
83
84         // Format the modifier keys, in sorted order.
85
SortedSet JavaDoc sortedModifierKeys = new TreeSet JavaDoc(getModifierKeyComparator());
86         sortedModifierKeys.addAll(modifierKeys);
87         Iterator JavaDoc sortedModifierKeyItr = sortedModifierKeys.iterator();
88         while (sortedModifierKeyItr.hasNext()) {
89             stringBuffer.append(format((ModifierKey) sortedModifierKeyItr
90                     .next()));
91             stringBuffer.append(keyDelimiter);
92         }
93
94         // Format the natural key, if any.
95
Iterator JavaDoc naturalKeyItr = naturalKeys.iterator();
96         while (naturalKeyItr.hasNext()) {
97             Object JavaDoc naturalKey = naturalKeyItr.next();
98             if (naturalKey instanceof NaturalKey) {
99                 stringBuffer.append(format((NaturalKey) naturalKey));
100                 if (naturalKeyItr.hasNext()) {
101                     stringBuffer.append(keyDelimiter);
102                 }
103             }
104         }
105
106         return stringBuffer.toString();
107
108     }
109 }
110
Popular Tags