]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/wizard/AnnotationTypeExportPage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / wizard / AnnotationTypeExportPage.java
index 570cb0d5ff7a1759d98d12fc8c4dd52224663a09..50ff3923332ec10d0566b78f0f61d80ec32eddbb 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.annotation.ui.wizard;\r
-\r
-import java.io.File;\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.TreeMap;\r
-\r
-import org.eclipse.jface.layout.GridDataFactory;\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.NameLabelMode;\r
-import org.simantics.NameLabelUtil;\r
-import org.simantics.annotation.ontology.AnnotationResource;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.NamedResource;\r
-import org.simantics.db.common.request.PossibleIndexRoot;\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.adapter.Instances;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.utils.strings.AlphanumComparator;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- * @author Teemu Mätäsniemi\r
- */\r
-public class AnnotationTypeExportPage extends WizardPage {\r
-\r
-    ExportPlan          exportModel;\r
-    CCombo              model;\r
-    CCombo              exportLocation;\r
-\r
-    List<NamedResource> models = Collections.emptyList();\r
-    private Button      overwrite;\r
-\r
-    protected AnnotationTypeExportPage(ExportPlan model) {\r
-        super("Export Annotation Type", "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
-        new Label(container, SWT.NONE).setText("Exported &annotation type:");\r
-        model = new CCombo(container, SWT.BORDER);\r
-        {\r
-            model.setEditable(false);\r
-            model.setText("");\r
-            model.setToolTipText("Selects the annotation type to export.");\r
-            GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);\r
-            model.addModifyListener(new ModifyListener(){\r
-                @Override\r
-                public void modifyText(ModifyEvent e) {\r
-                    validatePage();\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[] { "*.annotationType" });\r
-                    dialog.setFilterNames(new String[] { "Annotation Type (*.annotationType)" });\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
-        final List<Resource> resources = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);\r
-\r
-        models = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<List<NamedResource>>() {\r
-            AnnotationResource ANNO;\r
-            Layer0 L0;\r
-            NameLabelMode mode;\r
-\r
-            @Override\r
-            public List<NamedResource> perform(ReadGraph graph) throws DatabaseException {\r
-                ANNO = AnnotationResource.getInstance(graph);\r
-                L0 = Layer0.getInstance(exportModel.sessionContext.getSession());\r
-                mode = NameLabelMode.NAME; //NameLabelUtil.getNameLabelMode(graph);\r
-\r
-                Map<String, NamedResource> result = new TreeMap<String, NamedResource>(AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);\r
-                for (Resource r : resources)\r
-                    process(graph, r, result);\r
-\r
-                if (result.isEmpty()) {\r
-                    // Look for all annotation types in the index roots contained by the selected resources.\r
-                    Set<Resource> visited = new HashSet<Resource>();\r
-                    Instances types = graph.adapt(ANNO.AnnotationType, Instances.class);\r
-                    for (Resource r : resources) {\r
-                        Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(r));\r
-                        if (indexRoot == null || !visited.add(indexRoot))\r
-                            continue;\r
-                        for (Resource at : types.find(graph, indexRoot)) {\r
-                            Resource atRoot = graph.syncRequest(new PossibleIndexRoot(at));\r
-                            if (indexRoot.equals(atRoot))\r
-                                process(graph, at, result);\r
-                        }\r
-                    }\r
-                }\r
-\r
-                return new ArrayList<NamedResource>( result.values() );\r
-            }\r
-\r
-            private NamedResource process(ReadGraph graph, Resource r, Map<String, NamedResource> result) throws DatabaseException {\r
-                NamedResource nr = process(graph, r);\r
-                if (nr != null)\r
-                    result.put(nr.getName(), nr);\r
-                return nr;\r
-            }\r
-\r
-            private NamedResource process(ReadGraph graph, Resource r) throws DatabaseException {\r
-                if (!graph.isInstanceOf(r, ANNO.AnnotationType))\r
-                    return null;\r
-                Resource property = graph.getPossibleObject(r, L0.HasRange_Inverse);\r
-                String name = pathName(graph, r, property != null ? property : r);\r
-                return new NamedResource(name, r);\r
-            }\r
-\r
-            private String pathName(ReadGraph graph, Resource r, Resource namedEntity) throws DatabaseException {\r
-                String name = graph.getRelatedValue(namedEntity, L0.HasName, Bindings.STRING);\r
-\r
-                Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(r));\r
-                if (indexRoot == null)\r
-                    return name;\r
-                Variable rootV = Variables.getPossibleVariable(graph, indexRoot);\r
-                Variable v = Variables.getPossibleVariable(graph, r);\r
-                if (v == null || rootV == null)\r
-                    return name;\r
-\r
-                List<Variable> path = Variables.getPath(graph, rootV, v);\r
-                path.add(0, rootV);\r
-                StringBuilder sb = new StringBuilder();\r
-                for (Variable p : path) {\r
-                    String segmentName = NameLabelUtil.modalName(graph, p, mode);\r
-                    sb.append(segmentName).append(" / ");\r
-                }\r
-                sb.append(name);\r
-                return sb.toString();\r
-            }\r
-        });\r
-\r
-        if (!models.isEmpty())\r
-            exportModel.model = models.get(0);\r
-\r
-        // Populate combo boxes\r
-        int i = 0;\r
-        for (NamedResource m : models) {\r
-            model.add(m.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 model to export from.");\r
-            setErrorMessage(null);\r
-            setPageComplete(false);\r
-            return;\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
-        if (!exportLoc.endsWith(".annotationType"))\r
-            exportLoc += exportLoc.endsWith(".") ? "annotationType" : ".annotationType";\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 annotation type to " + exportLoc + ".");\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.annotation.ui.wizard;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import org.eclipse.jface.layout.GridDataFactory;
+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.NameLabelMode;
+import org.simantics.NameLabelUtil;
+import org.simantics.annotation.ontology.AnnotationResource;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.request.PossibleIndexRoot;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.layer0.adapter.Instances;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.layer0.Layer0;
+import org.simantics.utils.strings.AlphanumComparator;
+import org.simantics.utils.ui.ISelectionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ * @author Teemu Mätäsniemi
+ */
+public class AnnotationTypeExportPage extends WizardPage {
+
+    ExportPlan          exportModel;
+    CCombo              model;
+    CCombo              exportLocation;
+
+    List<NamedResource> models = Collections.emptyList();
+    private Button      overwrite;
+
+    protected AnnotationTypeExportPage(ExportPlan model) {
+        super("Export Annotation Type", "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);
+        }
+
+        new Label(container, SWT.NONE).setText("Exported &annotation type:");
+        model = new CCombo(container, SWT.BORDER);
+        {
+            model.setEditable(false);
+            model.setText("");
+            model.setToolTipText("Selects the annotation type to export.");
+            GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
+            model.addModifyListener(new ModifyListener(){
+                @Override
+                public void modifyText(ModifyEvent e) {
+                    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[] { "*.annotationType" });
+                    dialog.setFilterNames(new String[] { "Annotation Type (*.annotationType)" });
+                    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 {
+        final List<Resource> resources = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
+
+        models = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<List<NamedResource>>() {
+            AnnotationResource ANNO;
+            Layer0 L0;
+            NameLabelMode mode;
+
+            @Override
+            public List<NamedResource> perform(ReadGraph graph) throws DatabaseException {
+                ANNO = AnnotationResource.getInstance(graph);
+                L0 = Layer0.getInstance(exportModel.sessionContext.getSession());
+                mode = NameLabelMode.NAME; //NameLabelUtil.getNameLabelMode(graph);
+
+                Map<String, NamedResource> result = new TreeMap<String, NamedResource>(AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);
+                for (Resource r : resources)
+                    process(graph, r, result);
+
+                if (result.isEmpty()) {
+                    // Look for all annotation types in the index roots contained by the selected resources.
+                    Set<Resource> visited = new HashSet<Resource>();
+                    Instances types = graph.adapt(ANNO.AnnotationType, Instances.class);
+                    for (Resource r : resources) {
+                        Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(r));
+                        if (indexRoot == null || !visited.add(indexRoot))
+                            continue;
+                        for (Resource at : types.find(graph, indexRoot)) {
+                            Resource atRoot = graph.syncRequest(new PossibleIndexRoot(at));
+                            if (indexRoot.equals(atRoot))
+                                process(graph, at, result);
+                        }
+                    }
+                }
+
+                return new ArrayList<NamedResource>( result.values() );
+            }
+
+            private NamedResource process(ReadGraph graph, Resource r, Map<String, NamedResource> result) throws DatabaseException {
+                NamedResource nr = process(graph, r);
+                if (nr != null)
+                    result.put(nr.getName(), nr);
+                return nr;
+            }
+
+            private NamedResource process(ReadGraph graph, Resource r) throws DatabaseException {
+                if (!graph.isInstanceOf(r, ANNO.AnnotationType))
+                    return null;
+                Resource property = graph.getPossibleObject(r, L0.HasRange_Inverse);
+                String name = pathName(graph, r, property != null ? property : r);
+                return new NamedResource(name, r);
+            }
+
+            private String pathName(ReadGraph graph, Resource r, Resource namedEntity) throws DatabaseException {
+                String name = graph.getRelatedValue(namedEntity, L0.HasName, Bindings.STRING);
+
+                Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(r));
+                if (indexRoot == null)
+                    return name;
+                Variable rootV = Variables.getPossibleVariable(graph, indexRoot);
+                Variable v = Variables.getPossibleVariable(graph, r);
+                if (v == null || rootV == null)
+                    return name;
+
+                List<Variable> path = Variables.getPath(graph, rootV, v);
+                path.add(0, rootV);
+                StringBuilder sb = new StringBuilder();
+                for (Variable p : path) {
+                    String segmentName = NameLabelUtil.modalName(graph, p, mode);
+                    sb.append(segmentName).append(" / ");
+                }
+                sb.append(name);
+                return sb.toString();
+            }
+        });
+
+        if (!models.isEmpty())
+            exportModel.model = models.get(0);
+
+        // Populate combo boxes
+        int i = 0;
+        for (NamedResource m : models) {
+            model.add(m.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 model to export from.");
+            setErrorMessage(null);
+            setPageComplete(false);
+            return;
+        }
+
+        String exportLoc = exportLocation.getText();
+        if (exportLoc.isEmpty()) {
+            setMessage("Select target file.");
+            setErrorMessage(null);
+            setPageComplete(false);
+            return;
+        }
+        if (!exportLoc.endsWith(".annotationType"))
+            exportLoc += exportLoc.endsWith(".") ? "annotationType" : ".annotationType";
+        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 annotation type to " + exportLoc + ".");
+        setPageComplete(true);
+    }
+
+}