1 29 30 package echo2example.chatclient; 31 32 import java.io.IOException ; 33 34 import nextapp.echo2.app.ApplicationInstance; 35 import nextapp.echo2.app.TaskQueueHandle; 36 import nextapp.echo2.app.Window; 37 import nextapp.echo2.app.event.ActionEvent; 38 import nextapp.echo2.app.event.ActionListener; 39 import nextapp.echo2.webcontainer.ContainerContext; 40 41 44 public class ChatApp extends ApplicationInstance { 45 46 51 public static ChatApp getApp() { 52 return (ChatApp) getActive(); 53 } 54 55 private ChatSession chatSession; 56 private TaskQueueHandle incomingMessageQueue; 57 58 private static final int POST_INTERVAL_AUTO_LOGOUT_WARNING = 2 * 60 * 1000; private static final int POST_INTERVAL_AUTO_LOGOUT = 3 * 60 * 1000; 62 private long lastActionTime; 63 private long lastPostTime; 64 private int pollingInterval = 1000; 65 66 private MessageDialog logoutWarningDialog; 67 68 75 private int calculatePollingInterval() { 76 long delta = System.currentTimeMillis() - lastActionTime; 77 if (delta < 10 * 1000) { 78 return 1000; 80 } else if (delta < 20 * 1000) { 81 return 2000; 83 } else if (delta < 30 * 1000) { 84 return 3000; 86 } else if (delta < 60 * 1000) { 87 return 5000; 89 } else { 90 return 10000; 92 } 93 } 94 95 104 public boolean connect(String userName) { 105 try { 106 chatSession = ChatSession.forUserName(userName); 107 } catch (IOException ex) { 108 throw new RuntimeException (ex); 109 } 110 if (chatSession == null) { 111 return false; 112 } else { 113 if (incomingMessageQueue != null) { 114 throw new IllegalStateException (); 115 } 116 incomingMessageQueue = createTaskQueue(); 117 updatePollingInterval(true); 118 lastPostTime = System.currentTimeMillis(); 119 120 getDefaultWindow().setContent(new ChatScreen()); 121 return true; 122 } 123 } 124 125 129 public void disconnect() { 130 try { 131 chatSession.dispose(); 132 chatSession = null; 133 if (incomingMessageQueue != null) { 134 removeTaskQueue(incomingMessageQueue); 135 incomingMessageQueue = null; 136 } 137 logoutWarningDialog = null; 138 getDefaultWindow().setContent(new LoginScreen()); 139 } catch (IOException ex) { 140 throw new RuntimeException (ex); 141 } 142 } 143 144 152 public ChatSession.Message[] getNewMessages() { 153 return chatSession.getNewMessages(); 154 } 155 156 161 public String getUserName() { 162 return chatSession == null ? null : chatSession.getUserName(); 163 } 164 165 174 public boolean hasQueuedTasks() { 175 if (pollServer()) { 177 final ChatScreen chatScreen = (ChatScreen) getDefaultWindow().getContent(); 179 enqueueTask(incomingMessageQueue, new Runnable (){ 180 public void run() { 181 chatScreen.updateMessageList(); 182 updatePollingInterval(true); 183 } 184 }); 185 } 186 187 if (pollingInterval != calculatePollingInterval()) { 190 enqueueTask(incomingMessageQueue, new Runnable () { 191 public void run() { 192 updatePollingInterval(false); 193 } 194 }); 195 } 196 197 if (System.currentTimeMillis() - lastPostTime > POST_INTERVAL_AUTO_LOGOUT) { 198 enqueueTask(incomingMessageQueue, new Runnable () { 201 public void run() { 202 disconnect(); 203 } 204 }); 205 } else if (System.currentTimeMillis() - lastPostTime > POST_INTERVAL_AUTO_LOGOUT_WARNING) { 206 enqueueTask(incomingMessageQueue, new Runnable () { 210 public void run() { 211 if (logoutWarningDialog == null) { 212 logoutWarningDialog = new MessageDialog(Messages.getString("AutoLogoutWarningDialog.Title"), 213 Messages.getString("AutoLogoutWarningDialog.Message"), MessageDialog.TYPE_CONFIRM, 214 MessageDialog.CONTROLS_OK); 215 getDefaultWindow().getContent().add(logoutWarningDialog); 216 logoutWarningDialog.addActionListener(new ActionListener() { 217 218 223 public void actionPerformed(ActionEvent e) { 224 lastPostTime = System.currentTimeMillis(); 225 logoutWarningDialog = null; 226 } 227 }); 228 } 229 } 230 }); 231 } 232 233 return super.hasQueuedTasks(); 234 } 235 236 239 public Window init() { 240 setStyleSheet(Styles.DEFAULT_STYLE_SHEET); 241 Window window = new Window(); 242 window.setTitle(Messages.getString("Application.Title.Window")); 243 window.setContent(new LoginScreen()); 244 return window; 245 } 246 247 253 private boolean pollServer() { 254 if (chatSession == null) { 255 return false; 256 } 257 try { 258 chatSession.pollServer(); 259 return chatSession.hasNewMessages(); 260 } catch (IOException ex) { 261 throw new RuntimeException (ex); 262 } 263 } 264 265 270 public void postMessage(String content) { 271 try { 272 chatSession.postMessage(content); 273 updatePollingInterval(true); 274 lastPostTime = System.currentTimeMillis(); 275 } catch (IOException ex) { 276 throw new RuntimeException (ex); 277 } 278 } 279 280 289 private void updatePollingInterval(boolean reset) { 290 if (reset) { 291 lastActionTime = System.currentTimeMillis(); 292 } 293 pollingInterval = calculatePollingInterval(); 294 ContainerContext containerContext = (ContainerContext) getContextProperty( 295 ContainerContext.CONTEXT_PROPERTY_NAME); 296 containerContext.setTaskQueueCallbackInterval(incomingMessageQueue, pollingInterval); 297 } 298 } 299 | Popular Tags |