| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2007 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 org.eclipse.jface.action.IContributionItem; |
| 15 | import org.eclipse.jface.action.IStatusLineManager; |
| 16 | import org.eclipse.jface.action.StatusLineContributionItem; |
| 17 | import org.eclipse.jface.bindings.keys.KeySequence; |
| 18 | import org.eclipse.ui.IWorkbench; |
| 19 | import org.eclipse.ui.IWorkbenchWindow; |
| 20 | import org.eclipse.ui.internal.WorkbenchWindow; |
| 21 | |
| 22 | /** |
| 23 | * <p> |
| 24 | * The mutable state of the key binding architecture. This is the only piece of |
| 25 | * the key binding architecture that changes (internally). It keeps track of |
| 26 | * what partial key strokes the user has entered. In the case of functional |
| 27 | * groups of key bindings, it allows the user to keep part of the key sequence |
| 28 | * even after a match has been made. Only after releasing all of the modifier |
| 29 | * keys would the sequence reset itself. |
| 30 | * </p> |
| 31 | * <p> |
| 32 | * In the current implementation, a partial reset results in only one key |
| 33 | * stroke being left in the sequence. However, this may change in the future. |
| 34 | * </p> |
| 35 | * |
| 36 | * @since 3.0 |
| 37 | */ |
| 38 | class KeyBindingState { |
| 39 | |
| 40 | /** |
| 41 | * The workbench window associated with this state. The state can only |
| 42 | * exist for one window. When the focus leaves this window then the mode |
| 43 | * must automatically be reset. |
| 44 | */ |
| 45 | private IWorkbenchWindow associatedWindow; |
| 46 | |
| 47 | /** |
| 48 | * This is the current extent of the sequence entered by the user. In an |
| 49 | * application with only single-stroke key bindings, this will also be |
| 50 | * empty. However, in applications with multi-stroke key bindings, this is |
| 51 | * the sequence entered by the user that partially matches another one of |
| 52 | * the application's active key bindings. |
| 53 | */ |
| 54 | private KeySequence currentSequence; |
| 55 | |
| 56 | /** |
| 57 | * The workbench that should be notified of changes to the key binding |
| 58 | * state. This is done by updating one of the contribution items on the |
| 59 | * status line. |
| 60 | */ |
| 61 | private final IWorkbench workbench; |
| 62 | |
| 63 | /** |
| 64 | * Constructs a new instance of <code>KeyBindingState</code> with an |
| 65 | * empty key sequence, set to reset fully. |
| 66 | * |
| 67 | * @param workbenchToNotify |
| 68 | * The workbench that this state should keep advised of changes |
| 69 | * to the key binding state; must not be <code>null</code>. |
| 70 | */ |
| 71 | KeyBindingState(IWorkbench workbenchToNotify) { |
| 72 | currentSequence = KeySequence.getInstance(); |
| 73 | workbench = workbenchToNotify; |
| 74 | associatedWindow = workbench.getActiveWorkbenchWindow(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * An accessor for the workbench window associated with this state. This |
| 79 | * should never be <code>null</code>, as the setting follows the last |
| 80 | * workbench window to have focus. |
| 81 | * |
| 82 | * @return The workbench window to which the key binding architecture is |
| 83 | * currently attached; should never be <code>null</code>. |
| 84 | */ |
| 85 | IWorkbenchWindow getAssociatedWindow() { |
| 86 | return associatedWindow; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * An accessor for the current key sequence waiting for completion. |
| 91 | * |
| 92 | * @return The current incomplete key sequence; never <code>null</code>, |
| 93 | * but may be empty. |
| 94 | */ |
| 95 | KeySequence getCurrentSequence() { |
| 96 | return currentSequence; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gets the status line contribution item which the key binding |
| 101 | * architecture uses to keep the user up-to-date as to the current state. |
| 102 | * |
| 103 | * @return The status line contribution item, if any; <code>null</code>, |
| 104 | * if none. |
| 105 | */ |
| 106 | StatusLineContributionItem getStatusLine() { |
| 107 | if (associatedWindow instanceof WorkbenchWindow) { |
| 108 | WorkbenchWindow window = (WorkbenchWindow) associatedWindow; |
| 109 | IStatusLineManager statusLine = window.getStatusLineManager(); |
| 110 | // TODO implicit dependency on IDE's action builder |
| 111 | // @issue implicit dependency on IDE's action builder |
| 112 | if (statusLine != null) { // this can be null if we're exiting |
| 113 | IContributionItem item = statusLine |
| 114 | .find("ModeContributionItem"); //$NON-NLS-1$ |
| 115 | if (item instanceof StatusLineContributionItem) { |
| 116 | return ((StatusLineContributionItem) item); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * <p> |
| 126 | * Resets the state based on the current properties. If the state is to |
| 127 | * collapse fully or if there are no key strokes, then it sets the state to |
| 128 | * have an empty key sequence. Otherwise, it leaves the first key stroke in |
| 129 | * the sequence. |
| 130 | * </p> |
| 131 | * <p> |
| 132 | * The workbench's status lines are updated, if appropriate. |
| 133 | * </p> |
| 134 | */ |
| 135 | void reset() { |
| 136 | currentSequence = KeySequence.getInstance(); |
| 137 | updateStatusLines(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * A mutator for the workbench window to which this state is associated. |
| 142 | * |
| 143 | * @param window |
| 144 | * The workbench window to associated; should never be <code>null</code>. |
| 145 | */ |
| 146 | void setAssociatedWindow(IWorkbenchWindow window) { |
| 147 | associatedWindow = window; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * A mutator for the partial sequence entered by the user. |
| 152 | * |
| 153 | * @param sequence |
| 154 | * The current key sequence; should not be <code>null</code>, |
| 155 | * but may be empty. |
| 156 | */ |
| 157 | void setCurrentSequence(KeySequence sequence) { |
| 158 | currentSequence = sequence; |
| 159 | updateStatusLines(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Updates the text of the status line of the associated shell with the |
| 164 | * current sequence. |
| 165 | */ |
| 166 | private void updateStatusLines() { |
| 167 | StatusLineContributionItem statusLine = getStatusLine(); |
| 168 | if (statusLine != null) { |
| 169 | statusLine.setText(getCurrentSequence().format()); |
| 170 | } |
| 171 | } |
| 172 | } |