OSDN Git Service

06c8ef8dfcebb901d11a534fdf7ff479d8fba88e
[importpicture/importpicture.git] / importPicture / src / osm / jp / gpx / matchtime / gui / AdjustTime.java
1 package osm.jp.gpx.matchtime.gui;
2 import java.awt.*;
3 import java.awt.event.ActionEvent;
4 import java.io.File;
5 import java.util.Date;
6 import javax.swing.*;
7 import osm.jp.gpx.ImportPicture;
8
9 /**
10  * 本プログラムのメインクラス
11  */
12 @SuppressWarnings("serial")
13 public class AdjustTime extends JFrame
14 {
15     public static final String PROGRAM_NAME = "AdjustTime for JOSM";
16     public static final String PROGRAM_VARSION = "3.0";
17     public static final String PROGRAM_UPDATE = "2015/07/26";
18         
19     // Used for addNotify check.
20     boolean fComponentsAdjusted = false;
21     
22     //{{DECLARE_CONTROLS
23     JPanel cardPanel;       // ウィザード形式パネル(カード型)
24     JPanel[] cards;
25     int cardPanelNo = 0;
26     JPanel argsPanel;           // パラメータ設定パネル       (上部)
27     JScrollPane imageSPane;     // スクロールパネル
28     JLabel imageLabel;          // 基準時刻画像表示
29     JTextArea textArea;         // 実行結果表示領域
30     //ImagePreview imagePane;   // 基準時刻画像表示
31     ParameterPanelFolder arg1Panel_1;
32     ParameterPanelImageFile arg1Panel_2;
33     ParameterPanelTime arg1Panel_3;
34     ParameterPanelSelecter arg1Panel_4;         // EXIF 書き出しモード
35     ParameterPanelFolder arg1Panel_5;       // GPXファイル・フォルダ
36     
37     JPanel buttonPanel;         // ボタンパネル   (下部)
38     JButton openButton;         // [Fit]ボタン
39     JButton zoomInButton;       // [Zoom in]ボタン
40     JButton zoomOutButton;      // [Zoom out]ボタン
41     JButton nextButton;     // [次へ]ボタン
42     JButton backButton;     // [戻る]ボタン
43     //}}
44
45     //{{DECLARE_MENUS
46     java.awt.MenuBar mainMenuBar;
47     java.awt.Menu menu1;
48     java.awt.MenuItem miDoNewFileList;
49     java.awt.MenuItem miDoDirSize;
50     java.awt.MenuItem miDoReadXML;
51     java.awt.MenuItem miExit;
52     java.awt.Menu menu3;
53     java.awt.MenuItem miAbout;
54     //}}
55
56     class SymWindow extends java.awt.event.WindowAdapter {
57         /**
58          * このFrameが閉じられるときの動作。
59          * このパネルが閉じられたら、このアプリケーションも終了させる。
60          */
61         @Override
62         public void windowClosing(java.awt.event.WindowEvent event) {
63             Object object = event.getSource();
64             if (object == AdjustTime.this) {
65                 DbMang_WindowClosing(event);
66             }
67         }
68     }
69
70     class SymAction implements java.awt.event.ActionListener {
71         @Override
72         public void actionPerformed(java.awt.event.ActionEvent event) {
73             Object object = event.getSource();
74             if (object == miAbout) {
75                 miAbout_Action(event);
76             }
77             else if (object == miExit) {
78                 miExit_Action(event);
79             }
80             else if (object == openButton) {
81                 imageView_Action(event);
82             }
83             else if (object == zoomInButton) {
84                 zoomin_Action(event);
85             }
86             else if (object == zoomOutButton) {
87                 zoomout_Action(event);
88             }
89             else if (object == arg1Panel_2.argField) {
90                 imageView_Action(event);
91             }
92             else if (object == arg1Panel_2.openButton) {
93                 selectImage_Action(event);
94                 imageView_Action(event);
95             }
96             else if (object == arg1Panel_3.doButton) {
97                 doButton_Action(event);
98             }
99             else if (object == arg1Panel_4.field) {
100                 doButton_Action(event);
101             }
102             else if (object == nextButton) {
103                 nextButton_Action(event);
104             }
105             else if (object == backButton) {
106                 backButton_Action(event);
107             }
108         }
109     }
110
111         /**
112          * データベース内のテーブルを一覧で表示するFrame
113          */
114     public AdjustTime()
115     {
116         // INIT_CONTROLS
117         Container container = getContentPane();
118         container.setLayout(new BorderLayout());
119         setSize(getInsets().left + getInsets().right + 960,getInsets().top + getInsets().bottom + 480);
120         setTitle(AdjustTime.PROGRAM_NAME +" v"+ AdjustTime.PROGRAM_VARSION);
121         
122         //---------------------------------------------------------------------
123         cardPanel = new JPanel();
124         cardPanel.setLayout(new CardLayout());
125         container.add(cardPanel, BorderLayout.CENTER);
126         
127         nextButton = new JButton("次へ");
128         container.add(nextButton, BorderLayout.EAST);
129
130         backButton = new JButton("戻る");
131         container.add(backButton, BorderLayout.WEST);
132
133         cards = new JPanel[5];
134         for (int i=0; i < 5; i++) {
135             cards[i] = new JPanel();
136             cardPanel.add(cards[i], String.valueOf(i));
137         }
138         cardPanelNo = 0;
139
140         //---------------------------------------------------------------------
141         // 1.[対象フォルダ]設定パネル
142         int cardNo = 0;
143         cards[cardNo].setLayout(new BorderLayout());
144         JLabel label1 = new JLabel();
145         label1.setText("<html><p>位置情報を付加したい画像ファイルが格納されているフォルダを選択してください。</p><ul><li>コピー動作を行うと、ファイル更新時刻がコピーを実行した時刻に書き換わってしまうことがあります。オリジナルのカメラUSB内のフォルダを直接指定することをおすすめします。</li></ul>");
146         cards[cardNo].add(label1, BorderLayout.NORTH);
147         
148         argsPanel = new JPanel();
149         argsPanel.setLayout(new BoxLayout(argsPanel, BoxLayout.Y_AXIS));
150         arg1Panel_1 = new ParameterPanelFolder("対象フォルダ: ", ".");
151         argsPanel.add(arg1Panel_1);
152         cards[cardNo].add(argsPanel, BorderLayout.CENTER);
153         
154         //---------------------------------------------------------------------
155         // 2.[基準時刻画像]設定パネル
156         cardNo++;
157         cards[cardNo].setLayout(new BorderLayout());
158         JLabel label2 = new JLabel();
159         label2.setText("<html><p>正確な撮影時刻が判明できる画像を選んでください。</p><ul><li>スマートフォンの時計画面(秒が判別できること)を撮影した画像</li><li>カメラの時計が正確ならば、どの画像を選んでも構いません。</li></ul>");
160         cards[cardNo].add(label2, BorderLayout.NORTH);
161
162         argsPanel = new JPanel();
163         argsPanel.setLayout(new BoxLayout(argsPanel, BoxLayout.Y_AXIS));
164         arg1Panel_2 = new ParameterPanelImageFile("基準時刻画像: ", "", arg1Panel_1);
165         argsPanel.add(arg1Panel_2);
166         cards[cardNo].add(argsPanel, BorderLayout.CENTER);
167         
168         //---------------------------------------------------------------------
169         // 3.基準時刻の入力画面
170         cardNo++;
171         cards[cardNo].setLayout(new BorderLayout());
172         argsPanel = new JPanel();
173         argsPanel.setLayout(new GridLayout(2, 1));
174
175         JLabel label3 = new JLabel();
176         label3.setText("<html><p>正確な撮影時刻を入力してください。</p><ul><li>カメラの時計が正確ならば、設定を変更する必要はありません。</li></ul>");
177         argsPanel.add(label3);
178         arg1Panel_3 = new ParameterPanelTime("  基準時刻: ", ImportPicture.TIME_FORMAT_STRING);
179         argsPanel.add(arg1Panel_3);
180         cards[cardNo].add(argsPanel, BorderLayout.NORTH);
181
182         // 参考画像
183         imageLabel = new JLabel();
184         imageSPane = new JScrollPane(imageLabel);
185         cards[cardNo].add(imageSPane, BorderLayout.CENTER);
186               
187         // 画像ファイル選択ダイアログを起動するボタン
188         buttonPanel = new JPanel();
189         buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
190         openButton = new JButton(createImageIcon("images/Fit16.gif"));
191         buttonPanel.add(openButton);
192         zoomInButton = new JButton(createImageIcon("images/ZoomIn16.gif"));
193         buttonPanel.add(zoomInButton);
194         zoomOutButton = new JButton(createImageIcon("images/ZoomOut16.gif"));
195         buttonPanel.add(zoomOutButton);
196         cards[cardNo].add(buttonPanel, BorderLayout.SOUTH);
197         
198         //---------------------------------------------------------------------
199         // 4.GPXファイル設定画面
200         cardNo++;
201         cards[cardNo].setLayout(new BorderLayout());
202         JPanel tmpPanel4 = new JPanel();
203         tmpPanel4.setLayout(new BoxLayout(tmpPanel4, BoxLayout.Y_AXIS));
204         File gpxDir = new File(".");
205         arg1Panel_5 = new ParameterPanelFolder("GPXフォルダ: ", gpxDir.getAbsolutePath());
206         tmpPanel4.add(arg1Panel_5);
207         cards[cardNo].add(tmpPanel4, BorderLayout.CENTER);
208         
209         //---------------------------------------------------------------------
210         // 5.EXIF更新設定画面
211         cardNo++;
212         cards[cardNo].setLayout(new BorderLayout());
213         JPanel tmpPanel5 = new JPanel();
214         tmpPanel5.setLayout(new BoxLayout(tmpPanel5, BoxLayout.Y_AXIS));
215         String[] combodata = {"更新しない", "更新する"};
216         arg1Panel_4 = new ParameterPanelSelecter("EXIF変更: ", combodata);
217         tmpPanel5.add(arg1Panel_4);
218         cards[cardNo].add(tmpPanel5, BorderLayout.CENTER);
219
220         //---------------------------------------------------------------------
221         // INIT_MENUS
222         menu1 = new java.awt.Menu("File");
223         miExit = new java.awt.MenuItem(QuitDialog.TITLE);
224         miExit.setFont(new Font("Dialog", Font.PLAIN, 12));
225         menu1.add(miExit);
226
227         miAbout = new java.awt.MenuItem("About...");
228         miAbout.setFont(new Font("Dialog", Font.PLAIN, 12));
229
230         menu3 = new java.awt.Menu("Help");
231         menu3.setFont(new Font("Dialog", Font.PLAIN, 12));
232         menu3.add(miAbout);
233
234         mainMenuBar = new java.awt.MenuBar();
235         mainMenuBar.setHelpMenu(menu3);
236         mainMenuBar.add(menu1);
237         mainMenuBar.add(menu3);
238         setMenuBar(mainMenuBar);
239         
240
241         //{{REGISTER_LISTENERS
242         SymWindow aSymWindow = new SymWindow();
243         this.addWindowListener(aSymWindow);
244         SymAction lSymAction = new SymAction();
245         miAbout.addActionListener(lSymAction);
246         miExit.addActionListener(lSymAction);
247         openButton.addActionListener(lSymAction);
248         zoomOutButton.addActionListener(lSymAction);
249         zoomInButton.addActionListener(lSymAction);
250         arg1Panel_2.argField.addActionListener(lSymAction);
251         arg1Panel_2.openButton.addActionListener(lSymAction);
252         arg1Panel_3.doButton.addActionListener(lSymAction);
253         arg1Panel_4.field.addActionListener(lSymAction);
254         nextButton.addActionListener(lSymAction);
255         backButton.addActionListener(lSymAction);
256         //}}
257     }
258     
259     /**
260      * Shows or hides the component depending on the boolean flag b.
261      * @param b trueのときコンポーネントを表示; その他のとき, componentを隠す.
262      * @see java.awt.Component#isVisible
263      */
264     @Override
265     public void setVisible(boolean b) {
266         if(b) {
267             setLocation(50, 50);
268         }
269         super.setVisible(b);
270     }
271     
272     /**
273      * このクラスをインスタンスを生成して表示する。
274      * コマンドラインの引数はありません。
275      * @param args
276      */    
277     static public void main(String args[]) {
278         SwingUtilities.invokeLater(() -> {
279             createAndShowGUI();
280         });
281     }
282     private static void createAndShowGUI() {
283         (new AdjustTime()).setVisible(true);
284     }
285
286     @Override
287     public void addNotify()     {
288         // Record the size of the window prior to calling parents addNotify.
289         Dimension d = getSize();
290
291         super.addNotify();
292
293         if (fComponentsAdjusted)
294             return;
295
296         // Adjust components according to the insets
297         setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);
298         Component components[] = getComponents();
299         for (Component component : components) {
300             Point p = component.getLocation();
301             p.translate(getInsets().left, getInsets().top);
302             component.setLocation(p);
303         }
304         fComponentsAdjusted = true;
305     }
306
307     void DbMang_WindowClosing(java.awt.event.WindowEvent event) {
308         setVisible(false);  // hide the Manager
309         dispose();                      // free the system resources
310         System.exit(0);         // close the application
311     }
312
313     void miAbout_Action(java.awt.event.ActionEvent event) {
314         // Action from About Create and show as modal
315         (new AboutDialog(this, true)).setVisible(true);
316     }
317     
318     void miExit_Action(java.awt.event.ActionEvent event) {
319         // Action from Exit Create and show as modal
320         //(new hayashi.yuu.tools.gui.QuitDialog(this, true)).setVisible(true);
321         (new QuitDialog(this, true)).setVisible(true);
322     }
323
324     ImageIcon refImage;
325     
326     /**
327      * 選択された画像ファイルを表示する
328      * 基準画像ボタンがクリックされた時に、基準時刻フィールドに基準画像の作成日時を設定する。
329      * @param ev
330      */
331     public void imageView_Action(ActionEvent ev) {
332         String path = (new File(arg1Panel_1.getText(), arg1Panel_2.getText())).getPath();
333         
334         File timeFile = new File(path);
335         long lastModifyTime = timeFile.lastModified();
336         arg1Panel_3.argField.setText(ImportPicture.dfjp.format(new Date(lastModifyTime)));
337         
338         int size_x = imageSPane.getWidth() - 8;
339         ImageIcon tmpIcon = new ImageIcon(path);
340         refImage = tmpIcon;
341         if (tmpIcon.getIconWidth() > size_x) {
342             refImage = new ImageIcon(tmpIcon.getImage().getScaledInstance(size_x, -1, Image.SCALE_DEFAULT));
343         }
344         imageLabel.setIcon(refImage);
345         repaint();
346     }
347
348     public void zoomin_Action(ActionEvent ev) {
349         if (refImage != null) {
350                 int size_x = imageLabel.getWidth();
351                 String path = (new File(arg1Panel_1.getText(), arg1Panel_2.getText())).getPath();
352                 ImageIcon tmpIcon = new ImageIcon(path);
353                 refImage = new ImageIcon(tmpIcon.getImage().getScaledInstance(size_x * 2, -1, Image.SCALE_DEFAULT));
354                 imageLabel.setIcon(refImage);
355             repaint();
356         }
357     }
358
359     public void zoomout_Action(ActionEvent ev) {
360         if (refImage != null) {
361                 int size_x = imageLabel.getWidth();
362                 ImageIcon tmpIcon = refImage;
363                 refImage = new ImageIcon(tmpIcon.getImage().getScaledInstance(size_x / 2, -1, Image.SCALE_DEFAULT));
364                 imageLabel.setIcon(refImage);
365             repaint();
366         }
367     }
368
369     public void selectImage_Action(ActionEvent ev) {
370         JFileChooser fc;
371
372         //Set up the file chooser.
373                 File sdir = new File(arg1Panel_1.getText());
374                 System.out.println(sdir.getPath());
375                 if (sdir.isDirectory()) {
376                 fc = new JFileChooser(sdir);
377                 }
378                 else {
379                 fc = new JFileChooser();
380                 }
381
382             //Add a custom file filter and disable the default
383             //(Accept All) file filter.
384         fc.addChoosableFileFilter(new ImageFilter());
385         fc.setAcceptAllFileFilterUsed(false);
386
387         //Add custom icons for file types.
388         fc.setFileView(new ImageFileView());
389
390         //Add the preview pane.
391         fc.setAccessory(new ImagePreview(fc));
392
393         //Show it.
394         int returnVal = fc.showDialog(this, "選択");
395
396         //Process the results.
397         if (returnVal == JFileChooser.APPROVE_OPTION) {
398             File file = fc.getSelectedFile();
399             arg1Panel_2.argField.setText(file.getName());
400         }
401
402         //Reset the file chooser for the next time it's shown.
403         fc.setSelectedFile(null);
404     }
405     
406     /** Returns an ImageIcon, or null if the path was invalid.
407      * @param path
408      * @return  */
409     public static ImageIcon createImageIcon(String path) {
410         java.net.URL imgURL = AdjustTime.class.getResource(path);
411         if (imgURL != null) {
412             return new ImageIcon(imgURL);
413         } else {
414             System.err.println("Couldn't find file: " + path);
415             return null;
416         }
417     }
418     
419     /**
420      * [実行]ボタンをクリックしたときの動作
421      * @param event
422      */
423     void doButton_Action(java.awt.event.ActionEvent event) {
424         arg1Panel_3.doButton.setEnabled(false);
425
426         String[] argv = new String[4];
427         argv[0] = arg1Panel_1.getText();
428         argv[1] = arg1Panel_2.getText();
429         argv[2] = arg1Panel_3.getText();
430         String value = (String) arg1Panel_4.field.getSelectedItem();
431         argv[3] = (value.equals("更新する") ? "EXIF" : "none");
432
433         (new DoDialog(this, argv)).setVisible(true);
434                 
435         arg1Panel_3.doButton.setEnabled(true);
436     }
437     
438     /**
439      * [次へ]ボタンをクリックした時の動作
440      * @param event 
441      */
442     void nextButton_Action(ActionEvent event) {
443         if ((cardPanelNo + 1) < cards.length) {
444             cardPanelNo++;
445             CardLayout cl = (CardLayout)(cardPanel.getLayout());
446             cl.show(cardPanel, String.valueOf(cardPanelNo));
447         }
448     }
449
450     /**
451      * [戻る]ボタンをクリックした時の動作
452      * @param event
453      */
454     void backButton_Action(ActionEvent event) {
455         if ((cardPanelNo - 1) >= 0) {
456             cardPanelNo--;
457             CardLayout cl = (CardLayout)(cardPanel.getLayout());
458             cl.show(cardPanel, String.valueOf(cardPanelNo));
459         }
460     }
461 }