private boolean checkLastImage() {
    File file = new File(DesignerApplicationManager.APP_DIR, LAST_PREVIEW_IMAGE_FILE_NAME);
    if (!file.exists()) {
      return false;
    }

    try {
      final VirtualFile virtualFile = toolWindowForm.getFile();
      BufferedImage image =
          IOUtil.readImage(
              file,
              new Processor<DataInputStream>() {
                @Override
                public boolean process(DataInputStream in) {
                  try {
                    return in.readLong() == virtualFile.getTimeStamp()
                        && in.readUTF().equals(virtualFile.getUrl());
                  } catch (IOException e) {
                    throw new RuntimeException(e);
                  }
                }
              });
      if (image != null) {
        toolWindowForm.getPreviewPanel().setImage(image);
        return true;
      }
    } catch (IOException e) {
      LogMessageUtil.LOG.warn("Can't read image for last document", e);
    }

    return false;
  }
 private void saveLastImage(final VirtualFile file) {
   BufferedImage image = toolWindowForm.getPreviewPanel().getImage();
   if (image != null) {
     try {
       IOUtil.saveImage(
           toolWindowForm.getPreviewPanel().getImage(),
           new File(DesignerApplicationManager.APP_DIR, LAST_PREVIEW_IMAGE_FILE_NAME),
           new Consumer<DataOutputStream>() {
             @Override
             public void consume(DataOutputStream out) {
               try {
                 out.writeLong(file.getTimeStamp());
                 out.writeUTF(file.getUrl());
               } catch (IOException e) {
                 throw new RuntimeException(e);
               }
             }
           });
     } catch (Throwable e) {
       LogMessageUtil.LOG.warn("Can't save image for last document", e);
     }
   }
 }
  private void render(final boolean isSlow, final boolean clearPreviousOnError) {
    if (!toolWindowVisible) {
      return;
    }

    final VirtualFile file = toolWindowForm.getFile();
    if (file == null) {
      return;
    }

    toolWindowForm.getPreviewPanel().clearCannotRender();

    if (isSlow) {
      toolWindowForm.getPreviewPanel().getLoadingDecorator().startLoading(false);
      loadingDecoratorStarted++;
    }

    toolWindowForm.waitingForGetDocument = true;
    XmlFile xmlFile = (XmlFile) PsiManager.getInstance(project).findFile(file);
    LogMessageUtil.LOG.assertTrue(xmlFile != null);
    AsyncResult<BufferedImage> result =
        DesignerApplicationManager.getInstance().getDocumentImage(xmlFile);
    result.doWhenDone(
        new QueuedAsyncResultHandler<BufferedImage>() {
          @Override
          protected boolean isExpired() {
            return toolWindowForm == null || !file.equals(toolWindowForm.getFile());
          }

          @Override
          public void process(final BufferedImage image) {
            UIUtil.invokeLaterIfNeeded(
                new Runnable() {
                  @Override
                  public void run() {
                    toolWindowForm.getPreviewPanel().setImage(image);
                  }
                });
          }
        });

    result.doWhenProcessed(
        new Runnable() {
          @Override
          public void run() {
            toolWindowForm.waitingForGetDocument = false;
            if (isSlow && --loadingDecoratorStarted == 0 && toolWindowForm != null) {
              toolWindowForm.getPreviewPanel().getLoadingDecorator().stopLoading();
            }
          }
        });

    result.doWhenRejected(
        new Runnable() {
          @Override
          public void run() {
            if (clearPreviousOnError) {
              toolWindowForm.getPreviewPanel().setImage(null);
            }
            UIUtil.invokeLaterIfNeeded(
                new Runnable() {
                  @Override
                  public void run() {
                    toolWindowForm.getPreviewPanel().showCannotRender();
                  }
                });
          }
        });
  }