]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/HintTracker.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / hints / HintTracker.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.datastructures.hints;
13
14 /**
15  * HintTracker is a helper class for tracking any hints of a single
16  * IHintContext.
17  * 
18  * <p>
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.
23  * </p>
24  * 
25  * <p>
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.
33  * </p>
34  * 
35  * <p>
36  * The implementation is thread-safe.
37  * </p>
38  * 
39  * <p>
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
43  * behavior.
44  * </p>
45  * 
46  * @author Tuukka Lehtonen
47  */
48 public class HintTracker extends HintStack implements IHintTracker {
49     
50     private IHintContext ctx;
51
52     public HintTracker() {
53     }
54
55     public HintTracker(IHintContext ctx) {
56         addHintContext(ctx, 0);
57     }
58
59     /* (non-Javadoc)
60      * @see org.simantics.utils.datastructures.hints.IHintTracker#track(org.simantics.utils.datastructures.hints.IHintContext)
61      */
62     public synchronized final void track(IHintContext ctx) {
63         IHintContext prevCtx = this.ctx;
64         if (ctx == prevCtx)
65             return;
66         this.ctx = ctx;
67
68         if (ctx != null) {
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);
75             } else {
76                 super.addHintContext(ctx, 0);
77                 if (prevCtx != null)
78                     super.removeHintContext(prevCtx);
79             }
80         } else {
81             if (prevCtx != null)
82                 super.removeHintContext(prevCtx);
83         }
84     }
85     
86     /* (non-Javadoc)
87      * @see org.simantics.utils.datastructures.hints.IHintTracker#untrack()
88      */
89     public final void untrack() {
90         track(null);
91     }
92     
93     @Override
94     public final void addHintContext(IHintContext hints, int priority) {
95         throw new UnsupportedOperationException();
96     }
97
98     @Override
99     public final boolean removeHintContext(IHintContext hints) {
100         throw new UnsupportedOperationException();
101     }
102
103 }