Source code: com/ciphermod/cipherchat/GUI.java
1 /* $Id: GUI.java,v 1.1 2001/03/26 10:48:43 cvsbob Exp $ */
2
3 /*
4 * GUI.java, gui front end for the CipherChat ChatClient.
5 * Copyright (C) 2001 Robert Bushman.
6 *
7 * I reserve the right to release this program under seperate license.
8 * If you require a special license grant contact Robert Bushman.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
24 */
25
26 package com.ciphermod.cipherchat;
27
28 import java.awt.BorderLayout;
29 import java.awt.Dimension;
30 import java.awt.Point;
31 import java.awt.event.ActionListener;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.KeyListener;
34 import java.awt.event.KeyEvent;
35 import java.awt.event.WindowAdapter;
36 import java.awt.event.WindowEvent;
37 import javax.swing.JFrame;
38 import javax.swing.JScrollPane;
39 import javax.swing.JTextArea;
40 import javax.swing.JTextField;
41 import javax.swing.JViewport;
42
43 public class GUI extends JFrame implements KeyListener, ActionListener {
44
45 // ----------------------------------------------
46 // CONSTRUCTORS
47 // ----------------------------------------------
48
49 public GUI() {
50 super( "CipherChat" );
51 initializeSelf();
52 getContentPane().setLayout( new BorderLayout() );
53 getContentPane().add( getScrollArea(), BorderLayout.CENTER );
54 getContentPane().add( getInputField(), BorderLayout.SOUTH );
55 addWindowListener( getWindowClosingAdapter() );
56 setJMenuBar( new ChatMenuBar( this ) );
57 }
58
59 protected void initializeSelf() {
60 // This doesn't work on Linux
61 initializeChatAreas();
62 }
63
64 protected void initializeChatAreas() {
65 getInputField().addActionListener( this );
66 getInputField().addKeyListener( this );
67 getMessageArea().setEditable( false );
68 getMessageArea().setLineWrap( true );
69 getMessageArea().setWrapStyleWord( true );
70 getScrollArea().setVerticalScrollBarPolicy
71 ( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
72 getScrollArea().setPreferredSize( new Dimension( 250, 250 ) );
73 }
74
75 // -----------------------------------------------
76 // PUBLIC API
77 // ----------------------------------------------
78
79 public void close() {
80 if( getPoster() != null ) {
81 getPoster().pleaseStop();
82 }
83 getChat().close();
84 System.exit( 0 );
85 }
86
87 protected void finalize() {
88 close();
89 }
90
91 // -----------------------------------------------
92 // ACTIONLISTENER IMPLEMENTATION
93 // -----------------------------------------------
94
95 public void actionPerformed( ActionEvent event ) {
96 String command = event.getActionCommand();
97 if( command.equals( "Connect" ) ) {
98 openConnectDialog();
99 }
100 if( command.equals( "Exit" ) ) {
101 close();
102 }
103 if( command.equals( "Connect >> Cancel" ) ) {
104 getConnectDialog().dispose();
105 }
106 if( command.equals( "Connect >> Connect" ) ) {
107 processConnectDialog();
108 }
109 }
110
111 // --------------------------------------------------
112 // KEYLISTENER IMPLEMENTATION
113 // --------------------------------------------------
114
115 public void keyPressed( KeyEvent event ) {}
116 public void keyReleased( KeyEvent event ) {}
117 public void keyTyped( KeyEvent event ) {
118 if( event.getKeyChar() == '\n' ) {
119 String message = getInputField().getText();
120 if( message != null && ! message.equals( "" ) ) {
121 getInputField().setText( "" );
122 System.out.println( "Action: Add Message: " + message );
123 getChat().sendMessage( message );
124 }
125 }
126 }
127
128 // ----------------------------------------------
129 // INTERNAL API
130 // ----------------------------------------------
131
132 protected void connect( String chatName, String hostName, int port ) {
133 ChatClient chat = new ChatClient( chatName, hostName, port );
134 setChat( chat );
135 chat.initialize();
136 MessagePoster poster = new MessagePoster( getMessageArea(),
137 getScrollArea(),
138 getChat() );
139 poster.start();
140 setPoster( poster );
141 }
142
143 protected WindowAdapter getWindowClosingAdapter() {
144 WindowAdapter closingAdapter = new WindowAdapter() {
145 public void windowClosing( WindowEvent e ) {
146 close();
147 }
148 };
149 return( closingAdapter );
150 }
151
152 protected void openConnectDialog() {
153 setConnectDialog( new ConnectDialog( this, this ) );
154 getConnectDialog().pack();
155 getConnectDialog().show();
156 }
157
158 protected void processConnectDialog() {
159 String hostName = getConnectDialog().getHostName();
160 int port = getConnectDialog().getPort();
161 String chatName = getConnectDialog().getChatName();
162 setTitle( "CipherChat: " + chatName + "@" + hostName );
163 connect( chatName, hostName, port );
164 getConnectDialog().dispose();
165 }
166
167 // ----------------------------------------------
168 // INSTANCE PARAMETERS AND ACCESSORS
169 // ----------------------------------------------
170
171 // PARAMETERS
172 private ChatClient _chat;
173 private JTextField _inputField = new JTextField();
174 private JTextArea _messageArea = new JTextArea();
175 private JScrollPane _scrollArea = new JScrollPane( _messageArea );
176 private MessagePoster _poster = null;
177 private ConnectDialog _connectDialog;
178
179 // SETTERS
180 protected void setChat( ChatClient chat ) { _chat = chat; }
181 protected void setPoster( MessagePoster poster ) { _poster = poster; }
182 protected void setConnectDialog( ConnectDialog connectDialog ) {
183 _connectDialog = connectDialog;
184 }
185
186 // GETTERS
187 protected ChatClient getChat() { return( _chat ); }
188 protected JTextField getInputField() { return( _inputField ); }
189 protected JTextArea getMessageArea() { return( _messageArea ); }
190 protected JScrollPane getScrollArea() { return( _scrollArea ); }
191 protected MessagePoster getPoster() { return( _poster ); }
192 protected ConnectDialog getConnectDialog() { return( _connectDialog ); }
193
194 // ----------------------------------------------
195 // RUNTIME
196 // ----------------------------------------------
197
198 public static void main( String[] args ) {
199 GUI app = new GUI();
200 app.pack();
201 app.show();
202 }
203 }
204
205 class MessagePoster extends Thread {
206 private JTextArea _area;
207 private JScrollPane _scroller;
208 private ChatClient _client;
209 public MessagePoster( JTextArea area,
210 JScrollPane scroller,
211 ChatClient client ) {
212 _area = area;
213 _scroller = scroller;
214 _client = client;
215 }
216 private boolean _stopRequested = false;
217 public void run() {
218 Object waiter = new Object();
219 while( ! _stopRequested ) {
220 try{synchronized(waiter){waiter.wait(1000);}}catch(Exception e){}
221 Object[] messages = _client.getMessages();
222 for( int i = 0; i < messages.length; i++ ) {
223 String message = (String)messages[i];
224 if( message != null && ! message.equals( "" ) ) {
225 _area.append( message + "\n" );
226 scrollToBottom();
227 }
228 }
229 }
230 }
231
232 protected void scrollToBottom() {
233 JViewport currentV = _scroller.getViewport();
234 Dimension viewSize = currentV.getExtentSize();
235 Dimension panelSize = _area.getSize();
236 int newVX = 0;
237 int newVY = (int)( panelSize.getHeight() - viewSize.getHeight() );
238 Point newPosition = new Point( newVX, newVY );
239 currentV.setViewPosition( newPosition );
240 }
241
242 public void pleaseStop() {
243 _stopRequested = true;
244 }
245 }
246