summaryrefslogtreecommitdiff
path: root/src/Client/UserInterface.java
blob: 984ecb6e96c2f4f3b58a7bd950162a409259cd83 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package Client;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class UserInterface extends JFrame implements ActionListener, KeyListener, WindowListener
{	
	
	private JTextField message;
	private JTextArea output;
	private ChatClient client;
	private JTextField host = new JTextField("localhost", 15);
	private JTextField port = new JTextField("6667", 4);
	private JButton connect;
	private JButton send;
	
	/**showing your  message on the output text area. */
	public void outputWriter (String line)
	{
		output.append(line + "\n");
	}
	
	/**showing the message on the output text area. */
	private void msgSendRun ()
	{
		if (message.getText().equals(""))
		{	
			return;
		}
			outputWriter("me: " + message.getText());
			client.PRIVMSG(message.getText());
			message.setText("");	
			output.setCaretPosition(output.getText().length());
	}
	
	/**sending connection request and and verify it happened only one time */
	private void msgConnectRun ()
	{
		output.append("Connecting..." + "\n");
		counterClick++;
		this.send.setEnabled(true);
		this.connect.setEnabled(false);
	}
	
	/** clearing the output*/
	public void Clear ()
	{
		output.setText("");
	}
	
	/** Graphic interface recipient the client*/
	public UserInterface(ChatClient client)
	{
		
		this.setTitle("Dor-Chat IRC");
		this.client = client;
		this.setSize(800, 400);
		this.setMinimumSize(new Dimension(600,170));
		
		Font font = new Font ("Ariel" , Font.BOLD ,12);
		
		output = new JTextArea(10, 20);
		output.setBackground(Color.white);
		output.setFont(font);
		output.setEditable(false);
		output.setLineWrap(true);
		output.setWrapStyleWord(true);
				
		JPanel msgPanel = new JPanel();
		JPanel conPanel = new JPanel();
		JPanel vertPanel = new JPanel();
		
	    JScrollPane scrollText = new JScrollPane(output);
		
		message = new JTextField(40);
		message.addKeyListener(this);
		message.setFont(font);
		message.setForeground(Color.BLUE);
		JButton stop = new JButton("Stop");
		stop.setActionCommand("stop");
		stop.addActionListener(this);
		stop.setToolTipText("Click this button to exit.");
		
		this.send = new JButton("Send");
		send.setActionCommand("send");
		send.addActionListener(this);
		send.setToolTipText("Click this button to send message.");
			
		this.connect = new JButton("Connect");
		connect.setActionCommand("connect");
		connect.addActionListener(this);
		connect.setToolTipText("Click this button to connect the server.");
		this.send.setEnabled(false);
		
		vertPanel.setLayout(new BoxLayout(vertPanel, BoxLayout.PAGE_AXIS));	
		
		Dimension MaximumSize = new Dimension (800 , send.getHeight());
		
		msgPanel.add(send);	
		msgPanel.add(new JLabel("Message:"));
		msgPanel.add(message);
		msgPanel.setMaximumSize(MaximumSize);
		
		conPanel.add(connect);
		conPanel.add(new JLabel("host:"));
		conPanel.add(host);
		conPanel.add(new JLabel("port:"));
		conPanel.add(port);
		conPanel.add(stop);
		conPanel.setMaximumSize(MaximumSize);
		
		vertPanel.add(scrollText);
		vertPanel.add(conPanel);
		vertPanel.add(msgPanel);
		
		add(vertPanel);

		setVisible(true);
		
		this.addWindowListener(this);
	}

	int counterClick = 0;
	
	/** Syncing the the action that the button send to the perform
	 * @param e -name of the action  */
	public void actionPerformed(ActionEvent e)
	{
		String command = e.getActionCommand();
		
		if (command == "stop")
		{	
			client.shutdown();
			System.exit(0);
		}	
		
		if (command == "send")
		{
			msgSendRun();
		}
		
		if (command == "connect" && counterClick == 0)
		{
			if (client.connect(host.getText(),Integer.parseInt(port.getText())))
			{
				msgConnectRun ();
			}
		}
	}
	
	/** Syncing the the action that the event caused to the perform
	 * @param e - event key press  */
	@Override
	public void keyPressed(KeyEvent e)
	{
		
		if (e.getKeyChar() == KeyEvent.VK_ENTER && counterClick != 0)
		{
			msgSendRun();		
		}
		
		if (e.getKeyChar() == KeyEvent.VK_ESCAPE)
		{
			client.shutdown();
			System.exit(0);
		}
		
		if ((e.getModifiersEx() & KeyEvent.ALT_DOWN_MASK) != 0  && (e.getKeyChar() == KeyEvent.VK_C && counterClick == 0)) 
			
		if (client.connect(host.getText(),Integer.parseInt(port.getText())))
		{
			msgConnectRun();
		}

	}
	
	/** Syncing the the action closing window to the shutdown
	 * @param e - event window closing  */
	public void windowClosing(WindowEvent e) 
	{
		client.shutdown();
		System.exit(0);
    }
    
	public void keyReleased(KeyEvent arg0) {} 

	public void keyTyped(KeyEvent arg0) {}

	@Override
	public void windowActivated(WindowEvent arg0){}

	@Override
	public void windowClosed(WindowEvent arg0){}
	
	@Override
	public void windowDeactivated(WindowEvent arg0){}
	
	@Override
	public void windowDeiconified(WindowEvent arg0){}
	

	@Override
	public void windowIconified(WindowEvent arg0){}
	
	@Override
	public void windowOpened(WindowEvent arg0){}
	
}