001         package com.croftsoft.apps.chat.view;
002    
003         import java.awt.*;
004         import java.awt.event.*;
005         import java.io.*;
006         import java.util.*;
007         import javax.swing.*;
008    
009         import com.croftsoft.core.animation.AnimatedComponent;
010         import com.croftsoft.core.animation.ComponentAnimator;
011         import com.croftsoft.core.gui.LogPanel;
012         import com.croftsoft.core.lang.NullArgumentException;
013         import com.croftsoft.core.lang.lifecycle.Lifecycle;
014         import com.croftsoft.core.util.consumer.NullConsumer;
015         import com.croftsoft.core.util.queue.Queue;
016    
017         import com.croftsoft.apps.chat.ChatConstants;
018         import com.croftsoft.apps.chat.controller.ChatController;
019         import com.croftsoft.apps.chat.event.TalkEvent;
020         import com.croftsoft.apps.chat.model.ChatGame;
021         import com.croftsoft.apps.chat.model.ChatWorld;
022         import com.croftsoft.apps.chat.model.seri.SeriChatGame;
023    
024         /*********************************************************************
025         * Chat client user interface.
026         *
027         * @version
028         *   2003-06-25
029         * @since
030         *   2000-04-20
031         * @author
032         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
033         *********************************************************************/
034    
035         public final class  ChatPanel
036           extends JPanel
037           implements ComponentAnimator, Lifecycle
038         //////////////////////////////////////////////////////////////////////
039         //////////////////////////////////////////////////////////////////////
040         {
041    
042         private static final boolean  DEBUG = true;
043    
044         private static final Font     FONT
045           = new Font ( "Arioso", Font.BOLD, 12 );
046    
047         private static final Color    BACKGROUND_COLOR = Color.BLACK;
048    
049         private static final Color    FOREGROUND_COLOR = Color.GREEN;
050    
051         private static final int      TEXT_LENGTH_MAX  = 10000;
052    
053         private static final int      DIVIDER_LOCATION = 300;
054    
055         //
056    
057         private final Queue              eventQueue;
058    
059         private final ChatController     chatController;
060    
061         private final ChatGame           chatGame;
062    
063         private final ChatSynchronizer   chatSynchronizer;
064    
065         private final JMenuBar           menuBar;
066    
067         private final ChatGameAnimator   chatGameAnimator;
068    
069         private final AnimatedComponent  animatedComponent;
070    
071         private final LogPanel           logPanel;
072    
073         private final JTextField         textField;
074    
075         private final JMenu              avatarMenu;
076    
077         private final JCheckBoxMenuItem  musicCheckBoxMenuItem;
078    
079         //////////////////////////////////////////////////////////////////////
080         //////////////////////////////////////////////////////////////////////
081    
082         public ChatPanel (
083           Queue           eventQueue,
084           ChatController  chatController )
085         //////////////////////////////////////////////////////////////////////
086         {
087           super ( new BorderLayout ( ) );
088    
089           NullArgumentException.check ( this.eventQueue = eventQueue );
090    
091           NullArgumentException.check (
092             this.chatController = chatController );
093    
094           chatGame = new SeriChatGame ( );
095    
096           chatSynchronizer = new ChatSynchronizer (
097             chatGame.getChatWorld ( ),
098             NullConsumer.INSTANCE );
099    
100           avatarMenu = new JMenu ( "Avatar" );
101    
102           musicCheckBoxMenuItem = new JCheckBoxMenuItem ( "Music?", true );
103    
104           menuBar = createMenuBar ( );
105    
106           animatedComponent = new AnimatedComponent ( this );
107    
108           animatedComponent.setBackground ( BACKGROUND_COLOR );
109    
110           animatedComponent.setForeground ( FOREGROUND_COLOR );
111    
112           animatedComponent.setFont       ( FONT             );
113    
114           chatGameAnimator = new ChatGameAnimator (
115             chatGame,
116             animatedComponent,
117             getClass ( ).getClassLoader ( ),
118             ChatConstants.MEDIA_DIR );
119    
120           logPanel = new LogPanel (
121             TEXT_LENGTH_MAX,
122             BACKGROUND_COLOR,        
123             FONT );
124    
125           textField = new JTextField ( );
126    
127           JPanel  southPanel = new JPanel ( new BorderLayout ( ) );
128    
129           southPanel.add (
130             new JScrollPane ( logPanel ), BorderLayout.CENTER );
131    
132           southPanel.add ( textField, BorderLayout.SOUTH );
133    
134           JSplitPane  splitPane = new JSplitPane (
135             JSplitPane.VERTICAL_SPLIT,
136             animatedComponent,
137             southPanel );
138    
139           splitPane.setOneTouchExpandable ( true );
140    
141           splitPane.setDividerLocation ( DIVIDER_LOCATION );
142    
143           add ( splitPane, BorderLayout.CENTER );
144    
145           // viewCanvas.load_graphics ( );
146         }
147    
148         //////////////////////////////////////////////////////////////////////
149         // interface lifecycle methods
150         //////////////////////////////////////////////////////////////////////
151    
152         public void  init ( )
153         //////////////////////////////////////////////////////////////////////
154         {
155           animatedComponent.init ( );
156    
157           animatedComponent.addComponentListener (
158             new ComponentAdapter ( )
159             {
160               public void  componentResized (
161                 ComponentEvent  componentEvent )
162               {
163                 animatedComponent.repaint ( );
164               }
165             } );
166    
167           chatController.setAvatarMenu ( avatarMenu );
168    
169           chatController.setMoveComponent ( animatedComponent );
170    
171           chatController.setMusicCheckBoxMenuItem ( musicCheckBoxMenuItem );
172    
173           chatController.setTalkTextField ( textField );
174         }
175    
176         public void  start ( )
177         //////////////////////////////////////////////////////////////////////
178         {
179           animatedComponent.start ( );
180         }
181    
182         public void  stop ( )
183         //////////////////////////////////////////////////////////////////////
184         {
185           animatedComponent.stop ( );
186         }
187    
188         public void  destroy ( )
189         //////////////////////////////////////////////////////////////////////
190         {
191           animatedComponent.destroy ( );
192         }
193    
194         //////////////////////////////////////////////////////////////////////
195         // accessor methods
196         //////////////////////////////////////////////////////////////////////
197    
198         public JMenuBar  getMenuBar ( ) { return menuBar; }
199    
200         //////////////////////////////////////////////////////////////////////
201         //////////////////////////////////////////////////////////////////////
202    
203         public void  update ( JComponent  component )
204         //////////////////////////////////////////////////////////////////////
205         {
206           chatGame.update ( );
207    
208           Object  o;
209    
210           while ( ( o = eventQueue.poll ( ) ) != null )
211           {
212             if ( o instanceof TalkEvent )
213             {
214               logPanel.record ( ( ( TalkEvent ) o ).getText ( ) );
215             }
216             else
217             {
218               chatSynchronizer.consume ( o );
219             }
220           }
221    
222           chatGameAnimator.update ( component );
223         }
224    
225         public void  paint (
226           JComponent  component,
227           Graphics2D  graphics )
228         //////////////////////////////////////////////////////////////////////
229         {
230           chatGameAnimator.paint ( component, graphics );
231         }
232    
233         //////////////////////////////////////////////////////////////////////
234         // private methods
235         //////////////////////////////////////////////////////////////////////
236    
237         private JMenuBar  createMenuBar ( )
238         //////////////////////////////////////////////////////////////////////
239         {
240           JMenuBar  menuBar = new JMenuBar ( );
241    
242           //
243    
244           avatarMenu.setMnemonic ( KeyEvent.VK_A );
245    
246           menuBar.add ( avatarMenu );
247    
248           //
249    
250           ButtonGroup  buttonGroup = new ButtonGroup ( );
251    
252           for ( int  i = 0; i < ChatConstants.AVATAR_TYPES.length; i++ )
253           {
254             JRadioButtonMenuItem  radioButtonMenuItem
255               = new JRadioButtonMenuItem (
256               ChatConstants.AVATAR_TYPES [ i ],
257               i == ChatConstants.DEFAULT_AVATAR_TYPE_INDEX );
258    
259             buttonGroup.add ( radioButtonMenuItem );
260    
261             avatarMenu.add ( radioButtonMenuItem );
262           }
263    
264           //
265    
266           JMenu  optionsMenu = new JMenu ( "Options" );
267    
268           optionsMenu.setMnemonic ( KeyEvent.VK_O );
269    
270           menuBar.add ( optionsMenu );
271    
272           //
273    
274           optionsMenu.add ( musicCheckBoxMenuItem );
275    
276           return menuBar;
277         }
278    
279         //////////////////////////////////////////////////////////////////////
280         //////////////////////////////////////////////////////////////////////
281         }