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 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;
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.
34 * @author Tuukka Lehtonen
36 public class LabelModifier implements Modifier, Callback<DatabaseException> {
38 private final Session session;
39 protected final Resource resource;
40 protected final Resource targetProperty;
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.
48 public LabelModifier(Session session, Resource resource) {
49 this(session, resource, session.getService(Layer0.class).HasLabel);
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
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.
60 public LabelModifier(Session session, Resource resource, Resource targetProperty) {
61 this.session = session;
62 this.resource = resource;
63 this.targetProperty = targetProperty;
67 public String getValue() {
69 return session.syncRequest(new GetLabel(resource));
70 } catch (DatabaseException e) {
71 ErrorLogger.defaultLogWarning("Problem reading current label, see exception for details.", e);
77 public String isValid(String label) {
79 return "Empty label not allowed";
84 public void modify(String label) {
85 session.asyncRequest(getWriteRequest(label), this);
88 protected Write getWriteRequest(final String label) {
89 return new WriteRequest() {
91 public void perform(WriteGraph g) throws DatabaseException {
93 Layer0Utils.addCommentMetadata(g, "Renamed " + g.getRelatedValue2(resource, Layer0.getInstance(g).HasName, Bindings.STRING) + " to " + label + " " + resource.toString());
94 g.claimLiteral(resource, targetProperty, label);
100 public void run(DatabaseException parameter) {
101 if (parameter != null) {
102 ExceptionUtils.logError("Label modification failed, see exception for details.", parameter);