-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChatWindow.java
More file actions
executable file
·171 lines (129 loc) · 3.79 KB
/
ChatWindow.java
File metadata and controls
executable file
·171 lines (129 loc) · 3.79 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
package dchat;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.*;
public class ChatWindow extends JFrame {
private static final long serialVersionUID = 3421135597049702610L;
public static final String DATE_FORMAT_NOW = "HH:mm:ss";
JTextArea textArea;
JTextField inputArea;
JLabel label;
ChatWindowClient client = null;
private WindowListener windowListener = new WindowListener() {
public void windowActivated(WindowEvent e) {}
//Handles the event of the window becoming closed.
public void windowClosed(WindowEvent e) {
//Exit the application
if(client != null)
client.windowClosed();
}
//Handles the event of a user trying to close the window
public void windowClosing(WindowEvent e) {
//Disposing the Window
e.getWindow().dispose();
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
//Handles the event of a user starting the window
public void windowOpened(WindowEvent e) {
if(client != null)
client.windowOpened();
}
};
private KeyListener keyListener = new KeyListener(){
/*
* Handles the event of a keypress. If the key is an ENTER, the text is
* sent to the client and the input is cleared.
*
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
*/
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 0x0a) {
if (inputArea.getText().length() > 0) {
if (client != null)
try {
client.textAvailable(inputArea.getText() + '\n');
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
inputArea.setText(new String());
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
};
public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
/*
* Main Constructor for the ChatWindow class Accepts a ChatWindowClient as
* parameter
*/
public ChatWindow(String name, ChatWindowClient cl) {
client = cl;
label = new JLabel("Nick: " + name);
textArea = new JTextArea();
inputArea = new JTextField();
// Add a Label with nick
getContentPane().add(label, BorderLayout.NORTH);
// Add a scrolling text area
textArea.setEditable(false);
textArea.setRows(20);
textArea.setColumns(80);
textArea.setFont(new Font("Courier", 14, 14));
JScrollPane scroll = new JScrollPane(textArea);
scroll.setSize(100, 100);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
getContentPane().add(scroll, BorderLayout.CENTER);
// Add a text input area
inputArea.setEditable(true);
inputArea.setColumns(80);
inputArea.addKeyListener(keyListener);
getContentPane().add(inputArea, BorderLayout.SOUTH);
addWindowListener(windowListener);
// Pack and set visible
pack();
setVisible(true);
}
/*
* Sets the current nick to the string provided
*/
public void setNick(String name) {
label.setText("Nick: " + name);
}
/*
* Writes a String to the Output Text Area
*/
public void writeText(String text) {
textArea.append(now()+" : "+text);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
/*
* Clears the Output Text Area
*/
public void clearTextArea() {
textArea.setText(new String());
}
/*
* Clears the Input Text Area
*/
public void clearInputArea(){
inputArea.setText(new String());
}
/*
* Sets the value of the ChatWindowClient
*/
public void setClient(ChatWindowClient cl) {
client = cl;
}
};