]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/HintTracker.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / hints / HintTracker.java
diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/HintTracker.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/HintTracker.java
new file mode 100644 (file)
index 0000000..79f1ce6
--- /dev/null
@@ -0,0 +1,103 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.datastructures.hints;\r
+\r
+/**\r
+ * HintTracker is a helper class for tracking any hints of a single\r
+ * IHintContext.\r
+ * \r
+ * <p>\r
+ * The main purpose of this class is to enable very easy switching of the\r
+ * tracked hint context. Only a call to the {@link #track(IHintContext)} method\r
+ * is required. All hint listeners registered into the tracked will\r
+ * automatically be switched to listen to the new context instead.\r
+ * </p>\r
+ * \r
+ * <p>\r
+ * The tracker is implemented using HintStack but the\r
+ * {@link #addHintContext(IHintContext, int)} and\r
+ * {@link #removeHintContext(IHintContext)} methods are disabled by throwing\r
+ * {@link UnsupportedOperationException} in both. This is to prevent invalid use\r
+ * of the class as it is only meant for tracking a single context and nothing\r
+ * more. Otherwise this class functions exactly like {@link IHintStack} and\r
+ * {@link IHintObservable} are specified to work.\r
+ * </p>\r
+ * \r
+ * <p>\r
+ * The implementation is thread-safe.\r
+ * </p>\r
+ * \r
+ * <p>\r
+ * Always be sure to {@link #untrack()} any IHintContext that you're tracking\r
+ * after the tracker is no longer needed. Otherwise you will most likely have\r
+ * invalid listeners within the tracked IHintContext resulting in erroneous\r
+ * behavior.\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class HintTracker extends HintStack implements IHintTracker {\r
+    \r
+    private IHintContext ctx;\r
+\r
+    public HintTracker() {\r
+    }\r
+\r
+    public HintTracker(IHintContext ctx) {\r
+        addHintContext(ctx, 0);\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see org.simantics.utils.datastructures.hints.IHintTracker#track(org.simantics.utils.datastructures.hints.IHintContext)\r
+     */\r
+    public synchronized final void track(IHintContext ctx) {\r
+        IHintContext prevCtx = this.ctx;\r
+        if (ctx == prevCtx)\r
+            return;\r
+        this.ctx = ctx;\r
+\r
+        if (ctx != null) {\r
+            if (ctx.equals(prevCtx)) {\r
+                // remove+add order is necessary in this case.\r
+                // add+remove would fail since the hint context would\r
+                // already be found in the hint stack.\r
+                super.removeHintContext(prevCtx);\r
+                super.addHintContext(ctx, 0);\r
+            } else {\r
+                super.addHintContext(ctx, 0);\r
+                if (prevCtx != null)\r
+                    super.removeHintContext(prevCtx);\r
+            }\r
+        } else {\r
+            if (prevCtx != null)\r
+                super.removeHintContext(prevCtx);\r
+        }\r
+    }\r
+    \r
+    /* (non-Javadoc)\r
+     * @see org.simantics.utils.datastructures.hints.IHintTracker#untrack()\r
+     */\r
+    public final void untrack() {\r
+        track(null);\r
+    }\r
+    \r
+    @Override\r
+    public final void addHintContext(IHintContext hints, int priority) {\r
+        throw new UnsupportedOperationException();\r
+    }\r
+\r
+    @Override\r
+    public final boolean removeHintContext(IHintContext hints) {\r
+        throw new UnsupportedOperationException();\r
+    }\r
+\r
+}
\ No newline at end of file