]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
726ab4ac594431a4b737f5563d725d59e6db7206
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.properties.widgets.externalFiles;\r
13 \r
14 import org.simantics.browsing.ui.common.node.AbstractNode;\r
15 import org.simantics.browsing.ui.common.node.DeleteException;\r
16 import org.simantics.browsing.ui.common.node.IDeletableNode;\r
17 import org.simantics.browsing.ui.common.node.IModifiableNode;\r
18 import org.simantics.browsing.ui.content.Labeler.Modifier;\r
19 import org.simantics.browsing.ui.graph.impl.LabelModifier;\r
20 import org.simantics.db.Resource;\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.util.RemoverUtil;\r
25 import org.simantics.db.request.Write;\r
26 import org.simantics.layer0.Layer0;\r
27 import org.simantics.sysdyn.manager.FunctionUtils;\r
28 import org.simantics.ui.SimanticsUI;\r
29 \r
30 public class ExternalFileNode extends AbstractNode<Resource> implements IModifiableNode, IDeletableNode {\r
31 \r
32         public ExternalFileNode(Resource data) {\r
33                 super(data);\r
34         }\r
35 \r
36         @Override\r
37         public Modifier getModifier(String columnId) {\r
38                 try {\r
39                         final Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
40                         LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
41                                 @Override\r
42                                 public String isValid(String label) {\r
43                                         if (label.isEmpty())\r
44                                                 return "Empty label not allowed";\r
45                                         if (label.contains(" "))\r
46                                                 return "Spaces are not allowed";\r
47                                         return null;\r
48                                 }\r
49 \r
50                                 @Override\r
51                                 protected Write getWriteRequest(final String label) {\r
52                                         return new WriteRequest() {\r
53                                                 @Override\r
54                                                 public void perform(WriteGraph g) throws DatabaseException {\r
55                                                         FunctionUtils.removeExternalFunctionFile(g, data);\r
56                                                         g.claimLiteral(data, hasName, label);\r
57                                                         FunctionUtils.createExternalFunctionFile(g, data);\r
58                                                 }\r
59                                         };\r
60                                 }\r
61                         };\r
62                         return modifier;\r
63                 } catch (DatabaseException e) {\r
64                         e.printStackTrace();\r
65                 }\r
66                 return null;\r
67         }\r
68 \r
69         @Override\r
70         public void delete() throws DeleteException {\r
71                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
72 \r
73                         @Override\r
74                         public void perform(WriteGraph graph) throws DatabaseException {\r
75                                 FunctionUtils.removeExternalFunctionFile(graph, data);\r
76                                 RemoverUtil.remove(graph, data);\r
77                         }\r
78                 });\r
79         }\r
80 \r
81 }\r