[kune-commits] r1631 - trunk/src/main/java/cc/kune/chat/client/actions

Vicente J. Ruiz Jurado vjrj_ at ourproject.org
Tue Nov 29 02:15:21 CET 2011


Author: vjrj_
Date: 2011-11-29 02:15:20 +0100 (Tue, 29 Nov 2011)
New Revision: 1631

Modified:
   trunk/src/main/java/cc/kune/chat/client/actions/ChatSitebarActions.java
Log:
CLOSED - # 172: Remove "available for chat" status 
http://kune.ourproject.org/issues/ticket/172

Modified: trunk/src/main/java/cc/kune/chat/client/actions/ChatSitebarActions.java
===================================================================
--- trunk/src/main/java/cc/kune/chat/client/actions/ChatSitebarActions.java	2011-11-28 16:19:48 UTC (rev 1630)
+++ trunk/src/main/java/cc/kune/chat/client/actions/ChatSitebarActions.java	2011-11-29 01:15:20 UTC (rev 1631)
@@ -19,15 +19,21 @@
  */
 package cc.kune.chat.client.actions;
 
+import org.waveprotocol.box.webclient.client.ClientEvents;
+import org.waveprotocol.box.webclient.client.events.NetworkStatusEvent;
+import org.waveprotocol.box.webclient.client.events.NetworkStatusEventHandler;
+
 import cc.kune.chat.client.ChatClient;
 import cc.kune.chat.client.ChatInstances;
 import cc.kune.chat.client.resources.ChatResources;
 import cc.kune.common.client.actions.AbstractAction;
 import cc.kune.common.client.actions.AbstractExtendedAction;
 import cc.kune.common.client.actions.ActionEvent;
+import cc.kune.common.client.actions.ui.descrip.MenuItemDescriptor;
 import cc.kune.common.client.actions.ui.descrip.MenuRadioItemDescriptor;
 import cc.kune.common.client.actions.ui.descrip.MenuSeparatorDescriptor;
 import cc.kune.common.client.actions.ui.descrip.MenuTitleItemDescriptor;
+import cc.kune.core.client.events.WindowFocusEvent;
 import cc.kune.core.client.sitebar.SiteUserOptions;
 import cc.kune.core.client.sitebar.SiteUserOptionsPresenter;
 import cc.kune.core.client.state.Session;
@@ -41,7 +47,9 @@
 import com.calclab.emite.im.client.presence.PresenceManager;
 import com.calclab.emite.im.client.presence.events.OwnPresenceChangedEvent;
 import com.calclab.emite.im.client.presence.events.OwnPresenceChangedHandler;
+import com.google.gwt.event.shared.EventBus;
 import com.google.gwt.resources.client.ImageResource;
+import com.google.gwt.user.client.Timer;
 import com.google.inject.Inject;
 
 public class ChatSitebarActions {
@@ -63,39 +71,32 @@
 
     @Override
     public void actionPerformed(final ActionEvent event) {
-      // presenceManager.changeOwnPresence(Presence.build(NO_STATUS,
-      // Show.xa));
       session.logout();
     }
   }
   public class ChangeOnlineStatusAction extends AbstractExtendedAction {
+    private final ImageResource icon;
     private final PresenceManager manager;
     private final Presence thisPresence;
 
     public ChangeOnlineStatusAction(final PresenceManager presenceManager, final String statusText,
         final Show show, final ImageResource icon) {
       this.manager = presenceManager;
+      this.icon = icon;
       thisPresence = Presence.build(statusText, show);
-      presenceManager.addOwnPresenceChangedHandler(new OwnPresenceChangedHandler() {
-
-        @Override
-        public void onOwnPresenceChanged(final OwnPresenceChangedEvent event) {
-          updateStatusIcon(icon, event.getCurrentPresence());
-        }
-
-      });
-      updateStatusIcon(icon, presenceManager.getOwnPresence());
+      updateStatusIcon(presenceManager.getOwnPresence());
     }
 
     @Override
     public void actionPerformed(final ActionEvent event) {
       if (!chatClient.loginIfNecessary()) {
+        awayTimer.cancel();
         manager.changeOwnPresence(thisPresence);
       }
       nextPresence = thisPresence;
     }
 
-    private void updateStatusIcon(final ImageResource icon, final Presence currentPresence) {
+    public void updateStatusIcon(final Presence currentPresence) {
       if (thisPresence.getShow().equals(currentPresence.getShow())
           && ((currentPresence.getStatus() == null) || currentPresence.getStatus().equals(
               thisPresence.getStatus()))) {
@@ -104,12 +105,17 @@
     }
   }
 
-  private static final String CHAT_STATUS = "k-chat-status-group";
   private static final String GROUP_CHAT_STATUS = "k-group-chat-status";
   private static final String NO_STATUS = null;
+  private MenuRadioItemDescriptor awayItem;
+  private Timer awayTimer;
+  private MenuRadioItemDescriptor busyItem;
   private final ChatClient chatClient;
+  private final EventBus eventBus;
   private final I18nTranslationService i18n;
   private Presence nextPresence = Presence.build(NO_STATUS, Show.notSpecified);
+  private MenuRadioItemDescriptor offlineItem;
+  private MenuRadioItemDescriptor onlineItem;
   private final PresenceManager presenceManager;
   private final ChatResources res;
   private final SiteUserOptions userOptions;
@@ -118,14 +124,16 @@
   @Inject
   public ChatSitebarActions(final Session session, final ChatClient chatClient,
       final SiteUserOptions userOptions, final I18nTranslationService i18n, final ChatResources res,
-      final ChatInstances chatInstances) {
+      final ChatInstances chatInstances, final EventBus eventBus) {
     this.chatClient = chatClient;
     this.userOptions = userOptions;
     this.i18n = i18n;
     this.res = res;
+    this.eventBus = eventBus;
     xmppSession = chatInstances.xmppSession;
     presenceManager = chatInstances.presenceManager;
     createActions();
+    createListener();
   }
 
   private void createActions() {
@@ -133,54 +141,120 @@
         SiteUserOptionsPresenter.LOGGED_USER_MENU, i18n.t("Set your chat status"));
     userOptions.addAction(new MenuSeparatorDescriptor(SiteUserOptionsPresenter.LOGGED_USER_MENU));
     userOptions.addAction(chatActionsTitle);
-    final MenuRadioItemDescriptor onlineItem = createChatStatusAction(res.online(), i18n.t("Available"),
+    onlineItem = createChatStatusAction(res.online(), i18n.t("Available"),
         onlineAction(NO_STATUS, Show.notSpecified, res.online()));
-    final MenuRadioItemDescriptor availableItem = createChatStatusAction(res.xa(),
-        i18n.t("Available for chat"), onlineAction(NO_STATUS, Show.chat, res.xa()));
-    final MenuRadioItemDescriptor awayItem = createChatStatusAction(res.away(), i18n.t("Away"),
+    awayItem = createChatStatusAction(res.away(), i18n.t("Away"),
         onlineAction(NO_STATUS, Show.away, res.away()));
-    final MenuRadioItemDescriptor busyItem = createChatStatusAction(res.busy(), i18n.t("Busy"),
+    busyItem = createChatStatusAction(res.busy(), i18n.t("Busy"),
         onlineAction(NO_STATUS, Show.dnd, res.busy()));
-    final MenuRadioItemDescriptor offlineItem = createChatStatusAction(res.offline(),
-        i18n.t("Sign out of chat"), new ChangeOfflineStatusAction(xmppSession, res.offline()));
-    xmppSession.addSessionStateChangedHandler(false, new StateChangedHandler() {
+    offlineItem = createChatStatusAction(res.offline(), i18n.t("Sign out of chat"),
+        new ChangeOfflineStatusAction(xmppSession, res.offline()));
+  }
 
+  private MenuRadioItemDescriptor createChatStatusAction(final ImageResource icon, final String text,
+      final AbstractAction action) {
+    final MenuRadioItemDescriptor item = new MenuRadioItemDescriptor(
+        SiteUserOptionsPresenter.LOGGED_USER_MENU, action, GROUP_CHAT_STATUS);
+    item.putValue(AbstractAction.NAME, text);
+    item.putValue(AbstractAction.SMALL_ICON, icon);
+    return item;
+  }
+
+  private void createListener() {
+    awayTimer = new Timer() {
       @Override
+      public void run() {
+        final Presence awayPresence = Presence.build(NO_STATUS, Show.away);
+        if (chatClient.isLoggedIn()) {
+          presenceManager.changeOwnPresence(awayPresence);
+          updateMenuPresence(awayPresence);
+        }
+      }
+    };
+    xmppSession.addSessionStateChangedHandler(false, new StateChangedHandler() {
+      @Override
       public void onStateChanged(final StateChangedEvent event) {
         if (xmppSession.isReady()) {
+          awayTimer.cancel();
           presenceManager.changeOwnPresence(nextPresence);
-          switch (nextPresence.getShow()) {
-          case notSpecified:
-            onlineItem.setChecked(true);
-            break;
-          case dnd:
-            busyItem.setChecked(true);
-            break;
-          case chat:
-            availableItem.setChecked(true);
-            break;
-          case away:
-            awayItem.setChecked(true);
-            break;
-          }
+          updateMenuPresence(nextPresence);
         } else {
           offlineItem.setChecked(true);
         }
       }
     });
+    presenceManager.addOwnPresenceChangedHandler(new OwnPresenceChangedHandler() {
+      @Override
+      public void onOwnPresenceChanged(final OwnPresenceChangedEvent event) {
+        updateMenuPresence(event.getCurrentPresence());
+      }
+    });
+    eventBus.addHandler(WindowFocusEvent.getType(), new WindowFocusEvent.WindowFocusHandler() {
+      @Override
+      public void onWindowFocus(final WindowFocusEvent event) {
+        if (event.isHasFocus()) {
+          awayTimer.cancel();
+          if (chatClient.isLoggedIn()) {
+            new Timer() {
+              @Override
+              public void run() {
+                presenceManager.changeOwnPresence(nextPresence);
+              }
+            }.schedule(1000);
+          }
+        } else {
+          awayTimer.schedule(3000);
+        }
+      }
+    });
+    ClientEvents.get().addNetworkStatusEventHandler(new NetworkStatusEventHandler() {
+      @Override
+      public void onNetworkStatus(final NetworkStatusEvent event) {
+        switch (event.getStatus()) {
+        case CONNECTED:
+        case RECONNECTED:
+          new Timer() {
+            @Override
+            public void run() {
+              ChatSitebarActions.this.chatClient.loginIfNecessary();
+            }
+          }.schedule(2000);
+          break;
+        case DISCONNECTED:
+        case RECONNECTING:
+          break;
+        }
+      }
+    });
   }
 
-  private MenuRadioItemDescriptor createChatStatusAction(final ImageResource icon, final String text,
-      final AbstractAction action) {
-    final MenuRadioItemDescriptor item = new MenuRadioItemDescriptor(
-        SiteUserOptionsPresenter.LOGGED_USER_MENU, action, GROUP_CHAT_STATUS);
-    item.putValue(AbstractAction.NAME, text);
-    item.putValue(AbstractAction.SMALL_ICON, icon);
-    return item;
-  }
-
   private AbstractExtendedAction onlineAction(final String statusText, final Show show,
       final ImageResource icon) {
     return new ChangeOnlineStatusAction(presenceManager, statusText, show, icon);
   }
+
+  private void updateMenuPresence(final Presence presence) {
+    switch (presence.getShow()) {
+    case notSpecified:
+      onlineItem.setChecked(true);
+      updateSitebarIconPresence(presence, onlineItem);
+      break;
+    case dnd:
+      busyItem.setChecked(true);
+      updateSitebarIconPresence(presence, busyItem);
+      break;
+    case chat:
+      onlineItem.setChecked(true);
+      updateSitebarIconPresence(presence, onlineItem);
+      break;
+    case away:
+      awayItem.setChecked(true);
+      updateSitebarIconPresence(presence, awayItem);
+      break;
+    }
+  }
+
+  private void updateSitebarIconPresence(final Presence presence, final MenuItemDescriptor itemDescriptor) {
+    ((ChatSitebarActions.ChangeOnlineStatusAction) itemDescriptor.getAction()).updateStatusIcon(presence);
+  }
 }




More information about the kune-commits mailing list