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