1 /********************************************************************************
2 * ******************************************************************************
3 * Copyright (c) 2005 Chris Rose and AIMedia All rights reserved.
4 * USDAImportDialog and the accompanying materials are made available under the
5 * terms of the Common Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/cpl-v10.html Contributors:
7 * Chris Rose
8 ******************************************************************************/
9 package ca.spaz.cron.ui;
10
11 import java.awt.*;
12 import java.awt.event.*;
13 import java.net.*;
14
15 import javax.swing.*;
16 import javax.swing.border.SoftBevelBorder;
17
18 import org.apache.log4j.Logger;
19
20 import se.datadosen.component.RiverLayout;
21 import ca.spaz.cron.datasource.sql.*;
22 import ca.spaz.cron.datasource.sql.USDAsr17.USDAImporter;
23 import ca.spaz.task.*;
24
25 import com.aimedia.ui.*;
26
27 /***
28 * This dialog encapsulates the behaviour involved in downloading and installing
29 * the USDA NR17 food info database.
30 *
31 * @author Chris Rose
32 */
33 public class USDAImportDialog extends JDialog implements ICancellable, TaskListener {
34 private static final String TITLE = "USDA sr17 Importer";
35
36 /***
37 * Logger for this class
38 */
39 private static final Logger logger = Logger
40 .getLogger(USDAImportDialog.class);
41
42 /***
43 * This action is fired to start the downloading process.
44 *
45 * @author Chris Rose
46 */
47 public class ImportAction extends AbstractAction {
48 /***
49 * Logger for this class
50 */
51 private final Logger logger = Logger.getLogger(ImportAction.class);
52
53 /***
54 * Construct a new ImportAction instance.
55 */
56 public ImportAction() {
57 super("Import");
58 putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_I));
59 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_I,
60 InputEvent.CTRL_DOWN_MASK));
61 }
62
63
64
65
66
67
68 public void actionPerformed(ActionEvent e) {
69 if (logger.isDebugEnabled()) {
70 logger.debug("actionPerformed(ActionEvent) - Import USDA Here");
71 }
72 DocumentOutputStream docout = new DocumentOutputStream(
73 getProgressField().getDocument());
74 USDAImporter worker = new USDAImporter(ConnectionManager
75 .getInstance(SQLDatasource.FOOD_DB_ID).getConnection(), docout);
76 worker.setSourceURL(getFoodSourceURL());
77 getTaskBar().executeTask(worker);
78
79
80
81
82
83 }
84
85 /***
86 * @return
87 */
88 private URL getFoodSourceURL() {
89 URL url = null;
90 try {
91 url = new URL("http://www.nal.usda.gov/fnic/foodcomp/Data/SR17/dnload/sr17.zip");
92 } catch (MalformedURLException e) {
93 logger.error("getFoodSourceURL()", e);
94 }
95 return url;
96 }
97
98 }
99
100 /***
101 * This action is to be fired when moving through the dialog.
102 *
103 * @author Chris Rose
104 */
105 public class NextAction extends AbstractAction {
106 /***
107 * Logger for this class
108 */
109 private final Logger logger = Logger.getLogger(NextAction.class);
110
111 /***
112 * Construct a new instance of NextAction
113 */
114 public NextAction() {
115 super("Next");
116 putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_N));
117 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N,
118 InputEvent.CTRL_DOWN_MASK));
119 }
120
121
122
123
124
125
126 public void actionPerformed(ActionEvent e) {
127 getNextButton().setAction(getImportAction());
128 getJContentPane().remove(getExplanation());
129 getJContentPane().add(getProgressPanel(), BorderLayout.CENTER);
130 }
131
132 }
133
134 /***
135 * This action is to be triggered when the import process is complete.
136 *
137 * @author Chris Rose
138 */
139 public class FinishAction extends AbstractAction {
140 /***
141 * Logger for this class
142 */
143 private final Logger logger = Logger.getLogger(FinishAction.class);
144
145 /***
146 * Construct a new FinishAction instance.
147 */
148 public FinishAction() {
149 super("Finish");
150 putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_F));
151 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F,
152 InputEvent.CTRL_DOWN_MASK));
153 }
154
155
156
157
158
159
160 public void actionPerformed(ActionEvent e) {
161 doCancel();
162 }
163
164 }
165
166 private javax.swing.JPanel jContentPane = null;
167
168 private JPanel controlPanel;
169
170 private JTextArea progressField;
171
172 private JButton cancelButton;
173
174 private Action cancelAction;
175
176 private JPanel explanation;
177
178 private static final String EXPLANATION_TEXT = "<html>The next part of this dialog will download<br>"
179 + "the USDA food database to your local machine.<br>"
180 + "This takes a bit of time, but when it is done you<br>"
181 + "will have a large collection of food to choose<br>"
182 + "your daily diet from.<br>"
183 + "<br>"
184 + "Please, press \"Next\" to continue...</html>";
185
186 private JButton advanceButton;
187
188 private Action nextAction;
189
190 private Action importAction;
191
192 private Action finishAction;
193
194 private JPanel progressPanel;
195
196 private TaskBar taskBar;
197
198 /***
199 * This is the default constructor
200 */
201 public USDAImportDialog(Frame owner) {
202 super(owner);
203 initialize();
204 }
205
206 /***
207 * This method initializes this
208 *
209 * @return void
210 */
211 private void initialize() {
212 this.setSize(400, 300);
213 this.setTitle(TITLE);
214 this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
215 this.setContentPane(getJContentPane());
216 }
217
218 /***
219 * This method initializes jContentPane
220 *
221 * @return javax.swing.JPanel
222 */
223 private javax.swing.JPanel getJContentPane() {
224 if (jContentPane == null) {
225 jContentPane = new javax.swing.JPanel();
226 jContentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
227 jContentPane.setLayout(new java.awt.BorderLayout());
228 jContentPane.add(getExplanation(), BorderLayout.CENTER);
229 jContentPane.add(getControlPanel(), BorderLayout.SOUTH);
230 }
231 return jContentPane;
232 }
233
234 /***
235 * @return
236 */
237 private JPanel getControlPanel() {
238 if (null == controlPanel) {
239 controlPanel = new JPanel(new GridLayout());
240 controlPanel.setBorder(BorderFactory.createEmptyBorder(14,4,4,4));
241 GridBagConstraints gc = new GridBagConstraints();
242 gc.gridx = 0;
243 gc.gridy = 0;
244 gc.anchor = GridBagConstraints.WEST;
245 controlPanel.add(getCancelButton(), gc);
246 gc.gridx = 1;
247 gc.fill = GridBagConstraints.BOTH;
248 controlPanel.add(Box.createHorizontalGlue(), gc);
249 gc.gridx = 2;
250 gc.fill = GridBagConstraints.NONE;
251 gc.anchor = GridBagConstraints.EAST;
252 controlPanel.add(getNextButton(), gc);
253 }
254 return controlPanel;
255 }
256
257 /***
258 * @return
259 */
260 private JButton getNextButton() {
261 if (null == advanceButton) {
262 advanceButton = new JButton();
263 advanceButton.setAction(getNextAction());
264 }
265 return advanceButton;
266 }
267
268 /***
269 * @return
270 */
271 private Action getNextAction() {
272 if (null == nextAction) {
273 nextAction = new NextAction();
274 }
275 return nextAction;
276 }
277
278 private Action getFinishAction() {
279 if (null == finishAction) {
280 finishAction = new FinishAction();
281 }
282 return finishAction;
283 }
284
285 /***
286 * @return
287 */
288 private JButton getCancelButton() {
289 if (null == cancelButton) {
290 cancelButton = new JButton(getCancelAction());
291 }
292 return cancelButton;
293 }
294
295 /***
296 * @return
297 */
298 private Action getCancelAction() {
299 if (null == cancelAction) {
300 cancelAction = new CancelAction(this);
301 }
302 return cancelAction;
303 }
304
305 private Action getImportAction() {
306 if (null == importAction) {
307 importAction = new ImportAction();
308 }
309 return importAction;
310 }
311
312 private JPanel getExplanation() {
313 if (null == explanation) {
314 explanation = new JPanel();
315 explanation.setLayout(new RiverLayout());
316 explanation.add(new JLabel(EXPLANATION_TEXT));
317 }
318 return explanation;
319 }
320
321 /***
322 * @return
323 */
324 private JTextArea getProgressField() {
325 if (null == progressField) {
326 progressField = new JTextArea();
327 }
328 return progressField;
329 }
330
331 /***
332 * @return
333 */
334 private JPanel getProgressPanel() {
335 if (null == progressPanel) {
336 progressPanel = new JPanel();
337 progressPanel.setLayout(new BorderLayout(4,4));
338 JScrollPane jsp = new JScrollPane(getProgressField());
339 jsp.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
340 progressPanel.add(jsp, BorderLayout.CENTER);
341 progressPanel.add(getTaskBar(), BorderLayout.SOUTH);
342 }
343 return progressPanel;
344 }
345
346 private TaskBar getTaskBar() {
347 if (taskBar == null) {
348 taskBar = new TaskBar();
349 taskBar.addTaskListener(this);
350 }
351 return taskBar;
352 }
353
354
355
356
357
358
359
360 public void doCancel() {
361 this.setVisible(false);
362 this.dispose();
363 }
364
365
366
367
368 public void taskStarted(Task t) {
369 getFinishAction().setEnabled(false);
370 getImportAction().setEnabled(false);
371 getCancelAction().setEnabled(false);
372 getNextButton().setAction(getFinishAction());
373 }
374
375
376
377
378 public void taskFinished(Task t) {
379 getFinishAction().setEnabled(true);
380 }
381
382
383
384
385 public void taskAborted(Task t) {
386 getCancelAction().setEnabled(true);
387 }
388
389 }