]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LabelModifier.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LabelModifier.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.content.Labeler.Modifier;\r
15 import org.simantics.browsing.ui.graph.impl.request.GetLabel;\r
16 import org.simantics.databoard.Bindings;\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.WriteRequest;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.layer0.util.Layer0Utils;\r
23 import org.simantics.db.request.Write;\r
24 import org.simantics.layer0.Layer0;\r
25 import org.simantics.utils.datastructures.Callback;\r
26 import org.simantics.utils.ui.ErrorLogger;\r
27 import org.simantics.utils.ui.ExceptionUtils;\r
28 \r
29 /**\r
30  * NOTE: By default writing Label property, NOT Name property! Name is\r
31  * considered a unique immutable identifier for the model. This helps with\r
32  * writing stuff on disk and keeping the names coherent.\r
33  * \r
34  * @author Tuukka Lehtonen\r
35  */\r
36 public class LabelModifier implements Modifier, Callback<DatabaseException> {\r
37 \r
38     private final Session  session;\r
39     protected final Resource resource;\r
40     protected final Resource targetProperty;\r
41 \r
42     /**\r
43      * @param session database access point\r
44      * @param resource the resource for which to modify the label\r
45      * @param writeCallback a callback that is invoked once the graph write has\r
46      *        finished. The exception will be null if the write was successful.\r
47      */\r
48     public LabelModifier(Session session, Resource resource) {\r
49         this(session, resource, session.getService(Layer0.class).HasLabel);\r
50     }\r
51 \r
52     /**\r
53      * @param session database access point\r
54      * @param resource the resource for which to modify the label\r
55      * @param targetProperty the relation of the target property to be claimed\r
56      *        by the modifier.\r
57      * @param writeCallback a callback that is invoked once the graph write has\r
58      *        finished. The exception will be null if the write was successful.\r
59      */\r
60     public LabelModifier(Session session, Resource resource, Resource targetProperty) {\r
61         this.session = session;\r
62         this.resource = resource;\r
63         this.targetProperty = targetProperty;\r
64     }\r
65 \r
66     @Override\r
67     public String getValue() {\r
68         try {\r
69             return session.syncRequest(new GetLabel(resource));\r
70         } catch (DatabaseException e) {\r
71             ErrorLogger.defaultLogWarning("Problem reading current label, see exception for details.", e);\r
72             return "";\r
73         }\r
74     }\r
75 \r
76     @Override\r
77     public String isValid(String label) {\r
78         if (label.isEmpty())\r
79             return "Empty label not allowed";\r
80         return null;\r
81     }\r
82 \r
83     @Override\r
84     public void modify(String label) {\r
85         session.asyncRequest(getWriteRequest(label), this);\r
86     }\r
87 \r
88     protected Write getWriteRequest(final String label) {\r
89         return new WriteRequest() {\r
90             @Override\r
91             public void perform(WriteGraph g) throws DatabaseException {\r
92                 g.markUndoPoint();\r
93                 Layer0Utils.addCommentMetadata(g, "Renamed " + g.getRelatedValue2(resource, Layer0.getInstance(g).HasName, Bindings.STRING) + " to " + label + " " + resource.toString());\r
94                 g.claimLiteral(resource, targetProperty, label);\r
95             }\r
96         };\r
97     }\r
98 \r
99     @Override\r
100     public void run(DatabaseException parameter) {\r
101         if (parameter != null) {\r
102             ExceptionUtils.logError("Label modification failed, see exception for details.", parameter);\r
103         }\r
104     }\r
105 \r
106 }\r