public class WordEditorSearch extends javax.swing.JDialog {
private JTextComponent textComp;
public WordEditorSearch(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
setVisible(false);
}
};
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
getRootPane().registerKeyboardAction(al, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
}
public void setTextComponent(JTextComponent textComp) {
this.textComp = textComp;
}
@SuppressWarnings("unchecked")
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Text to find");
setMinimumSize(new java.awt.Dimension(125, 23));
setResizable(false);
jPanel1.setPreferredSize(new java.awt.Dimension(300, 64));
jPanel1.setLayout(new java.awt.GridBagLayout());
jLabel1.setDisplayedMnemonic('t');
jLabel1.setLabelFor(jTextField1);
jLabel1.setText("Text To Find:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
jPanel1.add(jLabel1, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
jPanel1.add(jTextField1, gridBagConstraints);
jButton1.setMnemonic('f');
jButton1.setText("Find");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
jPanel1.add(jButton1, gridBagConstraints);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jTextField1.getText().length() == 0)
removeHighlights(textComp);
else
highlight(textComp, jTextField1.getText());
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
// An instance of the private subclass of the default highlight painter
Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(UIManager.getColor("Table.selectionBackground"));
// A private subclass of the default highlight painter
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
public MyHighlightPainter(Color color) {
super(color);
}
}
// Creates highlights around all occurrences of pattern in textComp
private void highlight(JTextComponent textComp, String pattern) {
// First remove all old highlights
removeHighlights(textComp);
Highlighter hilite = textComp.getHighlighter();
Document doc = textComp.getDocument();
String text = null;
try {
text = doc.getText(0, doc.getLength());
} catch (BadLocationException ex) {
Logger.getLogger(WordEditorSearch.class.getName()).log(Level.SEVERE, null, ex);
}
int pos = 0;
// Search for pattern
while ((pos = text.toLowerCase().indexOf(pattern.toLowerCase(), pos)) >= 0) {
try {
// Create highlighter using private painter and apply around pattern
hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
} catch (BadLocationException ex) {
Logger.getLogger(WordEditorSearch.class.getName()).log(Level.SEVERE, null, ex);
}
pos += pattern.length();
}
}
// Removes only our private highlights
private void removeHighlights(JTextComponent textComp) {
Highlighter hilite = textComp.getHighlighter();
Highlighter.Highlight[] hilites = hilite.getHighlights();
for (int i=0; i<hilites.length; i++) {
if (hilites[i].getPainter() instanceof MyHighlightPainter) {
hilite.removeHighlight(hilites[i]);
}
}
}
@Override
public void setVisible(boolean vis) {
super.setVisible(vis);
if (vis)
jTextField1.setText("");
else
removeHighlights(textComp);
}
}