1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
14 import java.util.function.Consumer;
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;
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.
35 * @author Tuukka Lehtonen
37 public class LabelModifier implements Modifier, Consumer<DatabaseException> {
39 private final Session session;
40 protected final Resource resource;
41 protected final Resource targetProperty;
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.
49 public LabelModifier(Session session, Resource resource) {
50 this(session, resource, session.getService(Layer0.class).HasLabel);
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
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.
61 public LabelModifier(Session session, Resource resource, Resource targetProperty) {
62 this.session = session;
63 this.resource = resource;
64 this.targetProperty = targetProperty;
68 public String getValue() {
70 return session.syncRequest(new GetLabel(resource));
71 } catch (DatabaseException e) {
72 ErrorLogger.defaultLogWarning("Problem reading current label, see exception for details.", e);
78 public String isValid(String label) {
80 return "Empty label not allowed";
85 public void modify(String label) {
86 session.asyncRequest(getWriteRequest(label), this);
89 protected Write getWriteRequest(final String label) {
90 return new WriteRequest() {
92 public void perform(WriteGraph g) throws DatabaseException {
94 Layer0Utils.addCommentMetadata(g, "Renamed " + g.getRelatedValue2(resource, Layer0.getInstance(g).HasName, Bindings.STRING) + " to " + label + " " + resource.toString());
95 g.claimLiteral(resource, targetProperty, label);
101 public void accept(DatabaseException parameter) {
102 if (parameter != null) {
103 ExceptionUtils.logError("Label modification failed, see exception for details.", parameter);