]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardCopyHandler.java
Removed trailing newline from clipboard text
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / StandardCopyHandler.java
index c8946df682bd9f7a7459f3954abbef3bfaee4bb4..e0c57a3d17ebd08a0f24a8eec6c86d0d86e53b77 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
+ * Copyright (c) 2007, 2018 VTT Technical Research Centre of Finland and others.
  * 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
@@ -7,11 +7,11 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - gitlab simantics/platform#133
  *******************************************************************************/
 package org.simantics.modeling.ui.modelBrowser.handlers;
 
-import gnu.trove.set.hash.THashSet;
-
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Set;
@@ -21,6 +21,7 @@ import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.jface.action.IStatusLineManager;
@@ -43,7 +44,6 @@ import org.simantics.browsing.ui.NodeContext;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.request.ReadRequest;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.SelectionHints;
 import org.simantics.db.layer0.adapter.CopyHandler;
@@ -56,9 +56,15 @@ import org.simantics.utils.ui.ISelectionUtils;
 import org.simantics.utils.ui.SWTUtils;
 import org.simantics.utils.ui.SWTUtils.ControlFilter;
 import org.simantics.utils.ui.workbench.WorkbenchUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import gnu.trove.set.hash.THashSet;
 
 public class StandardCopyHandler extends AbstractHandler {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(StandardCopyHandler.class);
+
     private static IStatusLineManager status;
 
     private static List<Variable> getVariables(ISelection selection) {
@@ -97,12 +103,11 @@ public class StandardCopyHandler extends AbstractHandler {
         formatSelectionToClipboardText(event);
         final Resource[] rs = ResourceAdaptionUtils.toResources(selection);
         Job job = new Job("Copy resources") {
-            
             @Override
             protected IStatus run(IProgressMonitor monitor) {
                 monitor.beginTask("Copy resources to clipboard", 1);
                 copyResourcesToClipboard(rs, selection, SubMonitor.convert(monitor, 1));
-                return null;
+                return Status.OK_STATUS;
             }
         };
         job.setUser(true);
@@ -125,7 +130,7 @@ public class StandardCopyHandler extends AbstractHandler {
                 setCopyMessage(builder.getContents().size(), "variable");
                 return null;
             }
-            setCopyMessage(0, "");
+            setCopyMessage(0, ""); //$NON-NLS-1$
             return null;
         }
 
@@ -143,7 +148,7 @@ public class StandardCopyHandler extends AbstractHandler {
             Simantics.setClipboard(builder);
             setCopyMessage(builder.getContents().size(), "resource");
         } catch (DatabaseException e) {
-            Logger.defaultLogError(e);
+            LOGGER.error("Failed to copy {} resources to clipboard: {}", rs.length, Arrays.toString(rs), e); //$NON-NLS-1$
         }
 
         return null;
@@ -159,8 +164,11 @@ public class StandardCopyHandler extends AbstractHandler {
     }
 
     private static void setStatus(String message) {
-        if (status != null)
-            status.setMessage(message);
+        if (status != null) {
+            SWTUtils.asyncExec(
+                    PlatformUI.getWorkbench().getDisplay(),
+                    () -> status.setMessage(message));
+        }
     }
 
     private boolean formatSelectionToClipboardText(ExecutionEvent event) {
@@ -185,21 +193,23 @@ public class StandardCopyHandler extends AbstractHandler {
         Set<TreeItem> items = new THashSet<TreeItem>(selection.length);
         for (TreeItem item : selection)
             items.add(item);
+        boolean firstRow = true;
         for (TreeItem item : selection) {
+            if (!firstRow)
+                sb.append('\n');
+            firstRow = false;
             int cc = item.getParent().getColumnCount();
             int indent = indentLevel(item, items);
             for (int i = 0; i < indent; ++i)
                 sb.append('\t');
-            boolean first = true;
+            boolean firstColumn = true;
             for (int c = 0; c < cc; ++c) {
                 String ct = item.getText(c);
-                if (!first) {
+                if (!firstColumn)
                     sb.append('\t');
-                }
-                first = false;
+                firstColumn = false;
                 sb.append(ct);
             }
-            sb.append('\n');
         }
         return sb;
     }