1 package org.simantics.sysdyn.ui.wizards.functions;
5 import org.eclipse.core.runtime.IStatus;
6 import org.eclipse.core.runtime.Path;
7 import org.eclipse.core.runtime.Status;
8 import org.eclipse.jface.layout.GridDataFactory;
9 import org.eclipse.jface.layout.PixelConverter;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.jface.viewers.ISelectionProvider;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.jface.wizard.WizardPage;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.FileDialog;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28 import org.eclipse.swt.widgets.Tree;
29 import org.simantics.browsing.ui.common.AdaptableHintContext;
30 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.request.Read;
35 import org.simantics.sysdyn.SysdynResource;
36 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;
37 import org.simantics.ui.SimanticsUI;
38 import org.simantics.utils.datastructures.ArrayMap;
40 public class WizardFunctionsImportPage extends WizardPage{
42 // dialog store id constants
43 private Text filePathField;
45 // Keep track of the archive that we browsed to last time
46 // the wizard was invoked.
47 private static String previouslyBrowsedFile = "";
49 private Button browseDirectoriesButton;
53 //private IStructuredSelection currentSelection;
55 private Resource selectedModel;
57 GraphExplorerComposite functionLibraryExplorer;
59 private boolean selectionMade = false;
62 * Creates a new project creation wizard page.
65 public WizardFunctionsImportPage() {
66 this("wizardFunctionsImportPage", null, null); //$NON-NLS-1$
70 * Create a new instance of the receiver.
74 public WizardFunctionsImportPage(String pageName) {
75 this(pageName,null, null);
79 * More (many more) parameters.
83 * @param currentSelection
86 public WizardFunctionsImportPage(String pageName,String initialPath,
87 IStructuredSelection currentSelection) {
89 setPageComplete(false);
90 //this.currentSelection = currentSelection;
91 setTitle("Import Function Library");
92 setDescription("Choose the Function Library file and the import location, then press Finish.");
95 public void createControl(Composite parent) {
97 initializeDialogUnits(parent);
99 Composite workArea = new Composite(parent, SWT.NONE);
100 setControl(workArea);
102 workArea.setLayout(new GridLayout());
103 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
104 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
106 createProjectsRoot(workArea);
108 createTree(workArea);
113 private void createProjectsRoot(Composite workArea) {
115 //set label for field
116 Label title = new Label(workArea, SWT.NONE);
117 title.setText("Select Function Library source:");
119 Composite projectGroup = new Composite(workArea, SWT.NONE);
120 GridLayout layout = new GridLayout();
121 layout.numColumns = 2;
122 layout.makeColumnsEqualWidth = false;
123 layout.marginWidth = 0;
125 projectGroup.setLayout(layout);
126 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
128 // module location entry field
129 this.filePathField = new Text(projectGroup, SWT.BORDER);
131 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
132 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
133 filePathField.setLayoutData(directoryPathData);
136 filePathField.addModifyListener(new ModifyListener(){
138 public void modifyText(ModifyEvent e) {
139 previouslyBrowsedFile = filePathField.getText();
142 if (previouslyBrowsedFile != null){
143 filePathField.setText(previouslyBrowsedFile);
148 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
149 browseDirectoriesButton.setText("Browse");
150 setButtonLayoutData(browseDirectoriesButton);
152 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
156 * @see org.eclipse.swt.events.SelectionAdapter#widgetS
157 * elected(org.eclipse.swt.events.SelectionEvent)
159 public void widgetSelected(SelectionEvent e) {
160 handleLocationDirectoryButtonPressed();
166 private void createTree(Composite workArea){
169 Label title = new Label(workArea, SWT.NONE);
170 title.setText("Select import location:");
173 Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {
176 public Resource perform(ReadGraph graph)
177 throws DatabaseException {
178 Resource model = SimanticsUI.getProject().get();
184 functionLibraryExplorer = new GraphExplorerComposite(ArrayMap.keys(
185 "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
187 functionLibraryExplorer
188 .setBrowseContexts(SysdynResource.URIs.FunctionTree);
190 functionLibraryExplorer.finish();
192 functionLibraryExplorer.setInput(null, input);
194 GridDataFactory.fillDefaults().grab(true, true).applyTo(
195 functionLibraryExplorer);
197 ((Tree)functionLibraryExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
200 public void widgetSelected(SelectionEvent e) {
201 selectionMade = true;
205 public void widgetDefaultSelected(SelectionEvent e) {
206 selectionMade = true;
211 } catch (DatabaseException e) {
217 //Set filePathField active
218 public void setVisible(boolean visible) {
219 super.setVisible(visible);
220 this.filePathField.setFocus();
223 //Open dialog for choosing the file
224 protected void handleLocationDirectoryButtonPressed() {
226 shell = filePathField.getShell();
228 FileDialog dialog = new FileDialog(shell, SWT.OPEN);
230 String[] ext = {"*.sysdynFunctions; *.tg", "*.*"};
231 dialog.setFilterExtensions(ext);
233 dialog.setText("Import Function Library");
235 String dirName = filePathField.getText().trim();
237 File path = new File(dirName);
239 dialog.setFilterPath(new Path(dirName).toOSString());
242 String selectedFile = dialog.open();
243 if (selectedFile != null) {
244 filePathField.setText(selectedFile);
249 //Get selection from the tree
250 @SuppressWarnings("unchecked")
251 public static <T> T getExplorerResource(GraphExplorerComposite explorer,
256 ISelection selection = ((ISelectionProvider) explorer
257 .getAdapter(ISelectionProvider.class)).getSelection();
258 if (selection == null)
260 IStructuredSelection iss = (IStructuredSelection) selection;
261 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();
264 final T resource = (T) inc.getAdapter(clazz);
269 //Create project after finish is pressed.
270 public boolean createProjects() {
272 selectedModel= getExplorerResource(functionLibraryExplorer, Resource.class);
273 if(selectedModel == null){
274 setErrorMessage("Error when retrieving resource");
278 String selected = previouslyBrowsedFile;
279 if(selected == null){
280 setErrorMessage("No file selected");
284 IStatus status = ImportUtilsUI.importFunctionLibrary(selectedModel, selected, null);
287 TransferableGraph1 tg = null;
289 tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));
290 } catch (RuntimeBindingConstructionException e) {
293 } catch (IOException e) {
294 setErrorMessage("The imported file is not of type: Function Library");
298 setErrorMessage("The imported file is not of type: Function Library");
304 Boolean hasSharedOntologies;
305 hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {
308 public Boolean perform(ReadGraph graph) throws DatabaseException {
310 graph.getResource("http://SharedOntologies");
311 } catch (ResourceNotFoundException e) {
318 if(!hasSharedOntologies) {
319 SimanticsUI.getSession().syncRequest(new WriteRequest() {
322 public void perform(WriteGraph graph) throws DatabaseException {
323 Layer0 l0 = Layer0.getInstance(graph);
324 GraphUtils.create2(graph, l0.Library,
325 l0.HasName, "SharedOntologies",
326 l0.PartOf, graph.getResource("http:/"));
331 } catch (DatabaseException e) {
337 SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(selectedModel);
339 DefaultPasteHandler.defaultExecute(tg, selectedModel, ia);
340 } catch (Exception e) {
344 final Resource root = ia.getRoot();
347 SimanticsUI.getSession().syncRequest(new WriteRequest() {
350 public void perform(WriteGraph graph) throws DatabaseException {
351 Layer0 l0 = Layer0.getInstance(graph);
352 if(graph.isInstanceOf(root, SysdynResource.getInstance(graph).SharedFunctionOntology)) {
353 Resource library = graph.getResource("http://SharedOntologies");
354 if(!graph.hasStatement(library, l0.ConsistsOf, root)) {
355 graph.claim(library, l0.ConsistsOf, root);
358 SysdynResource sr = SysdynResource.getInstance(graph);
359 Resource model = selectedModel;
360 while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology))
361 model = graph.getSingleObject(model, l0.PartOf);
362 if(graph.isInstanceOf(model, sr.SysdynModel)) {
363 graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, root);
366 } else if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) {
367 Resource instanceOf = graph.getPossibleObject(root,l0.InstanceOf);
369 if(instanceOf != null)
370 type = NameUtils.getSafeName(graph, instanceOf);
372 Resource inheritedFrom = graph.getPossibleObject(root, l0.Inherits);
373 if(inheritedFrom != null)
374 type = NameUtils.getSafeName(graph, inheritedFrom);
376 graph.deny(root, l0.PartOf);
381 } catch (DatabaseException e) {
384 if (!error.isEmpty()){
385 setErrorMessage("The imported file is not of type: Function Library (" + error +")");
390 if(status == null || !status.equals(Status.OK_STATUS)) {
391 setErrorMessage(status.getMessage());
398 private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor {
400 public SysdynFunctionLibraryImportAdvisor(Resource library) {
405 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
406 if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {
408 library = graph.getResource("http://SharedOntologies");
409 } catch (ResourceNotFoundException e) {
416 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
417 Layer0 l0 = graph.getService(Layer0.class);
418 this.root = graph.newResource();
419 graph.claim(library, l0.ConsistsOf, l0.PartOf, this.root);
420 String name = root.name;
421 String newName = nameMappings.get(name);
422 graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
430 void validatePage() {
432 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
433 setPageComplete(false);
436 setErrorMessage(null);
437 setPageComplete(true);