filebot/source/net/filebot/ui/rename/HighlightListCellRenderer.java

110 lines
2.8 KiB
Java
Raw Normal View History

2008-03-22 17:12:34 -04:00
2014-04-19 02:30:29 -04:00
package net.filebot.ui.rename;
2008-03-22 17:12:34 -04:00
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Insets;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Highlighter;
2008-03-22 17:12:34 -04:00
import javax.swing.text.JTextComponent;
2014-04-19 02:30:29 -04:00
import net.filebot.util.ui.AbstractFancyListCellRenderer;
2014-07-29 02:45:15 -04:00
import net.filebot.util.ui.SwingUI;
class HighlightListCellRenderer extends AbstractFancyListCellRenderer {
2015-07-25 18:47:19 -04:00
2009-10-28 21:22:00 -04:00
protected final JTextComponent textComponent = new JTextField();
2015-07-25 18:47:19 -04:00
2009-10-28 21:22:00 -04:00
protected final Pattern pattern;
protected final Highlighter.HighlightPainter highlightPainter;
2015-07-25 18:47:19 -04:00
2009-10-28 21:22:00 -04:00
2008-03-22 17:12:34 -04:00
public HighlightListCellRenderer(Pattern pattern, Highlighter.HighlightPainter highlightPainter, int padding) {
super(new Insets(0, 0, 0, 0));
2015-07-25 18:47:19 -04:00
this.pattern = pattern;
this.highlightPainter = highlightPainter;
2015-07-25 18:47:19 -04:00
// pad the cell from inside the text component,
// so the HighlightPainter may paint in this space as well
textComponent.setBorder(new EmptyBorder(padding, padding, padding, padding));
2015-07-25 18:47:19 -04:00
// make text component transparent, should work for all LAFs (setOpaque(false) may not, e.g. Nimbus)
2014-07-29 02:45:15 -04:00
textComponent.setBackground(SwingUI.TRANSLUCENT);
2015-07-25 18:47:19 -04:00
2008-03-22 17:12:34 -04:00
this.add(textComponent, BorderLayout.WEST);
2015-07-25 18:47:19 -04:00
2008-03-22 17:12:34 -04:00
textComponent.getDocument().addDocumentListener(new HighlightUpdateListener());
}
2015-07-25 18:47:19 -04:00
@Override
2008-03-22 17:12:34 -04:00
protected void configureListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.configureListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
2015-07-25 18:47:19 -04:00
2008-03-22 17:12:34 -04:00
textComponent.setText(value.toString());
}
2015-07-25 18:47:19 -04:00
protected void updateHighlighter() {
2008-03-22 17:12:34 -04:00
textComponent.getHighlighter().removeAllHighlights();
2015-07-25 18:47:19 -04:00
2008-03-22 17:12:34 -04:00
Matcher matcher = pattern.matcher(textComponent.getText());
2015-07-25 18:47:19 -04:00
while (matcher.find()) {
try {
2008-03-22 17:12:34 -04:00
textComponent.getHighlighter().addHighlight(matcher.start(0), matcher.end(0), highlightPainter);
} catch (BadLocationException e) {
//should not happen
Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.toString(), e);
}
}
}
2015-07-25 18:47:19 -04:00
2008-03-22 17:12:34 -04:00
@Override
public void setForeground(Color fg) {
super.setForeground(fg);
2015-07-25 18:47:19 -04:00
2008-03-22 17:12:34 -04:00
// textComponent is null while in super constructor
if (textComponent != null) {
textComponent.setForeground(fg);
}
}
2015-07-25 18:47:19 -04:00
2009-10-28 21:22:00 -04:00
private class HighlightUpdateListener implements DocumentListener {
2015-07-25 18:47:19 -04:00
@Override
public void changedUpdate(DocumentEvent e) {
updateHighlighter();
}
2015-07-25 18:47:19 -04:00
@Override
public void insertUpdate(DocumentEvent e) {
updateHighlighter();
}
2015-07-25 18:47:19 -04:00
@Override
public void removeUpdate(DocumentEvent e) {
updateHighlighter();
}
2015-07-25 18:47:19 -04:00
}
2015-07-25 18:47:19 -04:00
}