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