]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/AbstractStringModifier.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / AbstractStringModifier.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
13
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;
25
26 /**
27  * Please implement:
28  * <ul>
29  * <li>{@link #doModify(String)} - perform the requested modification.</li>
30  * </ul>
31  * 
32  * <p>
33  * Other points of customization:
34  * </p>
35  * <ul>
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
45  * Resource.</li>
46  * <li>{@link #verifyModification(String)} - allows for last chance denial of
47  * the modification after the user has signalled approval of the modification.</li>
48  * </ul>
49  * 
50  * @author Tuukka Lehtonen
51  * 
52  * @param <T> the input class of the used
53  *        {@link org.simantics.db.layer0.adapter.Modifier}
54  */
55 public abstract class AbstractStringModifier implements Modifier {
56
57     protected NodeContext   context;
58
59     protected Session        session;
60
61     protected String         initialValue;
62
63     protected StringModifier modifier;
64
65     /**
66      * If <code>non-null</code>, the modifier could not be fetched, e.g. adapted
67      * from the specified INodeContext.
68      */
69     protected Throwable      modifierFailed;
70
71
72     /**
73      * @param context
74      * @param session
75      */
76     public AbstractStringModifier(NodeContext context, RequestProcessor processor) {
77         this.context = context;
78         this.session = processor.getSession();
79
80         final Resource r = getResourceToModify();
81         if (r == null)
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));
83
84         try {
85             processor.syncRequest(new ReadRequest() {
86                 @Override
87                 public void run(ReadGraph g) throws DatabaseException {
88                     initialValue = getInitialValue(g);
89                     AbstractStringModifier.this.modifier = g.adapt(r, StringModifier.class);
90                     initializeModifier(g);
91                 }
92             });
93         } catch (DatabaseException e) {
94             modifierFailed = e;
95         }
96     }
97
98     protected void initializeModifier(ReadGraph g) {
99     }
100
101     /**
102      * @param g
103      * @return the value that shall be returned by {@link #getValue()}
104      */
105     protected String getInitialValue(ReadGraph g) throws DatabaseException {
106         return g.adapt(getResourceToModify(), String.class);
107     }
108
109     /**
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.
113      */
114     protected Resource getResourceToModify() {
115         return (Resource) context.getAdapter(Resource.class);
116     }
117
118     /**
119      * @return the modifier
120      */
121     protected StringModifier getModifier() {
122         return modifier;
123     }
124
125     @Override
126     public String getValue() {
127         return initialValue;
128     }
129
130     @Override
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);
136     }
137
138     @Override
139     public final void modify(String label) {
140         if (modifierFailed != null)
141             // TODO: throw exception?
142             return;
143         String t = createModifierInput(label);
144         if (!verifyModification(t))
145             return;
146         doModify(t);
147     }
148
149     /**
150      * Called one last time before actually performing the modifying write
151      * transaction to verify whether this is really desired or not.
152      * 
153      * <p>
154      * This default implementation will always allow the modification to proceed.
155      * </p>
156      * 
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
160      */
161     protected boolean verifyModification(String label) {
162         return true;
163     }
164
165     public abstract void doModify(String label);
166
167     /**
168      * Override if necessary.
169      * 
170      * @param label
171      * @return
172      */
173     public String createModifierInput(String label) {
174         return label;
175     }
176
177 };