| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
package ca.spaz.cron.ui; |
| 5 |
|
|
| 6 |
|
import java.awt.event.*; |
| 7 |
|
import java.io.*; |
| 8 |
|
import java.util.*; |
| 9 |
|
|
| 10 |
|
import javax.swing.*; |
| 11 |
|
import javax.xml.parsers.*; |
| 12 |
|
|
| 13 |
|
import org.apache.log4j.Logger; |
| 14 |
|
import org.w3c.dom.*; |
| 15 |
|
|
| 16 |
|
import ca.spaz.cron.CRONOMETER; |
| 17 |
|
import ca.spaz.cron.database.*; |
| 18 |
|
import ca.spaz.cron.datasource.*; |
| 19 |
|
import ca.spaz.cron.datasource.sql.SQLFood; |
| 20 |
|
import ca.spaz.util.ImageFactory; |
| 21 |
|
|
| 22 |
|
public class FoodDBToolBar extends JToolBar { |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
private static final Logger logger = Logger.getLogger(FoodDBToolBar.class); |
| 27 |
|
|
| 28 |
|
private Food selectedFood; |
| 29 |
|
|
| 30 |
|
private JButton deleteButton; |
| 31 |
|
private JButton editButton; |
| 32 |
|
private JButton exportButton; |
| 33 |
|
private JButton addButton; |
| 34 |
|
private JButton importButton; |
| 35 |
|
|
| 36 |
|
public FoodDBToolBar() { |
| 37 |
|
setFloatable(false); |
| 38 |
|
add(getEditButton()); |
| 39 |
|
add(getExportButton()); |
| 40 |
|
add(getDeleteButton()); |
| 41 |
|
add(Box.createHorizontalGlue()); |
| 42 |
|
add(getAddButton()); |
| 43 |
|
add(getImportButton()); |
| 44 |
|
setSelectedFood(null); |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
private JButton getEditButton() { |
| 49 |
|
if (null == editButton) { |
| 50 |
|
ImageIcon icon = new ImageIcon(ImageFactory.getInstance().loadImage("/img/Edit24.gif")); |
| 51 |
|
editButton = new JButton(icon); |
| 52 |
|
editButton.setToolTipText("Edit Food"); |
| 53 |
|
editButton.addActionListener(new ActionListener() { |
| 54 |
|
public void actionPerformed(ActionEvent e) { |
| 55 |
|
doEditFood(); |
| 56 |
|
} |
| 57 |
|
}); |
| 58 |
|
} |
| 59 |
|
return editButton; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
private void doEditFood() { |
| 63 |
|
if (selectedFood != null) { |
| 64 |
|
FoodEditor.editFood(selectedFood); |
| 65 |
|
} |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
private JButton getDeleteButton() { |
| 71 |
|
if (null == deleteButton) { |
| 72 |
|
deleteButton = new JButton(class="keyword">new ImageIcon(ImageFactory.getInstance().loadImage("/img/Delete24.gif"))); |
| 73 |
|
deleteButton.setToolTipText("Delete Food"); |
| 74 |
|
deleteButton.addActionListener(new ActionListener() { |
| 75 |
|
public void actionPerformed(ActionEvent e) { |
| 76 |
|
doDeleteFood(); |
| 77 |
|
} |
| 78 |
|
}); |
| 79 |
|
} |
| 80 |
|
return deleteButton; |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
private void doDeleteFood() { |
| 90 |
|
assert (getSelectedFood() != null); |
| 91 |
|
int rc = JOptionPane.showConfirmDialog(this, |
| 92 |
|
"Are you sure you want to delete '"+ |
| 93 |
|
getSelectedFood().getDescription() + "'?", |
| 94 |
|
"Delete Food?", JOptionPane.YES_NO_OPTION); |
| 95 |
|
if (rc == JOptionPane.YES_OPTION) { |
| 96 |
|
if (getSelectedFood().getDataSource() instanceof AbstractMutableFoodDatasource) { |
| 97 |
|
((AbstractMutableFoodDatasource)getSelectedFood().getDataSource()).removeFood(getSelectedFood()); |
| 98 |
|
CRONOMETER.getInstance().getSearchPanel().doDBSearch(); |
| 99 |
|
CRONOMETER.getInstance().getDailySummary().notifyObservers(); |
| 100 |
|
} |
| 101 |
|
} |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
private JButton getImportButton() { |
| 108 |
|
if (null == importButton) { |
| 109 |
|
importButton = new JButton(class="keyword">new ImageIcon(ImageFactory.getInstance().loadImage("/img/Import24.gif"))); |
| 110 |
|
importButton.setToolTipText("Import Food"); |
| 111 |
|
importButton.addActionListener(new ActionListener() { |
| 112 |
|
public void actionPerformed(ActionEvent e) { |
| 113 |
|
doImportFood(); |
| 114 |
|
} |
| 115 |
|
}); |
| 116 |
|
} |
| 117 |
|
return importButton; |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
private void doImportFood() { |
| 121 |
|
JFileChooser fd = new JFileChooser(); |
| 122 |
|
if (fd.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { |
| 123 |
|
File f = fd.getSelectedFile(); |
| 124 |
|
if (f != null) { |
| 125 |
|
importFood(f); |
| 126 |
|
} |
| 127 |
|
} |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
private void importFood(File f) { |
| 131 |
|
Food food = Datasources.getInstance().getMutableDataSource().createNewFood(); |
| 132 |
|
try { |
| 133 |
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 134 |
|
dbf.setNamespaceAware(true); |
| 135 |
|
DocumentBuilder db = dbf.newDocumentBuilder(); |
| 136 |
|
Document d = db.parse(f); |
| 137 |
|
Element e = d.getDocumentElement(); |
| 138 |
|
food.setDescription(e.getAttribute("name")); |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
List measures = new ArrayList(); |
| 143 |
|
measures.add(Measure.GRAM); |
| 144 |
|
NodeList nl = e.getElementsByTagName("measure"); |
| 145 |
|
for (int i=0; i<nl.getLength(); i++) { |
| 146 |
|
Element m = (Element)nl.item(i); |
| 147 |
|
Measure measure = new Measure(); |
| 148 |
|
measure.setDescription(m.getAttribute("name")); |
| 149 |
|
measure.setAmount(Double.parseDouble(m.getAttribute("amount"))); |
| 150 |
|
measure.setGrams(Double.parseDouble(m.getAttribute("grams"))); |
| 151 |
|
measures.add(measure); |
| 152 |
|
} |
| 153 |
|
food.setMeasures(measures); |
| 154 |
|
|
| 155 |
|
nl = e.getElementsByTagName("nutrient"); |
| 156 |
|
for (int i=0; i<nl.getLength(); i++) { |
| 157 |
|
Element n = (Element)nl.item(i); |
| 158 |
|
NutrientInfo ni = NutrientInfo.getByName(n.getAttribute("name")); |
| 159 |
|
if (ni != null) { |
| 160 |
|
food.setNutrientAmount(ni, |
| 161 |
|
Double.parseDouble(n.getAttribute("amount"))); |
| 162 |
|
} |
| 163 |
|
} |
| 164 |
|
Datasources.getInstance().getMutableDataSource().saveFood(food); |
| 165 |
|
} catch (Exception e) { |
| 166 |
|
e.printStackTrace(); |
| 167 |
|
} |
| 168 |
|
} |
| 169 |
|
|
| 170 |
|
private JButton getExportButton() { |
| 171 |
|
if (null == exportButton) { |
| 172 |
|
exportButton = new JButton(class="keyword">new ImageIcon(ImageFactory.getInstance().loadImage("/img/Export24.gif"))); |
| 173 |
|
exportButton.setToolTipText("Export Food"); |
| 174 |
|
exportButton.addActionListener(new ActionListener() { |
| 175 |
0 |
public void actionPerformed(ActionEvent e) { |
| 176 |
0 |
doExportFood(); |
| 177 |
0 |
} |
| 178 |
|
}); |
| 179 |
|
} |
| 180 |
|
return exportButton; |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
private void doExportFood() { |
| 184 |
|
assert(getSelectedFood() != null); |
| 185 |
|
|
| 186 |
|
JFileChooser fd = new JFileChooser(); |
| 187 |
|
fd.setSelectedFile(new File(getSelectedFood().getDescription()+".xml")); |
| 188 |
|
if (fd.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { |
| 189 |
|
File f = fd.getSelectedFile(); |
| 190 |
|
if (f != null) { |
| 191 |
|
try { |
| 192 |
|
PrintStream ps = new PrintStream( |
| 193 |
|
new BufferedOutputStream(class="keyword">new FileOutputStream(f))); |
| 194 |
|
((SQLFood)getSelectedFood()).writeXML(ps); |
| 195 |
|
((SQLFood)getSelectedFood()).writeXML(System.out); |
| 196 |
|
ps.close(); |
| 197 |
|
} catch (IOException ie) { |
| 198 |
|
logger.error(ie); |
| 199 |
|
} |
| 200 |
|
} |
| 201 |
|
} |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
private JButton getAddButton() { |
| 207 |
|
if (null == addButton) { |
| 208 |
|
addButton = new JButton(class="keyword">new ImageIcon(ImageFactory.getInstance().loadImage("/img/Add24.gif"))); |
| 209 |
|
addButton.setToolTipText("Create New Food"); |
| 210 |
|
addButton.addActionListener(new ActionListener() { |
| 211 |
|
public void actionPerformed(ActionEvent e) { |
| 212 |
|
doAddNewFood(); |
| 213 |
|
} |
| 214 |
|
}); |
| 215 |
|
} |
| 216 |
|
return addButton; |
| 217 |
|
} |
| 218 |
|
|
| 219 |
|
private void doAddNewFood() { |
| 220 |
|
Food f = Datasources.getInstance().getMutableDataSource().createNewFood(); |
| 221 |
|
FoodEditor.editFood(f); |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
public void setSelectedFood(Food f) { |
| 225 |
|
selectedFood = f; |
| 226 |
|
getEditButton().setEnabled(f != null); |
| 227 |
|
getDeleteButton().setEnabled(f != null); |
| 228 |
|
getExportButton().setEnabled(f != null); |
| 229 |
|
} |
| 230 |
|
|
| 231 |
|
public Food getSelectedFood() { |
| 232 |
|
return selectedFood; |
| 233 |
|
} |
| 234 |
|
|
| 235 |
|
} |