]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/AbstractFactoryStringModifier.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 / AbstractFactoryStringModifier.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.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;
24
25 /**
26  * Please implement:
27  * <ul>
28  * <li>{@link #doModify(String)} - perform the requested modification.</li>
29  * </ul>
30  * 
31  * <p>
32  * Other points of customization:
33  * </p>
34  * <ul>
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>
43  * </ul>
44  * 
45  * @author Tuukka Lehtonen
46  * 
47  * @param <T> the input class of the used
48  *        {@link org.simantics.db.layer0.adapter.Modifier}
49  */
50 public abstract class AbstractFactoryStringModifier implements Modifier {
51
52     protected Session        session;
53
54     protected Resource       subject;
55
56     protected Resource       predicate;
57     
58     protected Resource       object;
59
60     protected String         initialValue;
61
62     protected StringModifierFactory modifierFactory;
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 AbstractFactoryStringModifier(Resource subject, Resource predicate, Resource object, RequestProcessor processor) {
77         this.subject = subject;
78         this.predicate = predicate;
79         this.object = object;
80         this.session = processor.getSession();
81
82         try {
83             processor.syncRequest(new ReadRequest() {
84                 @Override
85                 public void run(ReadGraph g) throws DatabaseException {
86                     initialValue = getInitialValue(g);
87                     initializeModifier(g);
88                 }
89             });
90         } catch (DatabaseException e) {
91             modifierFailed = e;
92         }
93     }
94
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);
102         }
103     }
104
105     /**
106      * @param g
107      * @return the value that shall be returned by {@link #getValue()}
108      */
109     protected String getInitialValue(ReadGraph g) throws DatabaseException {
110         return g.adapt(object, String.class);
111     }
112
113     /**
114      * @return the modifier
115      */
116     protected StringModifier getModifier() {
117         return modifier;
118     }
119
120     protected StringModifierFactory getModifierFactory() {
121         return modifierFactory;
122     }
123     
124     @Override
125     public String getValue() {
126         return initialValue;
127     }
128
129     @Override
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);
137     }
138
139     @Override
140     public final void modify(String label) {
141         if (modifierFailed != null)
142             // TODO: throw exception?
143             return;
144         String t = createModifierInput(label);
145         if (!verifyModification(t))
146             return;
147         doModify(t);
148     }
149
150     /**
151      * Called one last time before actually performing the modifying write
152      * transaction to verify whether this is really desired or not.
153      * 
154      * <p>
155      * This default implementation will always allow the modification to proceed.
156      * </p>
157      * 
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
161      */
162     protected boolean verifyModification(String label) {
163         return true;
164     }
165
166     public abstract void doModify(String label);
167
168     /**
169      * Override if necessary.
170      * 
171      * @param label
172      * @return
173      */
174     public String createModifierInput(String label) {
175         return label;
176     }
177
178 };