001    /*
002     * $Id: TimeRangeSelection.java,v 1.6 2012/02/19 17:35:44 davep Exp $
003     *
004     * This file is part of McIDAS-V
005     *
006     * Copyright 2007-2012
007     * Space Science and Engineering Center (SSEC)
008     * University of Wisconsin - Madison
009     * 1225 W. Dayton Street, Madison, WI 53706, USA
010     * https://www.ssec.wisc.edu/mcidas
011     * 
012     * All Rights Reserved
013     * 
014     * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015     * some McIDAS-V source code is based on IDV and VisAD source code.  
016     * 
017     * McIDAS-V is free software; you can redistribute it and/or modify
018     * it under the terms of the GNU Lesser Public License as published by
019     * the Free Software Foundation; either version 3 of the License, or
020     * (at your option) any later version.
021     * 
022     * McIDAS-V is distributed in the hope that it will be useful,
023     * but WITHOUT ANY WARRANTY; without even the implied warranty of
024     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025     * GNU Lesser Public License for more details.
026     * 
027     * You should have received a copy of the GNU Lesser Public License
028     * along with this program.  If not, see http://www.gnu.org/licenses.
029     */
030    
031    package edu.wisc.ssec.mcidasv.data;
032    
033    import static javax.swing.GroupLayout.Alignment.BASELINE;
034    import static javax.swing.GroupLayout.Alignment.LEADING;
035    import static javax.swing.GroupLayout.Alignment.TRAILING;
036    import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;
037    import static javax.swing.LayoutStyle.ComponentPlacement.UNRELATED;
038    
039    import edu.wisc.ssec.mcidasv.Constants;
040    import edu.wisc.ssec.mcidasv.data.adde.sgp4.Time;
041    import edu.wisc.ssec.mcidasv.data.dateChooser.*;
042    
043    import java.awt.Dimension;
044    import java.rmi.RemoteException;
045    import java.util.Calendar;
046    import java.util.Date;
047    
048    import javax.swing.*;
049    
050    import org.slf4j.Logger;
051    import org.slf4j.LoggerFactory;
052    
053    import ucar.unidata.data.DataSelection;
054    import ucar.unidata.data.DataSelectionComponent;
055    import ucar.unidata.data.DataSourceImpl;
056    
057    import visad.VisADException;
058    
059    public class TimeRangeSelection extends DataSelectionComponent implements Constants {
060    
061          private static final Logger logger = LoggerFactory.getLogger(TimeRangeSelection.class);
062    
063          private DataSourceImpl dataSource;
064    
065          private JPanel beginDate;
066          private JPanel endTime;
067          private JPanel endDate;
068          private JTextField beginTimeFld;
069          private JTextField endTimeFld;
070          private String defaultBegTime = "00:00:00";
071          private String defaultEndTime = "23:59:59";
072          private Date defaultDay = null;
073    
074          private JDateChooser begDay = null;
075          private JDateChooser endDay = null;
076    
077          protected static final String PROP_BEGTIME = "BeginTime";
078          protected static final String PROP_ENDTIME = "EndTime";
079          protected static final String PROP_BTIME = "BTime";
080          protected static final String PROP_ETIME = "ETime";
081          protected static final String PROP_YEAR = "Year";
082          protected static final String PROP_MONTH = "Month";
083          protected static final String PROP_DAY = "Day";
084          protected static final String PROP_HOURS = "Hours";
085          protected static final String PROP_MINS = "Mins";
086          protected static final String PROP_SECS = "Secs";
087    
088          private JPanel timeRangeComp = new JPanel();
089    
090          public TimeRangeSelection(DataSourceImpl dataSource) 
091                  throws VisADException, RemoteException {
092              super("Time Range");
093              this.dataSource = dataSource;
094          }
095    
096        protected JComponent doMakeContents() {
097    
098            JLabel begLab = new JLabel("  Begin");
099            JLabel endLab = new JLabel("  End");
100            JLabel begTimeLab = new JLabel("      Time:");
101            JLabel endTimeLab = new JLabel("      Time:");
102            JLabel begDateLab = new JLabel("      Date:");
103            JLabel endDateLab = new JLabel("      Date:");
104            beginTimeFld = new JTextField(defaultBegTime, 8);
105            beginTimeFld.setMaximumSize(new Dimension(80, 40));
106            endTimeFld = new JTextField(defaultEndTime, 8);
107            endTimeFld.setMaximumSize(new Dimension(80, 40));
108            Calendar cal = Calendar.getInstance();
109            defaultDay = cal.getTime();
110            begDay = new JDateChooser(defaultDay);
111            begDay.setMaximumSize(new Dimension(140, 20));
112            endDay = new JDateChooser(defaultDay);
113            endDay.setMaximumSize(new Dimension(140, 20));
114    
115            GroupLayout layout = new GroupLayout(timeRangeComp);
116            timeRangeComp.setLayout(layout);
117            layout.setHorizontalGroup(
118                layout.createParallelGroup(LEADING)
119                    .addComponent(begLab)
120                    .addGroup(layout.createSequentialGroup()
121                        .addComponent(begTimeLab)
122                        .addGap(GAP_RELATED)
123                        .addComponent(beginTimeFld))
124                    .addGroup(layout.createSequentialGroup()
125                        .addComponent(begDateLab)
126                        .addGap(GAP_RELATED)
127                        .addComponent(begDay))
128                    .addComponent(endLab)
129                    .addGroup(layout.createSequentialGroup()
130                        .addComponent(endTimeLab)
131                        .addGap(GAP_RELATED)
132                        .addComponent(endTimeFld))
133                    .addGroup(layout.createSequentialGroup()
134                        .addComponent(endDateLab)
135                        .addGap(GAP_RELATED)
136                        .addComponent(endDay))
137             );
138    
139            layout.setVerticalGroup(
140                layout.createParallelGroup(LEADING)
141                .addGroup(layout.createSequentialGroup()
142                    .addComponent(begLab)
143                    .addGroup(layout.createParallelGroup(BASELINE)
144                        .addComponent(begTimeLab)
145                        .addComponent(beginTimeFld))
146                    .addPreferredGap(RELATED)
147                    .addGroup(layout.createParallelGroup(BASELINE)
148                        .addComponent(begDateLab)
149                        .addComponent(begDay))
150                    .addPreferredGap(UNRELATED)
151                    .addComponent(endLab)
152                    .addGroup(layout.createParallelGroup(BASELINE)
153                        .addComponent(endTimeLab)
154                        .addComponent(endTimeFld))
155                    .addPreferredGap(RELATED)
156                    .addGroup(layout.createParallelGroup(BASELINE)
157                        .addComponent(endDateLab)
158                        .addComponent(endDay)))
159             );
160    
161             return timeRangeComp;
162        }
163    
164        protected JComponent getTimeRangeComponent() {
165            return timeRangeComp;
166        }
167    
168        @Override public void applyToDataSelection(DataSelection dataSelection) {
169            //System.out.println("applyToDataSelection: dataSelection=" + dataSelection);
170    
171            if (dataSelection == null) {
172                dataSelection = new DataSelection(true);
173            }
174    
175            Date beg = begDay.getDate();
176            JCalendar cal = begDay.getJCalendar();
177            JDayChooser dayChooser = cal.getDayChooser();
178            int day = dayChooser.getDay();
179            JMonthChooser monthChooser = cal.getMonthChooser();
180            int month = monthChooser.getMonth() + 1;
181            JYearChooser yearChooser = cal.getYearChooser();
182            int year = yearChooser.getYear();
183    
184            int hours = 0;
185            int mins = 0;
186            double secs = 0.0;
187            String begTime = beginTimeFld.getText();
188            String[] timeStrings = begTime.split(":");
189            int num = timeStrings.length;
190            if (num > 0)
191                hours = (new Integer(timeStrings[0])).intValue();
192            if (num > 1)
193                mins = (new Integer(timeStrings[1])).intValue();
194            if (num > 2)
195                secs = (new Double(timeStrings[2] + ".0")).doubleValue();
196            if ((hours < 0) || (hours > 23)) hours = 0;
197            if ((mins < 0) || (mins > 59)) mins = 0;
198            if ((secs < 0.0) || (secs > 59.0)) secs = 0.0;
199    
200            Time bTime = new Time(year, month, day, hours, mins, secs);
201            double dVal = bTime.getJulianDate();
202            Double bigD = new Double(dVal);
203            String begTimeStr = bigD.toString();
204            dataSelection.putProperty(PROP_BEGTIME, bTime.getDateTimeStr());
205    
206            Integer intVal = new Integer(year);
207            dataSelection.putProperty(PROP_YEAR, intVal.toString());
208            intVal = new Integer(month);
209            dataSelection.putProperty(PROP_MONTH, intVal.toString());
210            intVal = new Integer(day);
211            dataSelection.putProperty(PROP_DAY, intVal.toString());
212            intVal = new Integer(hours);
213            dataSelection.putProperty(PROP_HOURS, intVal.toString());
214            intVal = new Integer(mins);
215            dataSelection.putProperty(PROP_MINS, intVal.toString());
216            Double doubleVal = new Double(secs);
217            dataSelection.putProperty(PROP_SECS, doubleVal.toString());
218    
219            Date end = endDay.getDate();
220            cal = endDay.getJCalendar();
221            dayChooser = cal.getDayChooser();
222            day = dayChooser.getDay();
223            monthChooser = cal.getMonthChooser();
224            month = monthChooser.getMonth() + 1;
225            yearChooser = cal.getYearChooser();
226            year = yearChooser.getYear();
227    
228            String endTime = endTimeFld.getText();
229            timeStrings = endTime.split(":");
230            num = timeStrings.length;
231            if (num > 0)
232                hours = (new Integer(timeStrings[0])).intValue();
233            if (num > 1)
234                mins = (new Integer(timeStrings[1])).intValue();
235            if (num > 2)
236                secs = (new Double(timeStrings[2] + ".0")).doubleValue();
237            if ((hours < 0) || (hours > 23)) hours = 0;
238            if ((mins < 0) || (mins > 59)) mins = 0;
239            if ((secs < 0.0) || (secs > 59.0)) secs = 0.0;
240    
241            Time eTime = new Time(year, month, day, hours, mins, secs);
242            dVal = eTime.getJulianDate();
243            bigD = new Double(dVal);
244            String endTimeStr = bigD.toString();
245    
246            dataSelection.putProperty(PROP_ENDTIME, eTime.getDateTimeStr());
247            dataSelection.putProperty(PROP_BTIME, begTimeStr);
248            dataSelection.putProperty(PROP_ETIME, endTimeStr);
249        }
250    }