001    /*
002     * $Id: AddeLightningDataChooser.java,v 1.8 2012/02/19 17:35:35 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.chooser.adde;
032    
033    
034    import java.util.ArrayList;
035    import java.util.Date;
036    import java.util.List;
037    
038    import javax.swing.ListSelectionModel;
039    
040    import org.w3c.dom.Element;
041    
042    import ucar.unidata.data.AddeUtil;
043    import ucar.unidata.idv.chooser.IdvChooserManager;
044    import ucar.unidata.util.TwoFacedObject;
045    import ucar.visad.UtcDate;
046    import visad.DateTime;
047    
048    import edu.wisc.ssec.mcidas.McIDASUtil;
049    
050    /**
051     * Selection widget for ADDE point data
052     *
053     * @author MetApps Development Team
054     * @version $Revision: 1.8 $ $Date: 2012/02/19 17:35:35 $
055     */
056    public class AddeLightningDataChooser extends AddePointDataChooser {
057    
058    
059        /**
060         * Create a new <code>AddeLightningDataChooser</code> with the preferred
061         * list of ADDE servers.
062         *
063         *
064         * @param mgr The chooser manager
065         * @param root The chooser.xml node
066         */
067        public AddeLightningDataChooser(IdvChooserManager mgr, Element root) {
068            super(mgr, root);
069        }
070    
071    
072        /**
073         * Get the default station model for this chooser.
074         * @return name of default station model
075         */
076        public String getDefaultStationModel() {
077            return "flash";
078        }
079    
080        /**
081         * This allows derived classes to provide their own name for labeling, etc.
082         *
083         * @return  the dataset name
084         */
085        public String getDataName() {
086            return "Lightning Data";
087        }
088    
089        /**
090         * Get the request string for times particular to this chooser
091         *
092         * @return request string
093         * protected String getTimesRequest() {
094         *   StringBuffer buf = getGroupUrl(REQ_POINTDATA, getGroup());
095         *   appendKeyValue(buf, PROP_DESCR, getDescriptor());
096         *   // this is hokey, but take a smattering of stations.
097         *   //appendKeyValue(buf, PROP_SELECT, "'CO US'");
098         *   appendKeyValue(buf, PROP_POS, "0");
099         *   appendKeyValue(buf, PROP_NUM, "ALL");
100         *   appendKeyValue(buf, PROP_PARAM, "DAY TIME");
101         *   return buf.toString();
102         * }
103         */
104    
105        /**
106         * Get the default datasets for the chooser.  The objects are
107         * a descriptive name and the ADDE group/descriptor
108         *
109         * @return  default datasets.
110         */
111        protected TwoFacedObject[] getDefaultDatasets() {
112            return new TwoFacedObject[] { new TwoFacedObject("NLDN", "LGT/NLDN"),
113                                          new TwoFacedObject("USPLN",
114                                          "LGT/USPLN") };
115        }
116    
117        /**
118         * Get the increment between times for relative time requests
119         *
120         * @return time increment (hours)
121         */
122        public float getRelativeTimeIncrement() {
123            return .5f;
124        }
125    
126        /**
127         * Create the date time selection string for the "select" clause
128         * of the ADDE URL.
129         *
130         * @return the select day and time strings
131         */
132        protected String getDayTimeSelectString() {
133            StringBuffer buf = new StringBuffer();
134            if (getDoAbsoluteTimes()) {
135                buf.append("time ");
136                List     times = getSelectedAbsoluteTimes();
137                DateTime dt    = (DateTime) times.get(0);
138                buf.append(UtcDate.getHMS(dt));
139                buf.append(" ");
140                dt = (DateTime) times.get(times.size() - 1);
141                buf.append(UtcDate.getHMS(dt));
142            } else {
143                buf.append(getRelativeTimeId());
144            }
145            return buf.toString();
146        }
147    
148        /**
149         * Get the identifier for relative time.  Subclasses can override.
150         * @return the identifier
151         */
152        protected String getRelativeTimeId() {
153            return AddeUtil.RELATIVE_TIME_RANGE;
154        }
155    
156        /**
157         * Get the selection mode for the absolute times panel. Subclasses
158         * can override.
159         *
160         * @return the list selection mode
161         */
162        protected int getAbsoluteTimeSelectMode() {
163            return ListSelectionModel.SINGLE_INTERVAL_SELECTION;
164        }
165    
166    
167        /**
168         * Set the list of available times.
169         */
170        public void readTimes() {
171            clearTimesList();
172            ArrayList uniqueTimes = new ArrayList();
173    
174            setState(STATE_CONNECTING);
175            try {
176                float    hours      = getRelativeTimeIncrement();
177                int      numTimes   = (int) (24f / hours);
178                DateTime currentDay = new DateTime(new Date());
179                int day = Integer.parseInt(UtcDate.formatUtcDate(currentDay,
180                              "yyyyMMdd"));
181                for (int i = 0; i < numTimes; i++) {
182                    int hour = McIDASUtil.mcDoubleToPackedInteger(i * hours);
183                    try {
184                        DateTime dt =
185                            new DateTime(McIDASUtil.mcDayTimeToSecs(day, hour));
186                        uniqueTimes.add(dt);
187                    } catch (Exception e) {}
188                }
189                setState(STATE_CONNECTED);
190                //System.out.println(
191                //       "found " + uniqueTimes.size() + " unique times");
192            } catch (Exception excp) {
193                handleConnectionError(excp);
194                return;
195            }
196            if (getDoAbsoluteTimes()) {
197                if ( !uniqueTimes.isEmpty()) {
198                    setAbsoluteTimes(new ArrayList(uniqueTimes));
199                }
200                int selectedIndex = getAbsoluteTimes().size() - 1;
201                setSelectedAbsoluteTime(selectedIndex);
202            }
203        }
204    
205    
206    }
207