OSDN Git Service

240a65e29544919a78adaa917869ee0766c22ffa
[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 javax.swing.*;
6
7 /**
8  * 本プログラムのメインクラス
9  */
10 @SuppressWarnings("serial")
11 public class AdjustTime extends JFrame
12 {
13         public static final String PROGRAM_NAME = "AdjustTime for JOSM";
14         public static final String PROGRAM_VARSION = "1.0";
15         public static final String PROGRAM_UPDATE = "2014/06/30";
16         
17     // Used for addNotify check.
18     boolean fComponentsAdjusted = false;
19     
20     //{{DECLARE_CONTROLS
21     JPanel argsPanel;           // パラメータ設定パネル       (上部)
22     JScrollPane imageSPane;     // スクロールパネル
23     JLabel imageLabel;          // 基準時刻画像表示
24     JTextArea textArea;         // 実行結果表示領域
25     //ImagePreview imagePane;   // 基準時刻画像表示
26     ParameterPanelFolder arg1Panel_1;
27     ParameterPanelImageFile arg1Panel_2;
28     ParameterPanelTime arg1Panel_3;
29     JPanel buttonPanel;         // ボタンパネル   (下部)
30     JButton openButton;         // [Fit]ボタン
31     JButton zoomInButton;       // [Zoom in]ボタン
32     JButton zoomOutButton;      // [Zoom out]ボタン
33     //}}
34
35     //{{DECLARE_MENUS
36     java.awt.MenuBar mainMenuBar;
37     java.awt.Menu menu1;
38     java.awt.MenuItem miDoNewFileList;
39     java.awt.MenuItem miDoDirSize;
40     java.awt.MenuItem miDoReadXML;
41     java.awt.MenuItem miExit;
42     java.awt.Menu menu3;
43     java.awt.MenuItem miAbout;
44     //}}
45
46     class SymWindow extends java.awt.event.WindowAdapter {
47         /**
48          * このFrameが閉じられるときの動作。
49          * 終了確認のパネルを表示して、[OK]が押されたら終了する
50          */
51         public void windowClosing(java.awt.event.WindowEvent event) {
52             Object object = event.getSource();
53             if (object == AdjustTime.this) {
54                 DbMang_WindowClosing(event);
55             }
56         }
57     }
58
59     class SymAction implements java.awt.event.ActionListener {
60         public void actionPerformed(java.awt.event.ActionEvent event) {
61             Object object = event.getSource();
62             if (object == miAbout) {
63                 miAbout_Action(event);
64             }
65             else if (object == miExit) {
66                 miExit_Action(event);
67             }
68             else if (object == openButton) {
69                 imageView_Action(event);
70             }
71             else if (object == zoomInButton) {
72                 zoomin_Action(event);
73             }
74             else if (object == zoomOutButton) {
75                 zoomout_Action(event);
76             }
77             else if (object == arg1Panel_2.argField) {
78                 imageView_Action(event);
79             }
80             else if (object == arg1Panel_2.openButton) {
81                 selectImage_Action(event);
82                 imageView_Action(event);
83             }
84             else if (object == arg1Panel_3.doButton) {
85                 doButton_Action(event);
86             }
87         }
88     }
89
90         /**
91          * データベース内のテーブルを一覧で表示するFrame
92          */
93     public AdjustTime()
94     {
95         // INIT_CONTROLS
96         //setLayout(new BorderLayout());
97         Container container = getContentPane();
98         container.setLayout(new BorderLayout());
99         setVisible(false);
100         setSize(getInsets().left + getInsets().right + 500,getInsets().top + getInsets().bottom + 480);
101         setTitle(AdjustTime.PROGRAM_NAME +" v"+ AdjustTime.PROGRAM_VARSION);
102         
103         // パラメータ設定パネル
104         argsPanel = new JPanel();
105         argsPanel.setLayout(new BoxLayout(argsPanel, BoxLayout.Y_AXIS));
106         arg1Panel_1 = new ParameterPanelFolder("対象フォルダ: ", ".");
107         argsPanel.add(arg1Panel_1);
108         arg1Panel_2 = new ParameterPanelImageFile("基準時刻画像: ", "", arg1Panel_1);
109         argsPanel.add(arg1Panel_2);
110         arg1Panel_3 = new ParameterPanelTime("  基準時刻: ", "yyyy-mm-ddTHH:MM:ss");
111         argsPanel.add(arg1Panel_3);
112         
113         this.getContentPane().add(argsPanel, BorderLayout.NORTH);
114         
115         // 基準時刻画像表示
116         imageLabel = new JLabel();
117         imageSPane = new JScrollPane(imageLabel);
118         this.getContentPane().add(imageSPane, BorderLayout.CENTER);
119         
120         // 画像ファイル選択ダイアログを起動するボタン
121         buttonPanel = new JPanel();
122         buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
123         openButton = new JButton(createImageIcon("images/Fit16.gif"));
124         buttonPanel.add(openButton);
125         zoomInButton = new JButton(createImageIcon("images/ZoomIn16.gif"));
126         buttonPanel.add(zoomInButton);
127         zoomOutButton = new JButton(createImageIcon("images/ZoomOut16.gif"));
128         buttonPanel.add(zoomOutButton);
129         this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
130         
131         // INIT_MENUS
132         menu1 = new java.awt.Menu("File");
133         miExit = new java.awt.MenuItem(QuitDialog.TITLE);
134         miExit.setFont(new Font("Dialog", Font.PLAIN, 12));
135         menu1.add(miExit);
136
137         miAbout = new java.awt.MenuItem("About...");
138         miAbout.setFont(new Font("Dialog", Font.PLAIN, 12));
139
140         menu3 = new java.awt.Menu("Help");
141         menu3.setFont(new Font("Dialog", Font.PLAIN, 12));
142         menu3.add(miAbout);
143
144         mainMenuBar = new java.awt.MenuBar();
145         mainMenuBar.setHelpMenu(menu3);
146         mainMenuBar.add(menu1);
147         mainMenuBar.add(menu3);
148         setMenuBar(mainMenuBar);
149         
150
151         //{{REGISTER_LISTENERS
152         SymWindow aSymWindow = new SymWindow();
153         this.addWindowListener(aSymWindow);
154         SymAction lSymAction = new SymAction();
155         miAbout.addActionListener(lSymAction);
156         miExit.addActionListener(lSymAction);
157         openButton.addActionListener(lSymAction);
158         zoomOutButton.addActionListener(lSymAction);
159         zoomInButton.addActionListener(lSymAction);
160         arg1Panel_2.argField.addActionListener(lSymAction);
161         arg1Panel_2.openButton.addActionListener(lSymAction);
162         arg1Panel_3.doButton.addActionListener(lSymAction);
163         //}}
164     }
165     
166     /**
167      * Shows or hides the component depending on the boolean flag b.
168      * @param b trueのときコンポーネントを表示; その他のとき, componentを隠す.
169      * @see java.awt.Component#isVisible
170      */
171     public void setVisible(boolean b) {
172         if(b) {
173             setLocation(50, 50);
174             
175         }
176         super.setVisible(b);
177     }
178     
179         /**
180          * このクラスをインスタンスを生成して表示する。
181          * コマンドラインの引数はありません。
182          */    
183     static public void main(String args[]) {
184         SwingUtilities.invokeLater(new Runnable() {
185             public void run() {
186                 createAndShowGUI();
187             }
188         });
189     }
190     private static void createAndShowGUI() {
191         (new AdjustTime()).setVisible(true);
192     }
193
194     public void addNotify()     {
195         // Record the size of the window prior to calling parents addNotify.
196         Dimension d = getSize();
197
198         super.addNotify();
199
200         if (fComponentsAdjusted)
201             return;
202
203         // Adjust components according to the insets
204         setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);
205         Component components[] = getComponents();
206         for (int i = 0; i < components.length; i++){
207             Point p = components[i].getLocation();
208             p.translate(getInsets().left, getInsets().top);
209             components[i].setLocation(p);
210         }
211         fComponentsAdjusted = true;
212     }
213
214     void DbMang_WindowClosing(java.awt.event.WindowEvent event) {
215         setVisible(false);  // hide the Manager
216         dispose();                      // free the system resources
217         System.exit(0);         // close the application
218     }
219
220     void miAbout_Action(java.awt.event.ActionEvent event) {
221         // Action from About Create and show as modal
222         (new AboutDialog(this, true)).setVisible(true);
223     }
224     
225     void miExit_Action(java.awt.event.ActionEvent event) {
226         // Action from Exit Create and show as modal
227         //(new hayashi.yuu.tools.gui.QuitDialog(this, true)).setVisible(true);
228         (new QuitDialog(this, true)).setVisible(true);
229     }
230
231     ImageIcon refImage;
232     
233     /**
234      * 選択された画像ファイルを表示する
235      * @param ev
236      */
237     public void imageView_Action(ActionEvent ev) {
238         String path = (new File(arg1Panel_1.getText(), arg1Panel_2.getText())).getPath();
239         int size_x = imageSPane.getWidth() - 8;
240         ImageIcon tmpIcon = new ImageIcon(path);
241         refImage = tmpIcon;
242         if (tmpIcon != null) {
243             if (tmpIcon.getIconWidth() > size_x) {
244                 refImage = new ImageIcon(tmpIcon.getImage().getScaledInstance(size_x, -1, Image.SCALE_DEFAULT));
245             }
246         }
247         imageLabel.setIcon(refImage);
248         repaint();
249     }
250
251     public void zoomin_Action(ActionEvent ev) {
252         if (refImage != null) {
253                 int size_x = imageLabel.getWidth();
254                 String path = (new File(arg1Panel_1.getText(), arg1Panel_2.getText())).getPath();
255                 ImageIcon tmpIcon = new ImageIcon(path);
256                 refImage = new ImageIcon(tmpIcon.getImage().getScaledInstance(size_x * 2, -1, Image.SCALE_DEFAULT));
257                 imageLabel.setIcon(refImage);
258             repaint();
259         }
260     }
261
262     public void zoomout_Action(ActionEvent ev) {
263         if (refImage != null) {
264                 int size_x = imageLabel.getWidth();
265                 ImageIcon tmpIcon = refImage;
266                 refImage = new ImageIcon(tmpIcon.getImage().getScaledInstance(size_x / 2, -1, Image.SCALE_DEFAULT));
267                 imageLabel.setIcon(refImage);
268             repaint();
269         }
270     }
271
272     public void selectImage_Action(ActionEvent ev) {
273         JFileChooser fc;
274
275         //Set up the file chooser.
276                 File sdir = new File(arg1Panel_1.getText());
277                 System.out.println(sdir.getPath());
278                 if (sdir.isDirectory()) {
279                 fc = new JFileChooser(sdir);
280                 }
281                 else {
282                 fc = new JFileChooser();
283                 }
284
285             //Add a custom file filter and disable the default
286             //(Accept All) file filter.
287         fc.addChoosableFileFilter(new ImageFilter());
288         fc.setAcceptAllFileFilterUsed(false);
289
290         //Add custom icons for file types.
291         fc.setFileView(new ImageFileView());
292
293         //Add the preview pane.
294         fc.setAccessory(new ImagePreview(fc));
295
296         //Show it.
297         int returnVal = fc.showDialog(this, "選択");
298
299         //Process the results.
300         if (returnVal == JFileChooser.APPROVE_OPTION) {
301             File file = fc.getSelectedFile();
302             arg1Panel_2.argField.setText(file.getName());
303         }
304
305         //Reset the file chooser for the next time it's shown.
306         fc.setSelectedFile(null);
307     }
308     
309     /** Returns an ImageIcon, or null if the path was invalid. */
310     public static ImageIcon createImageIcon(String path) {
311         java.net.URL imgURL = AdjustTime.class.getResource(path);
312         if (imgURL != null) {
313             return new ImageIcon(imgURL);
314         } else {
315             System.err.println("Couldn't find file: " + path);
316             return null;
317         }
318     }
319     
320     /**
321      * [実行]ボタンをクリックしたときの動作
322      * @param event
323      */
324     void doButton_Action(java.awt.event.ActionEvent event) {
325         arg1Panel_3.doButton.setEnabled(false);
326
327         String[] argv = new String[4];
328                 argv[0] = "list.csv";
329                 argv[1] = arg1Panel_1.getText();
330                 argv[2] = arg1Panel_2.getText();
331                 argv[3] = arg1Panel_3.getText();
332                 (new DoDialog(this, argv)).setVisible(true);
333                 
334         arg1Panel_3.doButton.setEnabled(true);
335     }
336
337 }