]> gerrit.simantics Code Review - simantics/platform.git/blob
28e712750b6316d852c42f615bcf47f276e8a2a1
[simantics/platform.git] /
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.model.modifiers;\r
13 \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
27 \r
28 /**\r
29  * @author Tuukka Lehtonen\r
30  */\r
31 public class StringPropertyModifierRule implements ModifierRule {\r
32 \r
33     private Resource propertyRelation;\r
34 \r
35     public StringPropertyModifierRule(ReadGraph graph, String propertyRelationURI) throws DatabaseException {\r
36         this.propertyRelation = graph.getResource(propertyRelationURI);\r
37     }\r
38 \r
39     @Override\r
40     public boolean isCompatible(Class<?> contentType) {\r
41         return contentType.equals(Resource.class);\r
42     }\r
43 \r
44     @Override\r
45     public Modifier getModifier(ReadGraph graph, Object content,\r
46             String columnKey) throws DatabaseException {\r
47         if(!ColumnKeys.SINGLE.equals(columnKey))\r
48             return null;\r
49         if(!(content instanceof Resource))\r
50             return null;\r
51 \r
52         Resource resource = (Resource)content;\r
53         final Statement nameStm = graph.getPossibleStatement(resource, propertyRelation);\r
54         if (nameStm == null)\r
55             return null;\r
56 \r
57         StringModifierFactory smf = graph.getPossibleAdapter(resource, StringModifierFactory.class);\r
58         if (smf != null) {\r
59             final StringModifier sm = smf.createModifier(graph, propertyRelation, nameStm.getObject());\r
60             if (sm != null)\r
61                 return modifierFor(graph.getSession(), sm, nameStm.getObject());\r
62         }\r
63 \r
64         return new L0StringModifier(graph.getSession(), resource, propertyRelation, nameStm.getObject());\r
65     }\r
66 \r
67     private Modifier modifierFor(final Session session, final StringModifier sm, final Resource valueResource) {\r
68         return new Modifier() {\r
69             @Override\r
70             public String getValue() {\r
71                 return sm.getValue();\r
72 //                try {\r
73 //                    return session.syncRequest(new PossibleAdapter<String>(valueResource, String.class));\r
74 //                } catch (DatabaseException e) {\r
75 //                    ErrorLogger.defaultLogError(e);\r
76 //                    return null;\r
77 //                }\r
78             }\r
79             @Override\r
80             public String isValid(String label) {\r
81                 return sm.isValid(label);\r
82             }\r
83             @Override\r
84             public void modify(final String label) {\r
85                 try {\r
86                     session.syncRequest(new WriteRequest() {\r
87                         @Override\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
92                         }\r
93                     });\r
94                 } catch (DatabaseException e) {\r
95                     ErrorLogger.defaultLogError(e);\r
96                 }\r
97             }\r
98         };\r
99     }\r
100 \r
101 }\r