public void setImage(BufferedImage image, String format) {
    ImageDocument document = imageComponent.getDocument();
    BufferedImage previousImage = document.getValue();
    document.setValue(image);
    if (image == null) return;
    document.setFormat(format);
    ImageZoomModel zoomModel = getZoomModel();
    if (previousImage == null || !zoomModel.isZoomLevelChanged()) {
      // Set smart zooming behaviour on open
      Options options = OptionsManager.getInstance().getOptions();
      ZoomOptions zoomOptions = options.getEditorOptions().getZoomOptions();
      // Open as actual size
      zoomModel.setZoomFactor(1.0d);

      if (zoomOptions.isSmartZooming()) {
        Dimension prefferedSize = zoomOptions.getPrefferedSize();
        if (prefferedSize.width > image.getWidth() && prefferedSize.height > image.getHeight()) {
          // Resize to preffered size
          // Calculate zoom factor

          double factor =
              (prefferedSize.getWidth() / (double) image.getWidth()
                      + prefferedSize.getHeight() / (double) image.getHeight())
                  / 2.0d;
          zoomModel.setZoomFactor(Math.ceil(factor));
        }
      }
    }
  }
  public void dispose() {
    Options options = OptionsManager.getInstance().getOptions();
    options.removePropertyChangeListener(optionsChangeListener);

    imageComponent.removeMouseWheelListener(wheelAdapter);
    imageComponent.getDocument().removeChangeListener(changeListener);

    removeAll();
  }
 public void mouseWheelMoved(MouseWheelEvent e) {
   Options options = OptionsManager.getInstance().getOptions();
   EditorOptions editorOptions = options.getEditorOptions();
   ZoomOptions zoomOptions = editorOptions.getZoomOptions();
   if (zoomOptions.isWheelZooming() && e.isControlDown()) {
     if (e.getWheelRotation() < 0) {
       zoomModel.zoomOut();
     } else {
       zoomModel.zoomIn();
     }
     e.consume();
   }
 }
  ImageEditorUI(@Nullable ImageEditor editor) {
    this.editor = editor;

    Options options = OptionsManager.getInstance().getOptions();
    EditorOptions editorOptions = options.getEditorOptions();
    options.addPropertyChangeListener(optionsChangeListener);

    final PsiActionSupportFactory factory = PsiActionSupportFactory.getInstance();
    if (factory != null && editor != null) {
      copyPasteSupport =
          factory.createPsiBasedCopyPasteSupport(
              editor.getProject(),
              this,
              new PsiActionSupportFactory.PsiElementSelector() {
                public PsiElement[] getSelectedElements() {
                  PsiElement[] data = LangDataKeys.PSI_ELEMENT_ARRAY.getData(ImageEditorUI.this);
                  return data == null ? PsiElement.EMPTY_ARRAY : data;
                }
              });
    } else {
      copyPasteSupport = null;
    }

    deleteProvider = factory == null ? null : factory.createPsiBasedDeleteProvider();

    ImageDocument document = imageComponent.getDocument();
    document.addChangeListener(changeListener);

    // Set options
    TransparencyChessboardOptions chessboardOptions =
        editorOptions.getTransparencyChessboardOptions();
    GridOptions gridOptions = editorOptions.getGridOptions();
    imageComponent.setTransparencyChessboardCellSize(chessboardOptions.getCellSize());
    imageComponent.setTransparencyChessboardWhiteColor(chessboardOptions.getWhiteColor());
    imageComponent.setTransparencyChessboardBlankColor(chessboardOptions.getBlackColor());
    imageComponent.setGridLineZoomFactor(gridOptions.getLineZoomFactor());
    imageComponent.setGridLineSpan(gridOptions.getLineSpan());
    imageComponent.setGridLineColor(gridOptions.getLineColor());

    // Create layout
    ImageContainerPane view = new ImageContainerPane(imageComponent);
    view.addMouseListener(new EditorMouseAdapter());
    view.addMouseListener(new FocusRequester());

    JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(view);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    // Zoom by wheel listener
    scrollPane.addMouseWheelListener(wheelAdapter);

    // Construct UI
    setLayout(new BorderLayout());

    ActionManager actionManager = ActionManager.getInstance();
    ActionGroup actionGroup =
        (ActionGroup) actionManager.getAction(ImageEditorActions.GROUP_TOOLBAR);
    ActionToolbar actionToolbar =
        actionManager.createActionToolbar(ImageEditorActions.ACTION_PLACE, actionGroup, true);

    // Make sure toolbar is 'ready' before it's added to component hierarchy
    // to prevent ActionToolbarImpl.updateActionsImpl(boolean, boolean) from increasing popup size
    // unnecessarily
    actionToolbar.updateActionsImmediately();

    actionToolbar.setTargetComponent(this);

    JComponent toolbarPanel = actionToolbar.getComponent();
    toolbarPanel.addMouseListener(new FocusRequester());

    JLabel errorLabel =
        new JLabel(
            ImagesBundle.message("error.broken.image.file.format"),
            Messages.getErrorIcon(),
            SwingConstants.CENTER);

    JPanel errorPanel = new JPanel(new BorderLayout());
    errorPanel.add(errorLabel, BorderLayout.CENTER);

    contentPanel = new JPanel(new CardLayout());
    contentPanel.add(scrollPane, IMAGE_PANEL);
    contentPanel.add(errorPanel, ERROR_PANEL);

    JPanel topPanel = new JPanel(new BorderLayout());
    topPanel.add(toolbarPanel, BorderLayout.WEST);
    infoLabel = new JLabel((String) null, SwingConstants.RIGHT);
    infoLabel.setBorder(IdeBorderFactory.createEmptyBorder(0, 0, 0, 2));
    topPanel.add(infoLabel, BorderLayout.EAST);

    add(topPanel, BorderLayout.NORTH);
    add(contentPanel, BorderLayout.CENTER);

    updateInfo();
  }