Exemple #1
0
    public CharPos charat(Coord c) {
      if (c.y < -sb.val) {
        if (msgs.size() < 1) return (null);
        Message msg = msgs.get(0);
        if (!(msg.text() instanceof RichText)) return (null);
        RichText.TextPart fp = null;
        for (RichText.Part part = ((RichText) msg.text()).parts; part != null; part = part.next) {
          if (part instanceof RichText.TextPart) {
            fp = (RichText.TextPart) part;
            break;
          }
        }
        if (fp == null) return (null);
        return (new CharPos(msg, fp, TextHitInfo.leading(0)));
      }

      Coord hc = new Coord();
      Message msg = messageat(c, hc);
      if ((msg == null) || !(msg.text() instanceof RichText)) return (null);
      RichText rt = (RichText) msg.text();
      RichText.Part p = rt.partat(hc);
      if (p == null) {
        RichText.TextPart lp = null;
        for (RichText.Part part = ((RichText) msg.text()).parts; part != null; part = part.next) {
          if (part instanceof RichText.TextPart) lp = (RichText.TextPart) part;
        }
        if (lp == null) return (null);
        return (new CharPos(msg, lp, TextHitInfo.trailing(lp.end - lp.start - 1)));
      }
      if (!(p instanceof RichText.TextPart)) return (null);
      RichText.TextPart tp = (RichText.TextPart) p;
      return (new CharPos(msg, tp, tp.charat(hc)));
    }
Exemple #2
0
  public static class ChatParser extends RichText.Parser {
    public static final Pattern urlpat =
        Pattern.compile(
            "\\b((https?://)|(www\\.[a-z0-9_.-]+\\.[a-z0-9_.-]+))[a-z0-9/_.~#%+?&:*=-]*",
            Pattern.CASE_INSENSITIVE);
    public static final Map<? extends Attribute, ?> urlstyle =
        RichText.fillattrs(
            TextAttribute.FOREGROUND,
            new Color(64, 175, 255),
            TextAttribute.UNDERLINE,
            TextAttribute.UNDERLINE_ON,
            TextAttribute.WEIGHT,
            TextAttribute.WEIGHT_BOLD);

    public ChatParser(Object... args) {
      super(args);
    }

    protected RichText.Part text(PState s, String text, Map<? extends Attribute, ?> attrs)
        throws IOException {
      RichText.Part ret = null;
      int p = 0;
      while (true) {
        Matcher m = urlpat.matcher(text);
        if (!m.find(p)) break;
        URL url;
        try {
          String su = text.substring(m.start(), m.end());
          if (su.indexOf(':') < 0) su = "http://" + su;
          url = new URL(su);
        } catch (java.net.MalformedURLException e) {
          p = m.end();
          continue;
        }
        RichText.Part lead = new RichText.TextPart(text.substring(0, m.start()), attrs);
        if (ret == null) ret = lead;
        else ret.append(lead);
        Map<Attribute, Object> na = new HashMap<Attribute, Object>(attrs);
        na.putAll(urlstyle);
        na.put(ChatAttribute.HYPERLINK, new FuckMeGentlyWithAChainsaw(url));
        ret.append(new RichText.TextPart(text.substring(m.start(), m.end()), na));
        p = m.end();
      }
      if (ret == null) ret = new RichText.TextPart(text, attrs);
      else ret.append(new RichText.TextPart(text.substring(p), attrs));
      return (ret);
    }
  }