]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LabelModifier.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LabelModifier.java
diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LabelModifier.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LabelModifier.java
new file mode 100644 (file)
index 0000000..e4ce0cc
--- /dev/null
@@ -0,0 +1,106 @@
+/*******************************************************************************\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.browsing.ui.graph.impl;\r
+\r
+import org.simantics.browsing.ui.content.Labeler.Modifier;\r
+import org.simantics.browsing.ui.graph.impl.request.GetLabel;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.db.request.Write;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.datastructures.Callback;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+\r
+/**\r
+ * NOTE: By default writing Label property, NOT Name property! Name is\r
+ * considered a unique immutable identifier for the model. This helps with\r
+ * writing stuff on disk and keeping the names coherent.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class LabelModifier implements Modifier, Callback<DatabaseException> {\r
+\r
+    private final Session  session;\r
+    protected final Resource resource;\r
+    protected final Resource targetProperty;\r
+\r
+    /**\r
+     * @param session database access point\r
+     * @param resource the resource for which to modify the label\r
+     * @param writeCallback a callback that is invoked once the graph write has\r
+     *        finished. The exception will be null if the write was successful.\r
+     */\r
+    public LabelModifier(Session session, Resource resource) {\r
+        this(session, resource, session.getService(Layer0.class).HasLabel);\r
+    }\r
+\r
+    /**\r
+     * @param session database access point\r
+     * @param resource the resource for which to modify the label\r
+     * @param targetProperty the relation of the target property to be claimed\r
+     *        by the modifier.\r
+     * @param writeCallback a callback that is invoked once the graph write has\r
+     *        finished. The exception will be null if the write was successful.\r
+     */\r
+    public LabelModifier(Session session, Resource resource, Resource targetProperty) {\r
+        this.session = session;\r
+        this.resource = resource;\r
+        this.targetProperty = targetProperty;\r
+    }\r
+\r
+    @Override\r
+    public String getValue() {\r
+        try {\r
+            return session.syncRequest(new GetLabel(resource));\r
+        } catch (DatabaseException e) {\r
+            ErrorLogger.defaultLogWarning("Problem reading current label, see exception for details.", e);\r
+            return "";\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public String isValid(String label) {\r
+        if (label.isEmpty())\r
+            return "Empty label not allowed";\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public void modify(String label) {\r
+        session.asyncRequest(getWriteRequest(label), this);\r
+    }\r
+\r
+    protected Write getWriteRequest(final String label) {\r
+        return new WriteRequest() {\r
+            @Override\r
+            public void perform(WriteGraph g) throws DatabaseException {\r
+                g.markUndoPoint();\r
+                Layer0Utils.addCommentMetadata(g, "Renamed " + g.getRelatedValue2(resource, Layer0.getInstance(g).HasName, Bindings.STRING) + " to " + label + " " + resource.toString());\r
+                g.claimLiteral(resource, targetProperty, label);\r
+            }\r
+        };\r
+    }\r
+\r
+    @Override\r
+    public void run(DatabaseException parameter) {\r
+        if (parameter != null) {\r
+            ExceptionUtils.logError("Label modification failed, see exception for details.", parameter);\r
+        }\r
+    }\r
+\r
+}\r