]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyImportPage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / SharedOntologyImportPage.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyImportPage.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyImportPage.java
new file mode 100644 (file)
index 0000000..5b032a3
--- /dev/null
@@ -0,0 +1,213 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.sharedontology.wizard;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.CCombo;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
+import org.simantics.db.common.NamedResource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.DraftStatusBean;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SharedOntologyImportPage extends WizardPage {\r
+\r
+    /**\r
+     * If non-null, the wizard cannot continue. This message tells why.\r
+     */\r
+    String              failure;\r
+\r
+    ImportPlan          importModel;\r
+\r
+    Composite           draft;\r
+    \r
+    Text                importTarget;\r
+    CCombo              importLocation;\r
+\r
+    List<NamedResource> models = Collections.emptyList();\r
+    Label               author;\r
+    Label               status;\r
+\r
+    protected SharedOntologyImportPage(ImportPlan model) {\r
+        super("Import Shared Library", "Define Import Location", null);\r
+        this.importModel = model;\r
+    }\r
+\r
+    @Override\r
+    public void createControl(Composite parent) {\r
+        Composite container = new Composite(parent, SWT.NONE);\r
+        {\r
+            GridLayout layout = new GridLayout();\r
+            layout.horizontalSpacing = 20;\r
+            layout.verticalSpacing = 10;\r
+            layout.numColumns = 3;\r
+            container.setLayout(layout);\r
+        }\r
+\r
+        draft = new Composite(container, SWT.NONE);\r
+        draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));\r
+        GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);\r
+       GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);\r
+        \r
+        Composite draft2 = new Composite(draft, SWT.NONE);\r
+        GridLayoutFactory.swtDefaults().applyTo(draft2);\r
+        GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);\r
+        new Label(draft2, SWT.NONE).setText("This library draft was not finished for publishing.");\r
+        \r
+        new Label(container, SWT.NONE).setText("&Shared library file:");\r
+        importLocation = new CCombo(container, SWT.BORDER);\r
+        {\r
+            importLocation.setText("");\r
+            GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);\r
+            importLocation.addModifyListener(new ModifyListener(){\r
+                @Override\r
+                public void modifyText(ModifyEvent e) {\r
+                    validatePage();\r
+                }\r
+            });\r
+        }\r
+        Button browseFileButton = new Button(container, SWT.PUSH);\r
+        {\r
+            browseFileButton.setText("Br&owse...");\r
+            browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
+            browseFileButton.addSelectionListener(new SelectionAdapter() {\r
+                @Override\r
+                public void widgetSelected(SelectionEvent e) {\r
+                    FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);\r
+                    dialog.setText("Choose Shared Library to Import");\r
+                    String loc = importLocation.getText();\r
+                    dialog.setFilterPath(loc);\r
+                    dialog.setFilterExtensions(new String[] { "*.sharedLibrary" });\r
+                    dialog.setFilterNames(new String[] { "Shared Library (*.sharedLibrary)" });\r
+                    String file = dialog.open();\r
+                    if (file == null)\r
+                        return;\r
+                    importLocation.setText(file);\r
+                    validatePage();\r
+                }\r
+            });\r
+        }\r
+        \r
+        author = new Label(container, SWT.NONE);\r
+        author.setText("");\r
+        GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(author);\r
+\r
+        status = new Label(container, SWT.NONE);\r
+        status.setText("");\r
+        GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(status);\r
+\r
+        try {\r
+            initializeData();\r
+        } catch (DatabaseException e) {\r
+            ErrorLogger.defaultLogError(e);\r
+        }\r
+\r
+        setControl(container);\r
+        validatePage();\r
+    }\r
+\r
+    private void initializeData() throws DatabaseException {\r
+        for (String path : importModel.recentLocations) {\r
+            importLocation.add(path);\r
+        }\r
+        if (importLocation.getItemCount() > 0)\r
+            importLocation.select(0);\r
+    }\r
+\r
+    void validatePage() {\r
+       \r
+        if (failure != null) {\r
+            setErrorMessage(failure);\r
+            setPageComplete(false);\r
+            return;\r
+        }\r
+        String importLoc = importLocation.getText();\r
+        if (importLoc.isEmpty()) {\r
+            setMessage("Select file to import.");\r
+            setErrorMessage(null);\r
+            setPageComplete(false);\r
+            return;\r
+        }\r
+        File file = new File(importLoc);\r
+        if (!file.exists() || !file.isFile()) {\r
+            setErrorMessage("Selected file is invalid.");\r
+            setPageComplete(false);\r
+            return;\r
+        }\r
+        importModel.importLocation = file;\r
+\r
+        try {\r
+\r
+               DataContainer container = DataContainers.readHeader(file);\r
+               Variant draftStatus = container.metadata.get(DraftStatusBean.EXTENSION_KEY);\r
+               if(draftStatus != null) {\r
+                       GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);\r
+                       draft.getParent().layout(true);\r
+               } else {\r
+                       GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);\r
+                       draft.getParent().layout(true);\r
+               }\r
+               \r
+               Variant authorVariant = container.metadata.get("author");\r
+               Variant dateVariant = container.metadata.get("date");\r
+               \r
+               if(authorVariant != null && dateVariant != null) {\r
+                       String auth = (String)authorVariant.getValue(Bindings.STRING);\r
+                       String date = (String)dateVariant.getValue(Bindings.STRING);\r
+                       author.setText("Created by " + auth + " on " + date);\r
+               } else {\r
+                       author.setText("");\r
+               }\r
+\r
+        } catch (IOException e) {\r
+               setErrorMessage("Could not read header information from " + file.getAbsolutePath());\r
+               setPageComplete(false);\r
+               return;\r
+        } catch (AdaptException e) {\r
+               setErrorMessage("Could not read header information from " + file.getAbsolutePath());\r
+               setPageComplete(false);\r
+               return;\r
+               }           \r
+        \r
+        setErrorMessage(null);\r
+        setMessage("Ready to import " + file.getName() + "");\r
+        setPageComplete(true);\r
+        \r
+    }\r
+\r
+}\r