001: /*******************************************************************************
002: * Copyright (c) 2004, 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.jface.bindings;
011:
012: import java.util.Collection;
013: import java.util.Map;
014:
015: import org.eclipse.core.commands.ParameterizedCommand;
016: import org.eclipse.core.commands.common.AbstractBitSetEvent;
017: import org.eclipse.jface.util.Util;
018:
019: /**
020: * An instance of this class describes changes to an instance of
021: * <code>BindingManager</code>.
022: * <p>
023: * This class is not intended to be extended by clients.
024: * </p>
025: *
026: * @since 3.1
027: * @see IBindingManagerListener#bindingManagerChanged(BindingManagerEvent)
028: */
029: public final class BindingManagerEvent extends AbstractBitSetEvent {
030:
031: /**
032: * The bit used to represent whether the map of active bindings has changed.
033: */
034: private static final int CHANGED_ACTIVE_BINDINGS = 1;
035:
036: /**
037: * The bit used to represent whether the active scheme has changed.
038: */
039: private static final int CHANGED_ACTIVE_SCHEME = 1 << 1;
040:
041: /**
042: * The bit used to represent whether the active locale has changed.
043: */
044: private static final int CHANGED_LOCALE = 1 << 2;
045:
046: /**
047: * The bit used to represent whether the active platform has changed.
048: */
049: private static final int CHANGED_PLATFORM = 1 << 3;
050:
051: /**
052: * The bit used to represent whether the scheme's defined state has changed.
053: */
054: private static final int CHANGED_SCHEME_DEFINED = 1 << 4;
055:
056: /**
057: * The binding manager that has changed; this value is never
058: * <code>null</code>.
059: */
060: private final BindingManager manager;
061:
062: /**
063: * The map of triggers (<code>Collection</code> of
064: * <code>TriggerSequence</code>) by parameterized command (<code>ParameterizedCommand</code>)
065: * before the change occurred. This map may be empty and it may be
066: * <code>null</code>.
067: */
068: private final Map previousTriggersByParameterizedCommand;
069:
070: /**
071: * The scheme that became defined or undefined. This value may be
072: * <code>null</code> if no scheme changed its defined state.
073: */
074: private final Scheme scheme;
075:
076: /**
077: * Creates a new instance of this class.
078: *
079: * @param manager
080: * the instance of the binding manager that changed; must not be
081: * <code>null</code>.
082: * @param activeBindingsChanged
083: * Whether the active bindings have changed.
084: * @param previousTriggersByParameterizedCommand
085: * The map of triggers (<code>TriggerSequence</code>) by
086: * fully-parameterized command (<code>ParameterizedCommand</code>)
087: * before the change occured. This map may be <code>null</code>
088: * or empty.
089: * @param activeSchemeChanged
090: * true, iff the active scheme changed.
091: * @param scheme
092: * The scheme that became defined or undefined; <code>null</code>
093: * if no scheme changed state.
094: * @param schemeDefined
095: * <code>true</code> if the given scheme became defined;
096: * <code>false</code> otherwise.
097: * @param localeChanged
098: * <code>true</code> iff the active locale changed
099: * @param platformChanged
100: * <code>true</code> iff the active platform changed
101: */
102: public BindingManagerEvent(final BindingManager manager,
103: final boolean activeBindingsChanged,
104: final Map previousTriggersByParameterizedCommand,
105: final boolean activeSchemeChanged, final Scheme scheme,
106: final boolean schemeDefined, final boolean localeChanged,
107: final boolean platformChanged) {
108: if (manager == null) {
109: throw new NullPointerException(
110: "A binding manager event needs a binding manager"); //$NON-NLS-1$
111: }
112: this .manager = manager;
113:
114: if (schemeDefined && (scheme == null)) {
115: throw new NullPointerException(
116: "If a scheme changed defined state, then there should be a scheme identifier"); //$NON-NLS-1$
117: }
118: this .scheme = scheme;
119:
120: this .previousTriggersByParameterizedCommand = previousTriggersByParameterizedCommand;
121:
122: if (activeBindingsChanged) {
123: changedValues |= CHANGED_ACTIVE_BINDINGS;
124: }
125: if (activeSchemeChanged) {
126: changedValues |= CHANGED_ACTIVE_SCHEME;
127: }
128: if (localeChanged) {
129: changedValues |= CHANGED_LOCALE;
130: }
131: if (platformChanged) {
132: changedValues |= CHANGED_PLATFORM;
133: }
134: if (schemeDefined) {
135: changedValues |= CHANGED_SCHEME_DEFINED;
136: }
137: }
138:
139: /**
140: * Returns the instance of the manager that changed.
141: *
142: * @return the instance of the manager that changed. Guaranteed not to be
143: * <code>null</code>.
144: */
145: public final BindingManager getManager() {
146: return manager;
147: }
148:
149: /**
150: * Returns the scheme that changed.
151: *
152: * @return The changed scheme
153: */
154: public final Scheme getScheme() {
155: return scheme;
156: }
157:
158: /**
159: * Returns whether the active bindings have changed.
160: *
161: * @return <code>true</code> if the active bindings have changed;
162: * <code>false</code> otherwise.
163: */
164: public final boolean isActiveBindingsChanged() {
165: return ((changedValues & CHANGED_ACTIVE_BINDINGS) != 0);
166: }
167:
168: /**
169: * Computes whether the active bindings have changed for a given command
170: * identifier.
171: *
172: * @param parameterizedCommand
173: * The fully-parameterized command whose bindings might have
174: * changed; must not be <code>null</code>.
175: * @return <code>true</code> if the active bindings have changed for the
176: * given command identifier; <code>false</code> otherwise.
177: */
178: public final boolean isActiveBindingsChangedFor(
179: final ParameterizedCommand parameterizedCommand) {
180: final TriggerSequence[] currentBindings = manager
181: .getActiveBindingsFor(parameterizedCommand);
182: final TriggerSequence[] previousBindings;
183: if (previousTriggersByParameterizedCommand != null) {
184: final Collection previousBindingCollection = (Collection) previousTriggersByParameterizedCommand
185: .get(parameterizedCommand);
186: if (previousBindingCollection == null) {
187: previousBindings = null;
188: } else {
189: previousBindings = (TriggerSequence[]) previousBindingCollection
190: .toArray(new TriggerSequence[previousBindingCollection
191: .size()]);
192: }
193: } else {
194: previousBindings = null;
195: }
196:
197: return !Util.equals(currentBindings, previousBindings);
198: }
199:
200: /**
201: * Returns whether or not the active scheme changed.
202: *
203: * @return true, iff the active scheme property changed.
204: */
205: public final boolean isActiveSchemeChanged() {
206: return ((changedValues & CHANGED_ACTIVE_SCHEME) != 0);
207: }
208:
209: /**
210: * Returns whether the locale has changed
211: *
212: * @return <code>true</code> if the locale changed; <code>false</code>
213: * otherwise.
214: */
215: public boolean isLocaleChanged() {
216: return ((changedValues & CHANGED_LOCALE) != 0);
217: }
218:
219: /**
220: * Returns whether the platform has changed
221: *
222: * @return <code>true</code> if the platform changed; <code>false</code>
223: * otherwise.
224: */
225: public boolean isPlatformChanged() {
226: return ((changedValues & CHANGED_PLATFORM) != 0);
227: }
228:
229: /**
230: * Returns whether the list of defined scheme identifiers has changed.
231: *
232: * @return <code>true</code> if the list of scheme identifiers has
233: * changed; <code>false</code> otherwise.
234: */
235: public final boolean isSchemeChanged() {
236: return (scheme != null);
237: }
238:
239: /**
240: * Returns whether or not the scheme became defined
241: *
242: * @return <code>true</code> if the scheme became defined.
243: */
244: public final boolean isSchemeDefined() {
245: return (((changedValues & CHANGED_SCHEME_DEFINED) != 0) && (scheme != null));
246: }
247: }
|