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