| 1 |
|
|
| 2 |
|
|
| 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 |
0 |
public class USDAImportWizardPanel extends WizardPanel implements ICancellable, TaskListener { |
| 23 |
|
|
| 24 |
0 |
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 |
0 |
public USDAImportWizardPanel() { |
| 53 |
0 |
this.setLayout(new BorderLayout(10,10)); |
| 54 |
0 |
this.setBorder(BorderFactory.createEmptyBorder(16,16,16,16)); |
| 55 |
0 |
this.add(getProgressPanel(), BorderLayout.CENTER); |
| 56 |
0 |
this.add(getControlPanel(), BorderLayout.SOUTH); |
| 57 |
0 |
} |
| 58 |
|
|
| 59 |
|
public String getWizardPanelTitle() { |
| 60 |
0 |
return "Installation"; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
public void commitChanges() { |
| 64 |
|
|
| 65 |
0 |
} |
| 66 |
|
|
| 67 |
|
public boolean isValid() { |
| 68 |
0 |
return valid; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
private JButton getCancelButton() { |
| 72 |
0 |
if (null == cancelButton) { |
| 73 |
0 |
cancelButton = new JButton(getCancelAction()); |
| 74 |
|
} |
| 75 |
0 |
return cancelButton; |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
private JButton getInstallButton() { |
| 80 |
0 |
if (null == installButton) { |
| 81 |
0 |
installButton = new JButton(getImportAction()); |
| 82 |
|
} |
| 83 |
0 |
return installButton; |
| 84 |
|
} |
| 85 |
|
|
| 86 |
0 |
private JTextArea getProgressField() { |
| 87 |
0 |
if (null == progressField) { |
| 88 |
0 |
progressField = new JTextArea(); |
| 89 |
0 |
progressField.setEditable(false); |
| 90 |
0 |
progressField.setWrapStyleWord(true); |
| 91 |
0 |
progressField.setLineWrap(true); |
| 92 |
0 |
progressField.setText(EXPLANATION_TEXT); |
| 93 |
|
} |
| 94 |
0 |
return progressField; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
private JPanel getProgressPanel() { |
| 98 |
0 |
if (null == progressPanel) { |
| 99 |
0 |
progressPanel = new JPanel(); |
| 100 |
0 |
progressPanel.setBorder(BorderFactory.createEmptyBorder(16,16,16,16)); |
| 101 |
0 |
progressPanel.setLayout(new BorderLayout(4,4)); |
| 102 |
0 |
JScrollPane jsp = new JScrollPane(getProgressField()); |
| 103 |
0 |
jsp.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED)); |
| 104 |
0 |
jsp.setPreferredSize(new Dimension(200,200)); |
| 105 |
0 |
progressPanel.add(jsp, BorderLayout.CENTER); |
| 106 |
0 |
progressPanel.add(getTaskBar(), BorderLayout.SOUTH); |
| 107 |
|
} |
| 108 |
0 |
return progressPanel; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
private JPanel getControlPanel() { |
| 113 |
0 |
if (null == controlPanel) { |
| 114 |
0 |
controlPanel = new JPanel(); |
| 115 |
0 |
controlPanel.setBorder(BorderFactory.createEmptyBorder(16,16,16,16)); |
| 116 |
0 |
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS)); |
| 117 |
0 |
controlPanel.add(Box.createGlue()); |
| 118 |
0 |
controlPanel.add(getInstallButton()); |
| 119 |
0 |
controlPanel.add(Box.createGlue()); |
| 120 |
|
} |
| 121 |
0 |
return controlPanel; |
| 122 |
|
} |
| 123 |
|
|
| 124 |
0 |
private TaskBar getTaskBar() { |
| 125 |
0 |
if (taskBar == null) { |
| 126 |
0 |
taskBar = new TaskBar(); |
| 127 |
0 |
taskBar.addTaskListener(this); |
| 128 |
|
} |
| 129 |
0 |
return taskBar; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
private Action getCancelAction() { |
| 136 |
0 |
if (null == cancelAction) { |
| 137 |
0 |
cancelAction = new CancelAction(this); |
| 138 |
|
} |
| 139 |
0 |
return cancelAction; |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
private Action getImportAction() { |
| 143 |
0 |
if (null == importAction) { |
| 144 |
0 |
importAction = new ImportAction(); |
| 145 |
|
} |
| 146 |
0 |
return importAction; |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
|
| 150 |
|
public void doCancel() { |
| 151 |
|
|
| 152 |
0 |
} |
| 153 |
|
|
| 154 |
|
|
| 155 |
|
public void taskStarted(Task t) { |
| 156 |
0 |
getImportAction().setEnabled(false); |
| 157 |
0 |
getCancelAction().setEnabled(true); |
| 158 |
0 |
valid = false; |
| 159 |
0 |
} |
| 160 |
|
|
| 161 |
|
public void taskFinished(Task t) { |
| 162 |
0 |
valid = true; |
| 163 |
0 |
getImportAction().setEnabled(true); |
| 164 |
0 |
} |
| 165 |
|
|
| 166 |
|
public void taskAborted(Task t) { |
| 167 |
0 |
getCancelAction().setEnabled(false); |
| 168 |
0 |
getImportAction().setEnabled(true); |
| 169 |
0 |
valid = true; |
| 170 |
0 |
} |
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
public class ImportAction extends AbstractAction { |
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
0 |
private final Logger logger = Logger.getLogger(ImportAction.class); |
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 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 |
|
} |