1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.undo.*;
public class Demo {
public static SimpleAttributeSet style_error = null;
public static void main(String[] args) {
style_error = new SimpleAttributeSet();
StyleConstants.setForeground(style_error, Color.RED);
StyleConstants.setBackground(style_error, Color.WHITE);
StyleConstants.setItalic(style_error, false);
final JTextPane textPane = new JTextPane();
textPane.setPreferredSize(new Dimension(200, 200));
textPane.setDocument(new DefaultStyledDocument());
final UndoManager undoManager = new UndoManager() {
public synchronized boolean addEdit(UndoableEdit anEdit) {
if (anEdit instanceof AbstractDocument.DefaultDocumentEvent) {
AbstractDocument.DefaultDocumentEvent de = (AbstractDocument.DefaultDocumentEvent) anEdit;
if (de.getType() == DocumentEvent.EventType.CHANGE) {
return false;
}
}
return super.addEdit(anEdit);
}
};
textPane.getStyledDocument().addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
}
});
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JScrollPane(textPane), BorderLayout.CENTER);
JPanel buttons = new JPanel();
panel.add(buttons, BorderLayout.SOUTH);
JButton undo = new JButton("Undo");
undo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
undoManager.undo();
textPane.requestFocus();
} catch (CannotUndoException ex) {
}
}
});
buttons.add(undo);
JButton redo = new JButton("Redo");
redo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
undoManager.redo();
textPane.requestFocus();
} catch (CannotRedoException ex) {
}
}
});
buttons.add(redo);
JButton style = new JButton("Style");
style.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Pattern p = Pattern.compile("text");
String out = textPane.getDocument().getText(0, textPane.getDocument().getLength()); // <=
// =
Matcher matcher = p.matcher(out);
while (matcher.find()) {
textPane.getStyledDocument().setCharacterAttributes(matcher.start(), matcher.end() - matcher.start(), style_error, true);
}
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
});
buttons.add(style);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
// Robot :
// 1 : insert text
try {
textPane.getDocument().insertString(0, "this is a text\nDoo, a new line in this text\nA new line again !", null);
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 2 : Apply style :
style.getActionListeners()[0].actionPerformed(null);
// 3 : Undo
undoManager.undo();
// 4 : Insert new text
try {
textPane.getDocument().insertString(0, "1\n2", null);
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} |
Partager