]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/modifiers/SingleObjectStringPropertyModifierRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / modifiers / SingleObjectStringPropertyModifierRule.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.model.modifiers;
13
14 import org.simantics.browsing.ui.common.ColumnKeys;
15 import org.simantics.browsing.ui.common.ErrorLogger;
16 import org.simantics.browsing.ui.content.Labeler.Modifier;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.Statement;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.primitiverequest.PossibleAdapter;
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.StringModifierFactory;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class SingleObjectStringPropertyModifierRule implements ModifierRule {
32
33         private Resource relation;
34     private Resource propertyRelation;
35
36     public SingleObjectStringPropertyModifierRule(ReadGraph graph, String relationURI, String propertyRelationURI) throws DatabaseException {
37         this.relation = graph.getResource(relationURI);
38         this.propertyRelation = graph.getResource(propertyRelationURI);
39     }
40
41     @Override
42     public boolean isCompatible(Class<?> contentType) {
43         return contentType.equals(Resource.class);
44     }
45
46     @Override
47     public Modifier getModifier(ReadGraph graph, Object content,
48             String columnKey) throws DatabaseException {
49         if(!ColumnKeys.SINGLE.equals(columnKey))
50             return null;
51         if(!(content instanceof Resource))
52             return null;
53
54         Resource resource_ = (Resource)content;
55         
56         Resource resource = graph.getSingleObject(resource_, relation);
57         
58         final Statement nameStm = graph.getPossibleStatement(resource, propertyRelation);
59         if (nameStm == null)
60             return null;
61
62         StringModifierFactory smf = graph.getPossibleAdapter(resource, StringModifierFactory.class);
63         if (smf != null) {
64             final StringModifier sm = smf.createModifier(graph, propertyRelation, nameStm.getObject());
65             if (sm != null)
66                 return modifierFor(graph.getSession(), sm, nameStm.getObject());
67         }
68
69         return new L0StringModifier(graph.getSession(), resource, propertyRelation, nameStm.getObject());
70     }
71
72     private Modifier modifierFor(final Session session, final StringModifier sm, final Resource valueResource) {
73         return new Modifier() {
74             @Override
75             public String getValue() {
76                 try {
77                     return session.syncRequest(new PossibleAdapter<String>(valueResource, String.class));
78                 } catch (DatabaseException e) {
79                     ErrorLogger.defaultLogError(e);
80                     return null;
81                 }
82             }
83             @Override
84             public String isValid(String label) {
85                 return sm.isValid(label);
86             }
87             @Override
88             public void modify(final String label) {
89                 try {
90                     session.syncRequest(new WriteRequest() {
91                         @Override
92                         public void perform(WriteGraph graph) throws DatabaseException {
93                             sm.modify(graph, label);
94                         }
95                     });
96                 } catch (DatabaseException e) {
97                     ErrorLogger.defaultLogError(e);
98                 }
99             }
100         };
101     }
102
103 }