1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
14 import org.simantics.browsing.ui.BuiltinKeys;
15 import org.simantics.browsing.ui.NodeContext;
16 import org.simantics.browsing.ui.content.Labeler.Modifier;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.RequestProcessor;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.adapter.StringModifier;
29 * <li>{@link #doModify(String)} - perform the requested modification.</li>
33 * Other points of customization:
36 * <li>{@link #createModifierInput(String)} - constructs an input for
37 * {@link org.simantics.db.layer0.adapter.Modifier#modify(WriteGraph, Object)}
38 * from the specified label given by the user.
39 * <li>{@link #getInitialValue(ReadGraph)} - returns the value that should be shown
40 * initially when editing. The default implementation just adapts the input to
41 * its String representation, but you may want to customize this.</li>
42 * <li>{@link #getResourceToModify()} - allows you to customize the way in which
43 * the input INodeContext is resolved into a Resource. The default
44 * implementation uses the IAdaptable interface of INodeContext to get the
46 * <li>{@link #verifyModification(String)} - allows for last chance denial of
47 * the modification after the user has signalled approval of the modification.</li>
50 * @author Tuukka Lehtonen
52 * @param <T> the input class of the used
53 * {@link org.simantics.db.layer0.adapter.Modifier}
55 public abstract class AbstractStringModifier implements Modifier {
57 protected NodeContext context;
59 protected Session session;
61 protected String initialValue;
63 protected StringModifier modifier;
66 * If <code>non-null</code>, the modifier could not be fetched, e.g. adapted
67 * from the specified INodeContext.
69 protected Throwable modifierFailed;
76 public AbstractStringModifier(NodeContext context, RequestProcessor processor) {
77 this.context = context;
78 this.session = processor.getSession();
80 final Resource r = getResourceToModify();
82 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));
85 processor.syncRequest(new ReadRequest() {
87 public void run(ReadGraph g) throws DatabaseException {
88 initialValue = getInitialValue(g);
89 AbstractStringModifier.this.modifier = g.adapt(r, StringModifier.class);
90 initializeModifier(g);
93 } catch (DatabaseException e) {
98 protected void initializeModifier(ReadGraph g) {
103 * @return the value that shall be returned by {@link #getValue()}
105 protected String getInitialValue(ReadGraph g) throws DatabaseException {
106 return g.adapt(getResourceToModify(), String.class);
110 * @return the Resource to modify based on the input INodeContext. This
111 * resource must be adaptable to a {@link StringModifier} in order
112 * for this modifier to work.
114 protected Resource getResourceToModify() {
115 return (Resource) context.getAdapter(Resource.class);
119 * @return the modifier
121 protected StringModifier getModifier() {
126 public String getValue() {
131 public String isValid(String label) {
132 if (modifierFailed != null)
133 return "Could not resolve validator for this value, modification denied. Reason: " + modifierFailed.getMessage();
134 String t = createModifierInput(label);
135 return modifier.isValid(t);
139 public final void modify(String label) {
140 if (modifierFailed != null)
141 // TODO: throw exception?
143 String t = createModifierInput(label);
144 if (!verifyModification(t))
150 * Called one last time before actually performing the modifying write
151 * transaction to verify whether this is really desired or not.
154 * This default implementation will always allow the modification to proceed.
157 * @param label the label to be given to the modifier
158 * @return <code>true</code> to go forward with the transaction,
159 * <code>false</code> to bail out
161 protected boolean verifyModification(String label) {
165 public abstract void doModify(String label);
168 * Override if necessary.
173 public String createModifierInput(String label) {