OSDN Git Service

自分宛のメッセージ,DMが到着した際に,タスクトレイの通知バーでメッセージ到着をお知らせする機能を追加
authorspark_xp <spark_xp@d8c9ecd3-d47d-4367-8645-de82c00e513f>
Sat, 13 Nov 2010 09:25:37 +0000 (09:25 +0000)
committerspark_xp <spark_xp@d8c9ecd3-d47d-4367-8645-de82c00e513f>
Sat, 13 Nov 2010 09:25:37 +0000 (09:25 +0000)
git-svn-id: http://svn.sourceforge.jp/svnroot/nt-manager/NishioTweetManager/trunk@110 d8c9ecd3-d47d-4367-8645-de82c00e513f

src/twitter/action/TweetDirectMessageGetter.java
src/twitter/action/TweetMentionGetter.java
src/twitter/gui/action/TweetMainAction.java
src/twitter/gui/form/NishioTweetManager.form
src/twitter/gui/form/NishioTweetManager.java
src/twitter/manage/TweetNotifyManager.java [new file with mode: 0644]

index 5b477ed..5bcd8e6 100644 (file)
@@ -5,10 +5,12 @@
 
 package twitter.action;
 
+import java.awt.TrayIcon;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import twitter.manage.TweetManager;
+import twitter.manage.TweetNotifyManager;
 import twitter4j.Status;
 import twitter4j.TwitterException;
 
@@ -20,6 +22,10 @@ public class TweetDirectMessageGetter implements TweetGetter{
 
     //tweet管理用
     private TweetManager tweetManager;
+    //通知
+    private TweetNotifyManager notifyManager = null;
+    //1回目の最初の呼び出しかどうか, 1回目の呼び出しの際は通知バーにメッセージを表示しない
+    private boolean isFirstTime = true;
 
     /**
      *
@@ -30,6 +36,16 @@ public class TweetDirectMessageGetter implements TweetGetter{
     }
 
     /**
+     * 
+     * @param tweetManager
+     * @param trayIcon
+     */
+    public TweetDirectMessageGetter(TweetManager tweetManager, TrayIcon trayIcon) {
+        this.tweetManager = tweetManager;
+        this.notifyManager = new TweetNotifyManager(trayIcon, "あなた宛のダイレクトメッセージ");
+    }
+
+    /**
      * DirectMessageツイートを指定した数だけ取得
      * @param num
      * @return
@@ -39,6 +55,10 @@ public class TweetDirectMessageGetter implements TweetGetter{
         List<Status> status = null;
         try {
             status = tweetManager.getDirectMessages(num);
+            if( notifyManager != null && isFirstTime == false) {
+                this.notifyManager.showNotifyMessage(status);
+            }
+            isFirstTime = false;
         } catch (TwitterException ex) {
             Logger.getLogger(TweetMentionGetter.class.getName()).log(Level.SEVERE, null, ex);
         }
@@ -55,6 +75,10 @@ public class TweetDirectMessageGetter implements TweetGetter{
         List<Status> status = null;
         try {
             status = tweetManager.getNewDirectMessages();
+            if( notifyManager != null && isFirstTime == false) {
+                this.notifyManager.showNotifyMessage(status);
+            }
+            isFirstTime = false;
         } catch (TwitterException ex) {
             Logger.getLogger(TweetMentionGetter.class.getName()).log(Level.SEVERE, null, ex);
         }
index 12db606..2e6a05f 100644 (file)
@@ -5,10 +5,12 @@
 
 package twitter.action;
 
+import java.awt.TrayIcon;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import twitter.manage.TweetManager;
+import twitter.manage.TweetNotifyManager;
 import twitter4j.Status;
 import twitter4j.TwitterException;
 
@@ -20,6 +22,10 @@ public class TweetMentionGetter implements TweetGetter{
 
     //tweet管理用
     private TweetManager tweetManager;
+    //通知
+    private TweetNotifyManager notifyManager = null;
+    //1回目の最初の呼び出しかどうか, 1回目の呼び出しの際は通知バーにメッセージを表示しない
+    private boolean isFirstTime = true;
 
     /**
      *
@@ -30,6 +36,16 @@ public class TweetMentionGetter implements TweetGetter{
     }
 
     /**
+     *
+     * @param tweetManager
+     * @param trayIcon タスクバーの通知用アイコン
+     */
+    public TweetMentionGetter(TweetManager tweetManager, TrayIcon trayIcon) {
+        this.tweetManager = tweetManager;
+        this.notifyManager = new TweetNotifyManager(trayIcon);
+    }
+
+    /**
      * Mentionツイートを指定した数だけ取得
      * @param num
      * @return
@@ -39,6 +55,10 @@ public class TweetMentionGetter implements TweetGetter{
         List<Status> status = null;
         try {
             status = tweetManager.getMentions(num);
+            if( notifyManager != null && isFirstTime == false) {
+                this.notifyManager.showNotifyMessage(status);
+            }
+            isFirstTime = false;
         } catch (TwitterException ex) {
             Logger.getLogger(TweetMentionGetter.class.getName()).log(Level.SEVERE, null, ex);
         }
@@ -54,6 +74,10 @@ public class TweetMentionGetter implements TweetGetter{
         List<Status> status = null;
         try {
             status = tweetManager.getNewMentionData();
+            if( notifyManager != null && isFirstTime == false) {
+                this.notifyManager.showNotifyMessage(status);
+            }
+            isFirstTime = false;
         } catch (TwitterException ex) {
             Logger.getLogger(TweetMentionGetter.class.getName()).log(Level.SEVERE, null, ex);
         }
index f9d4eb5..66aab60 100644 (file)
@@ -5,6 +5,7 @@ import java.awt.Desktop;
 import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Point;
+import java.awt.TrayIcon;
 import java.awt.event.ActionEvent;
 import java.io.File;
 import java.io.FileInputStream;
@@ -167,6 +168,9 @@ public class TweetMainAction {
     private JLabel tweetMessageCountLabel = null;
     private int uncheckedDirectMessageCount = 0;
     private int uncheckedMentionTweetCount = 0;
+    //自分宛のメッセージを通知バーに表示するか
+    private boolean isNotifyMentionMessage = true;
+    private boolean isNotifyDirectMessage = true;
 
     //Tweetの詳細情報を表示する部分
     private JLabel userImageLabel = null;
@@ -179,6 +183,8 @@ public class TweetMainAction {
     private JLabel updateLabel = null;
     private JEditorPane userIntroBox = null;
     private JEditorPane userWebBox = null;
+    //トレイアイコン
+    private TrayIcon trayIcon = null;
 
     //checkbox関係
     private javax.swing.JToggleButton timelineToggleButton;
@@ -266,7 +272,8 @@ public class TweetMainAction {
             JCheckBoxMenuItem timelineCheckBoxMenuItem,
             JCheckBoxMenuItem mentionCheckBoxMenuItem,
             JCheckBoxMenuItem dmCheckBoxMenuItem,
-            JCheckBoxMenuItem sendCheckBoxMenuItem) {
+            JCheckBoxMenuItem sendCheckBoxMenuItem,
+            TrayIcon trayIcon) {
         this.mainFrame = mainFrame;
         this.tweetManager = tweetManager;
         this.statusBarLabel = statusBarLabel;
@@ -298,6 +305,9 @@ public class TweetMainAction {
         this.sendCheckBoxMenuItem = sendCheckBoxMenuItem;
         this.sendDMToggleButton = sendToggleButton;
 
+        //トレイアイコン
+        this.trayIcon = trayIcon;
+
         //罰ボタンを押した時のイベントを追加
         if( this.tweetMainTab instanceof DnDTabbedPane ) {
             ((DnDTabbedPane)this.tweetMainTab).setMainAction(this);
@@ -454,8 +464,14 @@ public class TweetMainAction {
             //既にIDが存在していたらここで例外発生
             timerID.addID(id);
             //検索結果を表示するタブを生成
-            actionAddTab(id, period, new TweetMentionGetter(tweetManager),
-                    TweetMainAction.TAB_MENTION_STRING);
+            if( this.isNotifyMentionMessage ) {
+                //メッセージが到着したら通知を行う
+                actionAddTab(id, period, new TweetMentionGetter(tweetManager, this.trayIcon),
+                        TweetMainAction.TAB_MENTION_STRING);
+            }else {
+                actionAddTab(id, period, new TweetMentionGetter(tweetManager),
+                        TweetMainAction.TAB_MENTION_STRING);
+            }
         } catch (ExistTimerIDException ex) {
             JOptionPane.showMessageDialog(null, "そのタブは既に存在しています",
                     "Error", JOptionPane.ERROR_MESSAGE);
@@ -517,8 +533,13 @@ public class TweetMainAction {
             //既にIDが存在していたらここで例外発生
             timerID.addID(id);
             //検索結果を表示するタブを生成
-            actionAddTab(id, period, new TweetDirectMessageGetter(tweetManager),
-                    TweetMainAction.TAB_DIRECT_MESSAGE_STRING);
+            if( this.isNotifyDirectMessage == true ) {
+                actionAddTab(id, period, new TweetDirectMessageGetter(tweetManager, this.trayIcon),
+                        TweetMainAction.TAB_DIRECT_MESSAGE_STRING);
+            }else {
+                actionAddTab(id, period, new TweetDirectMessageGetter(tweetManager),
+                        TweetMainAction.TAB_DIRECT_MESSAGE_STRING);
+            }
         } catch (ExistTimerIDException ex) {
             JOptionPane.showMessageDialog(null, "そのタブは既に存在しています",
                     "Error", JOptionPane.ERROR_MESSAGE);
@@ -1579,6 +1600,10 @@ public class TweetMainAction {
         String mfw = this.property.getProperty("mainFrameWidth");
         String mfh = this.property.getProperty("mainFrameHeight");
 
+        //メッセージ通知を行うか
+        String nm = this.property.getProperty("notifyMessage");
+        String ndm = this.property.getProperty("notifyDirectMessage");
+
         try {
             this.newTableColor = new Color(Integer.parseInt(ntrgb));
             this.tlFontSize = Integer.parseInt(tfs);
@@ -1592,6 +1617,10 @@ public class TweetMainAction {
             this.getMentionPeriod = Integer.parseInt(gmp);
             this.getDirectMessagePeriod = Integer.parseInt(gdmp);
             this.getSendDirectMessagePeriod = Integer.parseInt(gsdmp);
+
+            //通知関係
+            this.isNotifyMentionMessage = Boolean.parseBoolean(nm);
+            this.isNotifyMentionMessage = Boolean.parseBoolean(ndm);
         } catch (NumberFormatException e) {
             e.printStackTrace();
         }
@@ -1638,6 +1667,11 @@ public class TweetMainAction {
         }
         this.property.setProperty("mainFrameWidth", this.mainFrameWidth + "");
         this.property.setProperty("mainFrameHeight", this.mainFrameHeight + "");
+
+        //メッセージ通知を行うか
+        this.property.setProperty("notifyMention", this.isNotifyMentionMessage + "");
+        this.property.setProperty("notifyDirectMessage", this.isNotifyDirectMessage + "");
+
         // プロパティのリストを保存
         property.store(new FileOutputStream("./" + PROPERTIES_DIRECTORY + "/"
                 + BASIC_SETTING_FILENAME), null);
index 6015907..c78712a 100644 (file)
               </Group>
               <Group type="102" alignment="0" attributes="0">
                   <Component id="jScrollPane9" min="-2" pref="53" max="-2" attributes="0"/>
-                  <EmptySpace max="32767" attributes="0"/>
+                  <EmptySpace pref="29" max="32767" attributes="0"/>
               </Group>
           </Group>
         </DimensionLayout>
index a7d5544..9fdb380 100644 (file)
@@ -1325,7 +1325,7 @@ public class NishioTweetManager extends javax.swing.JFrame {
                 userNameLabel, updateTimeLabel, followerLabel, followingLabel, locationLabel,
                 clientNameLabel, updateLabel, userIntroBox, userWebBox, 
                 jToggleButton3, jToggleButton4, jToggleButton5, jToggleButton6,
-                jCheckBoxMenuItem3,jCheckBoxMenuItem4, jCheckBoxMenuItem5, jCheckBoxMenuItem6);
+                jCheckBoxMenuItem3,jCheckBoxMenuItem4, jCheckBoxMenuItem5, jCheckBoxMenuItem6, trayIcon);
         //ハッシュタグ用のハイパーリンクリスナーにアクション登録
         this.hashTagHyperlinkListener.setMainAction(mainAction);
         //もしログインに失敗したら,アカウント設定画面を出す
diff --git a/src/twitter/manage/TweetNotifyManager.java b/src/twitter/manage/TweetNotifyManager.java
new file mode 100644 (file)
index 0000000..af09959
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package twitter.manage;
+
+import java.awt.SystemTray;
+import java.awt.TrayIcon;
+import java.util.List;
+import twitter4j.Status;
+
+/**
+ * 通知すべきメッセージの管理を行う
+ * @author nishio
+ */
+public class TweetNotifyManager {
+
+    //トレイアイコン
+    private TrayIcon trayIcon = null;
+    //タイトル
+    private String title = "あなた宛のメッセージ";
+
+    /**
+     * 
+     * @param trayIcon
+     */
+    public TweetNotifyManager(TrayIcon trayIcon) {
+        this.trayIcon = trayIcon;
+    }
+
+    public TweetNotifyManager(TrayIcon trayIcon, String title) {
+        this.trayIcon = trayIcon;
+        this.title = title;
+    }
+
+    /**
+     * ツイート情報を通知バーに表示
+     * @param status
+     */
+    public void showNotifyMessage(List<Status> status) {
+        for(Status s : status) {
+            if( s.getUser() == null ) {
+                String message = s.getText();
+                this.trayIcon.displayMessage(title, message,TrayIcon.MessageType.INFO);
+            }else {
+                String name = s.getUser().getScreenName();
+                String message = s.getText();
+                this.trayIcon.displayMessage(title, name + "さんの発言: " + message, TrayIcon.MessageType.INFO);
+            }
+        }
+    }
+
+}