001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.keys;
011:
012: import java.util.ArrayList;
013: import java.util.Collections;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Set;
017: import java.util.SortedSet;
018: import java.util.TreeSet;
019:
020: import org.eclipse.ui.keys.KeySequence;
021: import org.eclipse.ui.keys.KeyStroke;
022: import org.eclipse.ui.keys.ModifierKey;
023: import org.eclipse.ui.keys.NaturalKey;
024:
025: /**
026: * A key formatter providing a special compact format for displaying key
027: * bindings.
028: *
029: * @since 3.0
030: */
031: public class CompactKeyFormatter extends NativeKeyFormatter {
032:
033: /*
034: * (non-Javadoc)
035: *
036: * @see org.eclipse.ui.keys.KeyFormatter#format(org.eclipse.ui.keys.KeySequence)
037: */
038: public String format(KeySequence keySequence) {
039: StringBuffer stringBuffer = new StringBuffer();
040:
041: List keyStrokes = keySequence.getKeyStrokes();
042: KeyStroke[] keyStrokeArray = (KeyStroke[]) keyStrokes
043: .toArray(new KeyStroke[keyStrokes.size()]);
044: Set previousModifierKeys = Collections.EMPTY_SET;
045: List naturalKeys = new ArrayList();
046: for (int i = 0; i < keyStrokeArray.length; i++) {
047: KeyStroke keyStroke = keyStrokeArray[i];
048: Set currentModifierKeys = keyStroke.getModifierKeys();
049:
050: if (!previousModifierKeys.equals(currentModifierKeys)) {
051: // End the old sequence fragment.
052: if (i > 0) {
053: stringBuffer.append(formatKeyStrokes(
054: previousModifierKeys, naturalKeys));
055: stringBuffer.append(getKeyStrokeDelimiter());
056: }
057:
058: // Start a new one.
059: previousModifierKeys = currentModifierKeys;
060: naturalKeys.clear();
061:
062: }
063:
064: naturalKeys.add(keyStroke.getNaturalKey());
065: }
066:
067: stringBuffer.append(formatKeyStrokes(previousModifierKeys,
068: naturalKeys));
069:
070: return stringBuffer.toString();
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see org.eclipse.ui.keys.KeyFormatter#formatKeyStroke(org.eclipse.ui.keys.KeyStroke)
077: */
078: public String formatKeyStrokes(Set modifierKeys, List naturalKeys) {
079: StringBuffer stringBuffer = new StringBuffer();
080: String keyDelimiter = getKeyDelimiter();
081:
082: // Format the modifier keys, in sorted order.
083: SortedSet sortedModifierKeys = new TreeSet(
084: getModifierKeyComparator());
085: sortedModifierKeys.addAll(modifierKeys);
086: Iterator sortedModifierKeyItr = sortedModifierKeys.iterator();
087: while (sortedModifierKeyItr.hasNext()) {
088: stringBuffer
089: .append(format((ModifierKey) sortedModifierKeyItr
090: .next()));
091: stringBuffer.append(keyDelimiter);
092: }
093:
094: // Format the natural key, if any.
095: Iterator naturalKeyItr = naturalKeys.iterator();
096: while (naturalKeyItr.hasNext()) {
097: Object naturalKey = naturalKeyItr.next();
098: if (naturalKey instanceof NaturalKey) {
099: stringBuffer.append(format((NaturalKey) naturalKey));
100: if (naturalKeyItr.hasNext()) {
101: stringBuffer.append(keyDelimiter);
102: }
103: }
104: }
105:
106: return stringBuffer.toString();
107:
108: }
109: }
|