PY-19960 PY-14096 Merge right-click menu creation for toggle output and split/merge...
[idea/community.git] / python / ipnb / src / org / jetbrains / plugins / ipnb / editor / panels / code / IpnbImagePanel.java
1 package org.jetbrains.plugins.ipnb.editor.panels.code;
2
3 import com.intellij.openapi.diagnostic.Logger;
4 import com.intellij.openapi.util.text.StringUtil;
5 import com.intellij.ui.components.JBLabel;
6 import org.jetbrains.annotations.NotNull;
7 import org.jetbrains.annotations.Nullable;
8 import org.jetbrains.plugins.ipnb.editor.IpnbEditorUtil;
9 import org.jetbrains.plugins.ipnb.format.cells.output.IpnbImageOutputCell;
10 import sun.misc.BASE64Decoder;
11
12 import javax.imageio.ImageIO;
13 import javax.swing.*;
14 import java.awt.image.BufferedImage;
15 import java.io.ByteArrayInputStream;
16
17 public class IpnbImagePanel extends IpnbCodeOutputPanel<IpnbImageOutputCell> {
18   private static final Logger LOG = Logger.getInstance(IpnbImagePanel.class);
19
20   public IpnbImagePanel(@NotNull final IpnbImageOutputCell cell, @Nullable IpnbCodePanel ipnbCodePanel) {
21     super(cell, null, ipnbCodePanel);
22   }
23
24   @Override
25   protected JComponent createViewPanel() {
26     final String png = myCell.getBase64String();
27
28     final JBLabel label = new JBLabel();
29     if (!StringUtil.isEmptyOrSpaces(png)) {
30       try {
31         byte[] btDataFile = new BASE64Decoder().decodeBuffer(png);
32         BufferedImage image = ImageIO.read(new ByteArrayInputStream(btDataFile));
33         label.setIcon(new ImageIcon(image));
34       }
35       catch (Exception e) {
36         LOG.error("Couldn't parse image. " + e.getMessage());
37       }
38     }
39
40     label.setBackground(IpnbEditorUtil.getBackground());
41     label.setOpaque(true);
42
43     return label;
44   }
45 }