| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 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 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
0 |
public class USDAImportDialog extends JDialog implements ICancellable, TaskListener { |
| 34 |
|
private static final String TITLE = "USDA sr17 Importer"; |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
0 |
private static final Logger logger = Logger |
| 40 |
0 |
.getLogger(USDAImportDialog.class); |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
public class ImportAction extends AbstractAction { |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
private final Logger logger = Logger.getLogger(ImportAction.class); |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 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 |
|
|
| 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 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
public class NextAction extends AbstractAction { |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
private final Logger logger = Logger.getLogger(NextAction.class); |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 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 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
public class FinishAction extends AbstractAction { |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
private final Logger logger = Logger.getLogger(FinishAction.class); |
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 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 |
0 |
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 |
|
|
| 200 |
|
|
| 201 |
|
public USDAImportDialog(Frame owner) { |
| 202 |
0 |
super(owner); |
| 203 |
0 |
initialize(); |
| 204 |
0 |
} |
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
|
private void initialize() { |
| 212 |
0 |
this.setSize(400, 300); |
| 213 |
0 |
this.setTitle(TITLE); |
| 214 |
0 |
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
| 215 |
0 |
this.setContentPane(getJContentPane()); |
| 216 |
0 |
} |
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
0 |
private javax.swing.JPanel getJContentPane() { |
| 224 |
0 |
if (jContentPane == null) { |
| 225 |
0 |
jContentPane = new javax.swing.JPanel(); |
| 226 |
0 |
jContentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); |
| 227 |
0 |
jContentPane.setLayout(new java.awt.BorderLayout()); |
| 228 |
0 |
jContentPane.add(getExplanation(), BorderLayout.CENTER); |
| 229 |
0 |
jContentPane.add(getControlPanel(), BorderLayout.SOUTH); |
| 230 |
|
} |
| 231 |
0 |
return jContentPane; |
| 232 |
|
} |
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
|
| 237 |
|
private JPanel getControlPanel() { |
| 238 |
0 |
if (null == controlPanel) { |
| 239 |
0 |
controlPanel = new JPanel(class="keyword">new GridLayout()); |
| 240 |
0 |
controlPanel.setBorder(BorderFactory.createEmptyBorder(14,4,4,4)); |
| 241 |
0 |
GridBagConstraints gc = new GridBagConstraints(); |
| 242 |
0 |
gc.gridx = 0; |
| 243 |
0 |
gc.gridy = 0; |
| 244 |
0 |
gc.anchor = GridBagConstraints.WEST; |
| 245 |
0 |
controlPanel.add(getCancelButton(), gc); |
| 246 |
0 |
gc.gridx = 1; |
| 247 |
0 |
gc.fill = GridBagConstraints.BOTH; |
| 248 |
0 |
controlPanel.add(Box.createHorizontalGlue(), gc); |
| 249 |
0 |
gc.gridx = 2; |
| 250 |
0 |
gc.fill = GridBagConstraints.NONE; |
| 251 |
0 |
gc.anchor = GridBagConstraints.EAST; |
| 252 |
0 |
controlPanel.add(getNextButton(), gc); |
| 253 |
|
} |
| 254 |
0 |
return controlPanel; |
| 255 |
|
} |
| 256 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
0 |
private JButton getNextButton() { |
| 261 |
0 |
if (null == advanceButton) { |
| 262 |
0 |
advanceButton = new JButton(); |
| 263 |
0 |
advanceButton.setAction(getNextAction()); |
| 264 |
|
} |
| 265 |
0 |
return advanceButton; |
| 266 |
|
} |
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
private Action getNextAction() { |
| 272 |
0 |
if (null == nextAction) { |
| 273 |
0 |
nextAction = new NextAction(); |
| 274 |
|
} |
| 275 |
0 |
return nextAction; |
| 276 |
|
} |
| 277 |
|
|
| 278 |
|
private Action getFinishAction() { |
| 279 |
0 |
if (null == finishAction) { |
| 280 |
0 |
finishAction = new FinishAction(); |
| 281 |
|
} |
| 282 |
0 |
return finishAction; |
| 283 |
|
} |
| 284 |
|
|
| 285 |
|
|
| 286 |
|
|
| 287 |
|
|
| 288 |
|
private JButton getCancelButton() { |
| 289 |
0 |
if (null == cancelButton) { |
| 290 |
0 |
cancelButton = new JButton(getCancelAction()); |
| 291 |
|
} |
| 292 |
0 |
return cancelButton; |
| 293 |
|
} |
| 294 |
|
|
| 295 |
|
|
| 296 |
|
|
| 297 |
|
|
| 298 |
|
private Action getCancelAction() { |
| 299 |
0 |
if (null == cancelAction) { |
| 300 |
0 |
cancelAction = new CancelAction(this); |
| 301 |
|
} |
| 302 |
0 |
return cancelAction; |
| 303 |
|
} |
| 304 |
|
|
| 305 |
0 |
private Action getImportAction() { |
| 306 |
0 |
if (null == importAction) { |
| 307 |
0 |
importAction = new ImportAction(); |
| 308 |
|
} |
| 309 |
0 |
return importAction; |
| 310 |
|
} |
| 311 |
|
|
| 312 |
0 |
private JPanel getExplanation() { |
| 313 |
0 |
if (null == explanation) { |
| 314 |
0 |
explanation = new JPanel(); |
| 315 |
0 |
explanation.setLayout(new RiverLayout()); |
| 316 |
0 |
explanation.add(new JLabel(EXPLANATION_TEXT)); |
| 317 |
|
} |
| 318 |
0 |
return explanation; |
| 319 |
|
} |
| 320 |
|
|
| 321 |
|
|
| 322 |
|
|
| 323 |
|
|
| 324 |
0 |
private JTextArea getProgressField() { |
| 325 |
0 |
if (null == progressField) { |
| 326 |
0 |
progressField = new JTextArea(); |
| 327 |
|
} |
| 328 |
0 |
return progressField; |
| 329 |
|
} |
| 330 |
|
|
| 331 |
|
|
| 332 |
|
|
| 333 |
|
|
| 334 |
0 |
private JPanel getProgressPanel() { |
| 335 |
0 |
if (null == progressPanel) { |
| 336 |
0 |
progressPanel = new JPanel(); |
| 337 |
0 |
progressPanel.setLayout(new BorderLayout(4,4)); |
| 338 |
0 |
JScrollPane jsp = new JScrollPane(getProgressField()); |
| 339 |
0 |
jsp.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED)); |
| 340 |
0 |
progressPanel.add(jsp, BorderLayout.CENTER); |
| 341 |
0 |
progressPanel.add(getTaskBar(), BorderLayout.SOUTH); |
| 342 |
|
} |
| 343 |
0 |
return progressPanel; |
| 344 |
|
} |
| 345 |
|
|
| 346 |
0 |
private TaskBar getTaskBar() { |
| 347 |
0 |
if (taskBar == null) { |
| 348 |
0 |
taskBar = new TaskBar(); |
| 349 |
0 |
taskBar.addTaskListener(this); |
| 350 |
|
} |
| 351 |
0 |
return taskBar; |
| 352 |
|
} |
| 353 |
|
|
| 354 |
|
|
| 355 |
|
|
| 356 |
|
|
| 357 |
|
|
| 358 |
|
|
| 359 |
|
|
| 360 |
|
public void doCancel() { |
| 361 |
0 |
this.setVisible(false); |
| 362 |
0 |
this.dispose(); |
| 363 |
0 |
} |
| 364 |
|
|
| 365 |
|
|
| 366 |
|
|
| 367 |
|
|
| 368 |
|
public void taskStarted(Task t) { |
| 369 |
0 |
getFinishAction().setEnabled(false); |
| 370 |
0 |
getImportAction().setEnabled(false); |
| 371 |
0 |
getCancelAction().setEnabled(false); |
| 372 |
0 |
getNextButton().setAction(getFinishAction()); |
| 373 |
0 |
} |
| 374 |
|
|
| 375 |
|
|
| 376 |
|
|
| 377 |
|
|
| 378 |
|
public void taskFinished(Task t) { |
| 379 |
0 |
getFinishAction().setEnabled(true); |
| 380 |
0 |
} |
| 381 |
|
|
| 382 |
|
|
| 383 |
|
|
| 384 |
|
|
| 385 |
|
public void taskAborted(Task t) { |
| 386 |
0 |
getCancelAction().setEnabled(true); |
| 387 |
0 |
} |
| 388 |
|
|
| 389 |
|
} |