Java Examples for javax.swing.text.DefaultHighlighter.DefaultHighlightPainter
The following java examples will help you to understand the usage of javax.swing.text.DefaultHighlighter.DefaultHighlightPainter. These source code samples are taken from different open source projects.
Example 1
Project: ocelot-master File: MatchScoreRenderer.java View source code |
/* * (non-Javadoc) * * @see * javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent * (javax.swing.JTable, java.lang.Object, boolean, boolean, int, int) */ @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); JTextPane textPane = new JTextPane(); textPane.setBackground(comp.getBackground()); textPane.setForeground(Color.white); textPane.setFont(comp.getFont()); if (comp != null && value != null) { int score = (int) value; Color highlightColor = comp.getBackground(); if (score == 100) { highlightColor = new Color(16, 164, 87); } else if (score >= 95) { highlightColor = new Color(95, 164, 16); } else if (score >= 85) { highlightColor = new Color(232, 206, 11); } else if (score >= 75) { highlightColor = new Color(232, 143, 11); } else if (score >= 65) { highlightColor = new Color(232, 99, 11); } else { highlightColor = new Color(232, 51, 11); } DefaultHighlightPainter painter = new DefaultHighlightPainter(highlightColor); StyledDocument style = textPane.getStyledDocument(); SimpleAttributeSet rightAlign = new SimpleAttributeSet(); StyleConstants.setAlignment(rightAlign, StyleConstants.ALIGN_RIGHT); try { style.insertString(style.getLength(), " " + value + "% ", rightAlign); textPane.getHighlighter().addHighlight(0, textPane.getText().length(), painter); } catch (BadLocationException e) { LOG.warn("Error while highlighting the score " + value + "at column " + column + " and row " + row + ".", e); } } return textPane; }
Example 2
Project: ClothoBiofabEdition-master File: SequenceView.java View source code |
/** * Adds the highlights for user selected areas. */ public void highlightUserSelected() { JTextPane textArea = _sequenceview.get_TextArea(); _h = textArea.getHighlighter(); int start = textArea.getSelectionStart(); int end = textArea.getSelectionEnd(); if (textArea.getSelectedText() != null) { textArea.select(0, 0); try { Highlight[] highlights = _h.getHighlights(); //stores highlights that will be temporarily removed ArrayList<Highlight> shuffledHighlights = new ArrayList(); for (int i = 0; i < highlights.length; i++) { if (highlights[i].getPainter() instanceof DefaultHighlighter.DefaultHighlightPainter) { shuffledHighlights.add(highlights[i]); _h.removeHighlight(highlights[i]); } } if (magicHighlight && lastUserHighlightTag != null) { if (userHighlightColor.equals(Color.CYAN)) { this.changeUserHighlightColor(Color.GRAY); } else if (userHighlightColor.equals(Color.GRAY)) { this.changeUserHighlightColor(Color.GREEN); } else if (userHighlightColor.equals(Color.GREEN)) { this.changeUserHighlightColor(Color.PINK); } else if (userHighlightColor.equals(Color.PINK)) { this.changeUserHighlightColor(Color.ORANGE); } else if (userHighlightColor.equals(Color.ORANGE)) { this.changeUserHighlightColor(Color.YELLOW); } else if (userHighlightColor.equals(Color.YELLOW)) { this.changeUserHighlightColor(Color.CYAN); } else { this.changeUserHighlightColor(Color.CYAN); } } lastUserHighlightTag = _h.addHighlight(start, end, new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(userHighlightColor)); for (Highlight h : shuffledHighlights) { _h.addHighlight(h.getStartOffset(), h.getEndOffset(), h.getPainter()); } textArea.grabFocus(); textArea.select(start, end); } catch (javax.swing.text.BadLocationException ble) { ble.printStackTrace(); } } }
Example 3
Project: zooinspector-master File: NodeViewerData.java View source code |
public void highlight(String selText) { highlighter.removeAllHighlights(); if (selText == null || selText.isEmpty()) { return; } selText = selText.toLowerCase(); DefaultHighlightPainter hPainter = new DefaultHighlightPainter(Color.YELLOW); // String selText = txtPane.getSelectedText(); // = jTextPane1.getText(); String contText = ""; DefaultStyledDocument document = (DefaultStyledDocument) dataArea.getDocument(); try { contText = document.getText(0, document.getLength()); contText = contText.toLowerCase(); } catch (BadLocationException ex) { LoggerFactory.getLogger().error(null, ex); } int index = -1; int firstPos = 0; while ((index = contText.indexOf(selText, index)) > -1) { if (firstPos == 0) { firstPos = index; } try { highlighter.addHighlight(index, selText.length() + index, hPainter); index = index + selText.length(); } catch (BadLocationException ex) { LoggerFactory.getLogger().error(null, ex); } } try { // Get the rectangle of the where the text would be visible... Rectangle viewRect = dataArea.modelToView(firstPos); // Scroll to make the rectangle visible dataArea.scrollRectToVisible(viewRect); // Highlight the text dataArea.setCaretPosition(firstPos); // dataArea.moveCaretPosition(index); } catch (BadLocationException e) { } }
Example 4
Project: Scute-master File: PromptTextUI.java View source code |
/**
* Creates a label component, if none has already been created. Sets the
* prompt components properties to reflect the given {@link JTextComponent}s
* properties and returns it.
*
* @param txt
* @return the adjusted prompt component
*/
public JTextComponent getPromptComponent(JTextComponent txt) {
if (promptComponent == null) {
promptComponent = createPromptComponent();
}
if (txt.isFocusOwner() && PromptSupport.getFocusBehavior(txt) == FocusBehavior.HIDE_PROMPT) {
promptComponent.setText(null);
} else {
promptComponent.setText(PromptSupport.getPrompt(txt));
}
promptComponent.getHighlighter().removeAllHighlights();
if (txt.isFocusOwner() && PromptSupport.getFocusBehavior(txt) == FocusBehavior.HIGHLIGHT_PROMPT) {
promptComponent.setForeground(txt.getSelectedTextColor());
try {
promptComponent.getHighlighter().addHighlight(0, promptComponent.getText().length(), new DefaultHighlightPainter(txt.getSelectionColor()));
} catch (BadLocationException e) {
e.printStackTrace();
}
} else {
promptComponent.setForeground(PromptSupport.getForeground(txt));
}
if (PromptSupport.getFontStyle(txt) == null) {
promptComponent.setFont(txt.getFont());
} else {
promptComponent.setFont(txt.getFont().deriveFont(PromptSupport.getFontStyle(txt)));
}
promptComponent.setBackground(PromptSupport.getBackground(txt));
promptComponent.setHighlighter(new PainterHighlighter(PromptSupport.getBackgroundPainter(txt)));
promptComponent.setEnabled(txt.isEnabled());
promptComponent.setOpaque(txt.isOpaque());
promptComponent.setBounds(txt.getBounds());
Border b = txt.getBorder();
if (b == null) {
promptComponent.setBorder(txt.getBorder());
} else {
Insets insets = b.getBorderInsets(txt);
promptComponent.setBorder(createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right));
}
promptComponent.setSelectedTextColor(txt.getSelectedTextColor());
promptComponent.setSelectionColor(txt.getSelectionColor());
promptComponent.setEditable(txt.isEditable());
promptComponent.setMargin(txt.getMargin());
return promptComponent;
}
Example 5
Project: MiniJavaEditor-master File: EditorView.java View source code |
public void highlightCurrentLine(Color highlightColor) { int position = jEditorPane1.getCaretPosition(); int lineStart = document.getLineStartOffset(position); int lineEnd = document.getLineEndOffset(position); DefaultHighlightPainter redPainter = new DefaultHighlighter.DefaultHighlightPainter(highlightColor); try { this.jEditorPane1.getHighlighter().addHighlight(lineStart, lineEnd, redPainter); } catch (BadLocationException ble) { } }
Example 6
Project: DressCode_v0.5-master File: CodeField.java View source code |
//TODO: CLEAN THIS UP- seriously this is sad code. public void highlightLine(int lineNumber) throws BadLocationException { this.getHighlighter().removeAllHighlights(); int lineNum = lineNumber - 1; System.out.println("lineNumber=" + lineNumber); String text = this.getText(); ArrayList<String> lines = new ArrayList<String>(); final BufferedReader br = new BufferedReader(new StringReader(text)); String line; try { while ((line = br.readLine()) != null) { lines.add(line); } } catch (IOException e) { e.printStackTrace(); } int pos = 0; for (int i = 0; i < lineNum; i++) { pos += lines.get(i).length(); if (i != 0) { pos++; } } String chunk = text.substring(pos, text.length()); if (chunk.startsWith("\n")) { chunk = chunk.substring(1, chunk.length()); } int endPos = chunk.indexOf("\n"); if (endPos < 1) { endPos = text.length() - pos; } else { endPos++; } DefaultHighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.GREEN); this.getHighlighter().addHighlight(pos, pos + endPos, painter); }
Example 7
Project: swoop-master File: SwoopFrame.java View source code |
/** * Highlight the definition of the selected OWL Entity in the * Abstract Syntax source code */ public void highlightEntityAS() { if (swoopModel.getSelectedEntity() != null) { String entityURI = ""; try { entityURI = swoopModel.shortForm(swoopModel.getSelectedEntity().getURI()).toString(); entityURI = entityURI.substring(entityURI.lastIndexOf(":") + 1); } catch (OWLException e) { } // determine type of entity String entityTag = "Class"; if (swoopModel.getSelectedEntity() instanceof OWLObjectProperty) entityTag = "ObjectProperty"; else if (swoopModel.getSelectedEntity() instanceof OWLDataProperty) entityTag = "DatatypeProperty"; else if (swoopModel.getSelectedEntity() instanceof OWLIndividual) entityTag = "Individual"; boolean saveCaseSetting = srcWindow.matchCase; boolean saveWordSetting = srcWindow.matchWord; srcWindow.matchCase = false; srcWindow.matchWord = false; // get the ontology URI String strOntologyURI = ""; try { strOntologyURI = swoopModel.selectedOntology.getURI().toString(); } catch (OWLException e) { } // with the ontologyURI , search in the text for a match String strOntologyPrefix = srcWindow.originalSrc.substring(srcWindow.originalSrc.substring(0, srcWindow.originalSrc.indexOf(strOntologyURI)).lastIndexOf("Namespace(") + 10, srcWindow.originalSrc.indexOf(strOntologyURI)); //now read the first token from strFound // and that's the prefix we are looking for String[] tokens = strOntologyPrefix.split("(\\s)+"); strOntologyPrefix = tokens[0]; // match starting position of entity definition in source code String find = entityTag + "(" + strOntologyPrefix + ":" + entityURI; srcWindow.findMatch = find; int startMatch = srcWindow.matchString(true, false); // if match found, find ending position and select entire code fragment for selected entity if (startMatch != -1) { //look for the matching closing paren int endMatch = srcWindow.findClosingParent(startMatch); // endMatch += srcWindow.findMatch.length(); try { // srcWindow.codePane.select(startMatch, endMatch); DefaultHighlightPainter defhp = new DefaultHighlighter.DefaultHighlightPainter(Color.BLUE); srcWindow.dh.addHighlight(startMatch, endMatch, defhp); } catch (BadLocationException e) { e.printStackTrace(); } } // reset settings srcWindow.matchCase = saveCaseSetting; srcWindow.matchWord = saveWordSetting; } }
Example 8
Project: SikuliX-2014-master File: PromptTextUI.java View source code |
/**
* Creates a label component, if none has already been created. Sets the
* prompt components properties to reflect the given {@link JTextComponent}s
* properties and returns it.
*
* @param txt
* @return the adjusted prompt component
*/
public JTextComponent getPromptComponent(JTextComponent txt) {
if (promptComponent == null) {
promptComponent = createPromptComponent();
}
if (txt.isFocusOwner() && PromptSupport.getFocusBehavior(txt) == FocusBehavior.HIDE_PROMPT) {
promptComponent.setText(null);
} else {
promptComponent.setText(PromptSupport.getPrompt(txt));
}
promptComponent.getHighlighter().removeAllHighlights();
if (txt.isFocusOwner() && PromptSupport.getFocusBehavior(txt) == FocusBehavior.HIGHLIGHT_PROMPT) {
promptComponent.setForeground(txt.getSelectedTextColor());
try {
promptComponent.getHighlighter().addHighlight(0, promptComponent.getText().length(), new DefaultHighlightPainter(txt.getSelectionColor()));
} catch (BadLocationException e) {
e.printStackTrace();
}
} else {
promptComponent.setForeground(PromptSupport.getForeground(txt));
}
if (PromptSupport.getFontStyle(txt) == null) {
promptComponent.setFont(txt.getFont());
} else {
promptComponent.setFont(txt.getFont().deriveFont(PromptSupport.getFontStyle(txt)));
}
promptComponent.setBackground(PromptSupport.getBackground(txt));
promptComponent.setHighlighter(new PainterHighlighter(PromptSupport.getBackgroundPainter(txt)));
promptComponent.setEnabled(txt.isEnabled());
promptComponent.setOpaque(txt.isOpaque());
promptComponent.setBounds(txt.getBounds());
Border b = txt.getBorder();
if (b == null) {
promptComponent.setBorder(txt.getBorder());
} else {
Insets insets = b.getBorderInsets(txt);
promptComponent.setBorder(createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right));
}
promptComponent.setSelectedTextColor(txt.getSelectedTextColor());
promptComponent.setSelectionColor(txt.getSelectionColor());
promptComponent.setEditable(txt.isEditable());
promptComponent.setMargin(txt.getMargin());
return promptComponent;
}
Example 9
Project: swingx-master File: PromptTextUI.java View source code |
/**
* Creates a label component, if none has already been created. Sets the
* prompt components properties to reflect the given {@link JTextComponent}s
* properties and returns it.
*
* @param txt
* @return the adjusted prompt component
*/
public JTextComponent getPromptComponent(JTextComponent txt) {
if (promptComponent == null) {
promptComponent = createPromptComponent();
}
if (txt.isFocusOwner() && PromptSupport.getFocusBehavior(txt) == FocusBehavior.HIDE_PROMPT) {
promptComponent.setText(null);
} else {
promptComponent.setText(PromptSupport.getPrompt(txt));
}
promptComponent.getHighlighter().removeAllHighlights();
if (txt.isFocusOwner() && PromptSupport.getFocusBehavior(txt) == FocusBehavior.HIGHLIGHT_PROMPT) {
promptComponent.setForeground(txt.getSelectedTextColor());
try {
promptComponent.getHighlighter().addHighlight(0, promptComponent.getText().length(), new DefaultHighlightPainter(txt.getSelectionColor()));
} catch (BadLocationException e) {
e.printStackTrace();
}
} else {
promptComponent.setForeground(PromptSupport.getForeground(txt));
}
if (PromptSupport.getFontStyle(txt) == null) {
promptComponent.setFont(txt.getFont());
} else {
promptComponent.setFont(txt.getFont().deriveFont(PromptSupport.getFontStyle(txt)));
}
promptComponent.setBackground(PromptSupport.getBackground(txt));
promptComponent.setHighlighter(new PainterHighlighter(PromptSupport.getBackgroundPainter(txt)));
promptComponent.setEnabled(txt.isEnabled());
promptComponent.setOpaque(txt.isOpaque());
promptComponent.setBounds(txt.getBounds());
Border b = txt.getBorder();
if (b == null) {
promptComponent.setBorder(txt.getBorder());
} else {
Insets insets = b.getBorderInsets(txt);
promptComponent.setBorder(createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right));
}
promptComponent.setSelectedTextColor(txt.getSelectedTextColor());
promptComponent.setSelectionColor(txt.getSelectionColor());
promptComponent.setEditable(txt.isEditable());
promptComponent.setMargin(txt.getMargin());
return promptComponent;
}
Example 10
Project: abstools-master File: GraphicalDebugger.java View source code |
private void createHighlightPainters() {
for (TaskState s : TaskState.values()) {
highlightPainters.put(s, new DefaultHighlightPainter(GraphicalDebugger.getColor(s)));
}
}