Coverage report

  %line %branch
ca.spaz.cron.ui.FoodEditor
0% 
0% 

 1  
 /*
 2  
  * Created on Apr 9, 2005 by davidson
 3  
  */
 4  
 package ca.spaz.cron.ui;
 5  
 
 6  
 import java.awt.BorderLayout;
 7  
 import java.awt.event.ActionEvent;
 8  
 import java.util.Vector;
 9  
 
 10  
 import javax.swing.*;
 11  
 import javax.swing.event.*;
 12  
 
 13  
 import se.datadosen.component.RiverLayout;
 14  
 import ca.spaz.cron.CRONOMETER;
 15  
 import ca.spaz.cron.database.*;
 16  
 import ca.spaz.cron.datasource.*;
 17  
 
 18  
 import com.aimedia.ui.*;
 19  
 
 20  
 /**
 21  
  * An editor panel for a food item in the database. The editor must be able to
 22  
  * add / update / delete a food which includes all nutrient entries, and weights
 23  
  * 
 24  
  * @todo: Import/Export
 25  
  * 
 26  
  * @author davidson
 27  
  */
 28  0
 public class FoodEditor extends JPanel implements ICancellable, ISaveable {
 29  
 
 30  
    private CRONOMETER cwapp;
 31  
 
 32  
    private JDialog dialog;
 33  
 
 34  0
    private Food food;
 35  
 
 36  
    private JTextField name;
 37  
 
 38  
    private JComboBox source;
 39  
 
 40  
    private JComboBox group;
 41  
 
 42  0
    private MeasureWidget measure;
 43  
 
 44  
    private MeasureEditor measureEditor;
 45  
 
 46  
    private NutrientEditorTable macro, minerals, vitamins, aminoacids, lipids;
 47  
 
 48  
    private Action editGroupAction;
 49  
 
 50  
    private Action editSourceAction;
 51  
 
 52  
    private JButton saveButton;
 53  
 
 54  
    private JButton cancelButton;
 55  
 
 56  
    private JButton editGroupsButton;
 57  
 
 58  
    private JButton editSourceButton;
 59  
 
 60  
    private JButton editWeightsButton;
 61  
 
 62  
    private JPanel buttonPanel;
 63  
 
 64  
    private JPanel generalPanel;
 65  
 
 66  
    private JTabbedPane tabPanel;
 67  
 
 68  0
    public FoodEditor(CRONOMETER app, Food f) {
 69  0
       this.cwapp = app;
 70  0
       this.food = f;
 71  
 
 72  0
       initialize();
 73  
 
 74  0
       JDialog dialog = getDialog();
 75  0
       getMeasureSelector().focus();
 76  0
       dialog.setVisible(true);
 77  0
    }
 78  
 
 79  
    /**
 80  
     * @param app
 81  
     * @return
 82  
     */
 83  
    private JDialog getDialog() {
 84  0
       if (null == dialog) {
 85  0
          dialog = new JDialog(cwapp);
 86  0
          dialog.setTitle("Food Editor");
 87  0
          dialog.getContentPane().add(this);
 88  0
          dialog.pack();
 89  0
          dialog.setModal(true);
 90  0
          dialog.setLocationRelativeTo(cwapp);
 91  
       }
 92  0
       return dialog;
 93  
    }
 94  
 
 95  
    private void initialize() {
 96  0
       this.setLayout(new BorderLayout(4, 4));
 97  0
       this.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
 98  0
       this.add(getGeneralPanel(), BorderLayout.NORTH);
 99  
 
 100  0
       this.add(getTabPanel(), BorderLayout.CENTER);
 101  0
       this.add(getButtonPanel(), BorderLayout.SOUTH);
 102  0
    }
 103  
 
 104  
    public void doCancel() {
 105  0
       getDialog().dispose();
 106  0
    }
 107  
 
 108  0
    private void doEditGroups() {
 109  
 
 110  0
    }
 111  
 
 112  0
    private void doEditSource() {
 113  
 
 114  0
    }
 115  
 
 116  
    public void doSave() {
 117  0
       ILocalFoodDatasource ds = (ILocalFoodDatasource) getSourceCombo().getSelectedItem();
 118  0
       food.setDescription(getNameField().getText());
 119  0
       food.setFoodGroup((FoodGroup) getGroupCombo().getSelectedItem());
 120  0
       String sourceName = ds.toString();
 121  0
       food.setSource(sourceName);
 122  0
       food.setMeasures(getMeasureEditor().getMeasures());
 123  0
       ds.saveFood(food);
 124  0
       getDialog().dispose();
 125  0
    }
 126  
 
 127  
    private JPanel getButtonPanel() {
 128  0
       if (null == buttonPanel) {
 129  0
          buttonPanel = new JPanel();
 130  0
          buttonPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
 131  0
          buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
 132  0
          buttonPanel.add(Box.createHorizontalGlue());
 133  0
          buttonPanel.add(getCancelButton());
 134  0
          buttonPanel.add(Box.createHorizontalStrut(10));
 135  0
          buttonPanel.add(getSaveButton());
 136  0
          buttonPanel.add(Box.createHorizontalStrut(10));
 137  
       }
 138  0
       return buttonPanel;
 139  
    }
 140  
 
 141  
    /**
 142  
     * @return
 143  
     */
 144  
    private JButton getCancelButton() {
 145  0
       if (null == cancelButton) {
 146  0
          cancelButton = new JButton(class="keyword">new CancelAction(this));
 147  
       }
 148  0
       return cancelButton;
 149  
    }
 150  
 
 151  
    private JButton getSaveButton() {
 152  0
       if (null == saveButton) {
 153  0
          saveButton = new JButton(class="keyword">new SaveFoodAction(this));
 154  
       }
 155  0
       return saveButton;
 156  
    }
 157  
 
 158  
    private JComponent getEditGroupsButton() {
 159  0
       if (null == editGroupsButton) {
 160  0
          editGroupsButton = new JButton(getEditGroupsAction());
 161  
       }
 162  0
       return editGroupsButton;
 163  
    }
 164  
 
 165  
    private Action getEditGroupsAction() {
 166  0
       if (null == editGroupAction) {
 167  0
          editGroupAction = new EditGroupAction();
 168  
       }
 169  0
       return editGroupAction;
 170  
    }
 171  
 
 172  
    private JComponent getEditSourceButton() {
 173  0
       if (null == editSourceButton) {
 174  0
          editSourceButton = new JButton(getEditSourceAction());
 175  
       }
 176  0
       return editSourceButton;
 177  
    }
 178  
 
 179  
    /**
 180  
     * @return
 181  
     */
 182  
    private Action getEditSourceAction() {
 183  0
       if (null == editSourceAction) {
 184  0
          editSourceAction = new EditGroupAction();
 185  
       }
 186  0
       return editSourceAction;
 187  
    }
 188  
 
 189  
    private JPanel getGeneralPanel() {
 190  0
       if (null == generalPanel) {
 191  0
          final String TAB_HFILL = RiverLayout.TAB_STOP + " "
 192  
                + RiverLayout.HFILL;
 193  0
          generalPanel = new JPanel(class="keyword">new RiverLayout());
 194  0
          generalPanel.add(RiverLayout.LINE_BREAK, UIPartFactory
 195  
                .createFieldLabel("Name:"));
 196  0
          generalPanel.add(TAB_HFILL, getNameField());
 197  0
          generalPanel.add(RiverLayout.LINE_BREAK, UIPartFactory
 198  
                .createFieldLabel("Group:"));
 199  0
          generalPanel.add(TAB_HFILL, getGroupCombo());
 200  0
          generalPanel.add(RiverLayout.LINE_BREAK, UIPartFactory
 201  
                .createFieldLabel("Source:"));
 202  0
          generalPanel.add(TAB_HFILL, getSourceCombo());
 203  0
          generalPanel.add(RiverLayout.LINE_BREAK, UIPartFactory
 204  
                .createFieldLabel("Measure:"));
 205  0
          generalPanel.add(TAB_HFILL, getMeasureSelector());
 206  
       }
 207  0
       return generalPanel;
 208  
    }
 209  
 
 210  
    private JComboBox getSourceCombo() {
 211  0
       if (null == source) {
 212  0
          Vector allSources = new Vector();
 213  0
          allSources.addAll(Datasources.getInstance().getSources());
 214  0
          source = new JComboBox(allSources);
 215  0
          source.setEditable(true);
 216  0
          source.setSelectedItem(food.getDataSource());
 217  
       }
 218  0
       return source;
 219  
    }
 220  
 
 221  
    private JComboBox getGroupCombo() {
 222  0
       if (null == group) {
 223  0
          Vector allGroups = new Vector();
 224  0
          allGroups.addAll(Datasources.getInstance().getFoodGroups());
 225  0
          group = new JComboBox(allGroups);
 226  0
          for (int i=0; i<allGroups.size(); i++) {
 227  0
             FoodGroup fg = (FoodGroup)allGroups.get(i);
 228  0
             if (fg.equals(food.getFoodGroup())) {
 229  0
                group.setSelectedIndex(i);
 230  
             }
 231  
          }
 232  
          //group.setSelectedItem(food.getFoodGroup());
 233  0
          group.setEditable(true);
 234  
       }
 235  0
       return group;
 236  
    }
 237  
 
 238  
    private JTextField getNameField() {
 239  0
       if (null == name) {
 240  0
          name = new JTextField(40);
 241  0
          name.setText(food.getDescription());
 242  
       }
 243  0
       return name;
 244  
    }
 245  
 
 246  0
    private NutrientEditorTable getMacroNutrientsTable() {
 247  0
       if (macro == null) {
 248  0
          macro = new NutrientEditorTable(NutrientInfo.getMacroNutrients());
 249  0
          macro.setFood(food);
 250  0
          macro.setMultiplier(getMeasureSelector().getMultiplier());
 251  
       }
 252  0
       return macro;
 253  
    }
 254  
 
 255  0
    private NutrientEditorTable getMineralsTable() {
 256  0
       if (minerals == null) {
 257  0
          minerals = new NutrientEditorTable(NutrientInfo.getMinerals());
 258  0
          minerals.setFood(food);
 259  0
          minerals.setMultiplier(getMeasureSelector().getMultiplier());
 260  
       }
 261  0
       return minerals;
 262  
    }
 263  
    
 264  0
    private NutrientEditorTable getVitaminsTable() {
 265  0
       if (vitamins == null) {
 266  0
          vitamins = new NutrientEditorTable(NutrientInfo.getVitamins());
 267  0
          vitamins.setFood(food);
 268  0
          vitamins.setMultiplier(getMeasureSelector().getMultiplier());
 269  
       }
 270  0
       return vitamins;
 271  
    }
 272  
    
 273  0
    private NutrientEditorTable getAminoAcidsTable() {
 274  0
       if (aminoacids == null) {
 275  0
          aminoacids = new NutrientEditorTable(NutrientInfo.getAminoAcids());
 276  0
          aminoacids.setFood(food);
 277  0
          aminoacids.setMultiplier(getMeasureSelector().getMultiplier());
 278  
       }
 279  0
       return aminoacids;
 280  
    }
 281  
    
 282  0
    private NutrientEditorTable getLipidsTable() {
 283  0
       if (lipids == null) {
 284  0
          lipids = new NutrientEditorTable(NutrientInfo.getLipids());
 285  0
          lipids.setFood(food);
 286  0
          lipids.setMultiplier(getMeasureSelector().getMultiplier());
 287  
       }
 288  0
       return lipids;
 289  
    }
 290  
 
 291  
    /**
 292  
     * @todo implement food comments.
 293  
     * @return the food comment panel.
 294  
     */
 295  
    private JPanel getCommentsPanel() {
 296  0
       return new JPanel();
 297  
    }
 298  
    
 299  
    private JTabbedPane getTabPanel() {
 300  0
       if (tabPanel == null) {
 301  0
          tabPanel = new JTabbedPane();
 302  0
          tabPanel.addTab("Measures", getMeasureEditor());
 303  0
          tabPanel.addTab("General", getMacroNutrientsTable());
 304  0
          tabPanel.addTab("Minerals", getMineralsTable());
 305  0
          tabPanel.addTab("Vitamins", getVitaminsTable());
 306  0
          tabPanel.addTab("Amino Acids", getAminoAcidsTable());
 307  0
          tabPanel.addTab("Lipids", getLipidsTable());
 308  0
          tabPanel.addTab("Comments", getCommentsPanel());
 309  
       }
 310  0
       return tabPanel;
 311  
    }
 312  
 
 313  0
    private MeasureEditor getMeasureEditor() {
 314  0
       if (measureEditor == null) {
 315  0
          measureEditor = new MeasureEditor(food);
 316  0
          measureEditor.addChangeListener(new ChangeListener() {
 317  
             public void stateChanged(ChangeEvent e) {
 318  
                food.setMeasures(getMeasureEditor().getMeasures());
 319  
                getMeasureSelector().setFood(food);
 320  
             }
 321  
          });
 322  
       }
 323  0
       return measureEditor;
 324  
    }
 325  
 
 326  0
    private MeasureWidget getMeasureSelector() {
 327  0
       if (null == measure) {
 328  0
          measure = new MeasureWidget();
 329  0
          measure.setFood(food);
 330  0
          measure.addChangeListener(new ChangeListener() {
 331  
             public void stateChanged(ChangeEvent e) {
 332  
                getMacroNutrientsTable().setMultiplier(measure.getMultiplier());
 333  
                getMineralsTable().setMultiplier(measure.getMultiplier());
 334  
                getVitaminsTable().setMultiplier(measure.getMultiplier());
 335  
                getAminoAcidsTable().setMultiplier(measure.getMultiplier());
 336  
                getLipidsTable().setMultiplier(measure.getMultiplier());
 337  
             }
 338  
          });
 339  
       }
 340  0
       return measure;
 341  
    }
 342  
 
 343  
 
 344  
    private class SaveFoodAction extends SaveAction {
 345  
       public SaveFoodAction(ISaveable target) {
 346  
          super(target);
 347  
          //putValue(AbstractAction.SMALL_ICON, new ImageIcon(
 348  
                //ImageFactory.getInstance().loadImage("/img/Save24.gif")));
 349  
          putValue(AbstractAction.SHORT_DESCRIPTION,
 350  
                "Save the changes made to this food");
 351  
       }
 352  
    }
 353  
 
 354  
    private class EditGroupAction extends AbstractAction {
 355  
       public EditGroupAction() {
 356  
          super("Edit");
 357  
       }
 358  
 
 359  
       public void actionPerformed(ActionEvent e) {
 360  
          doEditGroups();
 361  
       }
 362  
    }
 363  
 
 364  
    private class EditSourceAction extends AbstractAction {
 365  
       public EditSourceAction() {
 366  
          super("Edit");
 367  
       }
 368  
 
 369  
       public void actionPerformed(ActionEvent e) {
 370  
          doEditSource();
 371  
       }
 372  
    }
 373  
 
 374  0
    public static void editFood(Food f) {
 375  0
       FoodEditor editor = new FoodEditor(CRONOMETER.getInstance(), f);
 376  0
    }
 377  
 
 378  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.