001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
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 https://www.gnu.org/licenses/.
027 */
028
029package edu.wisc.ssec.mcidasv.ui;
030
031import static javax.swing.GroupLayout.Alignment.BASELINE;
032import static javax.swing.GroupLayout.Alignment.LEADING;
033import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;
034import static javax.swing.LayoutStyle.ComponentPlacement.UNRELATED;
035
036import java.awt.Dimension;
037import java.util.Calendar;
038
039import javax.swing.GroupLayout;
040import javax.swing.JComponent;
041import javax.swing.JLabel;
042import javax.swing.JPanel;
043import javax.swing.JTextField;
044
045import name.gano.astro.time.Time;
046
047import org.slf4j.Logger;
048import org.slf4j.LoggerFactory;
049
050public class JTimeRangePicker extends JPanel {
051
052    private static final long serialVersionUID = 1L;
053
054    private static final Logger logger = LoggerFactory.getLogger(JTimeRangePicker.class);
055
056    private JTextField beginTimeFld;
057    private JTextField endTimeFld;
058    private String defaultBegTime = "00:00:00";
059    private String defaultEndTime = "23:59:59";
060
061    public static final String PROP_BEGTIME = "BeginTime";
062    public static final String PROP_ENDTIME = "EndTime";
063    protected static final String PROP_BTIME = "BTime";
064    protected static final String PROP_ETIME = "ETime";
065    protected static final String PROP_YEAR = "Year";
066    protected static final String PROP_MONTH = "Month";
067    protected static final String PROP_DAY = "Day";
068    protected static final String PROP_HOURS = "Hours";
069    protected static final String PROP_MINS = "Mins";
070
071    // TJJ use this to seed time objects with year/month/day
072    Calendar cal = Calendar.getInstance();
073
074    public JTimeRangePicker() {
075        doMakeContents();
076    }
077
078    protected JComponent doMakeContents() {
079
080        logger.debug("creating the JTimeRangePicker panel...");
081
082        JLabel begTimeLab = new JLabel("Beg Time:");
083        JLabel endTimeLab = new JLabel("End Time:");
084        beginTimeFld = new JTextField(defaultBegTime, 8);
085        beginTimeFld.setMaximumSize(new Dimension(80, 40));
086        beginTimeFld.setToolTipText("HH:MM:SS");
087        endTimeFld = new JTextField(defaultEndTime, 8);
088        endTimeFld.setMaximumSize(new Dimension(80, 40));
089        endTimeFld.setToolTipText("HH:MM:SS");
090
091        GroupLayout layout = new GroupLayout(this);
092        this.setLayout(layout);
093        layout.setHorizontalGroup(layout
094                .createParallelGroup(LEADING)
095
096                .addGroup(
097                        layout.createSequentialGroup()
098                            .addComponent(begTimeLab)
099                            .addComponent(beginTimeFld))
100
101                .addGroup(
102                        layout.createSequentialGroup()
103                            .addComponent(endTimeLab)
104                            .addComponent(endTimeFld)));
105
106        layout.setVerticalGroup(layout.createParallelGroup(LEADING).addGroup(
107                layout.createSequentialGroup()
108
109                        .addGroup(
110                                layout.createParallelGroup(BASELINE)
111                                    .addComponent(begTimeLab)
112                                    .addComponent(beginTimeFld))
113                        .addPreferredGap(RELATED)
114                        .addPreferredGap(UNRELATED)
115
116                        .addGroup(
117                                layout.createParallelGroup(BASELINE)
118                                    .addComponent(endTimeLab)
119                                    .addComponent(endTimeFld)).addPreferredGap(RELATED)));
120
121        return this;
122    }
123
124    public JComponent getTimeRangeComponent() {
125        return this;
126    }
127
128    /**
129     * Validate start time field
130     * @return true if ok
131     */
132    
133    public boolean begTimeOk() {
134        String begTime = beginTimeFld.getText();
135        String[] timeStrings = begTime.split(":");
136        int num = timeStrings.length;
137        if (num != 3)
138            return false;
139        int hours = -1;
140        int mins = -1;
141        int seconds = -1;
142        try {
143            hours = Integer.parseInt(timeStrings[0]);
144            mins = Integer.parseInt(timeStrings[1]);
145            seconds = Integer.parseInt(timeStrings[2]);
146        } catch (NumberFormatException nfe) {
147            return false;
148        }
149        if ((hours < 0) || (hours > 23))
150            return false;
151        if ((mins < 0) || (mins > 59))
152            return false;
153        if ((seconds < 0) || (seconds > 59))
154            return false;
155        
156        return true;
157    }
158
159    /**
160     * Validate end time field
161     * @return true if ok
162     */
163    
164    public boolean endTimeOk() {
165        String endTime = endTimeFld.getText();
166        String[] timeStrings = endTime.split(":");
167        int num = timeStrings.length;
168        if (num != 3)
169            return false;
170        int hours = -1;
171        int mins = -1;
172        int seconds = -1;
173        try {
174            hours = Integer.parseInt(timeStrings[0]);
175            mins = Integer.parseInt(timeStrings[1]);
176            seconds = Integer.parseInt(timeStrings[2]);
177        } catch (NumberFormatException nfe) {
178            return false;
179        }
180        if ((hours < 0) || (hours > 23))
181            return false;
182        if ((mins < 0) || (mins > 59))
183            return false;
184        if ((seconds < 0) || (seconds > 59))
185            return false;
186        
187        return true;
188    }
189
190    /**
191     * Make sure the end date/time exceeds the beginning date/time.
192     * 
193     * @return true if condition met, false otherwise
194     * 
195     */
196
197    public boolean timeRangeOk() {
198
199        if (! begTimeOk())
200            return false;
201        if (! endTimeOk())
202            return false;
203
204        int hours = 0;
205        int mins = 0;
206        int seconds = 0;
207
208        String begTime = beginTimeFld.getText();
209        String[] timeStrings = begTime.split(":");
210        int num = timeStrings.length;
211        if (num > 0)
212            hours = (new Integer(timeStrings[0])).intValue();
213        if (num > 1)
214            mins = (new Integer(timeStrings[1])).intValue();
215        if (num > 2)
216            seconds = (new Integer(timeStrings[2])).intValue();
217        
218        // Year, Month, and Day are arbitrary, just need to match eTime vals
219        Time bTime = new Time(2017, 2, 2, hours, mins, seconds);
220
221        String endTime = endTimeFld.getText();
222        timeStrings = endTime.split(":");
223        num = timeStrings.length;
224        if (num > 0)
225            hours = (new Integer(timeStrings[0])).intValue();
226        if (num > 1)
227            mins = (new Integer(timeStrings[1])).intValue();
228        if (num > 2)
229            seconds = (new Integer(timeStrings[2])).intValue();
230        
231        // Year, Month, and Day are arbitrary, just need to match bTime vals
232        Time eTime = new Time(2017, 2, 2, hours, mins, seconds);
233
234        if (eTime.getJulianDate() < bTime.getJulianDate()) {
235            return false;
236        }
237        return true;
238    }
239
240    public String getBegTimeStr() {
241        return beginTimeFld.getText();
242    }
243
244    public String getEndTimeStr() {
245        return endTimeFld.getText();
246    }
247
248}