OSDN Git Service

対象フォルダが入力されたら次のパネルを有効にする
[importpicture/importpicture.git] / src / osm / jp / gpx / matchtime / gui / restamp / RestampDialog.java
1 package osm.jp.gpx.matchtime.gui.restamp;
2 import java.awt.*;
3 import java.awt.event.ActionEvent;
4 import java.io.File;
5 import java.io.IOException;
6 import java.util.Date;
7 import java.util.Observable;
8 import java.util.Observer;
9 import javax.swing.*;
10 import javax.swing.event.DocumentEvent;
11 import osm.jp.gpx.AppParameters;
12 import osm.jp.gpx.matchtime.gui.AdjustTime;
13 import osm.jp.gpx.matchtime.gui.Card;
14 import osm.jp.gpx.matchtime.gui.ImageFileView;
15 import osm.jp.gpx.matchtime.gui.ImageFilter;
16 import osm.jp.gpx.matchtime.gui.ImagePreview;
17 import osm.jp.gpx.matchtime.gui.ParameterPanelFolder;
18 import osm.jp.gpx.matchtime.gui.ParameterPanelImageFile;
19 import static osm.jp.gpx.matchtime.gui.AdjustTime.i18n;
20 import osm.jp.gpx.matchtime.gui.ParameterPanelTime;
21 import osm.jp.gpx.matchtime.gui.SimpleDocumentListener;
22
23 @SuppressWarnings("serial")
24 public class RestampDialog extends Dialog implements Observer
25 {
26     //{{DECLARE_CONTROLS
27     java.awt.Label label1;
28     java.awt.Button closeButton;
29     java.awt.Label label2;
30     JLabel imageLabel;          // 開始画像の基準時刻画像表示
31     JTabbedPane cardPanel;       // ウィザード形式パネル(タブ型)
32     JScrollPane imageSPane;     // スクロールパネル
33     Card[] cards;
34     ParameterPanelFolder arg1_srcFolder;    // 対象フォルダ
35     ParameterPanelImageFile arg2_baseTimeImg;   // 開始画像ファイルパス
36     ParameterPanelTime arg2_basetime;   // 開始画像の基準時刻:
37     AppParameters params;
38     //}}
39
40     // Used for addNotify redundency check.
41     boolean fComponentsAdjusted = false;
42
43     /**
44      * 
45      * @param arg0
46      * @param arg1 
47      */
48     @Override
49     public void update(Observable arg0, Object arg1) {
50         String str = (String) arg1;
51         System.out.println("私はAです。観察対象の通知を検知したよ。" + str);
52     }
53
54     class SymWindow extends java.awt.event.WindowAdapter
55     {
56         @Override
57         public void windowClosing(java.awt.event.WindowEvent event) {
58             Object object = event.getSource();
59             if (object == RestampDialog.this) {
60                 AboutDialog_WindowClosing(event);
61             }
62         }
63     }
64
65     class SymAction implements java.awt.event.ActionListener
66     {
67         @Override
68         public void actionPerformed(java.awt.event.ActionEvent event) {
69             Object object = event.getSource();
70             if (object == closeButton) {
71                 closeButton_Clicked(event);
72             }
73             else if (object == arg2_baseTimeImg.argField) {
74                 imageView_Action(event);
75             }
76             else if (object == arg2_baseTimeImg.openButton) {
77                 selectImage_Action(event);
78                 imageView_Action(event);
79             }
80         }
81     }
82
83     @SuppressWarnings("OverridableMethodCallInConstructor")
84     public RestampDialog(Frame parent, boolean modal) throws IOException {
85         super(parent, modal);
86
87         // INIT_CONTROLS
88         setLayout(new BorderLayout());
89         setSize(
90             getInsets().left + getInsets().right + 720,
91             getInsets().top + getInsets().bottom + 480
92         );
93         setTitle(i18n.getString("menu.restamp") + "... ");
94         
95         //---- CENTER -----
96         JPanel mainPanel = new JPanel();
97         mainPanel.setLayout(new BorderLayout());
98         add(mainPanel, BorderLayout.CENTER);
99         
100         //---- SOUTH -----
101         JPanel southPanel = new JPanel(new BorderLayout());
102         southPanel.add(Box.createVerticalStrut(10), BorderLayout.SOUTH);
103         southPanel.add(Box.createVerticalStrut(10), BorderLayout.NORTH);
104         add(southPanel, BorderLayout.SOUTH);
105         
106         //---- SPACE -----
107         add(Box.createVerticalStrut(30), BorderLayout.NORTH);
108         add(Box.createHorizontalStrut(10), BorderLayout.WEST);
109         add(Box.createHorizontalStrut(10), BorderLayout.EAST);
110         
111         closeButton = new java.awt.Button();
112         closeButton.setLabel(i18n.getString("button.close") );
113         closeButton.setBounds(145,65,66,27);
114         southPanel.add(closeButton);
115         //}}
116         
117         //---------------------------------------------------------------------
118         params = new AppParameters();
119         cards = new Card[3];
120         cardPanel = new JTabbedPane(JTabbedPane.LEFT);
121         mainPanel.add(cardPanel, BorderLayout.CENTER);
122         int cardNo = 0;
123         
124         //---------------------------------------------------------------------
125         // 1.[対象フォルダ]設定パネル
126         {
127             arg1_srcFolder = new ParameterPanelFolder(
128                     i18n.getString("label.110") +": ", 
129                     params.getProperty(AppParameters.IMG_SOURCE_FOLDER)
130             );
131             arg1_srcFolder.argField
132                 .getDocument()
133                 .addDocumentListener(
134                     new SimpleDocumentListener() {
135                         @Override
136                         public void update(DocumentEvent e) {
137                             toEnable(0, arg1_srcFolder.isEnable());
138                         }
139                     }
140                 );
141    
142             Card card = new CardSourceFolder(cardPanel, arg1_srcFolder);
143             cardPanel.addTab(card.getTitle(), card);
144             cardPanel.setEnabledAt(cardNo, true);
145             cards[cardNo] = card;
146             cardNo++;
147         }
148
149         //---------------------------------------------------------------------
150         // 2. [基準画像(開始)]選択パネル
151         {
152             // 基準時刻画像
153             arg2_baseTimeImg = new ParameterPanelImageFile(
154                 i18n.getString("label.210") +": ", 
155                 null, 
156                 arg1_srcFolder
157             );
158             arg2_baseTimeImg.argField
159                 .getDocument()
160                 .addDocumentListener(
161                     new SimpleDocumentListener() {
162                         @Override
163                         public void update(DocumentEvent e) {
164                             toEnable(1, arg2_baseTimeImg.isEnable());
165                         }
166                     }
167                 );
168             
169             CardFirstFile card = new CardFirstFile(cardPanel, arg2_baseTimeImg);
170             cardPanel.addTab(card.getTitle(), card);
171             cardPanel.setEnabledAt(cardNo, false);
172             cards[cardNo] = card;
173             cardNo++;
174         }
175
176         //---------------------------------------------------------------------
177         // 2a. 開始画像の本当の時刻を設定の入力画面
178         {
179             // 2a. 基準時刻:
180             arg2_basetime = new ParameterPanelTime(
181                     i18n.getString("label.310"), 
182                     null, 
183                     arg2_baseTimeImg
184             );
185             arg2_basetime.argField
186                 .getDocument()
187                 .addDocumentListener(
188                     new SimpleDocumentListener() {
189                         @Override
190                         public void update(DocumentEvent e) {
191                             toEnable(2, arg2_basetime.isEnable());
192                         }
193                     }
194                 );
195             
196             CardCorectTime card = new CardCorectTime(cardPanel, arg2_basetime);
197             cardPanel.addTab(card.getTitle(), card);
198             cardPanel.setEnabledAt(cardNo, false);
199             cards[cardNo] = card;
200             cardNo++;
201         }
202         
203         //{{REGISTER_LISTENERS
204         SymWindow aSymWindow = new SymWindow();
205         this.addWindowListener(aSymWindow);
206         SymAction lSymAction = new SymAction();
207         closeButton.addActionListener(lSymAction);
208         //}}
209     }
210     
211     
212     void toEnable(final int cardNo, final boolean enable) {
213         if ((cardNo >= 0) && (cardNo < cards.length)) {
214             cardPanel.setEnabledAt(cardNo, enable);
215             if ((cardNo -1) >= 0) {
216                 cards[cardNo -1].nextButton.setEnabled(enable);
217             }
218             if ((cardNo +1) < cards.length) {
219                 cardPanel.setEnabledAt(cardNo+1, enable);
220                 cards[cardNo +1].backButton.setEnabled(enable);
221                 cards[cardNo].nextButton.setEnabled(enable);
222             }
223         }
224     }
225
226     @SuppressWarnings("OverridableMethodCallInConstructor")
227     public RestampDialog(Frame parent, String title, boolean modal) throws IOException {
228         this(parent, modal);
229         setTitle(title);
230     }
231
232     @Override
233     public void addNotify() {
234         // Record the size of the window prior to calling parents addNotify.
235
236         super.addNotify();
237
238         // Only do this once.
239         if (fComponentsAdjusted) {
240             return;
241         }
242
243         // Adjust components according to the insets
244         setSize(getInsets().left + getInsets().right + getSize().width, getInsets().top + getInsets().bottom + getSize().height);
245         Component components[] = getComponents();
246         for (Component component : components) {
247             Point p = component.getLocation();
248             p.translate(getInsets().left, getInsets().top);
249             component.setLocation(p);
250         }
251
252         // Used for addNotify check.
253         fComponentsAdjusted = true;
254     }
255
256     /**
257     * Shows or hides the component depending on the boolean flag b.
258     * @param b  if true, show the component; otherwise, hide the component.
259     * @see java.awt.Component#isVisible
260     */
261     @Override
262     public void setVisible(boolean b) {
263         if(b) {
264             Rectangle bounds = getParent().getBounds();
265             Rectangle abounds = getBounds();
266             setLocation(bounds.x + (bounds.width - abounds.width)/ 2,
267             bounds.y + (bounds.height - abounds.height)/2);
268         }
269         super.setVisible(b);
270     }
271
272     void AboutDialog_WindowClosing(java.awt.event.WindowEvent event) {
273         dispose();
274     }
275
276     void closeButton_Clicked(java.awt.event.ActionEvent event) {
277         //{{CONNECTION
278         // Clicked from okButton Hide the Dialog
279         dispose();
280         //}}
281     }
282     
283     ImageIcon refImage;
284
285     /**
286      * 選択された画像ファイルを表示する
287      * 基準画像ボタンがクリックされた時に、基準時刻フィールドに基準画像の作成日時を設定する。
288      * @param ev 
289      */
290     @SuppressWarnings("UseSpecificCatch")
291     public void imageView_Action(ActionEvent ev) {
292         String path = (new File(arg1_srcFolder.getText(), arg2_baseTimeImg.getText())).getPath();
293         
294         File timeFile = new File(path);
295         long lastModifyTime = timeFile.lastModified();
296         arg2_basetime.argField.setText(AdjustTime.dfjp.format(new Date(lastModifyTime)));
297         
298         int size_x = imageSPane.getWidth() - 8;
299         ImageIcon tmpIcon = new ImageIcon(path);
300         refImage = tmpIcon;
301         if (tmpIcon.getIconWidth() > size_x) {
302             refImage = new ImageIcon(tmpIcon.getImage().getScaledInstance(size_x, -1, Image.SCALE_DEFAULT));
303         }
304         imageLabel.setIcon(refImage);
305         repaint();
306     }
307
308     public void selectImage_Action(ActionEvent ev) {
309         JFileChooser fc;
310
311         File sdir = new File(arg1_srcFolder.getText());
312         System.out.println(sdir.getPath());
313         if (sdir.isDirectory()) {
314             fc = new JFileChooser(sdir);
315         }
316         else {
317             fc = new JFileChooser();
318         }
319
320         fc.addChoosableFileFilter(new ImageFilter());
321         fc.setAcceptAllFileFilterUsed(false);
322         fc.setFileView(new ImageFileView());
323         fc.setAccessory(new ImagePreview(fc));
324
325         //Show it. "選択"
326         int returnVal = fc.showDialog(this, i18n.getString("dialog.select"));
327         if (returnVal == JFileChooser.APPROVE_OPTION) {
328             File file = fc.getSelectedFile();
329             arg2_baseTimeImg.argField.setText(file.getName());
330         }
331         fc.setSelectedFile(null);
332     }
333 }