1 11 12 package org.eclipse.jface.bindings.keys.formatting; 13 14 import java.util.Enumeration ; 15 import java.util.HashSet ; 16 import java.util.ResourceBundle ; 17 import java.util.Set ; 18 19 import org.eclipse.jface.bindings.keys.IKeyLookup; 20 import org.eclipse.jface.bindings.keys.KeyLookupFactory; 21 import org.eclipse.jface.bindings.keys.KeySequence; 22 import org.eclipse.jface.bindings.keys.KeyStroke; 23 import org.eclipse.jface.util.Util; 24 25 35 public abstract class AbstractKeyFormatter implements IKeyFormatter { 36 37 41 protected static final String KEY_DELIMITER_KEY = "KEY_DELIMITER"; 43 47 protected static final String KEY_STROKE_DELIMITER_KEY = "KEY_STROKE_DELIMITER"; 49 53 protected static final int[] NO_MODIFIER_KEYS = new int[0]; 54 55 61 private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle 62 .getBundle(AbstractKeyFormatter.class.getName()); 63 64 68 private static final Set resourceBundleKeys = new HashSet (); 69 70 static { 71 final Enumeration keyEnumeration = RESOURCE_BUNDLE.getKeys(); 72 while (keyEnumeration.hasMoreElements()) { 73 final Object element = keyEnumeration.nextElement(); 74 resourceBundleKeys.add(element); 75 } 76 } 77 78 83 public String format(final int key) { 84 final IKeyLookup lookup = KeyLookupFactory.getDefault(); 85 final String name = lookup.formalNameLookup(key); 86 87 if (resourceBundleKeys.contains(name)) { 88 return Util.translateString(RESOURCE_BUNDLE, name, name); 89 } 90 91 return name; 92 } 93 94 99 public String format(KeySequence keySequence) { 100 StringBuffer stringBuffer = new StringBuffer (); 101 102 final KeyStroke[] keyStrokes = keySequence.getKeyStrokes(); 103 final int keyStrokesLength = keyStrokes.length; 104 for (int i = 0; i < keyStrokesLength; i++) { 105 stringBuffer.append(format(keyStrokes[i])); 106 107 if (i + 1 < keyStrokesLength) { 108 stringBuffer.append(getKeyStrokeDelimiter()); 109 } 110 } 111 112 return stringBuffer.toString(); 113 } 114 115 120 public String format(final KeyStroke keyStroke) { 121 final String keyDelimiter = getKeyDelimiter(); 122 123 final int modifierKeys = keyStroke.getModifierKeys(); 125 final int[] sortedModifierKeys = sortModifierKeys(modifierKeys); 126 final StringBuffer stringBuffer = new StringBuffer (); 127 if (sortedModifierKeys != null) { 128 for (int i = 0; i < sortedModifierKeys.length; i++) { 129 final int modifierKey = sortedModifierKeys[i]; 130 if (modifierKey != KeyStroke.NO_KEY) { 131 stringBuffer.append(format(modifierKey)); 132 stringBuffer.append(keyDelimiter); 133 } 134 } 135 } 136 137 final int naturalKey = keyStroke.getNaturalKey(); 139 if (naturalKey != 0) { 140 stringBuffer.append(format(naturalKey)); 141 } 142 143 return stringBuffer.toString(); 144 145 } 146 147 154 protected abstract String getKeyDelimiter(); 155 156 164 protected abstract String getKeyStrokeDelimiter(); 165 166 177 protected abstract int[] sortModifierKeys(final int modifierKeys); 178 } 179 | Popular Tags |