Repository navigation
Expand file tree
/
Copy pathApplication.java
More file actions
190 lines (139 loc) · 5.3 KB
/
Copy pathApplication.java
File metadata and controls
190 lines (139 loc) · 5.3 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Font;
import java.io.PrintStream;
import gui.TextAreaOutputStream;
import signal.awt.WindowTransmitter;
import signal.TransmitterThread;
import signal.AsyncTransmitter;
import signal.Receiver;
import signal.Signal;
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// APPLICATION CLASS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class Application
{
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// PUBLIC METHODS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// CONSTRUCTORS -----------------------------------------------------------
public Application()
{
// Create windows
createConsoleWindow();
createMainWindow();
// Set close flag
close = new Boolean( false );
}
// CREATE CONSOLE WINDOW --------------------------------------------------
public void createConsoleWindow()
{
// Create console window
console = new JFrame();
console.setTitle( "Console" );
console.setBounds( 10, 10, 500, 500 );
// Create text area and add to console
textArea = new JTextArea();
textArea.setFont( new Font( "Monospaced", Font.PLAIN, 14 ) );
textArea.setEditable( false );
console.add( textArea );
console.add( new JScrollPane( textArea ) );
// Create output stream and redirect system.out
outputStream = new TextAreaOutputStream( textArea );
System.setOut( new PrintStream( outputStream, true ) );
}
// CREATE MAIN WINDOW -----------------------------------------------------
public void createMainWindow()
{
// Create main window
mainWindow = new JFrame();
mainWindow.setTitle( "Main Window" );
mainWindow.setBounds( 100, 100, 500, 500 );
// Create window transmitter
windowTransmitter = new WindowTransmitter();
// Register receivers
windowTransmitter.register
(
new Receiver()
{
public void receive( Signal signal )
{
System.out.print( "Main window activated.\n" );
}
},
WindowTransmitter.WINDOW_ACTIVATED
);
windowTransmitter.register
(
new Receiver()
{
public void receive( Signal signal )
{
System.out.print( "Main window closing.\n" );
synchronized( close )
{
close.notify();
}
}
},
WindowTransmitter.WINDOW_CLOSING
);
// Add transmitter as listener
mainWindow.addWindowListener( windowTransmitter );
}
// START ------------------------------------------------------------------
public void start()
{
// Show windows
console.setVisible( true );
mainWindow.setVisible( true );
// Create and start transmitter threads
TransmitterThread thread1 = AsyncTransmitter.createThread();
TransmitterThread thread2 = AsyncTransmitter.createThread();
TransmitterThread thread3 = AsyncTransmitter.createThread();
thread1.start();
thread2.start();
thread3.start();
// Wait for close
try
{
synchronized( close )
{
close.wait();
}
}
catch( Throwable t )
{
System.out.print( "Application wait state interrupted.\n" );
}
// Dispose of windows
mainWindow.dispose();
console.dispose();
// Stop transmitter threads
try
{
thread1.join();
thread2.join();
thread3.join();
}
catch( Throwable t ){}
}
// MAIN -------------------------------------------------------------------
public static void main( String[] argv )
{
Application app = new Application();
app.start();
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// PRIVATE DATA +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private JFrame console;
private JTextArea textArea;
private TextAreaOutputStream outputStream;
private JFrame mainWindow;
private WindowTransmitter windowTransmitter;
private Boolean close;
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++