import java.awt.BorderLayout; import java.awt.Cursor; import java.rmi.RemoteException; import javax.swing.JComboBox; import javax.swing.JOptionPane; import google.webapi.*; public class TodaysGoogle extends JGoogleBar { private JComboBox dateBox; private static String[] dates = {"今日の", "今週の", "今月の", "3ヶ月間の", "半年の", "今年の"}; private static long[] diffs = {1, 7, 31, 93, 186, 365}; private static final String DATERANGE = " daterange:"; public TodaysGoogle() { frame.setTitle("Today's Google"); dateBox = new JComboBox(dates); dateBox.setEditable(false); frame.getContentPane().add(dateBox, BorderLayout.WEST); } public void search(String keyword, int start, int max) { if (keyword.equals("")) { return; } dateBox.setEnabled(false); keywordField.setEnabled(false); searchButton.setEnabled(false); Cursor cursor = frame.getCursor(); frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); if (keyword.indexOf(DATERANGE) == -1) { keyword = calculateJulianDate(keyword); } try { GoogleSearchResult result = searcher.search(keyword, start, max); showResult(result, keyword, start); } catch (RemoteException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(frame, "検索に失敗しました", "Error", JOptionPane.ERROR_MESSAGE); } finally { dateBox.setEnabled(true); keywordField.setEnabled(true); searchButton.setEnabled(true); frame.setCursor(cursor); } } private String calculateJulianDate(String keyword) { long now = System.currentTimeMillis(); // 1 日は 86400000 ミリ秒 // 1970.1.1 はユリウス日では 2440588 long today = now / 86400000 + 2440588; long diff = diffs[dateBox.getSelectedIndex()]; return keyword + DATERANGE + (today - diff) + "-" + today; } public static void main(String[] args) { new TodaysGoogle().show(); } }