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;
      }

      StringBuffer text = new StringBuffer(ResourceUtil.loadText(url));
      updateShortcuts(text);
      updateImages(text, tipLoader);
      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);
      }
      adjustFontSize(((HTMLEditorKit) browser.getEditorKit()).getStyleSheet());
      browser.read(new StringReader(replaced), url);
    } catch (IOException e) {
      setCantReadText(browser, tip);
    }
  }
 @NotNull
 public static JEditorPane createTipBrowser() {
   JEditorPane browser = new JEditorPane();
   browser.setEditable(false);
   browser.setBackground(UIUtil.getTextFieldBackground());
   browser.addHyperlinkListener(
       new HyperlinkListener() {
         public void hyperlinkUpdate(HyperlinkEvent 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");
   final StyleSheet styleSheet = UIUtil.loadStyleSheet(resource);
   HTMLEditorKit kit =
       new HTMLEditorKit() {
         @Override
         public StyleSheet getStyleSheet() {
           return styleSheet != null ? styleSheet : super.getStyleSheet();
         }
       };
   browser.setEditorKit(kit);
   return browser;
 }
 public static String getLauncherText(String resourcePath) {
   try {
     return ResourceUtil.loadText(CfmlUnitRunConfiguration.class.getResource(resourcePath))
         .replaceFirst(
             "\\Q/*system_delimiter*/\\E", ("" + File.separatorChar).replace("\\", "\\\\\\\\"));
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
 private static String getLauncherTemplate(FlexBuildConfiguration bc) throws IOException {
   String templateName;
   if (bc.isPureAs()) {
     templateName = "LauncherTemplateAs.as";
   } else if (bc.getNature().isMobilePlatform()
       || bc.getDependencies().getComponentSet() == ComponentSet.SparkOnly) {
     templateName = "LauncherTemplateSpark.mxml";
   } else {
     templateName = "LauncherTemplateMx.mxml";
   }
   final URL resource =
       FlexUnitPrecompileTask.class.getResource("/unittestingsupport/" + templateName);
   return ResourceUtil.loadText(resource);
 }
  private static void updateImages(StringBuffer text, ClassLoader tipLoader) {
    final boolean dark = UIUtil.isUnderDarcula();
    final boolean retina = UIUtil.isRetina();
    //    if (!dark && !retina) {
    //      return;
    //    }

    String suffix = "";
    if (retina) suffix += "@2x";
    if (dark) suffix += "_dark";
    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")) {
          path += suffix + ".png";
          URL url = ResourceUtil.getResource(tipLoader, "/tips/", path);
          if (url != null) {
            String newImgTag = "<img src=\"" + path + "\" ";
            if (retina) {
              try {
                final BufferedImage image = ImageIO.read(url.openStream());
                final int w = image.getWidth() / 2;
                final int h = image.getHeight() / 2;
                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);
    }
  }
Esempio n. 6
0
  public TOSDialog() {
    super(null, false, IdeModalityType.IDE);
    init();
    setTitle("eddy - Logging preferences");
    setCrossClosesWindow(false);
    // get TOS page out of our jar or directory
    final String pathname = PathUtil.getJarPathForClass(TOSDialog.class);
    final File path = new File(pathname);
    try {
      final URL url;
      if (path.isDirectory()) {
        url = new File(path, "intro.html").toURI().toURL();
      } else {
        url = ResourceUtil.getResource(TOSDialog.class, "", "intro.html");
      }
      TOSTextPane.setPage(url);
    } catch (MalformedURLException e) {
      throw new RuntimeException("Cannot load intro text. Please try reinstalling.", e);
    } catch (IOException e) {
      throw new RuntimeException("Cannot load intro text. Please try reinstalling.", e);
    }

    HyperlinkListener l =
        new HyperlinkListener() {
          @Override
          public void hyperlinkUpdate(HyperlinkEvent e) {
            log("got link event: " + e);
            if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
              try {
                java.awt.Desktop.getDesktop().browse(e.getURL().toURI());
              } catch (IOException e1) {
                // wtf, do nothing
                log("exception: " + e1);
              } catch (URISyntaxException e2) {
                // broken link, do nothing
                log("exception: " + e2);
              }
            }
          }
        };
    TOSTextPane.addHyperlinkListener(l);
  }