タイムライン
さて、このアプリケーションを使って一番やりたいことは、つぶやきを送ることでした。おそらく、つぶやきの表示がタイムラインに従って更新されることも期待されるでしょう。ただし、Twitterのタイムライン機能をそのままコピーするわけではありません。Twitterでは、フォローするユーザー全員のつぶやきが一緒くたに更新されますが、今回のアプリケーションでは本人のつぶやきだけを表示します。では、メインのコンテンツに手を加えていきましょう。
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.Status;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class StatusTimeLine implements ActionListener {
Twitter twitter;
JPanel timeLinePanel;
JPanel updatePanel;
JTextField updateField;
JButton updateButton;
public StatusTimeLine(Twitter twitter) throws TwitterException {
this.twitter = twitter;
timeLinePanel = new JPanel();
updateField = new JTextField();
updatePanel = new JPanel(new FlowLayout());
updatePanel.setSize(800, 30);
updatePanel.setPreferredSize(new Dimension(800, 30));
updatePanel.setMaximumSize(new Dimension(800, 30));
updateButton = new JButton("Update");
updateButton.setSize(90, 20);
updateButton.setPreferredSize(new Dimension(90, 20));
updateButton.setMaximumSize(new Dimension(90, 20));
updateButton.addActionListener(this);
updateField = new JTextField();
updateField.setSize(600, 20);
updateField.setPreferredSize(new Dimension(600, 20));
updateField.setMaximumSize(new Dimension(600, 20));
updatePanel.add(updateField);
updatePanel.add(updateButton);
timeLinePanel.add(updatePanel);
updateTimePanel();
}
private void updateTimePanel() throws TwitterException {
java.util.List<STATUS> statusList = twitter.getUserTimeline();
String statusArr[] = new String[statusList.size()];
timeLinePanel.setLayout(new BoxLayout(timeLinePanel, BoxLayout.Y_AXIS));
for (int i = 0; i < statusList.size(); i++) {
Date tweetDate = statusList.get(i).getCreatedAt();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy HH:mm");
statusArr[i] = formatter.format(tweetDate) + "-" + statusList.get(i).getText();
}
JList statusJList = new JList(statusArr);
statusJList.setFixedCellHeight(20);
JScrollPane scrollPane = new JScrollPane(statusJList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, // 垂直バー
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
timeLinePanel.add(scrollPane);
}
public JPanel getTimeLinePanel() {
return timeLinePanel;
}
public void actionPerformed(ActionEvent event) {
try {
twitter.updateStatus(updateField.getText());
updateTimePanel();
timeLinePanel.remove(1);
timeLinePanel.updateUI();
updateField.setText("");
} catch (TwitterException exception) {
JOptionPane.showMessageDialog(null, "An error has occurred while updating.");
exception.printStackTrace();
}
}
}
このパネルの上部に、つぶやきの入力に使うJTextFieldを配置しました。隣には更新に使うJButtonを配置します。下のサブパネルには、すべてのユーザーのつぶやきを表示するJListがあります。[Update]ボタンをクリックすると、エラーメッセージを示すポップアップが表示されるか、つぶやきがユーザーアカウントに送られます。JListには、この新しいつぶやきが追加されます。
フォロワーパネルの場合と同様に、Twitterオブジェクトのインスタンスをコンストラクタに渡します。プログラムの読み込み時にステータスをJListに埋め込む際に使う更新リストを取得するために、TwitterオブジェクトのgetUserTimeline()メソッドを使ってつぶやきのリストを取得します。このメソッドは、Status型のListを返します。タイムラインのエリアには、つぶやきの日付とメッセージ本体を表示します。つぶやきの日付は、StatusオブジェクトのgetCreatedAt()メソッドを使って取得します。このメソッドからはDateオブジェクトが返されます。この日付をわかりやすいdd-MMM-yy HH:mm形式の文字列に変換するために、SimpleDateFormatオブジェクトを使用します。つぶやきのメッセージ本体は、getText()メソッドを呼び出して取得します。日付とメッセージは連結して文字列配列に挿入します。次に、ステータスのJListにステータスの文字列配列statusArrを埋め込みます。大量のつぶやきが表示されることはまず間違いないでしょうから、JListをスクロールバーの中に配置して見やすくします。
簡単に言えば、[Update]ボタンをクリックすると、actionPerformedメソッドからつぶやきが送られ、テキストエリアがクリアされ、タイムパネルが更新され、更新された情報で画面が再描画されます。更新のプロセスに何か異常があると、エラーを通知するポップアップが表示されます。

