001/*
002 * $Id: AddeImageParameterChooser.java,v 1.10 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.chooser.adde;
032
033import static javax.swing.GroupLayout.DEFAULT_SIZE;
034import static javax.swing.GroupLayout.PREFERRED_SIZE;
035import static javax.swing.GroupLayout.Alignment.BASELINE;
036import static javax.swing.GroupLayout.Alignment.LEADING;
037import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;
038
039import java.awt.event.ItemEvent;
040import java.awt.event.ItemListener;
041import java.util.Hashtable;
042import java.util.List;
043
044import javax.swing.GroupLayout;
045import javax.swing.JCheckBox;
046import javax.swing.JComponent;
047import javax.swing.JLabel;
048import javax.swing.JPanel;
049
050import org.w3c.dom.Element;
051
052import edu.wisc.ssec.mcidas.AreaDirectory;
053
054import ucar.unidata.data.imagery.BandInfo;
055import ucar.unidata.idv.chooser.IdvChooserManager;
056import ucar.unidata.util.TwoFacedObject;
057import ucar.unidata.xml.XmlObjectStore;
058
059import edu.wisc.ssec.mcidasv.Constants;
060import edu.wisc.ssec.mcidasv.util.McVGuiUtils;
061
062
063/**
064 * Widget to select images from a remote ADDE server
065 * Displays a list of the descriptors (names) of the image datasets
066 * available for a particular ADDE group on the remote server.
067 *
068 * @author Don Murray
069 */
070public class AddeImageParameterChooser extends AddeImageChooser implements Constants {
071
072    /**
073     * Public keys for server, group, dataset, user, project.
074     */
075    public final static String SIZE_KEY = "size";
076    public final static String BAND_KEY = "band";
077    public final static String PLACE_KEY = "place";
078    public final static String LATLON_KEY = "latlon";
079    public final static String LINELE_KEY = "linele";
080    public final static String MAG_KEY = "mag";
081    public final static String UNIT_KEY = "unit";
082    public final static String PREVIEW_KEY = "preview";
083    
084    /** Property for image default value unit */
085    protected static final String PROP_NAV = "NAV";
086
087    /** Property for image default value unit */
088    protected static final String PROP_UNIT = "UNIT";
089
090    /** Property for image default value band */
091    protected static final String PROP_BAND = "BAND";
092
093    /** Xml attr name for the defaults */
094    private static final String ATTR_NAV = "NAV";
095    private static final String ATTR_UNIT = "UNIT";
096    private static final String ATTR_BAND = "BAND";
097    private static final String ATTR_PLACE = "PLACE";
098    private static final String ATTR_SIZE = "SIZE";
099    private static final String ATTR_MAG = "MAG";
100    private static final String ATTR_LATLON = "LATLON";
101    private static final String ATTR_LINELE = "LINELE";
102
103    /** string for ALL */
104    private static final String ALL = "ALL";
105
106    private static JCheckBox previewBox = null;
107
108    /**
109     * Construct an Adde image selection widget
110     *
111     *
112     * @param mgr The chooser manager
113     * @param root The chooser.xml node
114     */
115    public AddeImageParameterChooser(IdvChooserManager mgr, Element root) {
116        super(mgr, root);
117        //DAVEP: Hiding parameter set picker for now... revisit after 1.0
118//        showParameterButton();
119    }
120    
121    /**
122     * Return the parameter type associated with this chooser.  Override!
123     */
124    @Override
125    protected String getParameterSetType() {
126        return "addeimagery";
127    }
128    
129    /**
130     * Return the data source ID.  Used by extending classes.
131     */
132    @Override
133    protected String getDataSourceId() {
134        return "ADDE.IMAGE.V";
135    }
136    
137    /**
138     * Restore the selected parameter set using element attributes
139     * 
140     * @param restoreElement
141     * @return
142     */
143    @Override
144    protected boolean restoreParameterSet(Element restoreElement) {
145        boolean okay = super.restoreParameterSet(restoreElement);
146        if (!okay) return okay;
147        
148        // Imagery specific restore
149        
150        // Restore nav
151        if (restoreElement.hasAttribute(ATTR_NAV)) {
152            String nav = restoreElement.getAttribute(ATTR_NAV);
153            TwoFacedObject tfo = new TwoFacedObject("Default", "X");
154            navComboBox.setSelectedItem((Object)tfo);
155            if (nav.toUpperCase().equals("LALO")) {
156                tfo = new TwoFacedObject("Lat/Lon", "LALO");
157            }
158            navComboBox.setSelectedItem((Object)tfo);
159        }
160        return true;
161    }
162    
163    /**
164     * Get the list of BandInfos for the current selected images
165     * @return list of BandInfos
166     */
167    public List<BandInfo> getSelectedBandInfos() {
168        return super.getBandInfos();
169    }
170
171    /**
172     * Get the value for the given property. This can either be the value
173     * supplied by the end user through the advanced GUI or is the default
174     *
175     * @param prop The property
176     * @param ad The AreaDirectory
177     *
178     * @return The value of the property to use in the request string
179     */
180    @Override
181    protected String getPropValue(String prop, AreaDirectory ad) {
182        String propValue = super.getPropValue(prop, ad);
183        if (prop.equals(PROP_NAV)) {
184            propValue = TwoFacedObject.getIdString(navComboBox.getSelectedItem());
185        }
186        return propValue;
187    }
188    
189    /**
190     * Optionally override any defaults per parameter chooser
191     * @param property
192     * @return
193     */
194    @Override
195    protected String getDefault(String property, String dflt) {
196        String paramDefault = super.getDefault(property, dflt);
197        if (property.equals(PROP_NAV)) {
198            if (restoreElement != null) {
199                paramDefault = restoreElement.getAttribute(ATTR_NAV);
200            }
201        } else if (property.equals(PROP_UNIT)) {
202            paramDefault = "";
203        } else if (property.equals(PROP_BAND)) {
204            paramDefault = ALL;
205        } else if (property.equals(PROP_PLACE)) {
206            paramDefault = "";
207        }
208        return paramDefault;
209    }
210    
211    /**
212     * Get the DataSource properties
213     * 
214     * @param ht
215     *            Hashtable of properties
216     */
217    @Override
218    protected void getDataSourceProperties(Hashtable ht) {
219        super.getDataSourceProperties(ht);
220        if (restoreElement != null) {
221            if (restoreElement.hasAttribute(ATTR_BAND)) {
222                ht.put(BAND_KEY, (Object)(restoreElement.getAttribute(ATTR_BAND)));
223            }
224            if (restoreElement.hasAttribute(ATTR_LATLON)) {
225                ht.put(LATLON_KEY, (Object)(restoreElement.getAttribute(ATTR_LATLON)));
226            }
227            if (restoreElement.hasAttribute(ATTR_LINELE)) {
228                ht.put(LINELE_KEY, (Object)(restoreElement.getAttribute(ATTR_LINELE)));
229            }
230            if (restoreElement.hasAttribute(ATTR_MAG)) {
231                ht.put(MAG_KEY, (Object)(restoreElement.getAttribute(ATTR_MAG)));
232            }
233            if (restoreElement.hasAttribute(ATTR_PLACE)) {
234                ht.put(PLACE_KEY, (Object)(restoreElement.getAttribute(ATTR_PLACE)));
235            }
236            if (restoreElement.hasAttribute(ATTR_SIZE)) {
237                ht.put(SIZE_KEY, (Object)(restoreElement.getAttribute(ATTR_SIZE)));
238            }
239            if (restoreElement.hasAttribute(ATTR_UNIT)) {
240                ht.put(UNIT_KEY, (Object)(restoreElement.getAttribute(ATTR_UNIT)));
241            }
242        }
243        
244        ht.put(PREVIEW_KEY, (Object)previewBox.isSelected());
245    }
246    
247    /**
248     * Should we use the user supplied property
249     * 
250     * @param propId
251     *            The property
252     * 
253     * @return Should use the value from the advanced widget
254     */
255    protected boolean usePropFromUser(String propId) {
256        boolean fromSuper = super.usePropFromUser(propId);
257        if (propId.equals(PROP_UNIT)) fromSuper = false;
258        else if (propId.equals(PROP_BAND)) fromSuper = false;
259        return fromSuper;
260    }
261    
262    /**
263     * Make the UI for this selector.
264     *
265     * @return The gui
266     */
267    @Override
268    public JComponent doMakeContents() {
269        JPanel myPanel = new JPanel();
270
271        JLabel timesLabel = McVGuiUtils.makeLabelRight("Times:");
272        addDescComp(timesLabel);
273
274        JPanel timesPanel = makeTimesPanel();
275        timesPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
276        addDescComp(timesPanel);
277
278        JLabel navigationLabel = McVGuiUtils.makeLabelRight("Navigation:");
279        addDescComp(navigationLabel);
280
281        // Use processPropertyComponents to build combo boxes that we rely on
282        processPropertyComponents();
283        addDescComp(navComboBox);
284        McVGuiUtils.setComponentWidth(navComboBox, McVGuiUtils.Width.DOUBLE);
285
286        // Preview checkbox
287        JLabel previewLabel = McVGuiUtils.makeLabelRight("Preview:");
288        addDescComp(previewLabel);
289        XmlObjectStore store = getIdv().getStore();
290        previewBox = new JCheckBox("Create preview image", store.get(Constants.PREF_IMAGE_PREVIEW, true));
291        previewBox.setToolTipText("Creating preview images takes extra time and network bandwidth");
292        previewBox.addItemListener(new ItemListener() {
293            public void itemStateChanged(ItemEvent e) {
294                XmlObjectStore store = getIdv().getStore();
295                store.put(Constants.PREF_IMAGE_PREVIEW, e.getStateChange());
296                store.save();
297            }
298        });
299        addDescComp(previewBox);
300
301        GroupLayout layout = new GroupLayout(myPanel);
302        myPanel.setLayout(layout);
303        layout.setHorizontalGroup(
304                layout.createParallelGroup(LEADING)
305                .addGroup(layout.createSequentialGroup()
306                        .addGroup(layout.createParallelGroup(LEADING)
307                                .addGroup(layout.createSequentialGroup()
308                                        .addComponent(descriptorLabel)
309                                        .addGap(GAP_RELATED)
310                                        .addComponent(descriptorComboBox))
311                                        .addGroup(layout.createSequentialGroup()
312                                                .addComponent(timesLabel)
313                                                .addGap(GAP_RELATED)
314                                                .addComponent(timesPanel, PREFERRED_SIZE, DEFAULT_SIZE, Short.MAX_VALUE))
315                                                .addGroup(layout.createSequentialGroup()
316                                                        .addComponent(navigationLabel)
317                                                        .addGap(GAP_RELATED)
318                                                        .addComponent(navComboBox))
319                                                        .addGroup(layout.createSequentialGroup()
320                                                                .addComponent(previewLabel)
321                                                                .addGap(GAP_RELATED)
322                                                                .addComponent(previewBox))))
323        );
324        layout.setVerticalGroup(
325                layout.createParallelGroup(LEADING)
326                .addGroup(layout.createSequentialGroup()
327                        .addGroup(layout.createParallelGroup(BASELINE)
328                                .addComponent(descriptorLabel)
329                                .addComponent(descriptorComboBox))
330                                .addPreferredGap(RELATED)
331                                .addGroup(layout.createParallelGroup(LEADING)
332                                        .addComponent(timesLabel)
333                                        .addComponent(timesPanel, PREFERRED_SIZE, DEFAULT_SIZE, Short.MAX_VALUE))
334                                        .addPreferredGap(RELATED)
335                                        .addGroup(layout.createParallelGroup(LEADING)
336                                                .addComponent(navigationLabel)
337                                                .addComponent(navComboBox))
338                                                .addGroup(layout.createParallelGroup(LEADING)
339                                                        .addComponent(previewLabel)
340                                                        .addComponent(previewBox)))
341        );
342        
343        setInnerPanel(myPanel);
344        return super.doMakeContents(true);
345    }
346
347}