com.intellij.util.ResourceUtil Java Examples

The following examples show how to use com.intellij.util.ResourceUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TipUIUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static JEditorPane createTipBrowser() {
  JEditorPane browser = new JEditorPane() {
    @Override
    public void setDocument(Document document) {
      super.setDocument(document);
      document.putProperty("imageCache", new URLDictionatyLoader());
    }
  };
  browser.setEditable(false);
  browser.setBackground(UIUtil.getTextFieldBackground());
  browser.addHyperlinkListener(e -> {
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      BrowserUtil.browse(e.getURL());
    }
  });
  URL resource = ResourceUtil.getResource(TipUIUtil.class, "/tips/css/", UIUtil.isUnderDarcula() ? "tips_darcula.css" : "tips.css");
  HTMLEditorKit kit = UIUtil.getHTMLEditorKit(false);
  kit.getStyleSheet().addStyleSheet(UIUtil.loadStyleSheet(resource));
  browser.setEditorKit(kit);
  return browser;
}
 
Example #2
Source File: YamlSchemaInspection.java    From intellij-swagger with MIT License 5 votes vote down vote up
private PsiElementVisitor createVisitor(
    final String schemaFileName,
    final ProblemsHolder holder,
    final LocalInspectionToolSession session,
    final PsiFile file) {
  List<YAMLDocument> documents = ((YAMLFile) file).getDocuments();
  if (documents.size() != 1) return PsiElementVisitor.EMPTY_VISITOR;

  PsiElement root = documents.get(0).getTopLevelValue();
  if (root == null) return PsiElementVisitor.EMPTY_VISITOR;

  JsonSchemaService service = JsonSchemaService.Impl.get(file.getProject());

  final URL url = ResourceUtil.getResource(getClass(), "schemas", schemaFileName);
  final VirtualFile virtualFile = VfsUtil.findFileByURL(url);

  final JsonSchemaObject schema = service.getSchemaObjectForSchemaFile(virtualFile);

  return new YamlPsiElementVisitor() {
    @Override
    public void visitElement(PsiElement element) {
      if (element != root) return;
      final JsonLikePsiWalker walker = JsonLikePsiWalker.getWalker(element, schema);
      if (walker == null) return;

      JsonComplianceCheckerOptions options = new JsonComplianceCheckerOptions(false);

      new JsonSchemaComplianceChecker(schema, holder, walker, session, options).annotate(element);
    }
  };
}
 
Example #3
Source File: JsonSchemaInspection.java    From intellij-swagger with MIT License 5 votes vote down vote up
private PsiElementVisitor createVisitor(
    final String schemaFileName,
    final ProblemsHolder holder,
    final LocalInspectionToolSession session,
    final PsiFile file) {

  JsonValue root =
      file instanceof JsonFile
          ? ObjectUtils.tryCast(file.getFirstChild(), JsonValue.class)
          : null;
  if (root == null) return PsiElementVisitor.EMPTY_VISITOR;

  JsonSchemaService service = JsonSchemaService.Impl.get(file.getProject());

  final URL url = ResourceUtil.getResource(getClass(), "schemas", schemaFileName);
  final VirtualFile virtualFile = VfsUtil.findFileByURL(url);

  final JsonSchemaObject schema = service.getSchemaObjectForSchemaFile(virtualFile);

  return new JsonElementVisitor() {
    @Override
    public void visitElement(PsiElement element) {
      if (element == root) {
        final JsonLikePsiWalker walker = JsonLikePsiWalker.getWalker(element, schema);
        if (walker == null) return;

        JsonComplianceCheckerOptions options = new JsonComplianceCheckerOptions(false);

        new JsonSchemaComplianceChecker(schema, holder, walker, session, options)
            .annotate(element);
      }
    }
  };
}
 
Example #4
Source File: BitrixFramework.java    From bxfs with MIT License 5 votes vote down vote up
@NotNull
private Optional<String> getResourceFileContent(@NotNull String resourceFilePath) {
	try {
		return Optional.of(ResourceUtil.loadText(BitrixFramework.class.getResource(
			"/pro/opcode/bitrix/resources/" + resourceFilePath
		)));
	}
	catch (IOException e) {
		return Optional.empty();
	}
}
 
Example #5
Source File: InspectionToolWrapper.java    From consulo with Apache License 2.0 5 votes vote down vote up
public String loadDescription() {
  final String description = getStaticDescription();
  if (description != null) return description;
  try {
    URL descriptionUrl = getDescriptionUrl();
    if (descriptionUrl == null) return null;
    return ResourceUtil.loadText(descriptionUrl);
  }
  catch (IOException ignored) { }

  return getTool().loadDescription();
}
 
Example #6
Source File: InspectionProfileEntry.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public String loadDescription() {
  final String description = getStaticDescription();
  if (description != null) return description;

  try {
    URL descriptionUrl = getDescriptionUrl();
    if (descriptionUrl == null) return null;
    return ResourceUtil.loadText(descriptionUrl);
  }
  catch (IOException ignored) { }

  return null;
}
 
Example #7
Source File: SearchableOptionsRegistrarImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public SearchableOptionsRegistrarImpl() {
  try {
    //stop words
    final String text = ResourceUtil.loadText(ResourceUtil.getResource(SearchableOptionsRegistrarImpl.class, "/search/", "ignore.txt"));
    final String[] stopWords = text.split("[\\W]");
    ContainerUtil.addAll(myStopWords, stopWords);
  }
  catch (IOException e) {
    LOG.error(e);
  }
}
 
Example #8
Source File: InspectionToolWrapper.java    From consulo with Apache License 2.0 4 votes vote down vote up
@javax.annotation.Nullable
protected URL superGetDescriptionUrl() {
  final String fileName = getDescriptionFileName();
  return ResourceUtil.getResource(getDescriptionContextClass(), "/inspectionDescriptions", fileName);
}
 
Example #9
Source File: InspectionProfileEntry.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
protected URL getDescriptionUrl() {
  final String fileName = getDescriptionFileName();
  if (fileName == null) return null;
  return ResourceUtil.getResource(getDescriptionContextClass(), "/inspectionDescriptions", fileName);
}
 
Example #10
Source File: TipUIUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static void openTipInBrowser(@Nullable TipAndTrickBean tip, JEditorPane browser) {
  if (tip == null) return;
  try {
    PluginDescriptor pluginDescriptor = tip.getPluginDescriptor();
    ClassLoader tipLoader = pluginDescriptor == null ? TipUIUtil.class.getClassLoader() : ObjectUtils.notNull(pluginDescriptor.getPluginClassLoader(), TipUIUtil.class.getClassLoader());

    URL url = ResourceUtil.getResource(tipLoader, "/tips/", tip.fileName);

    if (url == null) {
      setCantReadText(browser, tip);
      return;
    }

    StringBuilder text = new StringBuilder(ResourceUtil.loadText(url));
    updateShortcuts(text);
    updateImages(text, tipLoader, browser);
    String replaced = text.toString().replace("&productName;", ApplicationNamesInfo.getInstance().getFullProductName());
    String major = ApplicationInfo.getInstance().getMajorVersion();
    replaced = replaced.replace("&majorVersion;", major);
    String minor = ApplicationInfo.getInstance().getMinorVersion();
    replaced = replaced.replace("&minorVersion;", minor);
    replaced = replaced.replace("&majorMinorVersion;", major + ("0".equals(minor) ? "" : ("." + minor)));
    replaced = replaced.replace("&settingsPath;", CommonBundle.settingsActionPath());
    replaced = replaced.replaceFirst("<link rel=\"stylesheet\".*tips\\.css\">", ""); // don't reload the styles
    if (browser.getUI() == null) {
      browser.updateUI();
      boolean succeed = browser.getUI() != null;
      String message = "reinit JEditorPane.ui: " + (succeed ? "OK" : "FAIL") + ", laf=" + LafManager.getInstance().getCurrentLookAndFeel();
      if (succeed) {
        LOG.warn(message);
      }
      else {
        LOG.error(message);
      }
    }
    browser.read(new StringReader(replaced), url);
  }
  catch (IOException e) {
    setCantReadText(browser, tip);
  }
}
 
Example #11
Source File: TipUIUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void updateImages(StringBuilder text, ClassLoader tipLoader, JEditorPane browser) {
  final boolean dark = StyleManager.get().getCurrentStyle().isDark();

  IdeFrame af = IdeFrameUtil.findActiveRootIdeFrame();
  Component comp = af != null ? TargetAWT.to(af.getWindow()) : browser;
  int index = text.indexOf("<img", 0);
  while (index != -1) {
    final int end = text.indexOf(">", index + 1);
    if (end == -1) return;
    final String img = text.substring(index, end + 1).replace('\r', ' ').replace('\n', ' ');
    final int srcIndex = img.indexOf("src=");
    final int endIndex = img.indexOf(".png", srcIndex);
    if (endIndex != -1) {
      String path = img.substring(srcIndex + 5, endIndex);
      if (!path.endsWith("_dark") && !path.endsWith("@2x")) {
        boolean hidpi = JBUI.isPixHiDPI(comp);
        path += (hidpi ? "@2x" : "") + (dark ? "_dark" : "") + ".png";
        URL url = ResourceUtil.getResource(tipLoader, "/tips/", path);
        if (url != null) {
          String newImgTag = "<img src=\"" + path + "\" ";
          try {
            BufferedImage image = ImageIO.read(url.openStream());
            int w = image.getWidth();
            int h = image.getHeight();
            if (UIUtil.isJreHiDPI(comp)) {
              // compensate JRE scale
              float sysScale = JBUI.sysScale(comp);
              w = (int)(w / sysScale);
              h = (int)(h / sysScale);
            }
            else {
              // compensate image scale
              float imgScale = hidpi ? 2f : 1f;
              w = (int)(w / imgScale);
              h = (int)(h / imgScale);
            }
            // fit the user scale
            w = (int)(JBUI.scale((float)w));
            h = (int)(JBUI.scale((float)h));

            newImgTag += "width=\"" + w + "\" height=\"" + h + "\"";
          }
          catch (Exception ignore) {
            newImgTag += "width=\"400\" height=\"200\"";
          }
          newImgTag += "/>";
          text.replace(index, end + 1, newImgTag);
        }
      }
    }
    index = text.indexOf("<img", index + 1);
  }
}
 
Example #12
Source File: ResourceTextDescriptor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public String getText() throws IOException {
  return ResourceUtil.loadText(myUrl);
}
 
Example #13
Source File: DumpInspectionDescriptionsAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static URL getDescriptionUrl(final InspectionToolWrapper toolWrapper) {
  final Class aClass = getInspectionClass(toolWrapper);
  return ResourceUtil.getResource(aClass, "/inspectionDescriptions", toolWrapper.getShortName() + ".html");
}