1 11 12 package org.eclipse.ui.internal.keys; 13 14 import java.util.Comparator ; 15 import java.util.Iterator ; 16 import java.util.ResourceBundle ; 17 import java.util.SortedSet ; 18 import java.util.TreeSet ; 19 20 import org.eclipse.ui.internal.util.Util; 21 import org.eclipse.ui.keys.IKeyFormatter; 22 import org.eclipse.ui.keys.Key; 23 import org.eclipse.ui.keys.KeySequence; 24 import org.eclipse.ui.keys.KeyStroke; 25 import org.eclipse.ui.keys.ModifierKey; 26 import org.eclipse.ui.keys.NaturalKey; 27 28 36 public abstract class AbstractKeyFormatter implements IKeyFormatter { 37 38 42 protected final static String KEY_DELIMITER_KEY = "KEY_DELIMITER"; 44 48 protected final static String KEY_STROKE_DELIMITER_KEY = "KEY_STROKE_DELIMITER"; 50 56 private final static ResourceBundle RESOURCE_BUNDLE = ResourceBundle 57 .getBundle(AbstractKeyFormatter.class.getName()); 58 59 64 public String format(Key key) { 65 String name = key.toString(); 66 return Util.translateString(RESOURCE_BUNDLE, name, name, false, false); 67 } 68 69 74 public String format(KeySequence keySequence) { 75 StringBuffer stringBuffer = new StringBuffer (); 76 77 Iterator keyStrokeItr = keySequence.getKeyStrokes().iterator(); 78 while (keyStrokeItr.hasNext()) { 79 stringBuffer.append(format((KeyStroke) keyStrokeItr.next())); 80 81 if (keyStrokeItr.hasNext()) { 82 stringBuffer.append(getKeyStrokeDelimiter()); 83 } 84 } 85 86 return stringBuffer.toString(); 87 } 88 89 94 public String format(KeyStroke keyStroke) { 95 String keyDelimiter = getKeyDelimiter(); 96 97 SortedSet modifierKeys = new TreeSet (getModifierKeyComparator()); 99 modifierKeys.addAll(keyStroke.getModifierKeys()); 100 StringBuffer stringBuffer = new StringBuffer (); 101 Iterator modifierKeyItr = modifierKeys.iterator(); 102 while (modifierKeyItr.hasNext()) { 103 stringBuffer.append(format((ModifierKey) modifierKeyItr.next())); 104 stringBuffer.append(keyDelimiter); 105 } 106 107 NaturalKey naturalKey = keyStroke.getNaturalKey(); 109 if (naturalKey != null) { 110 stringBuffer.append(format(naturalKey)); 111 } 112 113 return stringBuffer.toString(); 114 115 } 116 117 123 protected abstract String getKeyDelimiter(); 124 125 132 protected abstract String getKeyStrokeDelimiter(); 133 134 141 protected abstract Comparator getModifierKeyComparator(); 142 143 } 144 | Popular Tags |