Exemplo n.º 1
0
  /**
   * Load image data for file and put user data attributes into file.
   *
   * @param file File
   * @return true if file image is loaded.
   * @throws java.io.IOException if image can not be loaded
   */
  private static boolean refresh(@NotNull VirtualFile file) throws IOException {
    Long loadedTimeStamp = file.getUserData(TIMESTAMP_KEY);
    SoftReference<BufferedImage> imageRef = file.getUserData(BUFFERED_IMAGE_REF_KEY);
    if (loadedTimeStamp == null
        || loadedTimeStamp.longValue() != file.getTimeStamp()
        || SoftReference.dereference(imageRef) == null) {
      try {
        final byte[] content = file.contentsToByteArray();

        if (ICO_FORMAT.equalsIgnoreCase(file.getExtension())) {
          try {
            final BufferedImage image =
                ICO_IMAGE_PARSER.getBufferedImage(new ByteSourceArray(content), null);
            file.putUserData(FORMAT_KEY, ICO_FORMAT);
            file.putUserData(BUFFERED_IMAGE_REF_KEY, new SoftReference<>(image));
            return true;
          } catch (ImageReadException ignore) {
          }
        }

        InputStream inputStream = new ByteArrayInputStream(content, 0, content.length);
        ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
        try {
          Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
          if (imageReaders.hasNext()) {
            ImageReader imageReader = imageReaders.next();
            try {
              file.putUserData(FORMAT_KEY, imageReader.getFormatName());
              ImageReadParam param = imageReader.getDefaultReadParam();
              imageReader.setInput(imageInputStream, true, true);
              int minIndex = imageReader.getMinIndex();
              BufferedImage image = imageReader.read(minIndex, param);
              file.putUserData(BUFFERED_IMAGE_REF_KEY, new SoftReference<>(image));
              return true;
            } finally {
              imageReader.dispose();
            }
          }
        } finally {
          imageInputStream.close();
        }
      } finally {
        // We perform loading no more needed
        file.putUserData(TIMESTAMP_KEY, file.getTimeStamp());
      }
    }
    return false;
  }
    @Override
    public void dragOutFinished(MouseEvent event, TabInfo source) {
      boolean copy = event.isMetaDown() || (!SystemInfo.isMac && event.isControlDown());
      if (!copy) {
        myFile.putUserData(FileEditorManagerImpl.CLOSING_TO_REOPEN, Boolean.TRUE);
        FileEditorManagerEx.getInstanceEx(myProject).closeFile(myFile, myWindow);
      } else {
        source.setHidden(false);
      }

      mySession.process(event);
      if (!copy) {
        myFile.putUserData(FileEditorManagerImpl.CLOSING_TO_REOPEN, null);
      }

      myFile = null;
      mySession = null;
    }
  @Override
  @Nullable
  public Document getDocument(@NotNull final VirtualFile file) {
    DocumentEx document = (DocumentEx) getCachedDocument(file);
    if (document == null) {
      if (file.isDirectory()
          || isBinaryWithoutDecompiler(file)
          || SingleRootFileViewProvider.isTooLargeForContentLoading(file)) {
        return null;
      }
      final CharSequence text = LoadTextUtil.loadText(file);

      synchronized (lock) {
        document = (DocumentEx) getCachedDocument(file);
        if (document != null) return document; // Double checking

        document = (DocumentEx) createDocument(text);
        document.setModificationStamp(file.getModificationStamp());
        final FileType fileType = file.getFileType();
        document.setReadOnly(!file.isWritable() || fileType.isBinary());
        file.putUserData(DOCUMENT_KEY, new WeakReference<Document>(document));
        document.putUserData(FILE_KEY, file);

        if (!(file instanceof LightVirtualFile
            || file.getFileSystem() instanceof DummyFileSystem)) {
          document.addDocumentListener(
              new DocumentAdapter() {
                @Override
                public void documentChanged(DocumentEvent e) {
                  final Document document = e.getDocument();
                  myUnsavedDocuments.add(document);
                  final Runnable currentCommand =
                      CommandProcessor.getInstance().getCurrentCommand();
                  Project project =
                      currentCommand == null
                          ? null
                          : CommandProcessor.getInstance().getCurrentCommandProject();
                  String lineSeparator = CodeStyleFacade.getInstance(project).getLineSeparator();
                  document.putUserData(LINE_SEPARATOR_KEY, lineSeparator);

                  // avoid documents piling up during batch processing
                  if (areTooManyDocumentsInTheQueue(myUnsavedDocuments)) {
                    saveAllDocumentsLater();
                  }
                }
              });
        }
      }

      myMultiCaster.fileContentLoaded(file, document);
    }

    return document;
  }
 @Nullable
 private static PsiJavaFileStubImpl getOrCreateJavaFileStub(@NotNull VirtualFile virtualFile) {
   CachedJavaStub cachedJavaStub = virtualFile.getUserData(cachedJavaStubKey);
   long fileModificationStamp = virtualFile.getModificationStamp();
   if (cachedJavaStub != null && cachedJavaStub.modificationStamp == fileModificationStamp) {
     return cachedJavaStub.javaFileStub;
   }
   PsiJavaFileStubImpl stub = (PsiJavaFileStubImpl) createStub(virtualFile);
   if (stub != null) {
     virtualFile.putUserData(cachedJavaStubKey, new CachedJavaStub(fileModificationStamp, stub));
   }
   return stub;
 }
Exemplo n.º 5
0
 @NotNull
 private static PsiFile copyFile(
     @NotNull PsiFile file, @NotNull StringBuilder fileContentWithoutKey) {
   final PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(file.getProject());
   PsiFile copy =
       psiFileFactory.createFileFromText(
           file.getName(), file.getFileType(), fileContentWithoutKey);
   VirtualFile vFile = copy.getVirtualFile();
   if (vFile != null) {
     vFile.putUserData(UndoConstants.DONT_RECORD_UNDO, Boolean.TRUE);
   }
   return copy;
 }
 public static void registerDocument(
     @NotNull final Document document, @NotNull VirtualFile virtualFile) {
   synchronized (lock) {
     virtualFile.putUserData(
         DOCUMENT_KEY,
         new SoftReference<Document>(document) {
           @Override
           public Document get() {
             return document;
           }
         });
     document.putUserData(FILE_KEY, virtualFile);
   }
 }
  @Override
  @Nullable
  public Document getCachedDocument(@NotNull VirtualFile file) {
    Reference<Document> reference = file.getUserData(DOCUMENT_KEY);
    Document document = reference == null ? null : reference.get();

    if (document != null && isBinaryWithoutDecompiler(file)) {
      file.putUserData(DOCUMENT_KEY, null);
      document.putUserData(FILE_KEY, null);
      return null;
    }

    return document;
  }
Exemplo n.º 8
0
  @Override
  public VirtualFile getVirtualFile() {
    final VirtualFile file = super.getVirtualFile();

    // #138: ignore "strip trailing white space" setting
    if (file != null) {
      Key<String> overrideStripTrailingSpacesKey = getOverrideStripTrailingSpacesKey();

      if (overrideStripTrailingSpacesKey != null) {
        file.putUserData(overrideStripTrailingSpacesKey, STRIP_TRAILING_SPACES_NONE);
      }
    }

    return file;
  }
  private <T> void applyConfigValueToUserData(
      VirtualFile file,
      Key<T> userDataKey,
      String editorConfigKey,
      String configValue,
      Map<String, T> configMap) {
    if (configValue.isEmpty()) return;

    final T data = configMap.get(configValue);
    if (data == null) {
      Utils.invalidConfigMessage(myProject, configValue, editorConfigKey, file.getCanonicalPath());
    } else {
      file.putUserData(userDataKey, data);
    }
  }
Exemplo n.º 10
0
 @NotNull
 private static XmlFile parseXmlFileInTemplate(
     String templateString, CustomTemplateCallback callback, boolean createPhysicalFile) {
   XmlFile xmlFile =
       (XmlFile)
           PsiFileFactory.getInstance(callback.getProject())
               .createFileFromText(
                   "dummy.xml",
                   StdFileTypes.XML,
                   templateString,
                   LocalTimeCounter.currentTime(),
                   createPhysicalFile);
   VirtualFile vFile = xmlFile.getVirtualFile();
   if (vFile != null) {
     vFile.putUserData(UndoConstants.DONT_RECORD_UNDO, Boolean.TRUE);
   }
   return xmlFile;
 }
Exemplo n.º 11
0
 public static void setCharsetWasDetectedFromBytes(
     @NotNull VirtualFile virtualFile,
     @Nullable("null if was not detected, otherwise the reason it was") String reason) {
   virtualFile.putUserData(CHARSET_WAS_DETECTED_FROM_BYTES, reason);
 }
 public static void doNotCheckFileSizeLimit(@NotNull VirtualFile vFile) {
   vFile.putUserData(OUR_NO_SIZE_LIMIT_KEY, Boolean.TRUE);
 }