]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/PermutativeFlagLabelingScheme.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / flag / PermutativeFlagLabelingScheme.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/PermutativeFlagLabelingScheme.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/PermutativeFlagLabelingScheme.java
new file mode 100644 (file)
index 0000000..de37824
--- /dev/null
@@ -0,0 +1,126 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 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.diagram.flag;\r
+\r
+import java.nio.CharBuffer;\r
+import java.util.Arrays;\r
+import java.util.Set;\r
+import java.util.TreeSet;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.primitiverequest.OrderedSet;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class PermutativeFlagLabelingScheme implements FlagLabelingScheme {\r
+\r
+    protected String initialValue;\r
+    protected char   min;\r
+    protected char   max;\r
+\r
+    /**\r
+     * @param initialValue\r
+     * @param min\r
+     * @param max\r
+     */\r
+    public PermutativeFlagLabelingScheme(String initialValue, char min, char max) {\r
+        this.initialValue = initialValue;\r
+        this.min = min;\r
+        this.max = max;\r
+    }\r
+\r
+    /**\r
+     * Support for graph adapter construction. Only uses the first character of\r
+     * both min and max parameters.\r
+     * \r
+     * @param initialValue\r
+     * @param min\r
+     * @param max\r
+     */\r
+    public PermutativeFlagLabelingScheme(String initialValue, String min, String max) {\r
+        this.initialValue = initialValue;\r
+        this.min = min.charAt(0);\r
+        this.max = max.charAt(0);\r
+    }\r
+\r
+    protected Set<String> getUsedLabels(ReadGraph graph, Resource diagram) throws DatabaseException {\r
+        DiagramResource DIA = DiagramResource.getInstance(graph);\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        Set<String> used = new TreeSet<String>(NameUtils.STRING_CHARBUFFER_COMPARATOR);\r
+        for (Resource element : graph.syncRequest(new OrderedSet(diagram))) {\r
+            if (graph.isInstanceOf(element, DIA.Flag)) {\r
+                String label = graph.getPossibleRelatedValue(element, L0.HasLabel, Bindings.STRING);\r
+                if (label != null && !label.isEmpty())\r
+                    used.add(label);\r
+            }\r
+        }\r
+        return used;\r
+    }\r
+\r
+    @Override\r
+    public String generateLabel(ReadGraph graph, Resource diagram) throws DatabaseException {\r
+        CharBuffer cb = CharBuffer.allocate(10);\r
+        Set<String> used = getUsedLabels(graph, diagram);\r
+        initialValue(cb);\r
+        cb.limit(cb.position());\r
+        while (used.contains(cb)) {\r
+            permutate(cb);\r
+            cb.position(cb.limit());\r
+        }\r
+        cb.rewind();\r
+        return cb.toString();\r
+    }\r
+\r
+    protected void initialValue(CharBuffer cb) {\r
+        cb.append(initialValue);\r
+    }\r
+\r
+    protected void permutate(CharBuffer cb) {\r
+        permutate(cb, min, max);\r
+    }\r
+\r
+    /**\r
+     * Takes an input char buffer and modifies it so that its last character is\r
+     * set to the next value within the range [min,max]. If a character is\r
+     * already equal to max, this check and modify process is continued towards\r
+     * the beginning of the buffer until a character less than max is found. In\r
+     * the case where all characters are already equal to max, the buffer is set\r
+     * to size + 1 min characters, thus expanding the permutation space.\r
+     * \r
+     * For example permutating the string "AA" produces a sequence "AB", "AC",\r
+     * "AD", ..., "AZ", "BA", "BB", ...\r
+     */\r
+    protected void permutate(CharBuffer str, char min, char max) {\r
+        // Produce the next permutation of an [{min}-{max}]* character sequence.\r
+        int s = str.limit();\r
+        char[] chars = str.array();\r
+        for (int i = s - 1; i >= 0; --i) {\r
+            if (chars[i] < max) {\r
+                chars[i]++;\r
+                for (int j = i + 1; j < s; ++j)\r
+                    chars[j] = min;\r
+                return;\r
+            }\r
+        }\r
+        // all permutations already in use, add more characters.\r
+        str.limit(s + 1);\r
+        Arrays.fill(chars, 0, s + 1, min);\r
+    }\r
+\r
+}
\ No newline at end of file