1 11 12 package org.eclipse.ui.internal.keys; 13 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 import java.util.SortedSet ; 20 import java.util.TreeSet ; 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 33 public class CompactKeyFormatter extends NativeKeyFormatter { 34 35 40 public String format(KeySequence keySequence) { 41 StringBuffer stringBuffer = new StringBuffer (); 42 43 List keyStrokes = keySequence.getKeyStrokes(); 44 KeyStroke[] keyStrokeArray = (KeyStroke[]) keyStrokes 45 .toArray(new KeyStroke[keyStrokes.size()]); 46 Set previousModifierKeys = Collections.EMPTY_SET; 47 List naturalKeys = new ArrayList (); 48 for (int i = 0; i < keyStrokeArray.length; i++) { 49 KeyStroke keyStroke = keyStrokeArray[i]; 50 Set currentModifierKeys = keyStroke.getModifierKeys(); 51 52 if (!previousModifierKeys.equals(currentModifierKeys)) { 53 if (i > 0) { 55 stringBuffer.append(formatKeyStrokes(previousModifierKeys, 56 naturalKeys)); 57 stringBuffer.append(getKeyStrokeDelimiter()); 58 } 59 60 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 80 public String formatKeyStrokes(Set modifierKeys, List naturalKeys) { 81 StringBuffer stringBuffer = new StringBuffer (); 82 String keyDelimiter = getKeyDelimiter(); 83 84 SortedSet sortedModifierKeys = new TreeSet (getModifierKeyComparator()); 86 sortedModifierKeys.addAll(modifierKeys); 87 Iterator sortedModifierKeyItr = sortedModifierKeys.iterator(); 88 while (sortedModifierKeyItr.hasNext()) { 89 stringBuffer.append(format((ModifierKey) sortedModifierKeyItr 90 .next())); 91 stringBuffer.append(keyDelimiter); 92 } 93 94 Iterator naturalKeyItr = naturalKeys.iterator(); 96 while (naturalKeyItr.hasNext()) { 97 Object 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 |