Пример #1
0
 @Override
 public void actionPerformed(ActionEvent e) {
   if (td.getTabCount() > 0) {
     TextDocument ta = (TextDocument) td.getComponentAt(td.getSelectedIndex());
     Pattern pn = Pattern.compile(tf1.getText());
     Matcher mt = pn.matcher(ta.getText());
     if (e.getSource() == jb2) { // 取代
       ta.setText(mt.replaceAll(tf2.getText()));
     } else if (e.getSource() == jb1) { // 尋找
       Highlighter hl = ta.getHighlighter();
       hl.removeAllHighlights();
       while (mt.find()) {
         try {
           hl.addHighlight(
               mt.start(), mt.end(), new DefaultHighlighter.DefaultHighlightPainter(null));
         } catch (Exception ex) {
         }
       } // 開啟及關閉介面
     } else if (e.getSource() == replace_searchMenuItem) {
       System.out.println("Replace/Search is show:" + !show);
       if (show) {
         getContentPane().remove(jp);
         show = false;
       } else {
         getContentPane().add(jp, BorderLayout.SOUTH);
         show = true;
       }
       validate(); // 刷新容器
     }
   } else if (e.getSource() == replace_searchMenuItem) {
     JOptionPane.showMessageDialog(
         null, "尚無檔案,無法使用!", "Repace/Search error", JOptionPane.ERROR_MESSAGE);
   }
 }
 public void colorationPolicyScript(String text, final StyledDocument doc) {
   Pattern p = Pattern.compile("(GraphOScript)");
   Matcher m = p.matcher(text);
   while (m.find() == true) {
     MutableAttributeSet attri = new SimpleAttributeSet();
     StyleConstants.setForeground(attri, Color.orange);
     StyleConstants.setBold(attri, true);
     final int start = m.start(0);
     final int end = m.end(0);
     final int length = end - start;
     final MutableAttributeSet style = attri;
     SwingUtilities.invokeLater(
         new Runnable() {
           public void run() {
             doc.setCharacterAttributes(start, length, style, true);
           }
         });
   }
 }
Пример #3
0
    @Override
    protected int drawUnselectedText(Graphics graphics, int x, int y, int p0, int p1)
        throws BadLocationException {
      try {

        Document doc = getDocument();
        String text = doc.getText(p0, p1 - p0);

        Segment segment = getLineBuffer();
        int initialXpos = x;

        SortedMap<Integer, Integer> startMap = new TreeMap<Integer, Integer>();
        SortedMap<Integer, Color> colorMap = new TreeMap<Integer, Color>();

        // Match all regexes on this snippet, store positions
        for (Map.Entry<Pattern, Color> entry : patternColors.entrySet()) {

          Matcher matcher = entry.getKey().matcher(text);

          while (matcher.find()) {
            startMap.put(matcher.start(1), matcher.end(1));
            colorMap.put(matcher.start(1), entry.getValue());
          }
        }

        // TODO: check the map for overlapping parts

        int i = 0;
        // add tag highlighted background colors
        if (!TAG_HIGHLIGHTED.isEmpty()) {
          Matcher highlightMatcher = Pattern.compile(TAG_HIGHLIGHTED).matcher(text);
          while (highlightMatcher.find()) {
            int start = highlightMatcher.start(1);
            int end = highlightMatcher.end(1);
            if (i < start) {
              graphics.setColor(Color.black);
              doc.getText(p0 + i, start - i, segment);
              x = Utilities.drawTabbedText(segment, x, y, graphics, this, i);
            }
            graphics.setColor(TAG_HIGHLIGHTED_COLOR);
            i = end;
            doc.getText(p0 + start, i - start, segment);
            int width =
                Utilities.getTabbedTextWidth(
                    segment, graphics.getFontMetrics(), x, this, p0 + start);
            // graphics.drawLine(x, y, width, y);graphics.getFontMetrics()
            graphics.fillRect(
                x,
                y - graphics.getFontMetrics().getHeight() + 2,
                width,
                graphics.getFontMetrics().getHeight());
            graphics.setColor(Color.black);
            doc.getText(p0 + start, i - start, segment);
            x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
          }
        }
        x = initialXpos;
        i = 0;
        // add highlighted background colors based on position
        // String textx = doc.getText(p0, p1 - p0);
        if ((HIGHLIGHTED_START < p1 && HIGHLIGHTED_START >= p0)
            || (HIGHLIGHTED_END <= p1 && HIGHLIGHTED_END > p0)
            || (HIGHLIGHTED_START < p1 && HIGHLIGHTED_END > p0)) {
          // select whole line
          int start = 0;
          int end = text.length();
          // test to see if only partial line is needed.
          if (HIGHLIGHTED_START > p0) start = HIGHLIGHTED_START - p0;
          if (HIGHLIGHTED_END < p1) end -= p1 - HIGHLIGHTED_END;
          if (i < start) { // fill in normal color if start highlight isn't at the beginning
            graphics.setColor(Color.black);
            doc.getText(p0 + i, start - i, segment);
            x = Utilities.drawTabbedText(segment, x, y, graphics, this, i);
          }
          graphics.setColor(HIGHLIGHTED_COLOR); // fill in the highlight color
          i = end;
          if (i - start > 0) {
            doc.getText(p0 + start, i - start, segment);
            int width =
                Utilities.getTabbedTextWidth(
                    segment, graphics.getFontMetrics(), x, this, p0 + start);
            // graphics.drawLine(x, y, width, y);graphics.getFontMetrics()
            graphics.fillRect(
                x,
                y - graphics.getFontMetrics().getHeight() + 2,
                width,
                graphics.getFontMetrics().getHeight());
            graphics.setColor(Color.black);
            doc.getText(p0 + start, i - start, segment);
            x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
          }
          // else
          //	System.out.println("invalid highlighting " + (i - start) + " is <= 0 (" +  p0 + "-" +
          // p1 + "=" + (p1 - p0) +") " + start + ", " + end + " len=" + text.length());
        }

        x = initialXpos;
        i = 0;
        // Color the parts of xml foreground font
        for (Map.Entry<Integer, Integer> entry : startMap.entrySet()) {
          int start = entry.getKey();
          int end = entry.getValue();

          if (i < start) {
            graphics.setColor(Color.black);
            doc.getText(p0 + i, start - i, segment);
            x = Utilities.drawTabbedText(segment, x, y, graphics, this, i);
          }

          graphics.setColor(colorMap.get(start));
          i = end;
          doc.getText(p0 + start, i - start, segment);
          x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
        }

        // Paint possible remaining text black
        if (i < text.length()) {
          graphics.setColor(Color.black);
          doc.getText(p0 + i, text.length() - i, segment);
          x = Utilities.drawTabbedText(segment, x, y, graphics, this, i);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return x;
    }