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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.graph.impl;
\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
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
34 * @author Tuukka Lehtonen
\r
36 public class LabelModifier implements Modifier, Callback<DatabaseException> {
\r
38 private final Session session;
\r
39 protected final Resource resource;
\r
40 protected final Resource targetProperty;
\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
48 public LabelModifier(Session session, Resource resource) {
\r
49 this(session, resource, session.getService(Layer0.class).HasLabel);
\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
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
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
67 public String getValue() {
\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
77 public String isValid(String label) {
\r
78 if (label.isEmpty())
\r
79 return "Empty label not allowed";
\r
84 public void modify(String label) {
\r
85 session.asyncRequest(getWriteRequest(label), this);
\r
88 protected Write getWriteRequest(final String label) {
\r
89 return new WriteRequest() {
\r
91 public void perform(WriteGraph g) throws DatabaseException {
\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
100 public void run(DatabaseException parameter) {
\r
101 if (parameter != null) {
\r
102 ExceptionUtils.logError("Label modification failed, see exception for details.", parameter);
\r