File: MetricEditorOld.java

package info (click to toggle)
cronometer 0.9.9%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 8,788 kB
  • sloc: java: 17,382; xml: 909; makefile: 19
file content (183 lines) | stat: -rw-r--r-- 5,630 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Created on 19-Nov-2005
 */
package ca.spaz.cron.metrics;

import java.awt.event.*;
import java.util.Iterator;

import javax.swing.*;

import org.jfree.chart.ui.UIUtils;

import ca.spaz.cron.CRONOMETER;
import ca.spaz.cron.user.UserManager;
import ca.spaz.gui.DoubleField;
import ca.spaz.gui.ErrorReporter;
import ca.spaz.util.ImageFactory;

/**
 * A panel for editing a biomarker metric.  
 */
public class MetricEditorOld extends JPanel {
   private Biomarker biomarker;
   private JCheckBox toggle;
   private JLabel label;
   private DoubleField entryField;
   private String metricType;
   private Metric curMetric;
   private JButton saveBtn;
   private JButton deleteBtn;   
   private JButton plotBtn;

   private BiomarkerPanelOld bmp;
//
//   public MetricEditor(BiomarkerPanelOld bmp, String type) {
//      this.metricType = type;
//      this.bmp = bmp;
//   }   

   public MetricEditorOld(BiomarkerPanelOld bmp, Biomarker biomarker) {
      this.metricType = biomarker.getName();
      this.biomarker = biomarker;
      this.bmp = bmp;
   } 
   
   public JButton getSaveButton() {
      if (saveBtn == null) {
         saveBtn = new JButton("Save");
         saveBtn.setEnabled(false);         
         saveBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               saveValue();
            }
         });
      }
      return saveBtn;
   }  

   public JButton getDeleteButton() {
      if (deleteBtn == null) {
         deleteBtn = new JButton("Delete");
         deleteBtn.setEnabled(false);           
         deleteBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               Metric metric = getMetric();
               // Set the value to null so if we re-save it later it will be considered 
               // new and will be added
               metric.setValue((Number)null);
               UserManager.getCurrentUser().removeMetric(metric);
               saveBtn.setEnabled(false);
               deleteBtn.setEnabled(false);
               entryField.setValue("");
            }
         });
      }
      return deleteBtn;
   }   

   public JLabel getLabel() {
      if (label == null) {
         label = new JLabel(metricType);
      }
      return label;
   }    

   public JButton getPlotButton() {
      if (plotBtn == null) {
         ImageIcon icon = new ImageIcon(ImageFactory.getInstance().loadImage("/img/graph.gif"));
         plotBtn = new JButton("Chart", icon);
         plotBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               plotMetric();
            }
         });
      }
      return plotBtn;
   }   

   public void plotMetric() {
      try {
         MetricChart chart = new MetricChart(metricType);
         chart.setBiomarker(biomarker);
         chart.pack();
         chart.setIconImage(CRONOMETER.getWindowIcon());
         UIUtils.centerFrameOnScreen(chart);
         chart.setVisible(true);
      } catch (Exception e) {
         ErrorReporter.showError(e, this);
      }
   }

   public Metric getMetric() {
      if (curMetric == null) {
         curMetric = new Metric(metricType, bmp.getDate());
      }
      return curMetric;
   }

   public DoubleField getEntryField() {
      if (entryField == null) {         
         entryField = new DoubleField(0,8); 
         entryField.setColumns(8);
         entryField.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
               entryField.selectAll();   
               saveBtn.setEnabled(true);  
            }
            public void focusLost(FocusEvent e) {
               saveBtn.setEnabled(true);
            }
         });             
         entryField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               saveValue();
            }
         });  
      }
      return entryField;
   }

   public void setMetrics(java.util.List metrics) {
      saveBtn.setEnabled(false);
      deleteBtn.setEnabled(false); 
      entryField.setValue("");       
      // loop through array and find an appropriate metric to install
      Iterator iter = metrics.iterator();
      while (iter.hasNext()) {
         Metric m = (Metric)iter.next();
         if (m.getName().equals(metricType) && m.getValue() != null) {
            curMetric = m;
            if (m.getValue().doubleValue() == 0.0) {
               entryField.setValue("");
            }
            else {
               entryField.setValue(Double.parseDouble(m.getValue().toString()));
               saveBtn.setEnabled(false);
               deleteBtn.setEnabled(true); 
            }          
            return;
         }
      }
      curMetric = null;     
   }

   private void saveValue() {
      if (entryField.getValue() == 0.0) {
         ErrorReporter.showError("0.0 is not a valid biomarker value", bmp);
      }
      else {
         Metric metric = getMetric();
         boolean isNewEntry = metric.getValue() == null;
         metric.setValue(Double.toString(entryField.getValue()));          
         if (isNewEntry) {
            UserManager.getCurrentUser().addMetric(metric);
         }
         else {
            UserManager.getCurrentUser().updateMetric(metric);
         }
         saveBtn.setEnabled(false);
         deleteBtn.setEnabled(true);
      }
   }
}