1: package com.jtkdemo;
2:
3: import java.awt.*;
4: import java.awt.event.*;
5: import javax.swing.*;
6: import javax.swing.border.*;
7: import com.jthreadkit.*;
8: import com.jthreadkit.swing.*;
9:
10: public class JTKLabelDemo extends JPanel {
11: private JTKButton startB;
12: private JTKLabel label;
13: private Worker worker;
14: private String[] values = {"Chef", "Kenny", "Stan", "Kyle", "Wendy",
15: "Cartman", "Mr. Hand", "Pip", "Ike",
16: "Terrance", "Phillip", "Timmy", "Shelly",
17: "Filmore"};
18:
19: public JTKLabelDemo() {
20: worker = new Worker();
21: this.setLayout(new BorderLayout(10, 5));
22: this.setBorder(BorderFactory.createEmptyBorder(10, 5, 5, 5));
23: buildTopPanel();
24: buildBottomPanel();
25: }
26:
27: private void buildTopPanel() {
28: // create a temp label to get a good preferred size...
29: JLabel tmpLbl = new JLabel("XXXXXXXXXXXXXXXXXXXX", JLabel.CENTER);
30: tmpLbl.setBorder(
31: BorderFactory.createCompoundBorder(
32: BorderFactory.createTitledBorder("JTK Label"),
33: BorderFactory.createCompoundBorder(
34: BorderFactory.createEmptyBorder(5, 10, 10, 10),
35: BorderFactory.createLineBorder(Color.black)
36: )
37: )
38: );
39:
40: label = new JTKLabel(tmpLbl);
41: this.add(label.getLabel(), BorderLayout.NORTH);
42: }
43:
44: private void buildBottomPanel() {
45: JButton tmp = new JButton("Start");
46: tmp.setMnemonic('S');
47: tmp.addActionListener(new ActionListener() {
48: public void actionPerformed(ActionEvent e) {
49: if ( e.getActionCommand().equals("Start") ) {
50: startDemo();
51: } else {
52: stopDemo();
53: }
54: }
55: });
56: // Use a JTKButton so we can easily switch the text from any thread
57: startB = new JTKButton(tmp);
58: JPanel bottomP = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
59: bottomP.add(startB.getButton());
60: this.add(bottomP, BorderLayout.CENTER);
61: }
62:
63: protected void initialize() {
64: label.setTextSafely("Ready...");
65: }
66:
67: private void startDemo() {
68: // change the label to stop
69: startB.setTextSafely("Stop");
70: worker.resumeRequest();
71: }
72:
73: private void stopDemo() {
74: worker.suspendRequest();
75: // change label back to start
76: startB.setTextSafely("Start");
77: }
78:
79: protected void endDemo() {
80: worker.stopRequest();
81: }
82:
83: public static JFrame createFramedInstance(final boolean exitOnClose) {
84: final JTKLabelDemo demo = new JTKLabelDemo();
85: final JFrame f = new JFrame("JTKLabel Demo");
86: f.setContentPane(demo);
87: f.addWindowListener(new WindowAdapter() {
88: public void windowOpened(WindowEvent e) {
89: demo.initialize();
90: }
91: public void windowClosing(WindowEvent w) {
92: demo.endDemo();
93: f.setVisible(false);
94: f.dispose();
95:
96: if ( exitOnClose ) {
97: System.exit(0);
98: }
99: }
100: });
101:
102: f.pack();
103:
104: Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
105: f.setLocation((screenDim.width - f.getSize().width) / 2,
106: (screenDim.height - f.getSize().height) / 2);
107:
108: return f;
109: }
110:
111: public static void main(String[] args) {
112: JFrame f = createFramedInstance(true);
113: f.setVisible(true);
114: }
115:
116: // Self running object with the ability to stop, start and resume
117: // the internal helper thread...
118: private class Worker extends Object {
119: private Thread internalThread;
120: private volatile boolean stopRequested;
121: private SyncBoolean suspendRequested;
122:
123: public Worker() {
124: stopRequested = false;
125: // start up in wait mode
126: suspendRequested = new SyncBoolean(true);
127:
128: Runnable r = new Runnable() {
129: public void run() {
130: try {
131: runWork();
132: } catch ( Exception x ) {
133: x.printStackTrace();
134: }
135: }
136: };
137: internalThread = new Thread(r);
138: internalThread.start();
139: }
140:
141: private void runWork() {
142: while ( !stopRequested ) {
143: try {
144: // will wait if we are in suspend status
145: waitWhileSuspended();
146: // do our work...
147: label.setTextSafely(
148: values[(int)(Math.random() * values.length)]);
149: // sleep to cause a delay...
150: Thread.sleep(1000);
151: } catch ( InterruptedException ix ) {
152: Thread.currentThread().interrupt();
153: continue;
154: }
155: }
156: }
157:
158: private void waitWhileSuspended() throws InterruptedException {
159: suspendRequested.waitUntilFalse();
160: }
161:
162: public void suspendRequest() {
163: suspendRequested.setValue(true);
164: }
165:
166: public void resumeRequest() {
167: suspendRequested.setValue(false);
168: }
169:
170: public void stopRequest() {
171: stopRequested = true;
172: internalThread.interrupt();
173: }
174: }
175: }
|