]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/CreateSharedOntologyDialog.java
Merge commit 'd7afa23'
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / CreateSharedOntologyDialog.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 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  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling;\r
13 \r
14 import java.util.Map;\r
15 \r
16 import org.eclipse.core.runtime.Status;\r
17 import org.eclipse.jface.dialogs.IDialogSettings;\r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.jface.layout.GridLayoutFactory;\r
20 import org.eclipse.jface.resource.ImageDescriptor;\r
21 import org.eclipse.jface.viewers.StructuredSelection;\r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.events.ModifyEvent;\r
24 import org.eclipse.swt.events.ModifyListener;\r
25 import org.eclipse.swt.widgets.Composite;\r
26 import org.eclipse.swt.widgets.Control;\r
27 import org.eclipse.swt.widgets.Label;\r
28 import org.eclipse.swt.widgets.Shell;\r
29 import org.eclipse.swt.widgets.Text;\r
30 import org.eclipse.ui.PlatformUI;\r
31 import org.simantics.Simantics;\r
32 import org.simantics.db.ReadGraph;\r
33 import org.simantics.db.Resource;\r
34 import org.simantics.db.common.request.UniqueRead;\r
35 import org.simantics.db.common.uri.UnescapedChildMapOfResource;\r
36 import org.simantics.db.exception.DatabaseException;\r
37 import org.simantics.ui.workbench.dialogs.ResourceSelectionDialog3;\r
38 import org.simantics.utils.datastructures.Pair;\r
39 \r
40 public class CreateSharedOntologyDialog extends ResourceSelectionDialog3<Resource> {\r
41 \r
42         private String name;\r
43         private Text t;\r
44 \r
45         public CreateSharedOntologyDialog(Shell shell,\r
46                         Map<Resource, Pair<String, ImageDescriptor>> parameter, String title) {\r
47                 super(shell, parameter, title);\r
48                 this.name = "";\r
49         }\r
50 \r
51         @Override\r
52         protected IDialogSettings getBaseDialogSettings() {\r
53                 return null;\r
54         }\r
55 \r
56         @Override\r
57         protected Control createExtendedContentArea(Composite parent) {\r
58                 Composite c = new Composite(parent, SWT.NONE);\r
59                 GridDataFactory.fillDefaults().grab(true, false).applyTo(c);\r
60                 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(c);\r
61                 Label l = new Label(c, SWT.NONE); \r
62                 l.setText("Enter URI for the shared library:  http://");\r
63                 GridDataFactory.fillDefaults().grab(false, false).applyTo(l);\r
64                 t = new Text(c, SWT.BORDER);\r
65                 t.addModifyListener(new ModifyListener() {\r
66                         @Override\r
67                         public void modifyText(ModifyEvent e) {\r
68                                 name = t.getText();\r
69                                 validatePage();\r
70                         }\r
71                 });\r
72                 GridDataFactory.fillDefaults().grab(true, false).applyTo(t);\r
73                 validatePage();\r
74                 return super.createExtendedContentArea(parent);\r
75         }\r
76 \r
77         @Override\r
78         public void create() {\r
79                 super.create();\r
80                 t.setFocus();\r
81         }\r
82         \r
83         @Override\r
84         protected void handleSelected(StructuredSelection selection) {\r
85                 super.handleSelected(selection);\r
86                 validatePage(); \r
87         }\r
88 \r
89         protected void validatePage() {\r
90                 StructuredSelection selection = getSelectedItems();\r
91                 String error = validateName(name);\r
92                 if (error != null) {\r
93                         updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, error));\r
94                 } else if (selection.isEmpty()) {\r
95                         updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "No Library selected"));\r
96                 } else {\r
97                         // Validate that the name is not in use.\r
98                         Resource library = (Resource) selection.getFirstElement();\r
99                         try {\r
100                                 Map<String, Resource> children = Simantics.sync(new UnescapedChildMapOfResource(library));\r
101                                 if (children.containsKey(name)) {\r
102                                         updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Name is already in use."));\r
103                                 } else {\r
104                                         updateStatus(new Status(Status.OK, PlatformUI.PLUGIN_ID, ""));\r
105                                 }\r
106                         } catch (DatabaseException e) {\r
107                                 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Failed to check validity of name. See error log.", e));\r
108                         }\r
109                 }\r
110         }\r
111 \r
112         protected String validateName(final String name) {\r
113                 \r
114                 try {\r
115                         boolean inUse = Simantics.sync(new UniqueRead<Boolean>() {\r
116 \r
117                                 @Override\r
118                                 public Boolean perform(ReadGraph graph) throws DatabaseException {\r
119                                         Resource r = graph.getPossibleResource("http://" + name + "@A");\r
120                                         return r != null;\r
121                                 }\r
122                                 \r
123                         });\r
124                         if(inUse) return "Shared Library exists already";\r
125                 } catch (DatabaseException e) {\r
126                         return e.getMessage();\r
127                 }\r
128                 \r
129                 if (name.trim().isEmpty())\r
130                         return "Name cannot be empty";\r
131                 if (name.startsWith("."))\r
132                         return "Name cannot begin with a dot";\r
133                 if (name.startsWith("/"))\r
134                         return "Name cannot begin with a slash";\r
135                 if (name.contains("//"))\r
136                         return "Successive slashes are not allowed";\r
137                 if (name.endsWith("/"))\r
138                         return "Name cannot end with slash";\r
139                 return null;\r
140         }\r
141 \r
142         public String getName() {\r
143                 return name;\r
144         }\r
145 \r
146 }