OSDN Git Service

getFollowingUser関数のpageの値によってindexOutOfBoundsExceptionが発生する問題を解決 refs #23442
[nt-manager/nt-manager.git] / src / twitter / gui / action / TweetMainAction.java
index dad3314..6adcda2 100644 (file)
@@ -18,8 +18,10 @@ import java.net.URL;
 import java.net.URLEncoder;
 import java.text.DateFormat;
 import java.util.ArrayList;
+import java.util.BitSet;
 import java.util.Comparator;
 import java.util.List;
+import java.util.Locale;
 import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
@@ -44,6 +46,9 @@ import javax.swing.text.Style;
 import javax.swing.text.StyleConstants;
 import javax.swing.text.html.HTMLDocument;
 import javax.swing.text.html.StyleSheet;
+
+import org.xml.sax.SAXParseException;
+
 import twitter.action.TweetDirectMessageGetter;
 import twitter.action.TweetFavoriteGetter;
 import twitter.action.TweetGetter;
@@ -69,9 +74,13 @@ import twitter.gui.form.ConfigurationDialog;
 import twitter.gui.form.DirectMessageDialog;
 import twitter.gui.form.HashtagSearchDialog;
 import twitter.gui.form.KeywordSearchDialog;
+import twitter.gui.form.OutputCSVLogDialog;
 import twitter.gui.form.UserListDialog;
+import twitter.gui.form.UserSearchDialog;
+import twitter.log.TwitterLogManager;
 import twitter.manage.TweetConfiguration;
 import twitter.manage.TweetManager;
+import twitter.manage.URLBitlyConverter;
 import twitter.task.ExistTimerIDException;
 import twitter.task.TimerID;
 import twitter.task.TweetTaskException;
@@ -80,6 +89,7 @@ import twitter.task.TweetUpdateTask;
 import twitter.util.HTMLEncode;
 import twitter4j.Status;
 import twitter4j.TwitterException;
+import twitter4j.User;
 
 /**
  * GUIのアクション部分
@@ -173,6 +183,11 @@ public class TweetMainAction {
        // 自分宛のメッセージを通知バーに表示するか
        private boolean isNotifyMentionMessage = true;
        private boolean isNotifyDirectMessage = true;
+       //前回開いていたタブの情報を復活する際に利用する(最初の一回だけ利用)
+       private boolean isTempOpenedTimelineTab = true;
+       private boolean isTempOpenedMentionTab = true;
+       private boolean isTempOpenedDMTab = true;
+       private boolean isTempOpenedSendDMTab = true;
 
        // Tweetの詳細情報を表示する部分
        private JLabel userImageLabel = null;
@@ -211,6 +226,10 @@ public class TweetMainAction {
 
        // リストダイアログ
        private UserListDialog userListDialog = null;
+        //ユーザサーチダイアログ
+        private UserSearchDialog userSearchDialog = null;
+        //CSVログ出力ダイアログ
+        private OutputCSVLogDialog outputCSVLogDialog = null;
 
        // 情報更新間隔[sec]
        private int getTimelinePeriod = 60;
@@ -442,12 +461,16 @@ public class TweetMainAction {
                                table.addTableToTab();
                                // タブリストに追加
                                this.tweetTabbedTableList.add(table);
+                               //設定を保存
+                               this.saveProperties();
                                // searchTable.updateTweetTable();
                                // フォーカスを新しいタブに移す
                                this.actionRequestForusToLastTab();
                        } catch (TweetTaskException ex) {
                                Logger.getLogger(TweetMainAction.class.getName()).log(
                                                Level.SEVERE, null, ex);
+                       } catch (IOException e) {
+                               e.printStackTrace();
                        }
                }
 
@@ -632,21 +655,19 @@ public class TweetMainAction {
         * 
         * @param username
         *            タブのタイトルにつけるユーザ名
-        * @param userID
-        *            ユーザID
         * @param period
         *            更新周期[sec]
         */
-       public void actionAddUserTimelineTab(String username, long userID,
+       public void actionAddUserTimelineTab(String username,
                        int period) {
                TimerID timerID = TimerID.getInstance();
-               String id = TimerID.createUserTimelineID(userID);
+               String id = TimerID.createUserTimelineID(username);
                try {
                        // 既にIDが存在していたらここで例外発生
                        timerID.addID(id);
                        // 検索結果を表示するタブを生成
                        actionAddTab(id, period, new TweetUserTimelineGetter(tweetManager,
-                                       userID), username + "の発言");
+                                       username), username + "の発言");
                } catch (ExistTimerIDException ex) {
                        JOptionPane.showMessageDialog(null, "そのタブは既に存在しています", "Error",
                                        JOptionPane.ERROR_MESSAGE);
@@ -864,6 +885,12 @@ public class TweetMainAction {
 
                        // checkboxの状態更新
                        this.updateCheckboxInformation();
+                       //設定保存
+                       try {
+                               saveProperties();
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
                }
        }
 
@@ -904,6 +931,12 @@ public class TweetMainAction {
 
                        // checkboxの状態更新
                        this.updateCheckboxInformation();
+                       //設定保存
+                       try {
+                               saveProperties();
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
                }
        }
 
@@ -1028,6 +1061,32 @@ public class TweetMainAction {
                Matcher matcher = convURLLinkPtn.matcher(message);
                return matcher.replaceAll("<a href=\"$0\">$0</a>");
        }
+       
+       /**
+        * メッセージ内にあるURLをBitlyに変換する
+        * @param message
+        * @return
+        */
+       public String actionConvertURLToBitly(String message) {
+               String result = new String( message );
+               //URLをbitlyに変換
+               Matcher matcher = convURLLinkPtn.matcher(message);
+               while( matcher.find() ) {
+                       String source = matcher.group();
+                       String conv = URLBitlyConverter.convertUrlToBitly( source );
+                       result = result.replaceAll( source , conv );
+               }
+               return result;
+       }
+       
+       /**
+        * つぶやきボックス内のURLをbitlyに変換
+        */
+       public void actionConvertTweetBoxURLToBitly() {
+               String message = this.tweetBoxPane.getText();
+               String conv = actionConvertURLToBitly(message);
+               this.tweetBoxPane.setText( conv );
+       }
 
        /**
         * @ユーザ名の部分をa hrefリンクに変換
@@ -1086,9 +1145,8 @@ public class TweetMainAction {
                        status = this.getCurrentStatus();
                }
                String username = status.getUser().getScreenName();
-               long userID = status.getUser().getId();
                // ユーザ
-               actionAddUserTimelineTab(username, userID, this.getGetTimelinePeriod());
+               actionAddUserTimelineTab(username, this.getGetTimelinePeriod());
        }
 
        /**
@@ -1290,6 +1348,24 @@ public class TweetMainAction {
                dialog.setVisible(true);
        }
 
+        /**
+         * 指定したユーザ検索ダイアログを表示
+         */
+        public void actionShowUserSearchDialog() {
+            UserSearchDialog dialog = getUserSearchDialog();
+            dialog.setLocationRelativeTo(null);
+            dialog.setVisible(true);
+        }
+
+        /**
+         * CSVログ出力ダイアログを表示
+         */
+        public void actionShowOutputCSVLogDialog() {
+            OutputCSVLogDialog dialog = getOutputCSVLogDialog();
+            dialog.setLocationRelativeTo(null);
+            dialog.setVisible(true);
+        }
+
        /**
         * ハッシュタグ検索ダイアログを表示
         */
@@ -1321,16 +1397,29 @@ public class TweetMainAction {
         * tweetBoxPaneに書かれた文字をつぶやく
         */
        public void actionTweet() {
-               if (this.replyStatus != null) {
-                       tweetManager.replyTweet(tweetBoxPane.getText(), this.replyStatus
-                                       .getId());
-               } else {
-                       tweetManager.tweet(tweetBoxPane.getText());
+               boolean isTweet = false;
+               try {
+                       if (this.replyStatus != null) {
+                               tweetManager.replyTweet(tweetBoxPane.getText(),
+                                               this.replyStatus.getId());
+                       } else {
+                               tweetManager.tweet(tweetBoxPane.getText());
+                       }
+                       isTweet = true;
+               } catch (Exception e) {
+                       e.printStackTrace();
                }
-               // ツイートした旨を表示
-               this.information("メッセージをつぶやきました. 発言:" + tweetBoxPane.getText());
 
-               tweetBoxPane.setText(""); // テキストをクリア
+               if (isTweet) {
+                       // ツイートした旨を表示
+                       this.information("メッセージをつぶやきました. 発言:" + tweetBoxPane.getText());
+                       tweetBoxPane.setText(""); // テキストをクリア
+               } else {
+                       this.information("つぶやきに失敗しました");
+                       JOptionPane.showMessageDialog(null,
+                                       "つぶやきに失敗しました。文字数がオーバーしているか、ツイッターに接続ができませんでした。",
+                                       "Tweet Error", JOptionPane.ERROR_MESSAGE);
+               }
 
        }
 
@@ -1408,16 +1497,80 @@ public class TweetMainAction {
         */
        public void actionUpdateTweetMessageCount() {
                int len = 140 - (tweetBoxPane.getText().length());
+               boolean over = false;
                if (len < 0) {
-                       len = 0;
+                       // len = 0;
+                       over = true;
+               }
+
+               if (over) {
+                       tweetMessageCountLabel.setForeground(Color.RED);
+                       // オーバーしたことを伝える
+                       tweetMessageCountLabel.setText("Over(" + len + ")");
+               } else {
+                       tweetMessageCountLabel.setForeground(Color.BLACK);
+                       // 残り文字数
+                       tweetMessageCountLabel.setText(len + "");
                }
-               tweetMessageCountLabel.setText(len + "");
 
                // 残りつぶやき数140の場合,reply状態も解除する
                if (len == 140) {
                        this.setReplyStatus(null);
                }
        }
+       
+       /**
+        * デバッグ用
+        */
+       public void debug() {
+/*             String message = "こんにちは http://densan-labs.net/でした。 http://google.com/ あいうえお";
+               System.out.println( this.actionConvertURLToBitly( message ));*/
+               //this.actionConvertTweetBoxURLToBitly();
+               for(int i=0; i < 5; i++) {
+                       List<User> lists = this.tweetManager.getFollowingUser("nishio_dens", i);
+                       if( lists != null) {
+                               for(User u : lists) {
+                                       System.out.println( u );
+                               }
+                       }
+               }
+//             String url = "http://google.com";
+//             System.out.println( URLBitlyConverter.convertUrlToBitly(url));
+       }
+
+        /**
+         * ログデータを保存
+         * @param logFilePath
+         * @param outputFilePath
+         * @param showUsername
+         * @param showScreenName
+         * @param showText
+         * @param showUpdateTime
+         * @param showClient
+         * @param showUserDescription
+         * @param showFollowing
+         * @param showFollower
+         * @param showUpdateCount
+         * @param showUserURL
+         * @param showProfileImageURL
+         * @throws SAXParseException
+         * @throws IOException
+         */
+        public void outputLogToCSV(String logFilePath, String outputFilePath,
+                boolean showUsername, boolean showScreenName,
+                       boolean showText,
+                       boolean showUpdateTime, boolean showClient,
+                       boolean showUserDescription,
+                       boolean showFollowing, boolean showFollower,
+                       boolean showUpdateCount, boolean showUserURL,
+                       boolean showProfileImageURL) throws SAXParseException, IOException {
+            TwitterLogManager logManager = new TwitterLogManager();
+            List<Status> statuses = logManager.get(logFilePath);
+            logManager.outputCSVLog( outputFilePath, statuses, showUsername, showScreenName,
+                    showText, showUpdateTime, showClient,
+                    showUserDescription, showFollowing, showFollower,
+                    showUpdateCount, showUserURL,showProfileImageURL);
+        }
 
        /**
         * 基本設定用ダイアログを取得
@@ -1480,6 +1633,28 @@ public class TweetMainAction {
                return keywordSearchDialog;
        }
 
+        /**
+         * ユーザ検索ダイアログを表示
+         * @return
+         */
+        public UserSearchDialog getUserSearchDialog() {
+            if( this.userSearchDialog == null ) {
+                this.userSearchDialog = new UserSearchDialog(mainFrame, true, this);
+            }
+            return this.userSearchDialog;
+        }
+
+        /**
+         * CSVログ出力ダイアログを表示
+         * @return
+         */
+        public OutputCSVLogDialog getOutputCSVLogDialog() {
+            if( this.outputCSVLogDialog == null ) {
+                this.outputCSVLogDialog = new OutputCSVLogDialog(mainFrame, true, this);
+            }
+            return this.outputCSVLogDialog;
+        }
+
        /**
         * hashtag検索ダイアログ
         * 
@@ -1547,9 +1722,10 @@ public class TweetMainAction {
                        // 詳細情報にテーブルで選択した人のツイート情報を表示
                        tweetMessageBox.setText(infoMessage);
                        // user icon
-                       //アイコンをキャッシュから取得
-                       ImageIcon icon = TwitterImageCache.getInstance().getProfileImage( st.getUser().getProfileImageURL().toString() );
-                       userImageLabel.setIcon( icon );
+                       // アイコンをキャッシュから取得
+                       ImageIcon icon = TwitterImageCache.getInstance().getProfileImage(
+                                       st.getUser().getProfileImageURL().toString());
+                       userImageLabel.setIcon(icon);
                        // user name and id
                        userNameLabel.setText(st.getUser().getName() + " / "
                                        + st.getUser().getScreenName());
@@ -1619,6 +1795,38 @@ public class TweetMainAction {
        }
 
        /**
+        * 前回タイムラインタブを開いていたか
+        * @return
+        */
+       public boolean isTempOpenedTimelineTab() {
+               return isTempOpenedTimelineTab;
+       }
+
+       /**
+        * 前回メンションタブを開いていたか
+        * @return
+        */
+       public boolean isTempOpenedMentionTab() {
+               return isTempOpenedMentionTab;
+       }
+
+       /**
+        * 前回DMタブを開いていたか
+        * @return
+        */
+       public boolean isTempOpenedDMTab() {
+               return isTempOpenedDMTab;
+       }
+
+       /**
+        * 前回SendDMタブを開いていたか
+        * @return
+        */
+       public boolean isTempOpenedSendDMTab() {
+               return isTempOpenedSendDMTab;
+       }
+
+       /**
         * 設定ファイルを読み込む
         * 
         * @throws IOException
@@ -1650,8 +1858,14 @@ public class TweetMainAction {
                String mfh = this.property.getProperty("mainFrameHeight");
 
                // メッセージ通知を行うか
-               String nm = this.property.getProperty("notifyMessage");
+               String nm = this.property.getProperty("notifyMention");
                String ndm = this.property.getProperty("notifyDirectMessage");
+               
+               //前回開いていたタブの情報
+               String ptl = this.property.getProperty("openTimelineTab");
+               String pm = this.property.getProperty("openMentionTab");
+               String podm = this.property.getProperty("openDirectMessageTab");
+               String posdmt = this.property.getProperty("openSendDirectMessageTab");
 
                try {
                        this.newTableColor = new Color(Integer.parseInt(ntrgb));
@@ -1669,7 +1883,13 @@ public class TweetMainAction {
 
                        // 通知関係
                        this.isNotifyMentionMessage = Boolean.parseBoolean(nm);
-                       this.isNotifyMentionMessage = Boolean.parseBoolean(ndm);
+                       this.isNotifyDirectMessage = Boolean.parseBoolean(ndm);
+                       
+                       //前回開いていたタブ情報
+                       this.isTempOpenedTimelineTab = Boolean.parseBoolean(ptl);
+                       this.isTempOpenedMentionTab = Boolean.parseBoolean(pm);
+                       this.isTempOpenedDMTab = Boolean.parseBoolean(podm);
+                       this.isTempOpenedSendDMTab = Boolean.parseBoolean(posdmt);
                } catch (NumberFormatException e) {
                        e.printStackTrace();
                }
@@ -1726,6 +1946,12 @@ public class TweetMainAction {
                                + "");
                this.property.setProperty("notifyDirectMessage",
                                this.isNotifyDirectMessage + "");
+               
+               //タブの保存
+               this.property.setProperty("openTimelineTab", this.isExistTimelineTab() + "");
+               this.property.setProperty("openMentionTab", this.isExistMentionTab() + "");
+               this.property.setProperty("openDirectMessageTab", this.isExistDirectMessageTab() + "");
+               this.property.setProperty("openSendDirectMessageTab", this.isExistSendDirectMessageTab() + "");
 
                // プロパティのリストを保存
                property.store(new FileOutputStream("./" + PROPERTIES_DIRECTORY + "/"
@@ -1854,6 +2080,39 @@ public class TweetMainAction {
        }
 
        /**
+        * 
+        * @param notify
+        */
+       public void setNotifyMention(boolean notify) {
+               this.isNotifyMentionMessage = notify;
+       }
+
+       /**
+        * 
+        * @return
+        */
+       public boolean getNotifyMention() {
+               return this.isNotifyMentionMessage;
+       }
+
+       /**
+        * 
+        * @param notify
+        * @return
+        */
+       public void setNotifyDirectMessage(boolean notify) {
+               this.isNotifyDirectMessage = notify;
+       }
+
+       /**
+        * 
+        * @return
+        */
+       public boolean getNotifyDirectMessage() {
+               return this.isNotifyDirectMessage;
+       }
+
+       /**
         * @param getTimelinePeriod
         *            the getTimelinePeriod to set
         */