001    /*
002     * $Id: AddeFrontChooser.java,v 1.24 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    import static javax.swing.GroupLayout.DEFAULT_SIZE;
034    import static javax.swing.GroupLayout.Alignment.BASELINE;
035    import static javax.swing.GroupLayout.Alignment.LEADING;
036    
037    import java.awt.Dimension;
038    import java.util.ArrayList;
039    import java.util.Hashtable;
040    import java.util.List;
041    import java.util.Vector;
042    
043    import javax.swing.GroupLayout;
044    import javax.swing.JComponent;
045    import javax.swing.JLabel;
046    import javax.swing.JPanel;
047    import javax.swing.JRadioButton;
048    import javax.swing.ListSelectionModel;
049    
050    import org.w3c.dom.Element;
051    
052    import ucar.unidata.data.DataSource;
053    import ucar.unidata.idv.chooser.IdvChooserManager;
054    import ucar.unidata.ui.ChooserList;
055    import ucar.unidata.util.GuiUtils;
056    
057    import edu.wisc.ssec.mcidasv.util.McVGuiUtils;
058    
059    /**
060     * A chooser for adde front products
061     *
062     *
063     *
064     * @author IDV development team
065     * @version $Revision: 1.24 $Date: 2012/02/19 17:35:35 $
066     */
067    public class AddeFrontChooser extends AddeChooser {
068        /** for gui */
069        ChooserList timesList;
070    
071        /** for gui */
072        JRadioButton forecastBtn;
073    
074        /** for gui */
075        JRadioButton observedBtn;
076    
077        /**
078         * Make a new one
079         *
080         * @param mgr The manager
081         * @param root The xml element that defined this object
082         *
083         */
084        public AddeFrontChooser(IdvChooserManager mgr, Element root) {
085            super(mgr, root);
086            
087            addServerComp(loadButton);
088    
089        }
090    
091        public void readTimes() {
092            if (canAccessServer())
093                    setState(STATE_CONNECTED);
094            else
095                    setState(STATE_UNCONNECTED);
096        }
097    
098        
099        /**
100         * update the buttons
101         */
102        protected void updateStatus() {
103            super.updateStatus();
104            setHaveData(true);
105        }
106    
107        /**
108         * Update the widget with the latest data.
109         *
110         * @throws Exception On badness
111         */
112        @Override public void handleUpdate() throws Exception {
113    //        updateServerList();
114            readTimes();
115            updateStatus();
116            showNormalCursor();
117            saveServerState();
118        }
119        
120        protected boolean haveDescriptorSelected() {
121            return true;
122        }
123    
124        /**
125         * Make the UI for this selector.
126         *
127         * @return The gui
128         */
129        public JComponent doMakeContents() {
130            JPanel myPanel = new JPanel();
131    
132            JLabel frontLabel = new JLabel();
133            observedBtn = new JRadioButton("Analysis Fronts", true);
134            forecastBtn = new JRadioButton("Forecast Fronts", false);
135                    
136            frontLabel = McVGuiUtils.makeLabelRight("Latest:");
137    
138            GuiUtils.buttonGroup(observedBtn, forecastBtn);
139    
140            frontLabel.setEnabled(false);
141            observedBtn.setEnabled(false);
142            forecastBtn.setEnabled(false);
143                    
144            addServerComp(frontLabel);
145            addServerComp(observedBtn);
146            addServerComp(forecastBtn);
147            
148            timesList = new ChooserList();
149            timesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
150            Vector items = new Vector();
151            for (int i = 0; i < 10; i++) {
152                if (i == 0) {
153                    items.add("Most recent day");
154                } else {
155                    items.add((i + 1) + " most recent days");
156                }
157            }
158            timesList.setListData(items);
159            timesList.setSelectedIndex(0);
160            timesList.getScroller().setPreferredSize(new Dimension(200, 100));
161            //        comps.add(GuiUtils.rLabel("Days:"));
162            //        comps.add(GuiUtils.left(timesList.getScroller()));
163            
164            GroupLayout layout = new GroupLayout(myPanel);
165            myPanel.setLayout(layout);
166            layout.setHorizontalGroup(
167                layout.createParallelGroup(LEADING)
168                .addGroup(layout.createSequentialGroup()
169                    .addComponent(frontLabel)
170                    .addGap(GAP_RELATED)
171                    .addComponent(observedBtn)
172                    .addGap(GAP_RELATED)
173                    .addComponent(forecastBtn)
174                    .addContainerGap(DEFAULT_SIZE, Short.MAX_VALUE))
175            );
176            layout.setVerticalGroup(
177                layout.createParallelGroup(LEADING)
178                .addGroup(layout.createSequentialGroup()
179                    .addGroup(layout.createParallelGroup(BASELINE)
180                        .addComponent(frontLabel)
181                        .addComponent(observedBtn)
182                        .addComponent(forecastBtn))
183                    .addGap(GAP_UNRELATED))
184            );
185            
186            setInnerPanel(myPanel);
187            return super.doMakeContents();
188        }
189    
190    
191        /**
192         * User said go, we go. Simply get the list of images
193         * from the imageChooser and create the ADDE.IMAGE
194         * DataSource
195         *
196         */
197        public void doLoadInThread() {
198            List   urls   = new ArrayList();
199            int    index  = timesList.getSelectedIndex();
200            String server = getAddeServer().getName();
201            String type   = (forecastBtn.isSelected()
202                             ? "SRP"
203                             : "SUS&wmo=ASUS01");
204            for (int i = 0; i <= index; i++) {
205                String url = "adde://" + server
206                             + "/wxtext?group=RTWXTEXT&apro=COD&astn=" + type
207                             + "&day=%DAY-" + i + "%";
208                urls.add(url);
209            }
210            Hashtable ht = new Hashtable();
211            //TODO: Change the name, maybe add the date
212            ht.put(DataSource.PROP_TITLE, (forecastBtn.isSelected()
213                                           ? "Forecast Front"
214                                           : "Analysis Fronts"));
215            makeDataSource(urls, "TEXT.FRONT", ht);
216            saveServerState();
217        }
218    
219        /**
220         * get the adde server grup type to use
221         *
222         * @return group type
223         */
224        @Override protected String getGroupType() {
225            return "text";
226        }
227    
228        public String getDataType() {
229            return "TEXT";
230        }
231    }
232