From: Tuukka Lehtonen Date: Wed, 28 Mar 2018 19:44:18 +0000 (+0300) Subject: Fixed compilation problems and SWTAWTComponent focus problem with Java9+ X-Git-Tag: v1.43.0~136^2~517 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=2444a7d3ebc4aea7f9899dbf8aa84c8f94e75fd9;p=simantics%2Fplatform.git Fixed compilation problems and SWTAWTComponent focus problem with Java9+ refs #7848 Change-Id: If166f7864d05c67f4c58b87018b273a36f085801 --- diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/dnd/LocalObjectTransfer.java b/bundles/org.simantics.ui/src/org/simantics/ui/dnd/LocalObjectTransfer.java index 5353ec1bb..a920c82c1 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/dnd/LocalObjectTransfer.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/dnd/LocalObjectTransfer.java @@ -11,10 +11,7 @@ *******************************************************************************/ package org.simantics.ui.dnd; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.FlavorTable; import java.awt.datatransfer.SystemFlavorMap; -import java.util.Map; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; @@ -23,8 +20,6 @@ import org.eclipse.jface.util.Policy; import org.eclipse.swt.dnd.ByteArrayTransfer; import org.eclipse.swt.dnd.TransferData; -import sun.awt.datatransfer.DataTransferer; - public class LocalObjectTransfer extends ByteArrayTransfer { // First attempt to create a UUID for the type name to make sure that @@ -32,17 +27,21 @@ public class LocalObjectTransfer extends ByteArrayTransfer { // LocalSelectionTransfer public static final String TYPE_NAME = SystemFlavorMap.encodeJavaMIMEType(LocalObjectTransferable.TYPE_NAME); - private static int getMapping() { - - for(Map.Entry entry : SystemFlavorMap.getDefaultFlavorMap().getNativesForFlavors(new DataFlavor[] { LocalObjectTransferable.FLAVOR }).entrySet()) { - for(long l : DataTransferer.getInstance().getFormatsForFlavorAsArray(entry.getKey(), (FlavorTable)SystemFlavorMap.getDefaultFlavorMap())) { - return (int)l; - } - } - - throw new Error("No mapping."); - - } +// private static int getMapping() { +// +// for(Map.Entry entry : SystemFlavorMap.getDefaultFlavorMap().getNativesForFlavors(new DataFlavor[] { LocalObjectTransferable.FLAVOR }).entrySet()) { +// for(long l : DataTransferer.getInstance().getFormatsForFlavorAsArray(entry.getKey(), (FlavorTable)SystemFlavorMap.getDefaultFlavorMap())) { +// return (int)l; +// } +// } +// DataFlavor[] flavors = SystemFlavorMap.getDefaultFlavorMap().getNativesForFlavors(new DataFlavor[] { LocalObjectTransferable.FLAVOR }).keySet().toArray(new DataFlavor[0]); +// for(long l : DataTransferer.getInstance().getFormatsForFlavors(flavors, (FlavorTable)SystemFlavorMap.getDefaultFlavorMap()).keySet()) { +// return (int)l; +// } +// +// throw new Error("No mapping."); +// +// } // In Linux (Ubuntu 14.04) using SystemFlavorMap to get the type id gives different result than what is received with drop data (TransferData.type) private static final int TYPEID = registerType(TYPE_NAME);//getMapping(); diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTAWTComponent.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTAWTComponent.java index 8031a93ab..2f2f604ec 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTAWTComponent.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTAWTComponent.java @@ -60,6 +60,8 @@ import org.simantics.utils.ui.internal.awt.AwtFocusHandler; import org.simantics.utils.ui.internal.awt.CleanResizeListener; import org.simantics.utils.ui.internal.awt.EmbeddedChildFocusTraversalPolicy; import org.simantics.utils.ui.internal.awt.SwtFocusHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** @@ -99,6 +101,8 @@ import org.simantics.utils.ui.internal.awt.SwtFocusHandler; */ public abstract class SWTAWTComponent extends Composite { + private static final Logger LOGGER = LoggerFactory.getLogger(SWTAWTComponent.class); + private static class AwtContext { private Frame frame; private Component swingComponent; @@ -342,14 +346,27 @@ public abstract class SWTAWTComponent extends Composite { private void workaroundJava7FocusProblem(Frame frame) { String ver = System.getProperty("java.version"); - if (ver.startsWith("1.7") || ver.startsWith("1.8")) { - try { - frame.addWindowListener(new Java7FocusFixListener(this, frame)); - } catch (SecurityException e) { - Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e)); - } catch (NoSuchMethodException e) { - Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e)); + String[] split = ver.split("."); + + if (split.length < 2) { + LOGGER.warn("Focus fix listener: unrecognized Java version: " + ver); + return; + } + + try { + int major = Integer.parseInt(split[0]); + int minor = Integer.parseInt(split[1]); + if ((major == 1 && (minor == 7 || minor == 8)) || major >= 9) { + try { + frame.addWindowListener(new Java7FocusFixListener(this, frame)); + } catch (SecurityException e) { + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e)); + } catch (NoSuchMethodException e) { + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e)); + } } + } catch (NumberFormatException e) { + LOGGER.error("Focus fix listener: unrecognized Java version: " + ver); } } diff --git a/bundles/org.simantics.utils.ui/testcases/org/simantics/utils/ui/SWTAWTTest.java b/bundles/org.simantics.utils.ui/testcases/org/simantics/utils/ui/SWTAWTTest.java index f0a445087..f963fe4c7 100644 --- a/bundles/org.simantics.utils.ui/testcases/org/simantics/utils/ui/SWTAWTTest.java +++ b/bundles/org.simantics.utils.ui/testcases/org/simantics/utils/ui/SWTAWTTest.java @@ -177,9 +177,9 @@ public class SWTAWTTest { private static JComponent createTable() { /* Creating components */ int nrows = 1000, ncolumns = 10; - Vector rows = new Vector(); + Vector> rows = new Vector<>(); for (int i = 0; i < nrows; i++) { - Vector row = new Vector(); + Vector row = new Vector<>(); for (int j = 0; j < ncolumns; j++) { row.addElement("Item " + i + "-" + j); }