1 /*******************************************************************************
\r
2 * Copyright (c) 2013 Association for Decentralized Information Management in
\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
10 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling;
\r
14 import java.util.Map;
\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
40 public class CreateSharedOntologyDialog extends ResourceSelectionDialog3<Resource> {
\r
42 private String name;
\r
45 public CreateSharedOntologyDialog(Shell shell,
\r
46 Map<Resource, Pair<String, ImageDescriptor>> parameter, String title) {
\r
47 super(shell, parameter, title);
\r
52 protected IDialogSettings getBaseDialogSettings() {
\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
67 public void modifyText(ModifyEvent e) {
\r
72 GridDataFactory.fillDefaults().grab(true, false).applyTo(t);
\r
74 return super.createExtendedContentArea(parent);
\r
78 public void create() {
\r
84 protected void handleSelected(StructuredSelection selection) {
\r
85 super.handleSelected(selection);
\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
97 // Validate that the name is not in use.
\r
98 Resource library = (Resource) selection.getFirstElement();
\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
104 updateStatus(new Status(Status.OK, PlatformUI.PLUGIN_ID, ""));
\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
112 protected String validateName(final String name) {
\r
115 boolean inUse = Simantics.sync(new UniqueRead<Boolean>() {
\r
118 public Boolean perform(ReadGraph graph) throws DatabaseException {
\r
119 Resource r = graph.getPossibleResource("http://" + name + "@A");
\r
124 if(inUse) return "Shared Library exists already";
\r
125 } catch (DatabaseException e) {
\r
126 return e.getMessage();
\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
142 public String getName() {
\r