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