1: package com.jtkdemo;
2:
3: import java.awt.*;
4: import java.awt.event.*;
5: import javax.swing.*;
6: import com.jthreadkit.*;
7: import com.jthreadkit.swing.*;
8:
9: public class JTKTextAreaDemo extends JPanel {
10: private JButton startB;
11: private JTKTextArea textArea;
12: private Worker worker;
13: private String[] values = {"Chef", "Kenny", "Stan", "Kyle", "Wendy",
14: "Cartman", "Mr. Hand", "Pip", "Ike", "",
15: "This is a pretty long line...Ending now...",
16: "Terrance", "Phillip", "Timmy!!!", "Shelly",
17: "Filmore", "blah, blah, blah....",
18: "I\nam\na\nbig block\nof\ntext\non\nmultiple" +
19: "\nlines", ""};
20:
21: public JTKTextAreaDemo() {
22: worker = new Worker();
23: this.setLayout(new BorderLayout(10, 5));
24: this.setBorder(BorderFactory.createEmptyBorder(10, 5, 5, 5));
25: buildTopPanel();
26: buildBottomPanel();
27: }
28:
29: private void buildTopPanel() {
30: textArea = new JTKTextArea(new JTextArea());
31: textArea.getTextArea().setEditable(false);
32: JScrollPane sp = new JScrollPane(textArea.getTextArea());
33: JPanel topPanel = new JPanel(new BorderLayout());
34: topPanel.setBorder(
35: BorderFactory.createTitledBorder("JTKTextArea"));
36: topPanel.add(sp);
37: this.add(topPanel, BorderLayout.CENTER);
38: }
39:
40: private void buildBottomPanel() {
41: startB = new JButton("Start");
42: startB.setMnemonic('S');
43: startB.addActionListener(new ActionListener() {
44: public void actionPerformed(ActionEvent e) {
45: if ( e.getActionCommand().equals("Start") ) {
46: startDemo();
47: startB.setText("Stop");
48: } else {
49: stopDemo();
50: startB.setText("Start");
51: }
52: }
53: });
54: JPanel bottomP = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
55: bottomP.add(startB);
56: this.add(bottomP, BorderLayout.SOUTH);
57: }
58:
59: protected void initialize() {
60: textArea.setTextSafely("Ready...\n\n");
61: }
62:
63: private void startDemo() {
64: worker.resumeRequest();
65: }
66:
67: private void stopDemo() {
68: worker.suspendRequest();
69: }
70:
71: protected void endDemo() {
72: worker.stopRequest();
73: }
74:
75: public static JFrame createFramedInstance(final boolean exitOnClose) {
76: final JTKTextAreaDemo demo = new JTKTextAreaDemo();
77: final JFrame f = new JFrame("JTKTextArea Demo");
78: f.setContentPane(demo);
79: f.addWindowListener(new WindowAdapter() {
80: public void windowOpened(WindowEvent e) {
81: demo.initialize();
82: }
83: public void windowClosing(WindowEvent w) {
84: demo.endDemo();
85: f.setVisible(false);
86: f.dispose();
87:
88: if ( exitOnClose ) {
89: System.exit(0);
90: }
91: }
92: });
93: f.setSize(350, 250);
94: Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
95: f.setLocation((screenDim.width - f.getSize().width) / 2,
96: (screenDim.height - f.getSize().height) / 2);
97:
98: return f;
99: }
100:
101: public static void main(String[] args) {
102: JFrame f = createFramedInstance(true);
103: f.setVisible(true);
104: }
105:
106: // Self running object with the ability to stop, start and resume
107: // the internal helper thread...
108: private class Worker extends Object {
109: private Thread internalThread;
110: private volatile boolean noStopRequested;
111: private SyncBoolean suspendRequested;
112:
113: public Worker() {
114: noStopRequested = true;
115: // start up in wait mode
116: suspendRequested = new SyncBoolean(true);
117:
118: Runnable r = new Runnable() {
119: public void run() {
120: runWork();
121: }
122: };
123: internalThread = new Thread(r);
124: internalThread.start();
125: }
126:
127: private void runWork() {
128: try {
129: while ( noStopRequested ) {
130: // will wait if we are in suspend status
131: waitWhileSuspended();
132: // do our work...
133: String item = values[(int)(Math.random() * values.length)];
134: if ( item.length() < 1 ) {
135: textArea.setTextSafely(item);
136: } else {
137: textArea.appendSafely(item + "\n");
138: }
139: // sleep to cause a delay...
140: Thread.sleep(1000);
141: }
142: } catch ( InterruptedException ix ) {
143: Thread.currentThread().interrupt();
144: }
145: }
146:
147: private void waitWhileSuspended() throws InterruptedException {
148: suspendRequested.waitUntilFalse();
149: }
150:
151: public void suspendRequest() {
152: suspendRequested.setValue(true);
153: }
154:
155: public void resumeRequest() {
156: suspendRequested.setValue(false);
157: }
158:
159: public void stopRequest() {
160: noStopRequested = false;
161: internalThread.interrupt();
162: }
163: }
164: }
|