1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.datastructures.hints;
15 * HintTracker is a helper class for tracking any hints of a single
19 * The main purpose of this class is to enable very easy switching of the
20 * tracked hint context. Only a call to the {@link #track(IHintContext)} method
21 * is required. All hint listeners registered into the tracked will
22 * automatically be switched to listen to the new context instead.
26 * The tracker is implemented using HintStack but the
27 * {@link #addHintContext(IHintContext, int)} and
28 * {@link #removeHintContext(IHintContext)} methods are disabled by throwing
29 * {@link UnsupportedOperationException} in both. This is to prevent invalid use
30 * of the class as it is only meant for tracking a single context and nothing
31 * more. Otherwise this class functions exactly like {@link IHintStack} and
32 * {@link IHintObservable} are specified to work.
36 * The implementation is thread-safe.
40 * Always be sure to {@link #untrack()} any IHintContext that you're tracking
41 * after the tracker is no longer needed. Otherwise you will most likely have
42 * invalid listeners within the tracked IHintContext resulting in erroneous
46 * @author Tuukka Lehtonen
48 public class HintTracker extends HintStack implements IHintTracker {
50 private IHintContext ctx;
52 public HintTracker() {
55 public HintTracker(IHintContext ctx) {
56 addHintContext(ctx, 0);
60 * @see org.simantics.utils.datastructures.hints.IHintTracker#track(org.simantics.utils.datastructures.hints.IHintContext)
62 public synchronized final void track(IHintContext ctx) {
63 IHintContext prevCtx = this.ctx;
69 if (ctx.equals(prevCtx)) {
70 // remove+add order is necessary in this case.
71 // add+remove would fail since the hint context would
72 // already be found in the hint stack.
73 super.removeHintContext(prevCtx);
74 super.addHintContext(ctx, 0);
76 super.addHintContext(ctx, 0);
78 super.removeHintContext(prevCtx);
82 super.removeHintContext(prevCtx);
87 * @see org.simantics.utils.datastructures.hints.IHintTracker#untrack()
89 public final void untrack() {
94 public final void addHintContext(IHintContext hints, int priority) {
95 throw new UnsupportedOperationException();
99 public final boolean removeHintContext(IHintContext hints) {
100 throw new UnsupportedOperationException();