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