001    /*
002     * $Id: PolarOrbitTrackChooser.java,v 1.7 2012/02/19 17:35:36 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;
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    import static javax.swing.GroupLayout.Alignment.TRAILING;
037    import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;
038    import static javax.swing.LayoutStyle.ComponentPlacement.UNRELATED;
039    
040    import java.awt.event.ActionEvent;
041    import java.awt.event.ActionListener;
042    import java.awt.event.KeyEvent;
043    import java.awt.event.KeyListener;
044    import java.util.ArrayList;
045    import java.util.Hashtable;
046    import java.util.List;
047    import java.util.Map;
048    
049    import javax.swing.GroupLayout;
050    import javax.swing.JButton;
051    import javax.swing.JComboBox;
052    import javax.swing.JComponent;
053    import javax.swing.JLabel;
054    import javax.swing.JPanel;
055    import javax.swing.JRadioButton;
056    import javax.swing.JTextField;
057    
058    import org.w3c.dom.Element;
059    
060    import ucar.unidata.idv.chooser.IdvChooserManager;
061    import ucar.unidata.util.GuiUtils;
062    import ucar.unidata.util.PreferenceList;
063    
064    import edu.wisc.ssec.mcidasv.Constants;
065    import edu.wisc.ssec.mcidasv.chooser.adde.AddeChooser;
066    import edu.wisc.ssec.mcidasv.util.McVGuiUtils;
067    import edu.wisc.ssec.mcidasv.util.McVGuiUtils.Width;
068    
069    
070    /**
071     * Widget to select images from a remote ADDE server
072     * Displays a list of the descriptors (names) of the image datasets
073     * available for a particular ADDE group on the remote server.
074     *
075     * @author Don Murray
076     */
077    public class PolarOrbitTrackChooser extends AddeChooser implements Constants {
078    
079        /** Connect button--we need to be able to disable this */
080        JButton connectButton = McVGuiUtils.makeImageTextButton(ICON_CONNECT_SMALL, "Connect");
081    
082        /** Manage button */
083        JButton manageButton =
084            McVGuiUtils.makeImageButton("/edu/wisc/ssec/mcidasv/resources/icons/toolbar/preferences-system22.png",
085                this, "doManager", null, "Manage servers");
086    
087        /** Public button--we need to draw a menu from this */
088        JButton publicButton =
089            McVGuiUtils.makeImageButton("/edu/wisc/ssec/mcidasv/resources/icons/toolbar/show-layer-controls22.png",
090                this, "showGroups", null, "List public datasets");
091    
092        private JComboBox serverSelector;
093        private JRadioButton addeBtn;
094        private JRadioButton urlBtn;
095        private JLabel descLabel;
096        List addeList = new ArrayList();
097    
098        /** Manages the pull down list of urls */
099        private PreferenceList prefList;
100    
101        /** The list of urls */
102        private JComboBox box;
103        private JTextField boxEditor;
104    
105        /** text type */
106        private static final String TLE_TYPE = "text";
107    
108        /** Property name to get the list or urls */
109        public static final String PREF_URLLIST = "idv.urllist";
110    
111        /**
112         * Property for the tle server name key.
113         * @see #getServer()
114         */
115        public static String TLE_SERVER_NAME_KEY = "tle_server";
116        public static String URL_NAME_KEY = "url_name";
117    
118        /**
119         * Property for the tle group name key.
120         * @see #getGroup()
121         */
122        public static String TLE_GROUP_NAME_KEY = "tle_group";
123    
124        /**
125         * Property for the tle user id
126         */
127        public static String TLE_USER_ID_KEY = "tle_user";
128    
129        /**
130         * Property for the tle project number
131         */
132        public static String TLE_PROJECT_NUMBER_KEY = "tle_proj";
133    
134        /**
135         * Construct an Adde image selection widget
136         *
137         *
138         * @param mgr The chooser manager
139         * @param root The chooser.xml node
140         */
141        public PolarOrbitTrackChooser(IdvChooserManager mgr, Element root) {
142            super(mgr, root);
143            serverSelector = getServerSelector();
144            showServers();
145        }
146        
147            /**
148             * Return the data source ID.  Used by extending classes.
149             */
150        @Override
151            protected String getDataSourceId() {
152                    return "TLE";
153            }
154        
155        /**
156         * Make the UI for this selector.
157         *
158         * @return The gui
159         */
160        @Override
161        public JComponent doMakeContents() {
162            JPanel outerPanel = new JPanel();
163            JPanel addePanel = new JPanel();
164            addePanel =  (JPanel)makeAddePanel();
165    
166            JButton helpButton = McVGuiUtils.makeImageButton(ICON_HELP, "Show help");
167            helpButton.setActionCommand(GuiUtils.CMD_HELP);
168            helpButton.addActionListener(this);
169    
170            JButton refreshButton = McVGuiUtils.makeImageButton(ICON_REFRESH, "Refresh");
171            refreshButton.setActionCommand(GuiUtils.CMD_UPDATE);
172            refreshButton.addActionListener(this);
173    
174            McVGuiUtils.setComponentWidth(loadButton, Width.DOUBLE);
175    
176            GroupLayout layout = new GroupLayout(outerPanel);
177            outerPanel.setLayout(layout);
178            layout.setHorizontalGroup(
179                layout.createParallelGroup(LEADING)
180                .addGroup(TRAILING, layout.createSequentialGroup()
181                    .addGroup(layout.createParallelGroup(TRAILING)
182                        .addGroup(layout.createSequentialGroup()
183                            .addComponent(helpButton)
184                            .addGap(GAP_RELATED)
185                            .addComponent(refreshButton)
186                            .addGap(GAP_RELATED)
187                            .addComponent(cancelButton)
188                            .addPreferredGap(RELATED)
189                            .addComponent(loadButton))
190                            .addGroup(LEADING, layout.createSequentialGroup()
191                            .addGroup(layout.createParallelGroup(LEADING)
192                                .addComponent(addePanel, DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)))))
193            );
194            layout.setVerticalGroup(
195                layout.createParallelGroup(LEADING)
196                .addGroup(layout.createSequentialGroup()
197                    .addComponent(addePanel, DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)
198                    .addGroup(layout.createParallelGroup(BASELINE)
199                        .addComponent(loadButton)
200                        .addComponent(cancelButton)
201                        .addComponent(refreshButton)
202                        .addComponent(helpButton)))
203            );
204    
205            return outerPanel;
206        }
207    
208        private JComponent makeAddePanel() {
209            JPanel outerPanel = new JPanel();
210    
211            addeBtn = new JRadioButton("ADDE", true);
212            urlBtn = new JRadioButton("URL", false);
213            GuiUtils.buttonGroup(addeBtn, urlBtn);
214            addeBtn.addActionListener(new ActionListener() {
215                public void actionPerformed(ActionEvent e) {
216                    for (int i=0; i<5; i++) {
217                        JComponent comp = (JComponent)(addeList.get(i));
218                        comp.setEnabled(true);
219                        enableDescriptors(true);
220                    }
221                    for (int i=5; i<7; i++) {
222                        JComponent comp = (JComponent)(addeList.get(i));
223                        comp.setEnabled(false);
224                    }
225                }
226            });
227            urlBtn.addActionListener(new ActionListener() {
228                public void actionPerformed(ActionEvent e) {
229                    for (int i=5; i<7; i++) {
230                        JComponent comp = (JComponent)(addeList.get(i));
231                        comp.setEnabled(true);
232                    }
233                    loadButton.setEnabled(true);
234                    for (int i=0; i<5; i++) {
235                        JComponent comp = (JComponent)(addeList.get(i));
236                        comp.setEnabled(false);
237                        enableDescriptors(false);
238                    }
239                }
240            });
241            JLabel serverLabel = new JLabel("     Server:");
242            JLabel urlLabel = new JLabel("     URL:");
243            descLabel = new JLabel("     Descriptor:");
244            descLabel.setEnabled(false);
245            descriptorComboBox.setEnabled(false);
246    
247            clearOnChange(serverSelector);
248            McVGuiUtils.setComponentWidth(serverSelector, Width.DOUBLE);
249    
250            JLabel groupLabel = McVGuiUtils.makeLabelRight("Dataset:");
251    
252            groupSelector.setEditable(isGroupEditable());
253            clearOnChange(groupSelector);
254            McVGuiUtils.setComponentWidth(groupSelector, Width.DOUBLE);
255    
256            McVGuiUtils.setComponentWidth(connectButton, Width.DOUBLE);
257            connectButton.setActionCommand(CMD_CONNECT);
258            connectButton.addActionListener(this);
259    
260            prefList = getPreferenceList(PREF_URLLIST);
261            box = prefList.createComboBox(CMD_LOAD, this);
262            boxEditor = (JTextField)box.getEditor().getEditorComponent();
263            boxEditor.addKeyListener(new KeyListener() {
264                public void keyPressed(KeyEvent e) {}
265                public void keyReleased(KeyEvent e) {
266                }
267                public void keyTyped(KeyEvent e) {}
268            });
269            urlLabel.setEnabled(false);
270            box.setEnabled(false);
271            JLabel spaceLab = new JLabel("     ");
272    
273            GroupLayout layout = new GroupLayout(outerPanel);
274            outerPanel.setLayout(layout);
275            layout.setHorizontalGroup(
276                layout.createParallelGroup(LEADING)
277                .addGroup(TRAILING, layout.createSequentialGroup()
278                    .addGroup(layout.createParallelGroup(TRAILING)
279                            .addGroup(LEADING, layout.createSequentialGroup()
280                            .addGroup(layout.createParallelGroup(LEADING)
281                            .addComponent(addeBtn)
282                            .addGroup(layout.createSequentialGroup()
283                                .addComponent(serverLabel)
284                                .addGap(GAP_RELATED)
285                                .addComponent(serverSelector)
286                                .addGap(GAP_RELATED)
287                                .addComponent(manageButton)
288                                .addGap(GAP_RELATED)
289                                .addComponent(groupLabel)
290                                .addGap(GAP_RELATED)
291                                .addComponent(groupSelector)
292                                .addGap(GAP_RELATED)
293                                .addComponent(publicButton)
294                                .addPreferredGap(RELATED, DEFAULT_SIZE, Short.MAX_VALUE)
295                                .addComponent(connectButton))
296                            .addGroup(layout.createSequentialGroup()
297                                .addComponent(spaceLab)
298                                .addComponent(descLabel)
299                                .addGap(GAP_RELATED)
300                                .addComponent(descriptorComboBox))
301                                .addGap(GAP_RELATED)
302                            .addComponent(urlBtn)
303                            .addGroup(layout.createSequentialGroup()
304                                .addComponent(urlLabel)
305                                .addGap(GAP_RELATED)
306                                .addComponent(box))))))
307            );
308    
309            layout.setVerticalGroup(
310                layout.createParallelGroup(LEADING)
311                .addGroup(layout.createSequentialGroup()
312                    .addComponent(addeBtn)
313                    .addGroup(layout.createParallelGroup(BASELINE)
314                        .addComponent(serverLabel)
315                        .addComponent(serverSelector)
316                        .addComponent(manageButton)
317                        .addComponent(groupLabel)
318                        .addComponent(groupSelector)
319                        .addComponent(publicButton)
320                        .addComponent(connectButton))
321                    .addPreferredGap(RELATED)
322                    .addGroup(layout.createParallelGroup(BASELINE)
323                        .addComponent(spaceLab)
324                        .addComponent(descLabel)
325                        .addComponent(descriptorComboBox))
326                    .addPreferredGap(UNRELATED)
327                    .addComponent(urlBtn)
328                    .addGroup(layout.createParallelGroup(BASELINE)
329                        .addComponent(urlLabel)
330                        .addComponent(box)))
331            );
332    
333            addeList.add(serverLabel);
334            addeList.add(serverSelector);
335            addeList.add(groupLabel);
336            addeList.add(groupSelector);
337            addeList.add(connectButton);
338            McVGuiUtils.setComponentWidth(descriptorComboBox, Width.DOUBLEDOUBLE);
339            addeList.add(urlLabel);
340            addeList.add(box);
341    
342            return outerPanel;
343        }
344    
345        private void enableDescriptors(boolean val) {
346            if (val) {
347                boolean connected;
348                if (getState() == STATE_CONNECTED) {
349                    connected = true;
350                } else {
351                    connected = false;
352                }
353                if (connected) {
354                    descLabel.setEnabled(true);
355                    descriptorComboBox.setEnabled(true);
356                } else {
357                    descLabel.setEnabled(false);
358                    descriptorComboBox.setEnabled(false);
359                }
360            } else {
361                descLabel.setEnabled(false);
362                descriptorComboBox.setEnabled(false);
363            }
364        }
365    
366        /**
367         * Update labels, enable widgets, etc.
368         */
369        protected void updateStatus() {
370            super.updateStatus();
371            enableWidgets();
372        }
373    
374        /**
375         * Get the data type ID
376         *
377         * @return  the data type
378         */
379    
380        public String getDataType() {
381            return "TEXT";
382        }
383    
384        /**
385          * get the adde server grup type to use
386          *
387          * @return group type
388          */
389        @Override
390        protected String getGroupType() {
391                return TLE_TYPE;
392        }
393    
394        /**
395         * User said go, we go. 
396         * Create the TLE DataSource
397         * 
398         */
399        public void doLoadInThread() {
400            String obj = "TLE";
401            prefList.saveState(box);
402            Hashtable ht = new Hashtable();
403            getDataSourceProperties(ht);
404            makeDataSource(obj, getDataSourceId(), ht);
405            saveServerState();
406        }
407    
408        /**
409         * Get the DataSource properties
410         * 
411         * @param ht
412         *            Hashtable of properties
413         */
414        protected void getDataSourceProperties(Hashtable ht) {
415            if (addeBtn.isSelected()) {
416                super.getDataSourceProperties(ht);
417                ht.put(DATASET_NAME_KEY, getDatasetName());
418                String server = getServer();
419                ht.put(TLE_SERVER_NAME_KEY, server);
420                String group = getGroup();
421                ht.put(TLE_GROUP_NAME_KEY, group);
422                Map<String, String> acct = getAccounting(server, group);
423                String user = acct.get("user");
424                String proj = acct.get("proj");
425                ht.put(TLE_USER_ID_KEY, user);
426                ht.put(TLE_PROJECT_NUMBER_KEY, proj);
427            } else {
428                ht.put(URL_NAME_KEY, box.getSelectedItem());
429            }
430        }
431    
432        private String getDatasetName() {
433            return (String) descriptorComboBox.getSelectedItem();
434        }
435    
436        public void handleConnectFromThread() {
437            super.handleConnectFromThread();
438            enableDescriptors(true);
439        }
440    
441        /**
442         * Get the default display type
443         *
444         * @return  the name of the default display
445         */
446        protected String getDefaultDisplayType() {
447            return "tledisplay";
448        }
449    }