1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.jface.dialogs.IDialogSettings;
18 import org.eclipse.jface.layout.GridDataFactory;
19 import org.eclipse.jface.layout.GridLayoutFactory;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.ModifyEvent;
24 import org.eclipse.swt.events.ModifyListener;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
30 import org.eclipse.ui.PlatformUI;
31 import org.simantics.Simantics;
32 import org.simantics.db.ReadGraph;
33 import org.simantics.db.Resource;
34 import org.simantics.db.common.request.UniqueRead;
35 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
36 import org.simantics.db.exception.DatabaseException;
37 import org.simantics.ui.workbench.dialogs.ResourceSelectionDialog3;
38 import org.simantics.utils.datastructures.Pair;
40 public class CreateSharedOntologyDialog extends ResourceSelectionDialog3<Resource> {
45 public CreateSharedOntologyDialog(Shell shell,
46 Map<Resource, Pair<String, ImageDescriptor>> parameter, String title) {
47 super(shell, parameter, title);
52 protected IDialogSettings getBaseDialogSettings() {
57 protected Control createExtendedContentArea(Composite parent) {
58 Composite c = new Composite(parent, SWT.NONE);
59 GridDataFactory.fillDefaults().grab(true, false).applyTo(c);
60 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(c);
61 Label l = new Label(c, SWT.NONE);
62 l.setText("Enter URI for the shared library: http://");
63 GridDataFactory.fillDefaults().grab(false, false).applyTo(l);
64 t = new Text(c, SWT.BORDER);
65 t.addModifyListener(new ModifyListener() {
67 public void modifyText(ModifyEvent e) {
72 GridDataFactory.fillDefaults().grab(true, false).applyTo(t);
74 return super.createExtendedContentArea(parent);
78 public void create() {
84 protected void handleSelected(StructuredSelection selection) {
85 super.handleSelected(selection);
89 protected void validatePage() {
90 StructuredSelection selection = getSelectedItems();
91 String error = validateName(name);
93 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, error));
94 } else if (selection.isEmpty()) {
95 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "No Library selected"));
97 // Validate that the name is not in use.
98 Resource library = (Resource) selection.getFirstElement();
100 Map<String, Resource> children = Simantics.sync(new UnescapedChildMapOfResource(library));
101 if (children.containsKey(name)) {
102 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Name is already in use."));
104 updateStatus(new Status(Status.OK, PlatformUI.PLUGIN_ID, ""));
106 } catch (DatabaseException e) {
107 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Failed to check validity of name. See error log.", e));
112 protected String validateName(final String name) {
115 boolean inUse = Simantics.sync(new UniqueRead<Boolean>() {
118 public Boolean perform(ReadGraph graph) throws DatabaseException {
119 Resource r = graph.getPossibleResource("http://" + name + "@A");
124 if(inUse) return "Shared Library exists already";
125 } catch (DatabaseException e) {
126 return e.getMessage();
129 if (name.trim().isEmpty())
130 return "Name cannot be empty";
131 if (name.startsWith("."))
132 return "Name cannot begin with a dot";
133 if (name.startsWith("/"))
134 return "Name cannot begin with a slash";
135 if (name.contains("//"))
136 return "Successive slashes are not allowed";
137 if (name.endsWith("/"))
138 return "Name cannot end with slash";
142 public String getName() {