001/*
002 * $Id: TimeRangeSelection.java,v 1.4 2011/03/24 16:06:32 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2011
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
031package edu.wisc.ssec.mcidasv.data;
032
033import edu.wisc.ssec.mcidasv.Constants;
034import edu.wisc.ssec.mcidasv.data.adde.sgp4.Time;
035import edu.wisc.ssec.mcidasv.data.dateChooser.*;
036
037import java.awt.Component;
038import java.awt.Insets;
039import java.awt.event.ActionEvent;
040import java.awt.event.ActionListener;
041import java.rmi.RemoteException;
042import java.text.SimpleDateFormat;
043import java.util.ArrayList;
044import java.util.Calendar;
045import java.util.Date;
046import java.util.List;
047
048import javax.swing.*;
049
050import org.slf4j.Logger;
051import org.slf4j.LoggerFactory;
052
053import ucar.unidata.data.DataChoice;
054import ucar.unidata.data.DataSelection;
055import ucar.unidata.data.DataSelectionComponent;
056import ucar.unidata.data.DataSourceImpl;
057import ucar.unidata.util.GuiUtils;
058
059import visad.VisADException;
060
061public class TimeRangeSelection extends DataSelectionComponent implements Constants {
062
063      private static final Logger logger = LoggerFactory.getLogger(TimeRangeSelection.class);
064
065      private DataSourceImpl dataSource;
066
067      /** The spacing used in the grid layout */
068      protected static final int GRID_SPACING = 3;
069
070      /** Used by derived classes when they do a GuiUtils.doLayout */
071      protected static final Insets GRID_INSETS = new Insets(GRID_SPACING,
072                                                      GRID_SPACING,
073                                                      GRID_SPACING,
074                                                      GRID_SPACING);
075      private double latitude;
076      private double longitude;
077      private double altitude;
078      private JPanel locationPanel;
079      private JPanel latLonAltPanel;
080      private JPanel beginTime;
081      private JPanel beginDate;
082      private JPanel endTime;
083      private JPanel endDate;
084      private JTextField beginTimeFld;
085      private JTextField endTimeFld;
086      private String defaultBegTime = "00:00:00";
087      private String defaultEndTime = "23:59:59";
088      private Date defaultDay = null;
089
090      private JDateChooser begDay = null;
091      private JDateChooser endDay = null;
092
093      protected static final String PROP_BEGTIME = "BeginTime";
094      protected static final String PROP_ENDTIME = "EndTime";
095      protected static final String PROP_BTIME = "BTime";
096      protected static final String PROP_ETIME = "ETime";
097      protected static final String PROP_YEAR = "Year";
098      protected static final String PROP_MONTH = "Month";
099      protected static final String PROP_DAY = "Day";
100      protected static final String PROP_HOURS = "Hours";
101      protected static final String PROP_MINS = "Mins";
102      protected static final String PROP_SECS = "Secs";
103
104      private JPanel timeRangeComp = new JPanel();
105      private JComboBox locationComboBox;
106
107      public TimeRangeSelection(DataSourceImpl dataSource) 
108              throws VisADException, RemoteException {
109          super("Time Range");
110          this.dataSource = dataSource;
111      }
112
113    protected JComponent doMakeContents() {
114
115         Insets  dfltGridSpacing = new Insets(4, 0, 4, 0);
116         String  dfltLblSpacing  = " ";
117         Calendar cal = Calendar.getInstance();
118         SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy");
119         defaultDay = cal.getTime();
120         List allComps = new ArrayList();
121         allComps.add(new JLabel(" "));
122         allComps.add(GuiUtils.lLabel("Begin:"));
123         beginTimeFld = new JTextField(defaultBegTime, 10);
124         beginTime = GuiUtils.doLayout(new Component[] {
125                            new JLabel("            Time: "),
126                            beginTimeFld }, 2,
127                            GuiUtils.WT_N, GuiUtils.WT_N);
128         allComps.add(beginTime);
129         begDay = new JDateChooser(defaultDay);
130         beginDate = GuiUtils.doLayout(new Component[] {
131                            new JLabel("            Date: "),
132                            begDay }, 2,
133                            GuiUtils.WT_N, GuiUtils.WT_N);
134         allComps.add(beginDate);
135         allComps.add(GuiUtils.lLabel("End:"));
136         endTimeFld = new JTextField(defaultEndTime, 10);
137         endTime = GuiUtils.doLayout(new Component[] {
138                            new JLabel("     "),
139                            new JLabel("          Time: "),
140                            endTimeFld }, 3,
141                            GuiUtils.WT_N, GuiUtils.WT_N);
142         allComps.add(endTime);
143         endDay = new JDateChooser(defaultDay);
144         endDate = GuiUtils.doLayout(new Component[] {
145                            new JLabel("     "),
146                            new JLabel("          Date: "),
147                            endDay }, 3,
148                            GuiUtils.WT_N, GuiUtils.WT_N);
149         allComps.add(endDate);
150
151         GuiUtils.tmpInsets = GRID_INSETS;
152         JPanel dateTimePanel = GuiUtils.doLayout(allComps, 1, GuiUtils.WT_NY,
153                                GuiUtils.WT_N);
154         timeRangeComp = GuiUtils.top(dateTimePanel);
155         return timeRangeComp;
156    }
157
158    protected JComponent getTimeRangeComponent() {
159        return timeRangeComp;
160    }
161
162    @Override public void applyToDataSelection(DataSelection dataSelection) {
163        //System.out.println("applyToDataSelection: dataSelection=" + dataSelection);
164
165        if (dataSelection == null) {
166            dataSelection = new DataSelection(true);
167        }
168
169        Date beg = begDay.getDate();
170        JCalendar cal = begDay.getJCalendar();
171        JDayChooser dayChooser = cal.getDayChooser();
172        int day = dayChooser.getDay();
173        JMonthChooser monthChooser = cal.getMonthChooser();
174        int month = monthChooser.getMonth() + 1;
175        JYearChooser yearChooser = cal.getYearChooser();
176        int year = yearChooser.getYear();
177
178        int hours = 0;
179        int mins = 0;
180        double secs = 0.0;
181        String begTime = beginTimeFld.getText();
182        String[] timeStrings = begTime.split(":");
183        int num = timeStrings.length;
184        if (num > 0)
185            hours = (new Integer(timeStrings[0])).intValue();
186        if (num > 1)
187            mins = (new Integer(timeStrings[1])).intValue();
188        if (num > 2)
189            secs = (new Double(timeStrings[2] + ".0")).doubleValue();
190        if ((hours < 0) || (hours > 23)) hours = 0;
191        if ((mins < 0) || (mins > 59)) mins = 0;
192        if ((secs < 0.0) || (secs > 59.0)) secs = 0.0;
193
194        Time bTime = new Time(year, month, day, hours, mins, secs);
195        double dVal = bTime.getJulianDate();
196        Double bigD = new Double(dVal);
197        String begTimeStr = bigD.toString();
198        dataSelection.putProperty(PROP_BEGTIME, bTime.getDateTimeStr());
199
200        Integer intVal = new Integer(year);
201        dataSelection.putProperty(PROP_YEAR, intVal.toString());
202        intVal = new Integer(month);
203        dataSelection.putProperty(PROP_MONTH, intVal.toString());
204        intVal = new Integer(day);
205        dataSelection.putProperty(PROP_DAY, intVal.toString());
206        intVal = new Integer(hours);
207        dataSelection.putProperty(PROP_HOURS, intVal.toString());
208        intVal = new Integer(mins);
209        dataSelection.putProperty(PROP_MINS, intVal.toString());
210        Double doubleVal = new Double(secs);
211        dataSelection.putProperty(PROP_SECS, doubleVal.toString());
212
213        Date end = endDay.getDate();
214        cal = endDay.getJCalendar();
215        dayChooser = cal.getDayChooser();
216        day = dayChooser.getDay();
217        monthChooser = cal.getMonthChooser();
218        month = monthChooser.getMonth() + 1;
219        yearChooser = cal.getYearChooser();
220        year = yearChooser.getYear();
221
222        String endTime = endTimeFld.getText();
223        timeStrings = endTime.split(":");
224        num = timeStrings.length;
225        if (num > 0)
226            hours = (new Integer(timeStrings[0])).intValue();
227        if (num > 1)
228            mins = (new Integer(timeStrings[1])).intValue();
229        if (num > 2)
230            secs = (new Double(timeStrings[2] + ".0")).doubleValue();
231        if ((hours < 0) || (hours > 23)) hours = 0;
232        if ((mins < 0) || (mins > 59)) mins = 0;
233        if ((secs < 0.0) || (secs > 59.0)) secs = 0.0;
234
235        Time eTime = new Time(year, month, day, hours, mins, secs);
236        dVal = eTime.getJulianDate();
237        bigD = new Double(dVal);
238        String endTimeStr = bigD.toString();
239
240        dataSelection.putProperty(PROP_ENDTIME, eTime.getDateTimeStr());
241        dataSelection.putProperty(PROP_BTIME, begTimeStr);
242        dataSelection.putProperty(PROP_ETIME, endTimeStr);
243    }
244}