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