Normand Briere
2018-07-07 e416acb9b012b17d1efe49ad2199ea7132d874d1
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
package timeflow.app.ui.filter;
 
import timeflow.data.db.*;
import timeflow.data.db.filter.*;
import timeflow.model.*;
 
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
import java.util.Date;
 
import javax.swing.*;
 
// in theory it should be easy to refactor this to share code with
// NumberFilterPanel.
// but, i'm not sure how to do it in a way that doesn't make the code
// seem too complicated.
 
public class FilterDatePanel  extends FilterDefinitionPanel 
{
   BabyHistogram histogram;
   Field field;
   JTextField startEntry;
   JTextField endEntry;
   JCheckBox nullBox;
   Runnable action;
   SimpleDateFormat df=new SimpleDateFormat("MMM dd yyyy");
   
   public FilterDatePanel(final Field field, final Runnable action, final FilterControlPanel parent)
   {
       this.field=field;
       this.action=action;
       setLayout(new BorderLayout());
       setBorder(BorderFactory.createEmptyBorder(0,5,0,5));
       setBackground(Color.white);
       add(new FilterTitle(field, parent, false), BorderLayout.NORTH);
       
       Runnable fullAction=new Runnable()
       {
           public void run()
           {
               startEntry.setText(format(histogram.getLow()));
               endEntry.setText(format(histogram.getHigh()));
               action.run();
           }
       };
       
       histogram=new BabyHistogram(fullAction);
       
       add(histogram, BorderLayout.CENTER);
       
       JPanel bottomStuff=new JPanel();
       bottomStuff.setLayout(new GridLayout(2,1));
       add(bottomStuff, BorderLayout.SOUTH);
       
       JPanel lowHighPanel=new JPanel();
       bottomStuff.add(lowHighPanel);
       lowHighPanel.setBackground(Color.white);
       lowHighPanel.setLayout(new BorderLayout());
       Font small=parent.getModel().getDisplay().small();
       
       startEntry=new JTextField(7);
       startEntry.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               setLowFromText();
               action.run();
           }});
       lowHighPanel.add(startEntry, BorderLayout.WEST);
       startEntry.setFont(small);
       
       JLabel rangeLabel=new JLabel("to", JLabel.CENTER);
       rangeLabel.setForeground(Color.gray);
       rangeLabel.setFont(small);
       lowHighPanel.add(rangeLabel, BorderLayout.CENTER);
       endEntry=new JTextField(7);
       lowHighPanel.add(endEntry, BorderLayout.EAST);
       endEntry.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               setHighFromText();
               action.run();
           }});
       endEntry.setFont(small);
       
       nullBox=new JCheckBox("Include Missing Values");
       nullBox.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               action.run();
           }});
       bottomStuff.add(nullBox);
       bottomStuff.setBackground(Color.white);
       nullBox.setBackground(Color.white);
       nullBox.setForeground(Color.gray);
       nullBox.setFont(small);
       
   }
   
   String format(double x)
   {
       Date date=new Date((long)x);
       return df.format(date);
   }
   
   void setLowFromText()
   {
       try
       {
           long low=df.parse(startEntry.getText()).getTime();
           long high=(long)histogram.getHigh();
           if (low>high)
           {
               high=low;
               endEntry.setText(startEntry.getText());
           }
           histogram.setTrueRange(low,high);
           
       }
       catch (Exception e)
       {
           
       }
   }
   
   
   void setHighFromText()
   {
       try
       {
           long high=df.parse(endEntry.getText()).getTime();
           double low=(long)histogram.getLow();
           if (low>high)
           {
               low=high;
               startEntry.setText(endEntry.getText());
           }
           histogram.setTrueRange(low,high);
           
       }
       catch (Exception e)
       {
           
       }        
   }
 
   public void setData(double[] data)
   {
       histogram.setData(data);
       startEntry.setText(format(histogram.getLow()));
       endEntry.setText(format(histogram.getHigh()));
       repaint();
   }
   
   public Dimension getPreferredSize()
   {
       return new Dimension(200,160);
   }
 
   @Override
   public ActFilter defineFilter() {
       long low=(long)histogram.getLow();
       long high=(long)histogram.getHigh();
       boolean acceptNull=nullBox.isSelected();
       return new TimeIntervalFilter(low, high, acceptNull, field);
   }
 
   @Override
   public void clearFilter() {
       histogram.setRelRange(0, 1);
   }
}