001 package com.croftsoft.apps.chat; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import java.io.*; 006 import java.net.*; 007 import java.util.*; 008 import javax.swing.*; 009 010 import com.croftsoft.core.gui.LifecycleWindowListener; 011 import com.croftsoft.core.io.SerializableCoder; 012 import com.croftsoft.core.lang.lifecycle.LifecycleLib; 013 import com.croftsoft.core.math.MathConstants; 014 import com.croftsoft.core.net.http.HttpLib; 015 import com.croftsoft.core.net.http.msg.HttpMessageClient; 016 import com.croftsoft.core.security.Authentication; 017 import com.croftsoft.core.util.consumer.Consumer; 018 import com.croftsoft.core.util.queue.Queue; 019 020 import com.croftsoft.apps.chat.client.ChatClient; 021 import com.croftsoft.apps.chat.controller.ChatController; 022 import com.croftsoft.apps.chat.view.ChatPanel; 023 024 /********************************************************************* 025 * Chat applet with 2D sprite avatars. 026 * 027 * @version 028 * 2003-06-17 029 * @since 030 * 2000-04-24 031 * @author 032 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 033 *********************************************************************/ 034 035 public final class ChatApplet 036 extends JApplet 037 ////////////////////////////////////////////////////////////////////// 038 ////////////////////////////////////////////////////////////////////// 039 { 040 041 ////////////////////////////////////////////////////////////////////// 042 // frame constants 043 ////////////////////////////////////////////////////////////////////// 044 045 private static final String FRAME_ICON_FILENAME = null; 046 047 private static final Dimension FRAME_SIZE = null; 048 049 private static final String SHUTDOWN_CONFIRMATION_PROMPT = null; 050 051 private static final boolean USE_FULL_SCREEN_TOGGLER = true; 052 053 ////////////////////////////////////////////////////////////////////// 054 // network constants 055 ////////////////////////////////////////////////////////////////////// 056 057 private static final String SERVLET_PATH = "servlet"; 058 059 ////////////////////////////////////////////////////////////////////// 060 // instance variables 061 ////////////////////////////////////////////////////////////////////// 062 063 private ChatClient chatClient; 064 065 private ChatPanel chatPanel; 066 067 ////////////////////////////////////////////////////////////////////// 068 // static methods 069 ////////////////////////////////////////////////////////////////////// 070 071 public static void main ( String [ ] args ) 072 throws Exception 073 ////////////////////////////////////////////////////////////////////// 074 { 075 LifecycleWindowListener.launchAppletAsDesktopApp ( 076 new ChatApplet ( ), 077 ChatConstants.TITLE, 078 FRAME_ICON_FILENAME, 079 ChatApplet.class.getClassLoader ( ), 080 USE_FULL_SCREEN_TOGGLER, 081 FRAME_SIZE, 082 SHUTDOWN_CONFIRMATION_PROMPT ); 083 } 084 085 ////////////////////////////////////////////////////////////////////// 086 // overridden applet methods 087 ////////////////////////////////////////////////////////////////////// 088 089 public String getAppletInfo ( ) { return ChatConstants.INFO; } 090 091 public void init ( ) 092 ////////////////////////////////////////////////////////////////////// 093 { 094 System.out.println ( getAppletInfo ( ) ); 095 096 // model 097 098 Authentication authentication = getAuthentication ( ); 099 100 chatClient = new ChatClient ( authentication, getServletURL ( ) ); 101 102 // controller 103 104 ChatController chatController = new ChatController ( 105 authentication, chatClient.getRequestQueue ( ) ); 106 107 // view 108 109 chatPanel = new ChatPanel ( 110 chatClient.getEventQueue ( ) , chatController ); 111 112 setJMenuBar ( chatPanel.getMenuBar ( ) ); 113 114 Container contentPane = getContentPane ( ); 115 116 contentPane.setLayout ( new BorderLayout ( ) ); 117 118 contentPane.add ( chatPanel, BorderLayout.CENTER ); 119 120 LifecycleLib.init ( chatPanel ); 121 122 validate ( ); 123 124 LifecycleLib.init ( chatClient ); 125 } 126 127 public void start ( ) 128 ////////////////////////////////////////////////////////////////////// 129 { 130 LifecycleLib.start ( chatPanel ); 131 132 LifecycleLib.start ( chatClient ); 133 } 134 135 public void stop ( ) 136 ////////////////////////////////////////////////////////////////////// 137 { 138 LifecycleLib.stop ( chatClient ); 139 140 LifecycleLib.stop ( chatPanel ); 141 } 142 143 public void destroy ( ) 144 ////////////////////////////////////////////////////////////////////// 145 { 146 LifecycleLib.destroy ( chatClient ); 147 148 LifecycleLib.destroy ( chatPanel ); 149 } 150 151 ////////////////////////////////////////////////////////////////////// 152 // private methods 153 ////////////////////////////////////////////////////////////////////// 154 155 private static Authentication getAuthentication ( ) 156 ////////////////////////////////////////////////////////////////////// 157 { 158 String username = Long.toString ( new Random ( ).nextLong ( ) ); 159 160 return new Authentication ( username, "password" ); 161 } 162 163 private URL getServletURL ( ) 164 ////////////////////////////////////////////////////////////////////// 165 { 166 URL codeBaseURL = null; 167 168 try 169 { 170 codeBaseURL = getCodeBase ( ); 171 } 172 catch ( Exception ex ) 173 { 174 } 175 176 try 177 { 178 if ( codeBaseURL != null ) 179 { 180 return new URL ( codeBaseURL, SERVLET_PATH ); 181 } 182 else 183 { 184 return null; 185 } 186 } 187 catch ( MalformedURLException ex ) 188 { 189 // this should never happen if SERVLET_PATH is good 190 191 ex.printStackTrace ( ); 192 193 return null; 194 } 195 } 196 197 ////////////////////////////////////////////////////////////////////// 198 ////////////////////////////////////////////////////////////////////// 199 }