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.content.Labeler.Modifier;
\r
15 import org.simantics.db.ReadGraph;
\r
16 import org.simantics.db.RequestProcessor;
\r
17 import org.simantics.db.Resource;
\r
18 import org.simantics.db.Session;
\r
19 import org.simantics.db.WriteGraph;
\r
20 import org.simantics.db.common.request.ReadRequest;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.layer0.adapter.StringModifier;
\r
23 import org.simantics.db.layer0.adapter.StringModifierFactory;
\r
28 * <li>{@link #doModify(String)} - perform the requested modification.</li>
\r
32 * Other points of customization:
\r
35 * <li>{@link #createModifierInput(String)} - constructs an input for
\r
36 * {@link org.simantics.db.layer0.adapter.Modifier#modify(WriteGraph, Object)}
\r
37 * from the specified label given by the user.
\r
38 * <li>{@link #getInitialValue(ReadGraph)} - returns the value that should be shown
\r
39 * initially when editing. The default implementation just adapts the input to
\r
40 * its String representation, but you may want to customize this.</li>
\r
41 * <li>{@link #verifyModification(String)} - allows for last chance denial of
\r
42 * the modification after the user has signalled approval of the modification.</li>
\r
45 * @author Tuukka Lehtonen
\r
47 * @param <T> the input class of the used
\r
48 * {@link org.simantics.db.layer0.adapter.Modifier}
\r
50 public abstract class AbstractFactoryStringModifier implements Modifier {
\r
52 protected Session session;
\r
54 protected Resource subject;
\r
56 protected Resource predicate;
\r
58 protected Resource object;
\r
60 protected String initialValue;
\r
62 protected StringModifierFactory modifierFactory;
\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 AbstractFactoryStringModifier(Resource subject, Resource predicate, Resource object, RequestProcessor processor) {
\r
77 this.subject = subject;
\r
78 this.predicate = predicate;
\r
79 this.object = object;
\r
80 this.session = processor.getSession();
\r
83 processor.syncRequest(new ReadRequest() {
\r
85 public void run(ReadGraph g) throws DatabaseException {
\r
86 initialValue = getInitialValue(g);
\r
87 initializeModifier(g);
\r
90 } catch (DatabaseException e) {
\r
95 protected void initializeModifier(ReadGraph graph) throws DatabaseException {
\r
96 modifierFactory = graph.getPossibleAdapter(subject, StringModifierFactory.class);
\r
97 if (modifierFactory != null)
\r
98 modifier = modifierFactory.createModifier(graph, predicate, object);
\r
99 if (modifier == null) {
\r
100 modifierFactory = null;
\r
101 modifier = graph.adapt(object, StringModifier.class);
\r
107 * @return the value that shall be returned by {@link #getValue()}
\r
109 protected String getInitialValue(ReadGraph g) throws DatabaseException {
\r
110 return g.adapt(object, String.class);
\r
114 * @return the modifier
\r
116 protected StringModifier getModifier() {
\r
120 protected StringModifierFactory getModifierFactory() {
\r
121 return modifierFactory;
\r
125 public String getValue() {
\r
126 return initialValue;
\r
130 public String isValid(String label) {
\r
131 if (modifierFailed != null)
\r
132 return modifierFailed.getMessage();
\r
133 if (modifier == null)
\r
134 return "No modifier available";
\r
135 String t = createModifierInput(label);
\r
136 return modifier.isValid(t);
\r
140 public final void modify(String label) {
\r
141 if (modifierFailed != null)
\r
142 // TODO: throw exception?
\r
144 String t = createModifierInput(label);
\r
145 if (!verifyModification(t))
\r
151 * Called one last time before actually performing the modifying write
\r
152 * transaction to verify whether this is really desired or not.
\r
155 * This default implementation will always allow the modification to proceed.
\r
158 * @param label the label to be given to the modifier
\r
159 * @return <code>true</code> to go forward with the transaction,
\r
160 * <code>false</code> to bail out
\r
162 protected boolean verifyModification(String label) {
\r
166 public abstract void doModify(String label);
\r
169 * Override if necessary.
\r
174 public String createModifierInput(String label) {
\r