1 package org.glassbox.gui;
2
3 import java.awt.event.FocusListener;
4 import java.awt.event.FocusEvent;
5 import java.awt.event.FocusAdapter;
6 import javax.swing.JTextArea;
7
8 /***
9 A extension of the JTextArea. This implementation resets its text automatically
10 on a focus-lost event to the previous value.
11 */
12 public class AutoResetTextArea extends JTextArea {
13 public interface TextProvider {
14 /***
15 Get the valid text to put into the text area
16 when a focus-lost event has been received by
17 the textarea.
18 */
19 String getValidText();
20 }
21
22 private FocusListener FOCUS_LISTENER = new FocusAdapter() {
23 public void focusLost(FocusEvent e) {
24 setText(_provider.getValidText());
25 }
26 };
27
28 private TextProvider _provider;
29
30 public AutoResetTextArea(TextProvider provider) {
31 super(provider.getValidText());
32 _provider = provider;
33 addFocusListener(FOCUS_LISTENER);
34 }
35
36 public AutoResetTextArea(TextProvider provider, int rows, int cols) {
37 super(provider.getValidText(), rows, cols);
38 _provider = provider;
39 addFocusListener(FOCUS_LISTENER);
40 }
41 }