]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceInput.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceInput.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceInput.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceInput.java
new file mode 100644 (file)
index 0000000..20399c6
--- /dev/null
@@ -0,0 +1,178 @@
+/*******************************************************************************\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.ui.workbench;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.request.Queries;\r
+import org.simantics.db.exception.DatabaseException;\r
+\r
+\r
+/**\r
+ * A wrapper class for parsing and generating the information given to Simantics\r
+ * views as a parameter through view secondary ID's.\r
+ * \r
+ * <p>\r
+ * To parse the input data (given in the secondary ID) just invoke:<br>\r
+ * <code>new ResourceInput(secondaryIdString)</code>\r
+ * </p>\r
+ * \r
+ * <p>\r
+ * To generate a secondary ID string based an input resource ID, invoke:<br>\r
+ * <code>new ResourceInput(inputResourceId).marshall()</code>\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @see ResourceViewPartUtils\r
+ */\r
+public class ResourceInput {\r
+\r
+    private static final String SUFFIX_SEPARATOR = "@";\r
+\r
+    private static final String EMPTY_SUFFIX = "";\r
+\r
+    private final String        inputResourceId;\r
+\r
+    private final String        suffix;\r
+\r
+    /**\r
+     * Construct a new input based on ID's.\r
+     * Use this to serialize ID data into an input string.\r
+     * \r
+     * @param randomAccessResourceId\r
+     */\r
+    public ResourceInput(String randomAccessResourceId) {\r
+        this.inputResourceId = randomAccessResourceId;\r
+        this.suffix = EMPTY_SUFFIX;\r
+    }\r
+\r
+    /**\r
+     * Construct a new input based on ID's. Use this to serialize ID data into\r
+     * an input string.\r
+     * \r
+     * @param randomAccessResourceId\r
+     * @param uniqueSuffix a unique suffix to add to the secondary ID or\r
+     *        <code>null</code> to ignore the unique suffix\r
+     */\r
+    public ResourceInput(String randomAccessResourceId, String uniqueSuffix) {\r
+        this.inputResourceId = randomAccessResourceId;\r
+        this.suffix = (uniqueSuffix == null) ? EMPTY_SUFFIX : uniqueSuffix;\r
+    }\r
+\r
+    /**\r
+     * Returns a new ResourceInput instance with the specified ID as\r
+     * input. This method exists in order to make sure that the client can get a\r
+     * clone of the original ResourceInput in every way except for the\r
+     * input ID in a unified fashion.\r
+     * \r
+     * @param newInputResourceId the new input resource ID\r
+     * @return new ResourceInput instance with the specified input resource ID\r
+     */\r
+    public ResourceInput changeInput(String newRandomAccessResourceId) {\r
+        if (suffix == EMPTY_SUFFIX)\r
+            return new ResourceInput(newRandomAccessResourceId);\r
+        return new ResourceInput(newRandomAccessResourceId, suffix);\r
+    }\r
+\r
+    public String getInputResourceId() {\r
+        return inputResourceId;\r
+    }\r
+\r
+    public String getSuffix() {\r
+        return suffix;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return marshall();\r
+    }\r
+\r
+    /**\r
+     * @param session the Session to use for initiating the\r
+     *        transaction for retrieving the input Resource.\r
+     * @return <code>null</code> if the random access reference of this\r
+     *         ResourceInput is invalid.\r
+     */\r
+    public Resource toResource(Session session) throws DatabaseException {\r
+        return session.syncRequest(Queries.resource(inputResourceId));\r
+    }\r
+\r
+    /**\r
+     * @param input Input format:\r
+     *        <code>client-id#input-resource-id[#unique-suffix]</code>\r
+     * @throws IllegalArgumentException if input is invalid\r
+     */\r
+    public String marshall() {\r
+        return String.format("%s@%s", inputResourceId, suffix);\r
+    }\r
+\r
+    /**\r
+     * @param a previously marshalled ResourceInput using\r
+     *        {@link #marshall()}\r
+     * @throws IllegalArgumentException if input is invalid\r
+     */\r
+    public static ResourceInput unmarshall(String input) throws IllegalArgumentException {\r
+        if (input == null)\r
+            throw new IllegalArgumentException("null input");\r
+        String[] parts = input.split(SUFFIX_SEPARATOR);\r
+        if (parts.length < 1)\r
+            throw new IllegalArgumentException("invalid input: " + input);\r
+\r
+        // Get input resource random access id\r
+        String id = parts[0];\r
+\r
+        // Get arbitrary suffix\r
+        String suffix = EMPTY_SUFFIX;\r
+        if (parts.length > 1) {\r
+            suffix = input.substring(id.length() + 1);\r
+        }\r
+        return new ResourceInput(id, suffix);\r
+    }\r
+\r
+    public static Resource unmarshallToResource(String input, Session session) throws DatabaseException {\r
+        ResourceInput in = unmarshall(input);\r
+        return in.toResource(session);\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        final int prime = 31;\r
+        int result = 1;\r
+        result = prime * result + ((inputResourceId == null) ? 0 : inputResourceId.hashCode());\r
+        result = prime * result + ((suffix == null) ? 0 : suffix.hashCode());\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if (this == obj)\r
+            return true;\r
+        if (obj == null)\r
+            return false;\r
+        if (getClass() != obj.getClass())\r
+            return false;\r
+        final ResourceInput other = (ResourceInput) obj;\r
+        if (inputResourceId == null) {\r
+            if (other.inputResourceId != null)\r
+                return false;\r
+        } else if (!inputResourceId.equals(other.inputResourceId))\r
+            return false;\r
+        if (suffix == null) {\r
+            if (other.suffix != null)\r
+                return false;\r
+        } else if (!suffix.equals(other.suffix))\r
+            return false;\r
+        return true;\r
+    }\r
+\r
+}\r