Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Nick2013-09-18 07:50:57 +0000
committerMatthias Villiger2013-09-18 09:43:08 +0000
commitc7b051917b46db3ec317ed1f97ca1d7a85479850 (patch)
tree05aa45cb974b267fad64a939d3236d6a97ec25bb
parent84253a658c8297a5de85b1f8d4f0adda33b6b075 (diff)
downloadorg.eclipse.scout.rt-c7b051917b46db3ec317ed1f97ca1d7a85479850.tar.gz
org.eclipse.scout.rt-c7b051917b46db3ec317ed1f97ca1d7a85479850.tar.xz
org.eclipse.scout.rt-c7b051917b46db3ec317ed1f97ca1d7a85479850.zip
bug 415476: SWING: SwingScoutClipboardService.setContents(..)-copied
content cannot be pasted within scout application https://bugs.eclipse.org/bugs/show_bug.cgi?id=415476 Reset InputStream if it is read again and added JUnit test Change-Id: Id3fc7b55b67a71c652ecd6d289c1731f42d5b8be Signed-off-by: Matthias Nick <Matthias.Nick@bsiag.com> Reviewed-on: https://git.eclipse.org/r/16535 Tested-by: Hudson CI Reviewed-by: Matthias Villiger <mvi@bsiag.com> IP-Clean: Matthias Villiger <mvi@bsiag.com>
-rw-r--r--org.eclipse.scout.rt.ui.swing.test/src/org/eclipse/scout/rt/ui/swing/dnd/clipboard/SwingScoutClipboardTest.java125
-rw-r--r--org.eclipse.scout.rt.ui.swing/src/org/eclipse/scout/rt/ui/swing/dnd/TextTransferable.java27
2 files changed, 151 insertions, 1 deletions
diff --git a/org.eclipse.scout.rt.ui.swing.test/src/org/eclipse/scout/rt/ui/swing/dnd/clipboard/SwingScoutClipboardTest.java b/org.eclipse.scout.rt.ui.swing.test/src/org/eclipse/scout/rt/ui/swing/dnd/clipboard/SwingScoutClipboardTest.java
new file mode 100644
index 0000000000..a2ae386892
--- /dev/null
+++ b/org.eclipse.scout.rt.ui.swing.test/src/org/eclipse/scout/rt/ui/swing/dnd/clipboard/SwingScoutClipboardTest.java
@@ -0,0 +1,125 @@
+/*******************************************************************************
+ * Copyright (c) 2013 BSI Business Systems Integration AG.
+ * 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:
+ * BSI Business Systems Integration AG - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.scout.rt.ui.swing.dnd.clipboard;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.awt.Toolkit;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import javax.swing.JTextField;
+
+import org.eclipse.scout.commons.StringUtility;
+import org.eclipse.scout.commons.dnd.TextTransferObject;
+import org.eclipse.scout.rt.ui.swing.services.common.clipboard.SwingScoutClipboardService;
+import org.junit.Test;
+
+/**
+ * This JUnit test tests the clipboard functionality for Swing
+ *
+ * @since 3.10.0-M2
+ */
+public class SwingScoutClipboardTest {
+
+ private static final String clipboardText = "some Text";
+
+ @Test
+ public void testBestTextFlavor() throws Exception {
+ setTextToClipboard();
+
+ Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
+ DataFlavor[] flavors = transferable.getTransferDataFlavors();
+ assertTrue(flavors.length > 0);
+ DataFlavor bestTextFlavor = new DataFlavor().selectBestTextFlavor(flavors);
+ Object content = transferable.getTransferData(bestTextFlavor);
+ if (content instanceof String) {
+ assertEquals(clipboardText, content);
+ }
+ }
+
+ @Test
+ public void testMultiplePastes() throws Exception {
+ setTextToClipboard();
+
+ Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
+ DataFlavor[] flavors = transferable.getTransferDataFlavors();
+ assertTrue(flavors.length > 0);
+
+ //simulates paste attempt 1
+ for (DataFlavor flavor : flavors) {
+ Object content = transferable.getTransferData(flavor);
+ if (content instanceof String) {
+ assertEquals(clipboardText, content);
+ }
+ else if (content instanceof InputStream) {
+ assertEquals(clipboardText, getStringOfInputStream((InputStream) content, flavor.getParameter("charset")));
+ }
+ }
+
+ //simulates paste attempt 2
+ for (DataFlavor flavor : flavors) {
+ Object content = transferable.getTransferData(flavor);
+ if (content instanceof String) {
+ assertEquals(clipboardText, content);
+ }
+ else if (content instanceof InputStream) {
+ assertEquals(clipboardText, getStringOfInputStream((InputStream) content, flavor.getParameter("charset")));
+ }
+ }
+ }
+
+ @Test
+ public void testPasteWithTextField() throws Exception {
+ setTextToClipboard();
+
+ JTextField textfield = new JTextField();
+ textfield.paste();
+
+ assertEquals(clipboardText, textfield.getText());
+
+ textfield.paste();
+ textfield.paste();
+ textfield.paste();
+
+ assertEquals(clipboardText + clipboardText + clipboardText + clipboardText, textfield.getText());
+ }
+
+ private void setTextToClipboard() throws Exception {
+ SwingScoutClipboardService service = new SwingScoutClipboardService();
+ TextTransferObject textTransferObject = new TextTransferObject(clipboardText);
+ service.setContents(textTransferObject);
+ Thread.sleep(500); //Wait half a second to make sure the content is in the clipboard
+ }
+
+ private String getStringOfInputStream(InputStream is, String encoding) throws Exception {
+ BufferedReader br = null;
+ Reader reader = null;
+ StringBuilder sb = new StringBuilder();
+ if (StringUtility.hasText(encoding)) {
+ reader = new InputStreamReader(is, encoding);
+ }
+ else {
+ reader = new InputStreamReader(is);
+ }
+ String line;
+ br = new BufferedReader(reader);
+ while ((line = br.readLine()) != null) {
+ sb.append(line);
+ }
+ return sb.toString();
+ }
+}
diff --git a/org.eclipse.scout.rt.ui.swing/src/org/eclipse/scout/rt/ui/swing/dnd/TextTransferable.java b/org.eclipse.scout.rt.ui.swing/src/org/eclipse/scout/rt/ui/swing/dnd/TextTransferable.java
index 238bf198c5..ef0c84abea 100644
--- a/org.eclipse.scout.rt.ui.swing/src/org/eclipse/scout/rt/ui/swing/dnd/TextTransferable.java
+++ b/org.eclipse.scout.rt.ui.swing/src/org/eclipse/scout/rt/ui/swing/dnd/TextTransferable.java
@@ -66,7 +66,32 @@ public class TextTransferable implements Transferable {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
- return m_flavorMap.get(flavor);
+ Object result = m_flavorMap.get(flavor);
+ if (result instanceof InputStream) {
+ resetInputStreamToStartFromBeginning((InputStream) result);
+ }
+ return result;
+ }
+
+ /**
+ * This method resets the InputStream by setting its 'pos' attribute to 0.
+ * When the paste command is called e.g. on a textfield and a InputStream is returned,
+ * the InputStream's content is pasted to the textfield. If paste is called
+ * again, the same InputStream is returned, however the 'pos' attribute is now at the
+ * end of the content. The paste command doesn't seem to work anymore. Thus we
+ * have to reset the start position of the InputStream to 0.
+ *
+ * @since 3.10.0-M2
+ */
+ private void resetInputStreamToStartFromBeginning(InputStream is) {
+ if (is != null) {
+ try {
+ is.reset();
+ }
+ catch (IOException e) {
+ LOG.error("failed to reset the InputStream", e);
+ }
+ }
}
private InputStream toInputStream(String value, String encoding) {

Back to the top