1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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.model.modifiers;
\r
14 import org.simantics.browsing.ui.common.ColumnKeys;
\r
15 import org.simantics.browsing.ui.common.ErrorLogger;
\r
16 import org.simantics.browsing.ui.content.Labeler.Modifier;
\r
17 import org.simantics.db.ReadGraph;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.Session;
\r
20 import org.simantics.db.Statement;
\r
21 import org.simantics.db.WriteGraph;
\r
22 import org.simantics.db.common.request.WriteRequest;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.db.layer0.adapter.StringModifier;
\r
25 import org.simantics.db.layer0.adapter.StringModifierFactory;
\r
26 import org.simantics.db.layer0.util.Layer0Utils;
\r
29 * @author Tuukka Lehtonen
\r
31 public class StringPropertyModifierRule implements ModifierRule {
\r
33 private Resource propertyRelation;
\r
35 public StringPropertyModifierRule(ReadGraph graph, String propertyRelationURI) throws DatabaseException {
\r
36 this.propertyRelation = graph.getResource(propertyRelationURI);
\r
40 public boolean isCompatible(Class<?> contentType) {
\r
41 return contentType.equals(Resource.class);
\r
45 public Modifier getModifier(ReadGraph graph, Object content,
\r
46 String columnKey) throws DatabaseException {
\r
47 if(!ColumnKeys.SINGLE.equals(columnKey))
\r
49 if(!(content instanceof Resource))
\r
52 Resource resource = (Resource)content;
\r
53 final Statement nameStm = graph.getPossibleStatement(resource, propertyRelation);
\r
54 if (nameStm == null)
\r
57 StringModifierFactory smf = graph.getPossibleAdapter(resource, StringModifierFactory.class);
\r
59 final StringModifier sm = smf.createModifier(graph, propertyRelation, nameStm.getObject());
\r
61 return modifierFor(graph.getSession(), sm, nameStm.getObject());
\r
64 return new L0StringModifier(graph.getSession(), resource, propertyRelation, nameStm.getObject());
\r
67 private Modifier modifierFor(final Session session, final StringModifier sm, final Resource valueResource) {
\r
68 return new Modifier() {
\r
70 public String getValue() {
\r
71 return sm.getValue();
\r
73 // return session.syncRequest(new PossibleAdapter<String>(valueResource, String.class));
\r
74 // } catch (DatabaseException e) {
\r
75 // ErrorLogger.defaultLogError(e);
\r
80 public String isValid(String label) {
\r
81 return sm.isValid(label);
\r
84 public void modify(final String label) {
\r
86 session.syncRequest(new WriteRequest() {
\r
88 public void perform(WriteGraph graph) throws DatabaseException {
\r
89 Layer0Utils.addCommentMetadata(graph, "Modify string");
\r
90 graph.markUndoPoint();
\r
91 sm.modify(graph, label);
\r
94 } catch (DatabaseException e) {
\r
95 ErrorLogger.defaultLogError(e);
\r