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