1 /*******************************************************************************
2 * Copyright (c) 2015 Association for Decentralized Information Management in
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 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser2.label;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.simantics.Simantics;
17 import org.simantics.browsing.ui.content.Labeler.Modifier;
18 import org.simantics.browsing.ui.model.modifiers.ModifierRule;
19 import org.simantics.browsing.ui.swt.Activator;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.adapter.StringModifier;
26 import org.simantics.db.layer0.adapter.impl.RelatedEntityNameModifier;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.modeling.ModelingResources;
29 import org.simantics.modeling.adapters.ConnectionRelationStringModifierFactory;
32 * @author Tuukka Lehtonen
33 * @see ConnectionRelationStringModifierFactory
35 public class ConnectionRelationModifierRule implements ModifierRule {
38 public boolean isCompatible(Class<?> contentType) {
39 return contentType.equals(Resource.class);
43 public Modifier getModifier(ReadGraph graph, Object content, String columnKey) throws DatabaseException {
44 Resource connectionRelation = (Resource) content;
46 // Always rename all (configuration and diagram) connection relations
47 Layer0 L0 = Layer0.getInstance(graph);
48 ModelingResources MOD = ModelingResources.getInstance(graph);
49 Resource cr = graph.getPossibleObject(connectionRelation, MOD.DiagramConnectionRelationToConnectionRelation);
50 Resource subject = cr != null ? cr : connectionRelation;
51 Resource property = graph.getSingleObject(subject, L0.HasName);
52 return new StringModifierAdapter(
53 new RelatedEntityNameModifier(graph, subject, property, MOD.ConnectionRelationToDiagramConnectionRelation));
56 static class StringModifierAdapter implements Modifier {
57 private StringModifier modifier;
59 public StringModifierAdapter(StringModifier modifier) {
60 this.modifier = modifier;
63 public void modify(final String label) {
65 Simantics.getSession().syncRequest(new WriteRequest() {
67 public void perform(WriteGraph graph) throws DatabaseException {
68 modifier.modify(graph, label);
71 } catch (DatabaseException e) {
72 Activator.getDefault().getLog().log(
73 new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Name modification failed, see exception for details.", e));
78 public String isValid(String label) {
79 return modifier.isValid(label);
83 public String getValue() {
84 return modifier.getValue();