]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GraphStringIndexModifier.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / GraphStringIndexModifier.java
index cb1fa176dc09d98f65f9e0fd71994042567d62bc..e9adba21e6ad2f2fe886d1d37dba8d19149d52a6 100644 (file)
-/*******************************************************************************\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 java.util.concurrent.Semaphore;\r
-\r
-import org.simantics.browsing.ui.BuiltinKeys;\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.content.Labeler.Modifier;\r
-import org.simantics.db.ReadGraph;\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.ReadRequest;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.StringIndexModifier;\r
-import org.simantics.db.layer0.adapter.StringModifier;\r
-import org.simantics.db.layer0.adapter.TObjectIntPair;\r
-import org.simantics.layer0.utils.representation.StringRepresentation2;\r
-import org.simantics.utils.datastructures.Callback;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-\r
-/**\r
- * Please implement:\r
- * <ul>\r
- * <li>{@link #createModifierInput(String)} - constructs an input for\r
- * {@link org.simantics.db.layer0.adapter.Modifier#modify(WriteGraph, Object)}\r
- * from the specified label given by the user.\r
- * <li>{@link #doModify(WriteGraph, String)} - perform the requested modification\r
- * into the graph.</li>\r
- * </ul>\r
- * \r
- * <p>\r
- * Other points of customization:\r
- * </p>\r
- * <ul>\r
- * <li>{@link #getInitialValue(ReadGraph)} - returns the value that should be shown\r
- * initially when editing. The default implementation just adapts the input to\r
- * its String representation, but you may want to customize this.</li>\r
- * <li>{@link #initializeGraphModifier(ReadGraph)} - allows you to perform custom\r
- * initialization of the modifier which uses the graph</li>\r
- * <li>{@link #getResourceToModify()} - allows you to customize the way in which\r
- * the input INodeContext is resolved into a Resource. The default\r
- * implementation uses the IAdaptable interface of INodeContext to get the\r
- * Resource.</li>\r
- * <li>{@link #verifyModification(String)} - allows for last chance denial of\r
- * the modification after the user has signalled approval of the modification.</li>\r
- * </ul>\r
- * \r
- * @author Tuukka Lehtonen\r
- * \r
- * @param <T> the input class of the used\r
- *        {@link org.simantics.db.layer0.adapter.Modifier}\r
- */\r
-public abstract class GraphStringIndexModifier implements Modifier {\r
-\r
-    protected NodeContext        context;\r
-\r
-    protected Session             session;\r
-\r
-    protected int                 index;\r
-\r
-    protected String              initialValue;\r
-\r
-    protected StringIndexModifier modifier;\r
-\r
-    /**\r
-     * Used to synchronize {@link #isValid(String)} and {@link #modify(String)}\r
-     * with the asynchronous modifier fetch.\r
-     */\r
-    protected Semaphore           modifierReady = new Semaphore(0);\r
-\r
-    /**\r
-     * If <code>non-null</code>, the modifier could not be fetched, e.g. adapted\r
-     * from the specified INodeContext.\r
-     */\r
-    protected Throwable           modifierFailed;\r
-\r
-\r
-    /**\r
-     * @param context\r
-     * @param session\r
-     */\r
-    public GraphStringIndexModifier(NodeContext context, Session session, int index) throws DatabaseException {\r
-        this.context = context;\r
-        this.session = session;\r
-        this.index = index;\r
-\r
-        final Resource r = getResourceToModify();\r
-        if (r == null)\r
-            throw new IllegalArgumentException("This modifier does not work for INodeContexts that are not adaptable to a Resource. The context input is: " + context.getConstant(BuiltinKeys.INPUT));\r
-\r
-        session.syncRequest(new ReadRequest() {\r
-            @Override\r
-            public void run(ReadGraph g) throws DatabaseException {\r
-                initialValue = getInitialValue(g);\r
-                GraphStringIndexModifier.this.modifier = g.adapt(r, StringIndexModifier.class);\r
-                initializeGraphModifier(g);\r
-                modifierReady.release();\r
-            }\r
-        });\r
-\r
-    }\r
-\r
-    /**\r
-     * @param g\r
-     * @return the value that shall be returned by {@link #getValue()}\r
-     */\r
-    protected String getInitialValue(ReadGraph g) throws DatabaseException {\r
-        StringRepresentation2 sr = g.adapt(getResourceToModify(), StringRepresentation2.class);\r
-        String s = sr.get(g, index);\r
-        return s;\r
-    }\r
-\r
-    /**\r
-     * Override to perform graph-based initialization actions for this modifier.\r
-     * \r
-     * @param g the graph handle\r
-     */\r
-    protected void initializeGraphModifier(ReadGraph g) {\r
-    }\r
-\r
-    /**\r
-     * @return the Resource to modify based on the input INodeContext. This\r
-     *         resource must be adaptable to a {@link StringModifier} in order\r
-     *         for this modifier to work.\r
-     */\r
-    protected Resource getResourceToModify() {\r
-        Resource r = (Resource) context.getAdapter(Resource.class);\r
-        if (r == null)\r
-            throw new AssertionError("context.getAdapter(Resource.class) returned null");\r
-        return r;\r
-    }\r
-\r
-    /**\r
-     * @return the modifier or <code>null</code> if the StringModifier adaption\r
-     *         has not completed yet.\r
-     */\r
-    protected StringIndexModifier getModifier() {\r
-        return modifier;\r
-    }\r
-\r
-    @Override\r
-    public String getValue() {\r
-        return initialValue;\r
-    }\r
-\r
-    @Override\r
-    public String isValid(String label) {\r
-        if (modifierFailed != null)\r
-            return "Could not resolve validator for this value, modification denied. Reason: " + modifierFailed.getMessage();\r
-        if (modifier == null)\r
-            // Cannot validate yet, the validator has not been resolved.\r
-            // For the time being, consider the value invalid for no\r
-            // apparent reason to the user.\r
-            return "";\r
-        TObjectIntPair<String> t = createModifierInput(label);\r
-        return modifier.isValid(t);\r
-    }\r
-\r
-    @Override\r
-    public final void modify(String label) {\r
-        if (modifier == null) {\r
-            try {\r
-                modifierReady.acquire();\r
-            } catch (InterruptedException e) {\r
-                // TODO: throw exception?\r
-                return;\r
-            }\r
-        }\r
-        if (modifierFailed != null)\r
-            // TODO: throw exception?\r
-            return;\r
-        final TObjectIntPair<String> t = createModifierInput(label);\r
-        if (!verifyModification(t))\r
-            return;\r
-        session.asyncRequest(new WriteRequest() {\r
-            @Override\r
-            public void perform(WriteGraph graph) throws DatabaseException {\r
-                doModify(graph, t);\r
-            }\r
-        }, new Callback<DatabaseException>() {\r
-            @Override\r
-            public void run(DatabaseException parameter) {\r
-                if (parameter != null)\r
-                    ErrorLogger.defaultLogError(parameter);\r
-            }\r
-        });\r
-    }\r
-\r
-    /**\r
-     * Called one last time before actually performing the modifying write\r
-     * transaction to verify whether this is really desired or not.\r
-     * \r
-     * <p>\r
-     * This default implementation will always allow the modification to proceed.\r
-     * </p>\r
-     * \r
-     * @param label the label to be given to the modifier\r
-     * @return <code>true</code> to go forward with the transaction,\r
-     *         <code>false</code> to bail out\r
-     */\r
-    protected boolean verifyModification(TObjectIntPair<String> label) {\r
-        return true;\r
-    }\r
-\r
-    public abstract void doModify(WriteGraph graph, TObjectIntPair<String> label) throws DatabaseException;\r
-\r
-    /**\r
-     * Constructs T from the specified label which is then given to\r
-     * {@link #doModify(WriteGraph, Object)} as the class T argument.\r
-     * \r
-     * @param fromLabel the modified label specified by the user\r
-     * @return the\r
-     *         {@link org.simantics.db.layer0.adapter.Modifier#modify(WriteGraph, Object)}\r
-     *         input\r
-     */\r
-    public abstract TObjectIntPair<String> createModifierInput(String fromLabel);\r
-\r
-};\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.browsing.ui.graph.impl;
+
+import java.util.concurrent.Semaphore;
+
+import org.simantics.browsing.ui.BuiltinKeys;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.content.Labeler.Modifier;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.StringIndexModifier;
+import org.simantics.db.layer0.adapter.StringModifier;
+import org.simantics.db.layer0.adapter.TObjectIntPair;
+import org.simantics.layer0.utils.representation.StringRepresentation2;
+import org.simantics.utils.datastructures.Callback;
+import org.simantics.utils.ui.ErrorLogger;
+
+/**
+ * Please implement:
+ * <ul>
+ * <li>{@link #createModifierInput(String)} - constructs an input for
+ * {@link org.simantics.db.layer0.adapter.Modifier#modify(WriteGraph, Object)}
+ * from the specified label given by the user.
+ * <li>{@link #doModify(WriteGraph, String)} - perform the requested modification
+ * into the graph.</li>
+ * </ul>
+ * 
+ * <p>
+ * Other points of customization:
+ * </p>
+ * <ul>
+ * <li>{@link #getInitialValue(ReadGraph)} - returns the value that should be shown
+ * initially when editing. The default implementation just adapts the input to
+ * its String representation, but you may want to customize this.</li>
+ * <li>{@link #initializeGraphModifier(ReadGraph)} - allows you to perform custom
+ * initialization of the modifier which uses the graph</li>
+ * <li>{@link #getResourceToModify()} - allows you to customize the way in which
+ * the input INodeContext is resolved into a Resource. The default
+ * implementation uses the IAdaptable interface of INodeContext to get the
+ * Resource.</li>
+ * <li>{@link #verifyModification(String)} - allows for last chance denial of
+ * the modification after the user has signalled approval of the modification.</li>
+ * </ul>
+ * 
+ * @author Tuukka Lehtonen
+ * 
+ * @param <T> the input class of the used
+ *        {@link org.simantics.db.layer0.adapter.Modifier}
+ */
+public abstract class GraphStringIndexModifier implements Modifier {
+
+    protected NodeContext        context;
+
+    protected Session             session;
+
+    protected int                 index;
+
+    protected String              initialValue;
+
+    protected StringIndexModifier modifier;
+
+    /**
+     * Used to synchronize {@link #isValid(String)} and {@link #modify(String)}
+     * with the asynchronous modifier fetch.
+     */
+    protected Semaphore           modifierReady = new Semaphore(0);
+
+    /**
+     * If <code>non-null</code>, the modifier could not be fetched, e.g. adapted
+     * from the specified INodeContext.
+     */
+    protected Throwable           modifierFailed;
+
+
+    /**
+     * @param context
+     * @param session
+     */
+    public GraphStringIndexModifier(NodeContext context, Session session, int index) throws DatabaseException {
+        this.context = context;
+        this.session = session;
+        this.index = index;
+
+        final Resource r = getResourceToModify();
+        if (r == null)
+            throw new IllegalArgumentException("This modifier does not work for INodeContexts that are not adaptable to a Resource. The context input is: " + context.getConstant(BuiltinKeys.INPUT));
+
+        session.syncRequest(new ReadRequest() {
+            @Override
+            public void run(ReadGraph g) throws DatabaseException {
+                initialValue = getInitialValue(g);
+                GraphStringIndexModifier.this.modifier = g.adapt(r, StringIndexModifier.class);
+                initializeGraphModifier(g);
+                modifierReady.release();
+            }
+        });
+
+    }
+
+    /**
+     * @param g
+     * @return the value that shall be returned by {@link #getValue()}
+     */
+    protected String getInitialValue(ReadGraph g) throws DatabaseException {
+        StringRepresentation2 sr = g.adapt(getResourceToModify(), StringRepresentation2.class);
+        String s = sr.get(g, index);
+        return s;
+    }
+
+    /**
+     * Override to perform graph-based initialization actions for this modifier.
+     * 
+     * @param g the graph handle
+     */
+    protected void initializeGraphModifier(ReadGraph g) {
+    }
+
+    /**
+     * @return the Resource to modify based on the input INodeContext. This
+     *         resource must be adaptable to a {@link StringModifier} in order
+     *         for this modifier to work.
+     */
+    protected Resource getResourceToModify() {
+        Resource r = (Resource) context.getAdapter(Resource.class);
+        if (r == null)
+            throw new AssertionError("context.getAdapter(Resource.class) returned null");
+        return r;
+    }
+
+    /**
+     * @return the modifier or <code>null</code> if the StringModifier adaption
+     *         has not completed yet.
+     */
+    protected StringIndexModifier getModifier() {
+        return modifier;
+    }
+
+    @Override
+    public String getValue() {
+        return initialValue;
+    }
+
+    @Override
+    public String isValid(String label) {
+        if (modifierFailed != null)
+            return "Could not resolve validator for this value, modification denied. Reason: " + modifierFailed.getMessage();
+        if (modifier == null)
+            // Cannot validate yet, the validator has not been resolved.
+            // For the time being, consider the value invalid for no
+            // apparent reason to the user.
+            return "";
+        TObjectIntPair<String> t = createModifierInput(label);
+        return modifier.isValid(t);
+    }
+
+    @Override
+    public final void modify(String label) {
+        if (modifier == null) {
+            try {
+                modifierReady.acquire();
+            } catch (InterruptedException e) {
+                // TODO: throw exception?
+                return;
+            }
+        }
+        if (modifierFailed != null)
+            // TODO: throw exception?
+            return;
+        final TObjectIntPair<String> t = createModifierInput(label);
+        if (!verifyModification(t))
+            return;
+        session.asyncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                doModify(graph, t);
+            }
+        }, new Callback<DatabaseException>() {
+            @Override
+            public void run(DatabaseException parameter) {
+                if (parameter != null)
+                    ErrorLogger.defaultLogError(parameter);
+            }
+        });
+    }
+
+    /**
+     * Called one last time before actually performing the modifying write
+     * transaction to verify whether this is really desired or not.
+     * 
+     * <p>
+     * This default implementation will always allow the modification to proceed.
+     * </p>
+     * 
+     * @param label the label to be given to the modifier
+     * @return <code>true</code> to go forward with the transaction,
+     *         <code>false</code> to bail out
+     */
+    protected boolean verifyModification(TObjectIntPair<String> label) {
+        return true;
+    }
+
+    public abstract void doModify(WriteGraph graph, TObjectIntPair<String> label) throws DatabaseException;
+
+    /**
+     * Constructs T from the specified label which is then given to
+     * {@link #doModify(WriteGraph, Object)} as the class T argument.
+     * 
+     * @param fromLabel the modified label specified by the user
+     * @return the
+     *         {@link org.simantics.db.layer0.adapter.Modifier#modify(WriteGraph, Object)}
+     *         input
+     */
+    public abstract TObjectIntPair<String> createModifierInput(String fromLabel);
+
+};