]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExportPage.java
(refs #7535) Allow only selecting models in the model export
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / ModelExportPage.java
index 05cbf4f3041454153fb303bcead9c4fb0db0bd70..8dc11477e9db3474b83e3033506f8efbc37bb999 100644 (file)
-/*******************************************************************************\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.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.Comparator;\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.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.SelectionHints;\r
-import org.simantics.db.layer0.util.DraftStatusBean;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.modeling.ModelingUtils;\r
-import org.simantics.modeling.ModelingUtils.LibraryInfo;\r
-import org.simantics.utils.strings.AlphanumComparator;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-\r
-/**\r
- * @author Antti Villberg\r
- */\r
-public class ModelExportPage extends WizardPage {\r
-\r
-    ExportPlan          exportModel;\r
-    Composite           draft;\r
-    CCombo              model;\r
-    CCombo              exportLocation;\r
-\r
-    List<LibraryInfo> models = Collections.emptyList();\r
-    private Button      overwrite;\r
-\r
-    protected ModelExportPage(ExportPlan model) {\r
-        super("Export Model", "Define Export Location", null);\r
-        this.exportModel = 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
-        GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);\r
-        draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));\r
-        GridLayoutFactory.swtDefaults().spacing(5, 5).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
-        Label draftText = new Label(draft2, SWT.WRAP);\r
-        draftText.setText("Some dependencies of the selected model are not published. The model can only be saved with draft status.");\r
-\r
-        new Label(container, SWT.NONE).setText("Exported &model:");\r
-        model = new CCombo(container, SWT.BORDER);\r
-        {\r
-            model.setEditable(false);\r
-            model.setText("");\r
-            model.setToolTipText("Selects the model to export.");\r
-            GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);\r
-            model.addSelectionListener(new SelectionAdapter() {\r
-                @Override\r
-                public void widgetSelected(SelectionEvent e) {\r
-                    int selected = model.getSelectionIndex();\r
-                    if (selected != -1) {\r
-                        exportModel.model = (LibraryInfo) model.getData(String.valueOf(selected)); \r
-                        validatePage();\r
-                    }\r
-                }\r
-            });\r
-        }\r
-\r
-        new Label(container, SWT.NONE).setText("&Target file:");\r
-        exportLocation = new CCombo(container, SWT.BORDER);\r
-        {\r
-            exportLocation.setText("");\r
-            GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);\r
-            exportLocation.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("Browse...");\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.SAVE);\r
-                    dialog.setText("Choose Export Target File");\r
-                    String loc = exportLocation.getText();\r
-                    dialog.setFilterPath(loc);\r
-                    dialog.setFilterExtensions(new String[] { "*.tg" });\r
-                    dialog.setFilterNames(new String[] { "Model (*.tg)" });\r
-                    dialog.setOverwrite(false);\r
-                    String file = dialog.open();\r
-                    if (file == null)\r
-                        return;\r
-                    exportLocation.setText(file);\r
-                    validatePage();\r
-                }\r
-            });\r
-        }\r
-\r
-        Label horizRule = new Label(container, SWT.BORDER);\r
-        GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);\r
-\r
-        overwrite = new Button(container, SWT.CHECK);\r
-        overwrite.setText("&Overwrite existing files without warning");\r
-        overwrite.setSelection(exportModel.overwrite);\r
-        GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);\r
-        overwrite.addSelectionListener(new SelectionAdapter() {\r
-            @Override\r
-            public void widgetSelected(SelectionEvent e) {\r
-                validatePage();\r
-            }\r
-        });\r
-\r
-        try {\r
-            initializeData();\r
-        } catch (DatabaseException e) {\r
-            e.printStackTrace();\r
-        }\r
-\r
-        setControl(container);\r
-        validatePage();\r
-    }\r
-\r
-    private void initializeData() throws DatabaseException {\r
-\r
-        List<Resource> selected = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);\r
-\r
-        models = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<List<LibraryInfo>>() {\r
-            @Override\r
-            public List<LibraryInfo> perform(ReadGraph graph) throws DatabaseException {\r
-                List<LibraryInfo> result = new ArrayList<>();\r
-                Layer0 L0 = Layer0.getInstance(graph);\r
-                for (Resource r : selected) {\r
-                    String name = graph.getRelatedValue(r, L0.HasName, Bindings.STRING);\r
-                    DraftStatusBean draft = ModelingUtils.getDependencyDraftStatus(graph, r);\r
-                    result.add(new LibraryInfo(name, r, draft));\r
-                }\r
-                Collections.sort(result, new Comparator<LibraryInfo>() {\r
-                    @Override\r
-                    public int compare(LibraryInfo o1, LibraryInfo o2) {\r
-                        return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.library.getName(), o2.library.getName());\r
-                    }\r
-                });\r
-                return result;\r
-            }\r
-        });\r
-\r
-        if (!models.isEmpty())\r
-            exportModel.model = models.get(0);\r
-\r
-        // Populate combo boxes\r
-        int i = 0;\r
-        for (LibraryInfo m : models) {\r
-            model.add(m.library.getName());\r
-            model.setData(String.valueOf(i), m);\r
-            if (m.equals(exportModel.model))\r
-                model.select(i);\r
-            ++i;\r
-        }\r
-\r
-        for (String path : exportModel.recentLocations) {\r
-            exportLocation.add(path);\r
-        }\r
-        if (exportLocation.getItemCount() > 0)\r
-            exportLocation.select(0);\r
-    }\r
-\r
-    void validatePage() {\r
-        if (exportModel.model == null) {\r
-            setMessage("Select library to export from.");\r
-            setErrorMessage(null);\r
-            setPageComplete(false);\r
-            return;\r
-        }\r
-\r
-        boolean draftVisible = draft.getVisible();\r
-        boolean hasDraftDependencies = exportModel.model.draft != null;\r
-        if (draftVisible != hasDraftDependencies) {\r
-            ((GridData) draft.getLayoutData()).exclude = !hasDraftDependencies;\r
-            draft.setVisible(hasDraftDependencies);\r
-            draft.getParent().getParent().layout(true, true);\r
-        }\r
-\r
-        String exportLoc = exportLocation.getText();\r
-        if (exportLoc.isEmpty()) {\r
-            setMessage("Select target file.");\r
-            setErrorMessage(null);\r
-            setPageComplete(false);\r
-            return;\r
-        }\r
-        File file = new File(exportLoc);\r
-        if (file.isDirectory()) {\r
-            setErrorMessage("The target is a directory.");\r
-            setPageComplete(false);\r
-            return;\r
-        }\r
-        File parent = file.getParentFile();\r
-        if (parent == null || !parent.isDirectory()) {\r
-            setErrorMessage("The target directory does not exist.");\r
-            setPageComplete(false);\r
-            return;\r
-        }\r
-        exportModel.exportLocation = file;\r
-        exportModel.overwrite = overwrite.getSelection();\r
-\r
-        setErrorMessage(null);\r
-        setMessage("Export selected model to " + exportModel.exportLocation + ".");\r
-        setPageComplete(true);\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.sharedontology.wizard;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.CCombo;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Label;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.layer0.util.DraftStatusBean;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ModelingUtils;
+import org.simantics.modeling.ModelingUtils.LibraryInfo;
+import org.simantics.simulation.ontology.SimulationResource;
+import org.simantics.utils.strings.AlphanumComparator;
+import org.simantics.utils.ui.ISelectionUtils;
+
+/**
+ * @author Antti Villberg
+ */
+public class ModelExportPage extends WizardPage {
+
+    ExportPlan          exportModel;
+    Composite           draft;
+    CCombo              model;
+    CCombo              exportLocation;
+
+    List<LibraryInfo> models = Collections.emptyList();
+    private Button      overwrite;
+
+    protected ModelExportPage(ExportPlan model) {
+        super("Export Model", "Define Export Location", null);
+        this.exportModel = model;
+    }
+
+    @Override
+    public void createControl(Composite parent) {
+        Composite container = new Composite(parent, SWT.NONE);
+        {
+            GridLayout layout = new GridLayout();
+            layout.horizontalSpacing = 20;
+            layout.verticalSpacing = 10;
+            layout.numColumns = 3;
+            container.setLayout(layout);
+        }
+
+        draft = new Composite(container, SWT.NONE);
+        GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
+        draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
+        GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
+
+        Composite draft2 = new Composite(draft, SWT.NONE);
+        GridLayoutFactory.swtDefaults().applyTo(draft2);
+        GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
+        Label draftText = new Label(draft2, SWT.WRAP);
+        draftText.setText("Some dependencies of the selected model are not published. The model can only be saved with draft status.");
+
+        new Label(container, SWT.NONE).setText("Exported &model:");
+        model = new CCombo(container, SWT.BORDER);
+        {
+            model.setEditable(false);
+            model.setText("");
+            model.setToolTipText("Selects the model to export.");
+            GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
+            model.addSelectionListener(new SelectionAdapter() {
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    int selected = model.getSelectionIndex();
+                    if (selected != -1) {
+                        exportModel.model = (LibraryInfo) model.getData(String.valueOf(selected)); 
+                        validatePage();
+                    }
+                }
+            });
+        }
+
+        new Label(container, SWT.NONE).setText("&Target file:");
+        exportLocation = new CCombo(container, SWT.BORDER);
+        {
+            exportLocation.setText("");
+            GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
+            exportLocation.addModifyListener(new ModifyListener(){
+                @Override
+                public void modifyText(ModifyEvent e) {
+                    validatePage();
+                }
+            });
+        }
+        Button browseFileButton = new Button(container, SWT.PUSH);
+        {
+            browseFileButton.setText("Browse...");
+            browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
+            browseFileButton.addSelectionListener(new SelectionAdapter() {
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
+                    dialog.setText("Choose Export Target File");
+                    String loc = exportLocation.getText();
+                    dialog.setFilterPath(loc);
+                    dialog.setFilterExtensions(new String[] { "*.tg" });
+                    dialog.setFilterNames(new String[] { "Model (*.tg)" });
+                    dialog.setOverwrite(false);
+                    String file = dialog.open();
+                    if (file == null)
+                        return;
+                    exportLocation.setText(file);
+                    validatePage();
+                }
+            });
+        }
+
+        Label horizRule = new Label(container, SWT.BORDER);
+        GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
+
+        overwrite = new Button(container, SWT.CHECK);
+        overwrite.setText("&Overwrite existing files without warning");
+        overwrite.setSelection(exportModel.overwrite);
+        GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
+        overwrite.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                validatePage();
+            }
+        });
+
+        try {
+            initializeData();
+        } catch (DatabaseException e) {
+            e.printStackTrace();
+        }
+
+        setControl(container);
+        validatePage();
+    }
+
+    private void initializeData() throws DatabaseException {
+
+        List<Resource> selected = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
+
+        models = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<List<LibraryInfo>>() {
+            @Override
+            public List<LibraryInfo> perform(ReadGraph graph) throws DatabaseException {
+                List<LibraryInfo> result = new ArrayList<>();
+                Layer0 L0 = Layer0.getInstance(graph);
+                SimulationResource SIMU = SimulationResource.getInstance(graph);
+                for (Resource r : selected) {
+                    Resource model = r;
+                    if(!graph.isInstanceOf(model, SIMU.Model)) {
+                        model = graph.syncRequest(new IndexRoot(r));
+                        if(!graph.isInstanceOf(model, SIMU.Model))
+                            continue;
+                    }
+                    String name = graph.getRelatedValue(model, L0.HasName, Bindings.STRING);
+                    DraftStatusBean draft = ModelingUtils.getDependencyDraftStatus(graph, model);
+                    result.add(new LibraryInfo(name, model, draft));
+                }
+                Collections.sort(result, new Comparator<LibraryInfo>() {
+                    @Override
+                    public int compare(LibraryInfo o1, LibraryInfo o2) {
+                        return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.library.getName(), o2.library.getName());
+                    }
+                });
+                return result;
+            }
+        });
+
+        if (!models.isEmpty())
+            exportModel.model = models.get(0);
+
+        // Populate combo boxes
+        int i = 0;
+        for (LibraryInfo m : models) {
+            model.add(m.library.getName());
+            model.setData(String.valueOf(i), m);
+            if (m.equals(exportModel.model))
+                model.select(i);
+            ++i;
+        }
+
+        for (String path : exportModel.recentLocations) {
+            exportLocation.add(path);
+        }
+        if (exportLocation.getItemCount() > 0)
+            exportLocation.select(0);
+    }
+
+    void validatePage() {
+        if (exportModel.model == null) {
+            setMessage("Select library to export from.");
+            setErrorMessage(null);
+            setPageComplete(false);
+            return;
+        }
+
+        boolean draftVisible = draft.getVisible();
+        boolean hasDraftDependencies = exportModel.model.draft != null;
+        if (draftVisible != hasDraftDependencies) {
+            ((GridData) draft.getLayoutData()).exclude = !hasDraftDependencies;
+            draft.setVisible(hasDraftDependencies);
+            draft.getParent().getParent().layout(true, true);
+        }
+
+        String exportLoc = exportLocation.getText();
+        if (exportLoc.isEmpty()) {
+            setMessage("Select target file.");
+            setErrorMessage(null);
+            setPageComplete(false);
+            return;
+        }
+        File file = new File(exportLoc);
+        if (file.isDirectory()) {
+            setErrorMessage("The target is a directory.");
+            setPageComplete(false);
+            return;
+        }
+        File parent = file.getParentFile();
+        if (parent == null || !parent.isDirectory()) {
+            setErrorMessage("The target directory does not exist.");
+            setPageComplete(false);
+            return;
+        }
+        exportModel.exportLocation = file;
+        exportModel.overwrite = overwrite.getSelection();
+
+        setErrorMessage(null);
+        setMessage("Export selected model to " + exportModel.exportLocation + ".");
+        setPageComplete(true);
+    }
+
+}