]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/ModelingUtils.java
Add utility class org.simantics.modeling.help.HelpContexts
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ModelingUtils.java
index 4a4d97a18a2b254f0be58c2a928483dcecd234c6..a42e51a162e190e05497a5f15918fbc1fac15eef 100644 (file)
@@ -29,6 +29,7 @@ import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.dialogs.MessageDialog;
@@ -69,6 +70,7 @@ import org.simantics.db.Session;
 import org.simantics.db.Statement;
 import org.simantics.db.VirtualGraph;
 import org.simantics.db.WriteGraph;
+import org.simantics.db.WriteOnlyGraph;
 import org.simantics.db.common.Indexing;
 import org.simantics.db.common.NamedResource;
 import org.simantics.db.common.QueryMemoryWatcher;
@@ -81,10 +83,10 @@ import org.simantics.db.common.request.ObjectsWithType;
 import org.simantics.db.common.request.PossibleIndexRoot;
 import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.common.request.ResourceRead2;
+import org.simantics.db.common.request.WriteOnlyRequest;
 import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.common.request.WriteResultRequest;
 import org.simantics.db.common.utils.ListUtils;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.common.utils.OrderedSetUtils;
 import org.simantics.db.common.utils.VersionInfo;
@@ -122,7 +124,6 @@ import org.simantics.db.layer0.util.SimanticsKeys;
 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.Variables;
-import org.simantics.db.request.Read;
 import org.simantics.db.service.ClusterControl;
 import org.simantics.db.service.CollectionSupport;
 import org.simantics.db.service.GraphChangeListenerSupport;
@@ -165,12 +166,16 @@ import org.simantics.utils.datastructures.Pair;
 import org.simantics.utils.datastructures.Triple;
 import org.simantics.utils.datastructures.hints.HintContext;
 import org.simantics.utils.ui.dialogs.ListDialog;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * @author Hannu Niemistö
  */
 public class ModelingUtils {
 
+       private static final Logger LOGGER = LoggerFactory.getLogger(ModelingUtils.class);
+
        private ReadGraph g;
        private WriteGraph wg;
        public Layer0 b;
@@ -477,7 +482,7 @@ public class ModelingUtils {
                        
 
                } catch (Exception e) {
-                       Logger.defaultLogError(e);
+                       LOGGER.error("Model import failed", e);
                }
 
        }
@@ -597,7 +602,9 @@ public class ModelingUtils {
        }
 
     public static void untrackDependencies() {
-       untrackDependencies(Simantics.getSession());
+        Session s = Simantics.peekSession();
+        if (s != null)
+            untrackDependencies(s);
     }
 
        public static void untrackDependencies(RequestProcessor processor) {
@@ -614,7 +621,7 @@ public class ModelingUtils {
                 
             });
         } catch (DatabaseException e) {
-            Logger.defaultLogError(e);
+            LOGGER.error("untrackDependenices failed", e);
         }
 
     }
@@ -637,7 +644,7 @@ public class ModelingUtils {
                 
             });
         } catch (DatabaseException e) {
-            Logger.defaultLogError(e);
+            LOGGER.error("trackDependenices failed", e);
         }
 
        }
@@ -1255,7 +1262,7 @@ public class ModelingUtils {
                        return advisor.getRoot();
 
                } catch (Throwable t) {
-                       Logger.defaultLogError("Unexpected exception while importing diagram template.", t);
+                       LOGGER.error("Unexpected exception while importing diagram template.", t);
                } finally {
                }
 
@@ -1441,7 +1448,7 @@ public class ModelingUtils {
                Variant draftStatus = dc.metadata.get(DraftStatusBean.EXTENSION_KEY);
                return MigrationUtils.importSharedOntology(Simantics.getSession(), tg, draftStatus == null);
        } catch (Exception e) {
-               Logger.defaultLogError(e);
+               LOGGER.error("importSharedOntologyWithResult failed", e);
                throw e;
        }
     }
@@ -1515,53 +1522,46 @@ public class ModelingUtils {
     }
        
     public static TransferableGraph1 exportSharedOntology(IProgressMonitor monitor, RequestProcessor processor, File location, String format, int version, final LibraryInfo info) throws DatabaseException, IOException {
-       
-       if(monitor == null) monitor = new NullProgressMonitor();
-       final IProgressMonitor finalMonitor = monitor;
+        SubMonitor mon = SubMonitor.convert(monitor, "Exporting shared library", 100);
+
         // TODO: figure out a way to make the TG go directly into a file
         // instead of having it all in memory at once.
 
-        monitor.beginTask("Exporting shared library...", 100);
-       SimanticsClipboard clipboard = processor.syncRequest(new Read<SimanticsClipboard>() {
-            @Override
-            public SimanticsClipboard perform(ReadGraph graph) throws DatabaseException {
-                CopyHandler ch = graph.adapt(info.library.getResource(), CopyHandler.class);
-                SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
-                ch.copyToClipboard(graph, clipboard, finalMonitor);
-                return clipboard;
-            }
+        SimanticsClipboard clipboard = processor.syncRequest((ReadGraph graph) -> {
+            CopyHandler ch = graph.adapt(info.library.getResource(), CopyHandler.class);
+            SimanticsClipboardImpl result = new SimanticsClipboardImpl();
+            ch.copyToClipboard(graph, result, mon.split(5));
+            return result;
         });
-       
+
         TreeMap<String,Variant> metadata = getExportMetadata();
         DraftStatusBean draft = info.draft;
         if(draft != null) {
-               metadata.put(DraftStatusBean.EXTENSION_KEY, new Variant(DraftStatusBean.BINDING ,draft));
+            metadata.put(DraftStatusBean.EXTENSION_KEY, new Variant(DraftStatusBean.BINDING ,draft));
         }
-        
+
         for (Set<Representation> object : clipboard.getContents()) {
-               
+            mon.subTask("Constructing exported material");
             TransferableGraph1 tg = ClipboardUtils.accept(processor, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
-            monitor.worked(95);
-            
+            mon.worked(90);
+
             Variant edb = tg.extensions.get(ExternalDownloadBean.EXTENSION_KEY);
             if(edb != null) {
-               metadata.put(ExternalDownloadBean.EXTENSION_KEY, edb);
+                metadata.put(ExternalDownloadBean.EXTENSION_KEY, edb);
             }
 
             if(location != null) {
-                   monitor.setTaskName("Writing transferable graph...");
+                   monitor.subTask("Writing transferable graph");
                    DataContainers.writeFile(location, new DataContainer(
                            format, version,
                            metadata, new Variant(TransferableGraph1.BINDING, tg)));
                    monitor.worked(5);
             }
-            
+
             return tg;
-            
         }
-        
-        throw new DatabaseException("Failed to export");
-        
+
+        throw new DatabaseException("Export failed, no contents to export");
     }
 
     public static TreeMap<String, Variant> getExportMetadata() {
@@ -2465,16 +2465,34 @@ public class ModelingUtils {
         return DiagramGraphUtil.getModelingRules(graph, diagramResource, null);
     }
 
+    //-------------------------------------------------------------------------
+
+    private static final String VG_CHANGE_INFORMATION = "changeInformation"; //$NON-NLS-1$
+
     public static void markChanged(WriteGraph graph, Resource r) throws DatabaseException {
-        VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
-        VirtualGraph vg = support.getWorkspacePersistent("changeInformation");
-        graph.syncRequest(new WriteRequest(vg) {
+        VirtualGraph vg = Simantics.getSession().getService(VirtualGraphSupport.class)
+                .getWorkspacePersistent(VG_CHANGE_INFORMATION);
+        ModelingResources MOD = ModelingResources.getInstance(graph);
+        graph.syncRequest(new WriteOnlyRequest(vg) {
             @Override
-            public void perform(WriteGraph graph) throws DatabaseException {
-                ModelingResources MOD = ModelingResources.getInstance(graph);
+            public void perform(WriteOnlyGraph graph) throws DatabaseException {
                 graph.claim(r, MOD.changed, MOD.changed, r);
             }
         });
     }
-    
+
+    public static void markChanged(RequestProcessor processor, Iterable<Resource> rs) throws DatabaseException {
+        VirtualGraph vg = Simantics.getSession().getService(VirtualGraphSupport.class)
+                .getWorkspacePersistent(VG_CHANGE_INFORMATION);
+        ModelingResources MOD = ModelingResources.getInstance(processor);
+        processor.syncRequest(new WriteOnlyRequest(vg) {
+            @Override
+            public void perform(WriteOnlyGraph graph) throws DatabaseException {
+                for (Resource r : rs) {
+                     graph.claim(r, MOD.changed, MOD.changed, r);
+                }
+            }
+        });
+    }
+
 }