]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/CreateSharedOntologyDialog.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / CreateSharedOntologyDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling;
13
14 import java.util.Map;
15
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;
39
40 public class CreateSharedOntologyDialog extends ResourceSelectionDialog3<Resource> {
41
42         private String name;
43         private Text t;
44
45         public CreateSharedOntologyDialog(Shell shell,
46                         Map<Resource, Pair<String, ImageDescriptor>> parameter, String title) {
47                 super(shell, parameter, title);
48                 this.name = "";
49         }
50
51         @Override
52         protected IDialogSettings getBaseDialogSettings() {
53                 return null;
54         }
55
56         @Override
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() {
66                         @Override
67                         public void modifyText(ModifyEvent e) {
68                                 name = t.getText();
69                                 validatePage();
70                         }
71                 });
72                 GridDataFactory.fillDefaults().grab(true, false).applyTo(t);
73                 validatePage();
74                 return super.createExtendedContentArea(parent);
75         }
76
77         @Override
78         public void create() {
79                 super.create();
80                 t.setFocus();
81         }
82         
83         @Override
84         protected void handleSelected(StructuredSelection selection) {
85                 super.handleSelected(selection);
86                 validatePage(); 
87         }
88
89         protected void validatePage() {
90                 StructuredSelection selection = getSelectedItems();
91                 String error = validateName(name);
92                 if (error != null) {
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"));
96                 } else {
97                         // Validate that the name is not in use.
98                         Resource library = (Resource) selection.getFirstElement();
99                         try {
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."));
103                                 } else {
104                                         updateStatus(new Status(Status.OK, PlatformUI.PLUGIN_ID, ""));
105                                 }
106                         } catch (DatabaseException e) {
107                                 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Failed to check validity of name. See error log.", e));
108                         }
109                 }
110         }
111
112         protected String validateName(final String name) {
113                 
114                 try {
115                         boolean inUse = Simantics.sync(new UniqueRead<Boolean>() {
116
117                                 @Override
118                                 public Boolean perform(ReadGraph graph) throws DatabaseException {
119                                         Resource r = graph.getPossibleResource("http://" + name + "@A");
120                                         return r != null;
121                                 }
122                                 
123                         });
124                         if(inUse) return "Shared Library exists already";
125                 } catch (DatabaseException e) {
126                         return e.getMessage();
127                 }
128                 
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";
139                 return null;
140         }
141
142         public String getName() {
143                 return name;
144         }
145
146 }