IDEA-129633 Copy/Paste from image viewer (a=Siva Velusamy)
authorMaxim.Mossienko <Maxim.Mossienko@jetbrains.com>
Tue, 9 Sep 2014 11:11:09 +0000 (13:11 +0200)
committerMaxim.Mossienko <Maxim.Mossienko@jetbrains.com>
Tue, 9 Sep 2014 11:14:32 +0000 (13:14 +0200)
images/src/org/intellij/images/editor/impl/ImageEditorUI.java

index f3d920d3d3c3cc14d6d36397e7a5e8f6d4217446..b14296626eba09624f4c871664e94696745bf31e 100644 (file)
 package org.intellij.images.editor.impl;
 
 import com.intellij.ide.CopyPasteSupport;
+import com.intellij.ide.CopyProvider;
 import com.intellij.ide.DeleteProvider;
 import com.intellij.ide.PsiActionSupportFactory;
 import com.intellij.openapi.actionSystem.*;
+import com.intellij.openapi.ide.CopyPasteManager;
 import com.intellij.openapi.ui.Messages;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
@@ -46,12 +48,16 @@ import javax.swing.*;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 import java.awt.*;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseWheelEvent;
 import java.awt.event.MouseWheelListener;
 import java.awt.image.BufferedImage;
 import java.awt.image.ColorModel;
+import java.io.IOException;
 import java.util.Locale;
 
 /**
@@ -59,7 +65,7 @@ import java.util.Locale;
  *
  * @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
  */
-final class ImageEditorUI extends JPanel implements DataProvider {
+final class ImageEditorUI extends JPanel implements DataProvider, CopyProvider {
   @NonNls
   private static final String IMAGE_PANEL = "image";
   @NonNls
@@ -390,7 +396,7 @@ final class ImageEditorUI extends JPanel implements DataProvider {
     } else if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
       return new PsiElement[]{(PsiElement)getData(CommonDataKeys.PSI_ELEMENT.getName())};
     } else if (PlatformDataKeys.COPY_PROVIDER.is(dataId) && copyPasteSupport != null) {
-      return copyPasteSupport.getCopyProvider();
+      return this;
     } else if (PlatformDataKeys.CUT_PROVIDER.is(dataId) && copyPasteSupport != null) {
       return copyPasteSupport.getCutProvider();
     } else if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
@@ -401,4 +407,47 @@ final class ImageEditorUI extends JPanel implements DataProvider {
 
     return null;
   }
+
+  @Override
+  public void performCopy(@NotNull DataContext dataContext) {
+    ImageDocument document = imageComponent.getDocument();
+    BufferedImage image = document.getValue();
+    CopyPasteManager.getInstance().setContents(new ImageTransferable(image));
+  }
+
+  @Override
+  public boolean isCopyEnabled(@NotNull DataContext dataContext) {
+    return true;
+  }
+
+  @Override
+  public boolean isCopyVisible(@NotNull DataContext dataContext) {
+    return true;
+  }
+
+  private static class ImageTransferable implements Transferable {
+    private final BufferedImage myImage;
+
+    public ImageTransferable(@NotNull BufferedImage image) {
+      myImage = image;
+    }
+
+    @Override
+    public DataFlavor[] getTransferDataFlavors() {
+      return new DataFlavor[] { DataFlavor.imageFlavor };
+    }
+
+    @Override
+    public boolean isDataFlavorSupported(DataFlavor dataFlavor) {
+      return DataFlavor.imageFlavor.equals(dataFlavor);
+    }
+
+    @Override
+    public Object getTransferData(DataFlavor dataFlavor) throws UnsupportedFlavorException, IOException {
+      if (!DataFlavor.imageFlavor.equals(dataFlavor)) {
+        throw new UnsupportedFlavorException(dataFlavor);
+      }
+      return myImage;
+    }
+  }
 }