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