/******************************************************************************* * 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.BuiltinKeys; import org.simantics.browsing.ui.NodeContext; 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; /** * Please implement: * * *

* Other points of customization: *

* * * @author Tuukka Lehtonen * * @param the input class of the used * {@link org.simantics.db.layer0.adapter.Modifier} */ public abstract class AbstractStringModifier implements Modifier { protected NodeContext context; protected Session session; protected String initialValue; protected StringModifier modifier; /** * If non-null, the modifier could not be fetched, e.g. adapted * from the specified INodeContext. */ protected Throwable modifierFailed; /** * @param context * @param session */ public AbstractStringModifier(NodeContext context, RequestProcessor processor) { this.context = context; this.session = processor.getSession(); 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)); try { processor.syncRequest(new ReadRequest() { @Override public void run(ReadGraph g) throws DatabaseException { initialValue = getInitialValue(g); AbstractStringModifier.this.modifier = g.adapt(r, StringModifier.class); initializeModifier(g); } }); } catch (DatabaseException e) { modifierFailed = e; } } protected void initializeModifier(ReadGraph g) { } /** * @param g * @return the value that shall be returned by {@link #getValue()} */ protected String getInitialValue(ReadGraph g) throws DatabaseException { return g.adapt(getResourceToModify(), String.class); } /** * @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() { return (Resource) context.getAdapter(Resource.class); } /** * @return the modifier */ protected StringModifier 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(); 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. * *

* This default implementation will always allow the modification to proceed. *

* * @param label the label to be given to the modifier * @return true to go forward with the transaction, * false 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; } };