Java Code Examples for javax.swing.text.Highlighter#removeAllHighlights()

The following examples show how to use javax.swing.text.Highlighter#removeAllHighlights() . 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: TreePosTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** Show an entry that has been selected. */
private void showEntry(Entry e) {
    try {
        // update simple fields
        setTitle(e.file.getName());
        checkField.setText(e.check);
        enclPanel.setInfo(e.encl);
        selfPanel.setInfo(e.self);
        // show file text with highlights
        body.setText(e.file.getCharContent(true).toString());
        Highlighter highlighter = body.getHighlighter();
        highlighter.removeAllHighlights();
        addHighlight(highlighter, e.encl, enclColor);
        addHighlight(highlighter, e.self, selfColor);
        scroll(body, getMinPos(enclPanel.info, selfPanel.info));
    } catch (IOException ex) {
        body.setText("Cannot read " + e.file.getName() + ": " + e);
    }
}
 
Example 2
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 3
Source File: CheckAttributedTree.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/** Show an entry that has been selected. */
private void showEntry(Entry e) {
    try {
        // update simple fields
        setTitle(e.file.getName());
        checkField.setText(e.check);
        enclPanel.setInfo(e.encl);
        selfPanel.setInfo(e.self);
        // show file text with highlights
        body.setText(e.file.getCharContent(true).toString());
        Highlighter highlighter = body.getHighlighter();
        highlighter.removeAllHighlights();
        addHighlight(highlighter, e.encl, enclColor);
        addHighlight(highlighter, e.self, selfColor);
        scroll(body, getMinPos(enclPanel.info, selfPanel.info));
    } catch (IOException ex) {
        body.setText("Cannot read " + e.file.getName() + ": " + e);
    }
}
 
Example 4
Source File: TreePosTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/** Show an entry that has been selected. */
private void showEntry(Entry e) {
    try {
        // update simple fields
        setTitle(e.file.getName());
        checkField.setText(e.check);
        enclPanel.setInfo(e.encl);
        selfPanel.setInfo(e.self);
        // show file text with highlights
        body.setText(e.file.getCharContent(true).toString());
        Highlighter highlighter = body.getHighlighter();
        highlighter.removeAllHighlights();
        addHighlight(highlighter, e.encl, enclColor);
        addHighlight(highlighter, e.self, selfColor);
        scroll(body, getMinPos(enclPanel.info, selfPanel.info));
    } catch (IOException ex) {
        body.setText("Cannot read " + e.file.getName() + ": " + e);
    }
}
 
Example 5
Source File: CheckAttributedTree.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/** Show an entry that has been selected. */
private void showEntry(Entry e) {
    try {
        // update simple fields
        setTitle(e.file.getName());
        checkField.setText(e.check);
        enclPanel.setInfo(e.encl);
        selfPanel.setInfo(e.self);
        // show file text with highlights
        body.setText(e.file.getCharContent(true).toString());
        Highlighter highlighter = body.getHighlighter();
        highlighter.removeAllHighlights();
        addHighlight(highlighter, e.encl, enclColor);
        addHighlight(highlighter, e.self, selfColor);
        scroll(body, getMinPos(enclPanel.info, selfPanel.info));
    } catch (IOException ex) {
        body.setText("Cannot read " + e.file.getName() + ": " + e);
    }
}
 
Example 6
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 7
Source File: CheckAttributedTree.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/** Show an entry that has been selected. */
private void showEntry(Entry e) {
    try {
        // update simple fields
        setTitle(e.file.getName());
        checkField.setText(e.check);
        enclPanel.setInfo(e.encl);
        selfPanel.setInfo(e.self);
        // show file text with highlights
        body.setText(e.file.getCharContent(true).toString());
        Highlighter highlighter = body.getHighlighter();
        highlighter.removeAllHighlights();
        addHighlight(highlighter, e.encl, enclColor);
        addHighlight(highlighter, e.self, selfColor);
        scroll(body, getMinPos(enclPanel.info, selfPanel.info));
    } catch (IOException ex) {
        body.setText("Cannot read " + e.file.getName() + ": " + e);
    }
}
 
Example 8
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
  String txt = Objects.toString(value, "");
  Highlighter highlighter = getHighlighter();
  highlighter.removeAllHighlights();
  setText(txt);
  setBackground(isSelected ? BACKGROUND_SELECTION_COLOR : Color.WHITE);
  if (Objects.nonNull(pattern) && !pattern.isEmpty() && !Objects.equals(pattern, prev)) {
    Matcher matcher = Pattern.compile(pattern).matcher(txt);
    int pos = 0;
    while (matcher.find(pos) && !matcher.group().isEmpty()) {
      int start = matcher.start();
      int end = matcher.end();
      try {
        highlighter.addHighlight(start, end, highlightPainter);
      } catch (BadLocationException | PatternSyntaxException ex) {
        UIManager.getLookAndFeel().provideErrorFeedback(this);
      }
      pos = end;
    }
  }
  return this;
}
 
Example 9
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 10
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 11
Source File: CheckAttributedTree.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/** Show an entry that has been selected. */
private void showEntry(Entry e) {
    try {
        // update simple fields
        setTitle(e.file.getName());
        checkField.setText(e.check);
        enclPanel.setInfo(e.encl);
        selfPanel.setInfo(e.self);
        // show file text with highlights
        body.setText(e.file.getCharContent(true).toString());
        Highlighter highlighter = body.getHighlighter();
        highlighter.removeAllHighlights();
        addHighlight(highlighter, e.encl, enclColor);
        addHighlight(highlighter, e.self, selfColor);
        scroll(body, getMinPos(enclPanel.info, selfPanel.info));
    } catch (IOException ex) {
        body.setText("Cannot read " + e.file.getName() + ": " + e);
    }
}
 
Example 12
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 13
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 14
Source File: GUIController.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void caretUpdate(CaretEvent e) {
	int cursor = e.getDot();
	JTextPane textPane = (JTextPane)e.getSource();
	TokenPositionAnalysis analysis = getAnalysisForCharIndex(cursor);
	Highlighter highlighter = textPane.getHighlighter();
	HighlightPainter painter = new DefaultHighlightPainter(Color.orange);
	try {
		highlighter.removeAllHighlights();
		if ( analysis!=null ) {
			highlighter.addHighlight(analysis.charIndexStart, analysis.charIndexStop+1, painter);
		}
		scope.injectNLConsole.setText(analysis!=null ? analysis.wsAnalysis : "");
		scope.injectNLConsole.setCaretPosition(0);
		scope.alignConsole.setText(analysis!=null ? analysis.alignAnalysis : "");
		scope.alignConsole.setCaretPosition(0);
	}
	catch (Exception ex) {
		ex.printStackTrace(System.err);
	}
}
 
Example 15
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
public static void setHighlight(JTextComponent jtc, String pattern, HighlightPainter painter) {
  Highlighter highlighter = jtc.getHighlighter();
  highlighter.removeAllHighlights();
  Document doc = jtc.getDocument();
  try {
    String text = doc.getText(0, doc.getLength());
    Matcher matcher = Pattern.compile(pattern).matcher(text);
    int pos = 0;
    while (matcher.find(pos) && !matcher.group().isEmpty()) {
      int start = matcher.start();
      int end = matcher.end();
      highlighter.addHighlight(start, end, painter);
      pos = end;
    }
  } catch (BadLocationException | PatternSyntaxException ex) {
    UIManager.getLookAndFeel().provideErrorFeedback(jtc);
  }
  jtc.repaint();
}
 
Example 16
Source File: CheckAttributedTree.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/** Show an entry that has been selected. */
private void showEntry(Entry e) {
    try {
        // update simple fields
        setTitle(e.file.getName());
        checkField.setText(e.check);
        enclPanel.setInfo(e.encl);
        selfPanel.setInfo(e.self);
        // show file text with highlights
        body.setText(e.file.getCharContent(true).toString());
        Highlighter highlighter = body.getHighlighter();
        highlighter.removeAllHighlights();
        addHighlight(highlighter, e.encl, enclColor);
        addHighlight(highlighter, e.self, selfColor);
        scroll(body, getMinPos(enclPanel.info, selfPanel.info));
    } catch (IOException ex) {
        body.setText("Cannot read " + e.file.getName() + ": " + e);
    }
}
 
Example 17
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 18
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 19
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void actionPerformed(ActionEvent e) {
  textArea.append(String.format("----%n%s%n", getValue(Action.NAME)));
  JToggleButton b = (JToggleButton) e.getSource();
  if (b.isSelected()) {
    for (Element root: editorPane.getDocument().getRootElements()) {
      traverseElementById(root);
    }
  } else {
    Highlighter highlighter = editorPane.getHighlighter();
    highlighter.removeAllHighlights();
  }
}
 
Example 20
Source File: CodeViewer.java    From littleluck with Apache License 2.0 5 votes vote down vote up
public void clearAllSnippetHighlights() {
    if (currentCodeFilesInfo != null) {
        snippetMap.setCurrentSet(null);
        for(CodeFileInfo code : currentCodeFilesInfo.values()) {
            if (code != null && code.textPane != null) {
                Highlighter highlighter = code.textPane.getHighlighter();
                highlighter.removeAllHighlights();
                code.textPane.repaint();
                code.veneer.repaint();
            }
        }
    }
}