]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Improved usability of shared library export wizard 48/1648/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 23 Mar 2018 14:06:36 +0000 (16:06 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 23 Mar 2018 14:32:13 +0000 (16:32 +0200)
Now allows selection of the exported shared library from the combo box
and does not allow the user to export any model-browser-selected
resource but only shared ontologies.

refs #7130

Change-Id: I19356805e37242967cb3bec7f26bb96689248d89
(cherry picked from commit bd8834c00c213045b3967794f5841f5f33bd809c)

bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyExportPage.java

index 26675fb33cd370eea845f3348b4da0b5c88865ad..90a155231e279dce3026c0c30126df579ac28ad1 100644 (file)
 package org.simantics.modeling.ui.sharedontology.wizard;
 
 import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
 
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.layout.GridDataFactory;
@@ -31,32 +36,39 @@ 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.NameLabelMode;
+import org.simantics.NameLabelUtil;
+import org.simantics.Simantics;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
-import org.simantics.db.common.primitiverequest.RelatedValue;
-import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.Session;
+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.util.DraftStatusBean;
 import org.simantics.db.layer0.util.Layer0Utils;
-import org.simantics.layer0.Layer0;
 import org.simantics.modeling.ModelingUtils.LibraryInfo;
+import org.simantics.scl.runtime.tuple.Tuple0;
 import org.simantics.utils.ui.ISelectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * @author Antti Villberg
  */
 public class SharedOntologyExportPage extends WizardPage {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(SharedOntologyExportPage.class);
+
     ExportPlan          exportModel;
     Composite           draft;
-    CCombo              model;
+    CCombo              library;
     CCombo              exportLocation;
-
-    List<LibraryInfo> models = Collections.emptyList();
     private Button      overwrite;
 
+    List<LibraryInfo>   libraries = Collections.emptyList();
+
     protected SharedOntologyExportPage(ExportPlan model) {
         super("Export Shared Library", "Define Export Location", null);
         this.exportModel = model;
@@ -84,15 +96,17 @@ public class SharedOntologyExportPage extends WizardPage {
         new Label(draft2, SWT.NONE).setText("The shared library has not been finished for publishing. The symbol can only be saved with draft status.");
         
         new Label(container, SWT.NONE).setText("Exported &shared library:");
-        model = new CCombo(container, SWT.BORDER);
+        library = new CCombo(container, SWT.BORDER);
         {
-            model.setEditable(false);
-            model.setText("");
-            model.setToolTipText("Selects the shared library to export.");
-            GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
-            model.addModifyListener(new ModifyListener(){
+            library.setEditable(false);
+            library.setText("");
+            library.setToolTipText("Selects the shared library to export.");
+            GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(library);
+            library.addSelectionListener(new SelectionAdapter() {
                 @Override
-                public void modifyText(ModifyEvent e) {
+                public void widgetSelected(SelectionEvent e) {
+                    int i = library.getSelectionIndex();
+                    exportModel.model = i >= 0 && libraries.size() > 0 ? libraries.get(i) : null;
                     validatePage();
                 }
             });
@@ -160,11 +174,10 @@ public class SharedOntologyExportPage extends WizardPage {
             });
         }
 
-        
         try {
             initializeData();
         } catch (DatabaseException e) {
-            e.printStackTrace();
+            LOGGER.error("Failed to initialize shared ontology wizard export page", e);
         }
 
         setControl(container);
@@ -173,34 +186,59 @@ public class SharedOntologyExportPage extends WizardPage {
 
     private void initializeData() throws DatabaseException {
 
-       List<Resource> libraries = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
-        if(libraries.size() != 1) throw new RuntimeException();
+        Session session = exportModel.sessionContext.getSession();
+        List<Resource> selection = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
+        AtomicReference<Set<Resource>> selectedRoots = new AtomicReference<>();
 
-        Layer0 L0 = Layer0.getInstance(exportModel.sessionContext.getSession());
-        String name = exportModel.sessionContext.getSession().sync(new RelatedValue<String>(libraries.get(0), L0.HasName, Bindings.STRING));
+        libraries = session.syncRequest(new UniqueRead<List<LibraryInfo>>() {
+            @Override
+            public List<LibraryInfo> perform(ReadGraph graph) throws DatabaseException {
+                List<LibraryInfo> result = new ArrayList<>();
+
+                Set<Resource> roots = toRoots(graph, selection);
+                selectedRoots.set(roots);
 
-        DraftStatusBean draft = exportModel.sessionContext.getSession().syncRequest(new ResourceRead<DraftStatusBean>(libraries.get(0)) {
+                List<Resource> libs = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+                for (Resource lib : libs)
+                    result.add(library(graph, lib));
 
-                       @Override
-                       public DraftStatusBean perform(ReadGraph graph) throws DatabaseException {
-                boolean published = Layer0Utils.isPublished(graph, resource);
-                if(!published) return new DraftStatusBean(new String[0]);
-                               return null;
-                       }
-               
+                return result;
+            }
+
+            private Set<Resource> toRoots(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
+                Set<Resource> result = new HashSet<>();
+                for (Resource sel : selection) {
+                    Resource root = graph.syncRequest(new PossibleIndexRoot(sel));
+                    if (root != null)
+                        result.add(root);
+                }
+                return result;
+            }
+
+            private LibraryInfo library(ReadGraph graph, Resource library) throws DatabaseException {
+                return new LibraryInfo(
+                        NameLabelUtil.modalName(graph, library, NameLabelMode.NAME),
+                        library,
+                        isDraft(graph, library));
+            }
+
+            private DraftStatusBean isDraft(ReadGraph graph, Resource resource) throws DatabaseException {
+                return Layer0Utils.isPublished(graph, resource) ? null : new DraftStatusBean(new String[0]);
+            }
         });
-        
-        // Load all states in the selected model
-        exportModel.model = new LibraryInfo(name, libraries.get(0), draft);
-        models = Collections.singletonList(exportModel.model);
+
+        for (LibraryInfo lib : libraries) {
+            if (selectedRoots.get().contains(lib.library.getResource()))
+                exportModel.model = lib;
+        }
 
         // Populate combo boxes
         int i = 0;
-        for (LibraryInfo m : models) {
-            model.add(m.library.getName());
-            model.setData(String.valueOf(i), m);
+        for (LibraryInfo m : libraries) {
+            library.add(m.library.getName());
+            library.setData(String.valueOf(i), m);
             if (m.equals(exportModel.model))
-                model.select(i);
+                library.select(i);
             ++i;
         }
 
@@ -221,12 +259,12 @@ public class SharedOntologyExportPage extends WizardPage {
 
         if(exportModel.model.draft != null) {
             GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
-               draft.getParent().layout(true);
+            draft.getParent().layout(true);
         } else {
-               GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
-               draft.getParent().layout(true);
+            GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
+            draft.getParent().layout(true);
         }
-        
+
         String exportLoc = exportLocation.getText();
         if (exportLoc.isEmpty()) {
             setMessage("Select target file.");