Exemplo n.º 1
0
  private void update() {
    if (update.isDisposed()) return;

    update.cancelAllRequests();
    update.addRequest(
        () -> {
          if (isDisposed()) return;

          VirtualFile file = getSelectedFile();
          actionEnabled = false;
          String charsetName = null;
          Pair<Charset, String> check = null;

          if (file != null) {
            check = EncodingUtil.checkSomeActionEnabled(file);
            Charset charset = null;

            if (LoadTextUtil.wasCharsetDetectedFromBytes(file) != null) {
              charset = cachedCharsetFromContent(file);
            }

            if (charset == null) {
              charset = file.getCharset();
            }

            actionEnabled = check == null || check.second == null;

            if (!actionEnabled) {
              charset = check.first;
            }

            if (charset != null) {
              charsetName = charset.displayName();
            }
          }

          if (charsetName == null) {
            charsetName = "n/a";
          }

          String toolTipText;

          if (actionEnabled) {
            toolTipText = String.format("File Encoding%n%s", charsetName);

            myComponent.setForeground(UIUtil.getActiveTextColor());
            myComponent.setTextAlignment(Component.LEFT_ALIGNMENT);
          } else {
            String failReason = check == null ? "" : check.second;
            toolTipText = String.format("File encoding is disabled%n%s", failReason);

            myComponent.setForeground(UIUtil.getInactiveTextColor());
            myComponent.setTextAlignment(Component.CENTER_ALIGNMENT);
          }

          myComponent.setToolTipText(toolTipText);
          myComponent.setText(charsetName);

          if (myStatusBar != null) {
            myStatusBar.updateWidget(ID());
          }
        },
        200,
        ModalityState.any());
  }