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