View Javadoc

1   /*
2    * Created on 8-Aug-2005
3    */
4   package ca.spaz.cron.user;
5   
6   import java.awt.*;
7   import java.awt.event.*;
8   import java.net.*;
9   
10  import javax.swing.*;
11  import javax.swing.border.SoftBevelBorder;
12  
13  import org.apache.log4j.Logger;
14  
15  import ca.spaz.cron.datasource.sql.*;
16  import ca.spaz.cron.datasource.sql.USDAsr17.USDAImporter;
17  import ca.spaz.task.*;
18  import ca.spaz.wizard.WizardPanel;
19  
20  import com.aimedia.ui.*;
21  
22  public class USDAImportWizardPanel extends WizardPanel implements ICancellable, TaskListener {
23     
24     private boolean valid = true;
25     
26     private JPanel controlPanel;
27  
28     private JTextArea progressField;
29  
30     private JButton cancelButton;
31     
32     private JButton installButton;
33  
34     private Action cancelAction;
35     
36     private static final String EXPLANATION_TEXT = 
37             "It is recommended that you install the optional "
38           + " USDA sr17 food database. "
39           + "This takes a bit of time, but when it is done you"
40           + " will have a large collection of food to choose."
41           + " your daily diet from.\n\n";
42  
43     private Action importAction;
44  
45     private Action finishAction;
46  
47     private JPanel progressPanel;
48  
49     private TaskBar taskBar;
50     
51     
52     public USDAImportWizardPanel() {
53         this.setLayout(new BorderLayout(10,10));
54         this.setBorder(BorderFactory.createEmptyBorder(16,16,16,16));
55         this.add(getProgressPanel(), BorderLayout.CENTER);
56         this.add(getControlPanel(), BorderLayout.SOUTH);
57     }
58     
59     public String getWizardPanelTitle() {
60        return "Installation";
61     }
62  
63     public void commitChanges() {
64        
65     }
66  
67     public boolean isValid() {
68        return valid;
69     }
70  
71     private JButton getCancelButton() {
72        if (null == cancelButton) {
73           cancelButton = new JButton(getCancelAction());
74        }
75        return cancelButton;
76     }
77     
78  
79     private JButton getInstallButton() {
80        if (null == installButton) {
81           installButton = new JButton(getImportAction());
82        }
83        return installButton;
84     }
85     
86     private JTextArea getProgressField() {
87        if (null == progressField) {
88           progressField = new JTextArea();
89           progressField.setEditable(false);
90           progressField.setWrapStyleWord(true);
91           progressField.setLineWrap(true);
92           progressField.setText(EXPLANATION_TEXT);
93        }
94        return progressField;
95     }
96  
97     private JPanel getProgressPanel() {
98        if (null == progressPanel) {
99           progressPanel = new JPanel();
100          progressPanel.setBorder(BorderFactory.createEmptyBorder(16,16,16,16));
101          progressPanel.setLayout(new BorderLayout(4,4));
102          JScrollPane jsp = new JScrollPane(getProgressField());
103          jsp.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
104          jsp.setPreferredSize(new Dimension(200,200));
105          progressPanel.add(jsp, BorderLayout.CENTER);
106          progressPanel.add(getTaskBar(), BorderLayout.SOUTH);
107       }
108       return progressPanel;
109    }
110    
111    
112    private JPanel getControlPanel() {
113       if (null == controlPanel) {
114          controlPanel = new JPanel();
115          controlPanel.setBorder(BorderFactory.createEmptyBorder(16,16,16,16));
116          controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
117          controlPanel.add(Box.createGlue());
118          controlPanel.add(getInstallButton());
119          controlPanel.add(Box.createGlue());         
120       }
121       return controlPanel;
122    }
123    
124    private TaskBar getTaskBar() {
125       if (taskBar == null) {
126          taskBar = new TaskBar();
127          taskBar.addTaskListener(this);
128       }
129       return taskBar;
130    }
131    
132    /***
133     * @return
134     */
135    private Action getCancelAction() {
136       if (null == cancelAction) {
137          cancelAction = new CancelAction(this);
138       }
139       return cancelAction;
140    }
141 
142    private Action getImportAction() {
143       if (null == importAction) {
144          importAction = new ImportAction();
145       }
146       return importAction;
147    }
148 
149    
150    public void doCancel() {
151 
152    }
153    
154 
155    public void taskStarted(Task t) {
156       getImportAction().setEnabled(false);
157       getCancelAction().setEnabled(true);
158       valid = false;
159    }
160 
161    public void taskFinished(Task t) {
162       valid = true;
163       getImportAction().setEnabled(true);
164    }
165 
166    public void taskAborted(Task t) {
167       getCancelAction().setEnabled(false);
168       getImportAction().setEnabled(true);
169       valid = true;
170    }
171    
172    /***
173     * This action is fired to start the downloading process.
174     * 
175     * @author Chris Rose
176     */
177    public class ImportAction extends AbstractAction {
178       /***
179        * Logger for this class
180        */
181       private final Logger logger = Logger.getLogger(ImportAction.class);
182 
183       /***
184        * Construct a new ImportAction instance.
185        */
186       public ImportAction() {
187          super("Import USDA Food Database");
188          putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_I));
189          putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_I,
190                InputEvent.CTRL_DOWN_MASK));
191       }
192 
193       public void actionPerformed(ActionEvent e) {
194          if (logger.isDebugEnabled()) {
195             logger.debug("actionPerformed(ActionEvent) - Import USDA Here");
196          }
197          DocumentOutputStream docout = new DocumentOutputStream(
198                getProgressField().getDocument());
199          USDAImporter worker = new USDAImporter(ConnectionManager
200                .getInstance(SQLDatasource.FOOD_DB_ID).getConnection(), docout);
201          worker.setSourceURL(getFoodSourceURL());
202          getTaskBar().executeTask(worker); 
203       }
204 
205       private URL getFoodSourceURL() {
206          URL url = null;
207          try {
208             url = new URL("http://www.nal.usda.gov/fnic/foodcomp/Data/SR17/dnload/sr17.zip");
209          } catch (MalformedURLException e) {
210             logger.error("getFoodSourceURL()", e);
211          }
212          return url;
213       }
214 
215    }
216 }