-/*******************************************************************************\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.db.ReadGraph;\r
-import org.simantics.db.RequestProcessor;\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.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.StringModifier;\r
-import org.simantics.db.layer0.adapter.StringModifierFactory;\r
-\r
-/**\r
- * Please implement:\r
- * <ul>\r
- * <li>{@link #doModify(String)} - perform the requested modification.</li>\r
- * </ul>\r
- * \r
- * <p>\r
- * Other points of customization:\r
- * </p>\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 #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 #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 AbstractFactoryStringModifier implements Modifier {\r
-\r
- protected Session session;\r
-\r
- protected Resource subject;\r
-\r
- protected Resource predicate;\r
- \r
- protected Resource object;\r
-\r
- protected String initialValue;\r
-\r
- protected StringModifierFactory modifierFactory;\r
- protected StringModifier modifier;\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 AbstractFactoryStringModifier(Resource subject, Resource predicate, Resource object, RequestProcessor processor) {\r
- this.subject = subject;\r
- this.predicate = predicate;\r
- this.object = object;\r
- this.session = processor.getSession();\r
-\r
- try {\r
- processor.syncRequest(new ReadRequest() {\r
- @Override\r
- public void run(ReadGraph g) throws DatabaseException {\r
- initialValue = getInitialValue(g);\r
- initializeModifier(g);\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- modifierFailed = e;\r
- }\r
- }\r
-\r
- protected void initializeModifier(ReadGraph graph) throws DatabaseException {\r
- modifierFactory = graph.getPossibleAdapter(subject, StringModifierFactory.class);\r
- if (modifierFactory != null)\r
- modifier = modifierFactory.createModifier(graph, predicate, object);\r
- if (modifier == null) {\r
- modifierFactory = null;\r
- modifier = graph.adapt(object, StringModifier.class);\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
- return g.adapt(object, String.class);\r
- }\r
-\r
- /**\r
- * @return the modifier\r
- */\r
- protected StringModifier getModifier() {\r
- return modifier;\r
- }\r
-\r
- protected StringModifierFactory getModifierFactory() {\r
- return modifierFactory;\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 modifierFailed.getMessage();\r
- if (modifier == null)\r
- return "No modifier available";\r
- String t = createModifierInput(label);\r
- return modifier.isValid(t);\r
- }\r
-\r
- @Override\r
- public final void modify(String label) {\r
- if (modifierFailed != null)\r
- // TODO: throw exception?\r
- return;\r
- String t = createModifierInput(label);\r
- if (!verifyModification(t))\r
- return;\r
- doModify(t);\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(String label) {\r
- return true;\r
- }\r
-\r
- public abstract void doModify(String label);\r
-\r
- /**\r
- * Override if necessary.\r
- * \r
- * @param label\r
- * @return\r
- */\r
- public String createModifierInput(String label) {\r
- return label;\r
- }\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 org.simantics.browsing.ui.content.Labeler.Modifier;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+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.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.StringModifier;
+import org.simantics.db.layer0.adapter.StringModifierFactory;
+
+/**
+ * Please implement:
+ * <ul>
+ * <li>{@link #doModify(String)} - perform the requested modification.</li>
+ * </ul>
+ *
+ * <p>
+ * Other points of customization:
+ * </p>
+ * <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 #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 #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 AbstractFactoryStringModifier implements Modifier {
+
+ protected Session session;
+
+ protected Resource subject;
+
+ protected Resource predicate;
+
+ protected Resource object;
+
+ protected String initialValue;
+
+ protected StringModifierFactory modifierFactory;
+ protected StringModifier modifier;
+
+ /**
+ * 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 AbstractFactoryStringModifier(Resource subject, Resource predicate, Resource object, RequestProcessor processor) {
+ this.subject = subject;
+ this.predicate = predicate;
+ this.object = object;
+ this.session = processor.getSession();
+
+ try {
+ processor.syncRequest(new ReadRequest() {
+ @Override
+ public void run(ReadGraph g) throws DatabaseException {
+ initialValue = getInitialValue(g);
+ initializeModifier(g);
+ }
+ });
+ } catch (DatabaseException e) {
+ modifierFailed = e;
+ }
+ }
+
+ protected void initializeModifier(ReadGraph graph) throws DatabaseException {
+ modifierFactory = graph.getPossibleAdapter(subject, StringModifierFactory.class);
+ if (modifierFactory != null)
+ modifier = modifierFactory.createModifier(graph, predicate, object);
+ if (modifier == null) {
+ modifierFactory = null;
+ modifier = graph.adapt(object, StringModifier.class);
+ }
+ }
+
+ /**
+ * @param g
+ * @return the value that shall be returned by {@link #getValue()}
+ */
+ protected String getInitialValue(ReadGraph g) throws DatabaseException {
+ return g.adapt(object, String.class);
+ }
+
+ /**
+ * @return the modifier
+ */
+ protected StringModifier getModifier() {
+ return modifier;
+ }
+
+ protected StringModifierFactory getModifierFactory() {
+ return modifierFactory;
+ }
+
+ @Override
+ public String getValue() {
+ return initialValue;
+ }
+
+ @Override
+ public String isValid(String label) {
+ if (modifierFailed != null)
+ return modifierFailed.getMessage();
+ if (modifier == null)
+ return "No modifier available";
+ String t = createModifierInput(label);
+ return modifier.isValid(t);
+ }
+
+ @Override
+ public final void modify(String label) {
+ if (modifierFailed != null)
+ // TODO: throw exception?
+ return;
+ String t = createModifierInput(label);
+ if (!verifyModification(t))
+ return;
+ doModify(t);
+ }
+
+ /**
+ * 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(String label) {
+ return true;
+ }
+
+ public abstract void doModify(String label);
+
+ /**
+ * Override if necessary.
+ *
+ * @param label
+ * @return
+ */
+ public String createModifierInput(String label) {
+ return label;
+ }
+
+};