-/*******************************************************************************\r
- * Copyright (c) 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * Semantum Oy - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling;\r
-\r
-import java.util.Map;\r
-\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.jface.dialogs.IDialogSettings;\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.layout.GridLayoutFactory;\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.jface.viewers.StructuredSelection;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.ModifyEvent;\r
-import org.eclipse.swt.events.ModifyListener;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.swt.widgets.Text;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.common.uri.UnescapedChildMapOfResource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.ui.workbench.dialogs.ResourceSelectionDialog3;\r
-import org.simantics.utils.datastructures.Pair;\r
-\r
-public class CreateSharedOntologyDialog extends ResourceSelectionDialog3<Resource> {\r
-\r
- private String name;\r
- private Text t;\r
-\r
- public CreateSharedOntologyDialog(Shell shell,\r
- Map<Resource, Pair<String, ImageDescriptor>> parameter, String title) {\r
- super(shell, parameter, title);\r
- this.name = "";\r
- }\r
-\r
- @Override\r
- protected IDialogSettings getBaseDialogSettings() {\r
- return null;\r
- }\r
-\r
- @Override\r
- protected Control createExtendedContentArea(Composite parent) {\r
- Composite c = new Composite(parent, SWT.NONE);\r
- GridDataFactory.fillDefaults().grab(true, false).applyTo(c);\r
- GridLayoutFactory.swtDefaults().numColumns(2).applyTo(c);\r
- Label l = new Label(c, SWT.NONE); \r
- l.setText("Enter URI for the shared library: http://");\r
- GridDataFactory.fillDefaults().grab(false, false).applyTo(l);\r
- t = new Text(c, SWT.BORDER);\r
- t.addModifyListener(new ModifyListener() {\r
- @Override\r
- public void modifyText(ModifyEvent e) {\r
- name = t.getText();\r
- validatePage();\r
- }\r
- });\r
- GridDataFactory.fillDefaults().grab(true, false).applyTo(t);\r
- validatePage();\r
- return super.createExtendedContentArea(parent);\r
- }\r
-\r
- @Override\r
- public void create() {\r
- super.create();\r
- t.setFocus();\r
- }\r
- \r
- @Override\r
- protected void handleSelected(StructuredSelection selection) {\r
- super.handleSelected(selection);\r
- validatePage(); \r
- }\r
-\r
- protected void validatePage() {\r
- StructuredSelection selection = getSelectedItems();\r
- String error = validateName(name);\r
- if (error != null) {\r
- updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, error));\r
- } else if (selection.isEmpty()) {\r
- updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "No Library selected"));\r
- } else {\r
- // Validate that the name is not in use.\r
- Resource library = (Resource) selection.getFirstElement();\r
- try {\r
- Map<String, Resource> children = Simantics.sync(new UnescapedChildMapOfResource(library));\r
- if (children.containsKey(name)) {\r
- updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Name is already in use."));\r
- } else {\r
- updateStatus(new Status(Status.OK, PlatformUI.PLUGIN_ID, ""));\r
- }\r
- } catch (DatabaseException e) {\r
- updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Failed to check validity of name. See error log.", e));\r
- }\r
- }\r
- }\r
-\r
- protected String validateName(final String name) {\r
- \r
- try {\r
- boolean inUse = Simantics.sync(new UniqueRead<Boolean>() {\r
-\r
- @Override\r
- public Boolean perform(ReadGraph graph) throws DatabaseException {\r
- Resource r = graph.getPossibleResource("http://" + name + "@A");\r
- return r != null;\r
- }\r
- \r
- });\r
- if(inUse) return "Shared Library exists already";\r
- } catch (DatabaseException e) {\r
- return e.getMessage();\r
- }\r
- \r
- if (name.trim().isEmpty())\r
- return "Name cannot be empty";\r
- if (name.startsWith("."))\r
- return "Name cannot begin with a dot";\r
- if (name.startsWith("/"))\r
- return "Name cannot begin with a slash";\r
- if (name.contains("//"))\r
- return "Successive slashes are not allowed";\r
- if (name.endsWith("/"))\r
- return "Name cannot end with slash";\r
- return null;\r
- }\r
-\r
- public String getName() {\r
- return name;\r
- }\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling;
+
+import java.util.Map;
+
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.IDialogSettings;
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.PlatformUI;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.uri.UnescapedChildMapOfResource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.ui.workbench.dialogs.ResourceSelectionDialog3;
+import org.simantics.utils.datastructures.Pair;
+
+public class CreateSharedOntologyDialog extends ResourceSelectionDialog3<Resource> {
+
+ private String name;
+ private Text t;
+
+ public CreateSharedOntologyDialog(Shell shell,
+ Map<Resource, Pair<String, ImageDescriptor>> parameter, String title) {
+ super(shell, parameter, title);
+ this.name = "";
+ }
+
+ @Override
+ protected IDialogSettings getBaseDialogSettings() {
+ return null;
+ }
+
+ @Override
+ protected Control createExtendedContentArea(Composite parent) {
+ Composite c = new Composite(parent, SWT.NONE);
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(c);
+ GridLayoutFactory.swtDefaults().numColumns(2).applyTo(c);
+ Label l = new Label(c, SWT.NONE);
+ l.setText("Enter URI for the shared library: http://");
+ GridDataFactory.fillDefaults().grab(false, false).applyTo(l);
+ t = new Text(c, SWT.BORDER);
+ t.addModifyListener(new ModifyListener() {
+ @Override
+ public void modifyText(ModifyEvent e) {
+ name = t.getText();
+ validatePage();
+ }
+ });
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(t);
+ validatePage();
+ return super.createExtendedContentArea(parent);
+ }
+
+ @Override
+ public void create() {
+ super.create();
+ t.setFocus();
+ }
+
+ @Override
+ protected void handleSelected(StructuredSelection selection) {
+ super.handleSelected(selection);
+ validatePage();
+ }
+
+ protected void validatePage() {
+ StructuredSelection selection = getSelectedItems();
+ String error = validateName(name);
+ if (error != null) {
+ updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, error));
+ } else if (selection.isEmpty()) {
+ updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "No Library selected"));
+ } else {
+ // Validate that the name is not in use.
+ Resource library = (Resource) selection.getFirstElement();
+ try {
+ Map<String, Resource> children = Simantics.sync(new UnescapedChildMapOfResource(library));
+ if (children.containsKey(name)) {
+ updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Name is already in use."));
+ } else {
+ updateStatus(new Status(Status.OK, PlatformUI.PLUGIN_ID, ""));
+ }
+ } catch (DatabaseException e) {
+ updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Failed to check validity of name. See error log.", e));
+ }
+ }
+ }
+
+ protected String validateName(final String name) {
+
+ try {
+ boolean inUse = Simantics.sync(new UniqueRead<Boolean>() {
+
+ @Override
+ public Boolean perform(ReadGraph graph) throws DatabaseException {
+ Resource r = graph.getPossibleResource("http://" + name + "@A");
+ return r != null;
+ }
+
+ });
+ if(inUse) return "Shared Library exists already";
+ } catch (DatabaseException e) {
+ return e.getMessage();
+ }
+
+ if (name.trim().isEmpty())
+ return "Name cannot be empty";
+ if (name.startsWith("."))
+ return "Name cannot begin with a dot";
+ if (name.startsWith("/"))
+ return "Name cannot begin with a slash";
+ if (name.contains("//"))
+ return "Successive slashes are not allowed";
+ if (name.endsWith("/"))
+ return "Name cannot end with slash";
+ return null;
+ }
+
+ public String getName() {
+ return name;
+ }
+
}
\ No newline at end of file