001    /*
002     * $Id: McIdasBridgeChooser.java,v 1.14 2012/02/19 17:35:37 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    
034    import static javax.swing.GroupLayout.DEFAULT_SIZE;
035    import static javax.swing.GroupLayout.Alignment.BASELINE;
036    import static javax.swing.GroupLayout.Alignment.LEADING;
037    import static javax.swing.GroupLayout.Alignment.TRAILING;
038    import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;
039    import static javax.swing.LayoutStyle.ComponentPlacement.UNRELATED;
040    
041    import java.awt.event.ActionEvent;
042    import java.awt.event.ActionListener;
043    import java.awt.event.FocusEvent;
044    import java.awt.event.FocusListener;
045    import java.util.ArrayList;
046    import java.util.Hashtable;
047    import java.util.List;
048    
049    import javax.swing.GroupLayout;
050    import javax.swing.JButton;
051    import javax.swing.JComponent;
052    import javax.swing.JLabel;
053    import javax.swing.JPanel;
054    import javax.swing.JTextField;
055    
056    import org.w3c.dom.Element;
057    
058    import ucar.unidata.idv.chooser.IdvChooser;
059    import ucar.unidata.idv.chooser.IdvChooserManager;
060    import ucar.unidata.util.GuiUtils;
061    import ucar.unidata.util.LogUtil;
062    
063    import edu.wisc.ssec.mcidasv.Constants;
064    import edu.wisc.ssec.mcidasv.data.McIdasFrame;
065    import edu.wisc.ssec.mcidasv.data.McIdasXInfo;
066    import edu.wisc.ssec.mcidasv.util.McVGuiUtils;
067    import edu.wisc.ssec.mcidasv.util.McVGuiUtils.Position;
068    import edu.wisc.ssec.mcidasv.util.McVGuiUtils.TextColor;
069    import edu.wisc.ssec.mcidasv.util.McVGuiUtils.Width;
070    
071    
072    public class McIdasBridgeChooser extends IdvChooser implements Constants {
073        
074        /** A widget for the command line text */
075        private JTextField hostLine = new JTextField("");
076        private JTextField portLine = new JTextField("");
077        private JTextField keyLine = new JTextField("");
078    
079        private McIdasXInfo mcidasxInfo;
080    
081        /**
082         * Create the chooser with the given manager and xml
083         *
084         * @param mgr The manager
085         * @param root The xml
086         *
087         */
088        public McIdasBridgeChooser(IdvChooserManager mgr, Element root) {
089            super(mgr, root);
090            
091            mcidasxInfo = new McIdasXInfo();
092            
093            loadButton = McVGuiUtils.makeImageTextButton(ICON_ACCEPT_SMALL, getLoadCommandName());
094            loadButton.setActionCommand(getLoadCommandName());
095            loadButton.addActionListener(this);
096        }
097    
098        public String getHost() {
099            return this.mcidasxInfo.getHostString();
100        }
101        
102        private void setHost() {
103            this.mcidasxInfo.setHostString((hostLine.getText()).trim());
104        }
105    
106        public String getPort() {
107            return this.mcidasxInfo.getPortString();
108        }
109        
110        private void setPort() {
111            this.mcidasxInfo.setPortString((portLine.getText()).trim());
112        }
113    
114        public String getKey() {
115            return this.mcidasxInfo.getPortString();
116        }
117        
118        private void setKey() {
119            this.mcidasxInfo.setKeyString((keyLine.getText()).trim());
120        }
121    
122        /**
123         * Returns a list of the images to load or null if none have been
124         * selected.
125         *
126         * @return  list  get the list of image descriptors
127         */
128        public List getFrameList() {
129            List frames = new ArrayList();
130            List xFrames = this.mcidasxInfo.getFrameNumbers();
131            if (xFrames.size() < 1) return frames;
132            for (int i = 0; i < xFrames.size(); i++) {
133                Integer frmInt = (Integer)xFrames.get(i);
134                McIdasFrame frame = new McIdasFrame(frmInt.intValue(), this.mcidasxInfo);
135                frames.add(frame);
136            }
137            return frames;
138        }
139        
140        /**
141         * Returns a list of the frame numbers to load or null if none have been
142         * selected.
143         *
144         * @return  list  get the list of frame numbers
145         */
146        public List getFrameNumbers() {
147            return this.mcidasxInfo.getFrameNumbers();
148        }
149        
150        public int getFrameCount() {
151            return getFrameNumbers().size();
152        }
153        
154        /**
155         * Load in an ADDE point data set based on the
156         * <code>PropertyChangeEvent<code>.
157         *
158         */
159        public void doLoadInThread() {
160            showWaitCursor();
161            List frames = getFrameList();
162            if (frames.size() < 1) {
163                LogUtil.userMessage("Connection to McIDAS-X Bridge Listener at " + getHost() + ":" + getPort() + " failed");
164                showNormalCursor();
165                return;
166            }
167    
168            Hashtable ht = new Hashtable();
169            ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.FRAME_NUMBERS_KEY, getFrameNumbers());
170            if (getFrameCount() > 1) {
171               ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.DATA_NAME_KEY,"Frame Sequence");
172            } else {
173               ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.DATA_NAME_KEY,"Frame");
174            }
175            ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.REQUEST_HOST, getHost());
176            ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.REQUEST_PORT, getPort());
177            ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.REQUEST_KEY, this.mcidasxInfo.getKeyString());
178            //System.out.println("    ht:  " + ht);
179            makeDataSource("", "MCIDASX", ht);
180            showNormalCursor();
181        }
182        
183    
184        /**
185         * Make the GUI
186         *
187         * @return The GUI
188         */
189        private JLabel statusLabel = new JLabel("Status");
190    
191        @Override
192        public void setStatus(String statusString, String foo) {
193            if (statusString == null)
194                statusString = "";
195            statusLabel.setText(statusString);
196        }
197        
198        protected JComponent doMakeContents() {
199            JPanel myPanel = new JPanel();
200            
201            
202            JLabel hostLabel = McVGuiUtils.makeLabelRight("Host:");
203            
204            hostLine.setText(mcidasxInfo.getHostString());
205            McVGuiUtils.setComponentWidth(hostLine, Width.DOUBLE);
206            hostLine.addFocusListener(new FocusListener() {
207                public void focusGained(FocusEvent e) {}
208                public void focusLost(FocusEvent e) { setHost(); }
209            });
210            hostLine.addActionListener(new ActionListener() {
211                public void actionPerformed(ActionEvent ae) { setHost(); }
212            });
213            
214            JLabel portLabel = McVGuiUtils.makeLabelRight("Port:");
215    
216            portLine.setText(mcidasxInfo.getPortString());
217            McVGuiUtils.setComponentWidth(portLine, Width.DOUBLE);
218            portLine.addFocusListener(new FocusListener() {
219                public void focusGained(FocusEvent e) {}
220                public void focusLost(FocusEvent e) { setPort(); }
221            });
222            portLine.addActionListener(new ActionListener() {
223                public void actionPerformed(ActionEvent ae) { setPort(); }
224            });
225                    
226            JLabel statusLabelLabel = McVGuiUtils.makeLabelRight("");
227    
228            statusLabel.setText("Press \"" + getLoadCommandName() + "\" to connect to the McIDAS-X Bridge Listener");
229            McVGuiUtils.setLabelPosition(statusLabel, Position.RIGHT);
230            McVGuiUtils.setComponentColor(statusLabel, TextColor.STATUS);
231                    
232            JButton helpButton = McVGuiUtils.makeImageButton(ICON_HELP, "Show help");
233            helpButton.setActionCommand(GuiUtils.CMD_HELP);
234            helpButton.addActionListener(this);
235            
236            McVGuiUtils.setComponentWidth(loadButton, Width.DOUBLE);
237            
238            GroupLayout layout = new GroupLayout(myPanel);
239            myPanel.setLayout(layout);
240            layout.setHorizontalGroup(
241                layout.createParallelGroup(LEADING)
242                .addGroup(layout.createSequentialGroup()
243                    .addContainerGap()
244                    .addGroup(layout.createParallelGroup(LEADING)
245                        .addGroup(layout.createSequentialGroup()
246                            .addComponent(hostLabel)
247                            .addGap(GAP_RELATED)
248                            .addComponent(hostLine))
249                        .addGroup(layout.createSequentialGroup()
250                            .addComponent(portLabel)
251                            .addGap(GAP_RELATED)
252                            .addComponent(portLine))
253                        .addGroup(TRAILING, layout.createSequentialGroup()
254                            .addComponent(helpButton)
255                            .addPreferredGap(RELATED)
256                            .addComponent(loadButton))
257                        .addGroup(layout.createSequentialGroup()
258                            .addComponent(statusLabelLabel)
259                            .addGap(GAP_RELATED)
260                            .addComponent(statusLabel, DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)))
261                    .addContainerGap())
262            );
263            layout.setVerticalGroup(
264                layout.createParallelGroup(LEADING)
265                .addGroup(TRAILING, layout.createSequentialGroup()
266                    .addContainerGap()
267                    .addGroup(layout.createParallelGroup(BASELINE)
268                        .addComponent(hostLine)
269                        .addComponent(hostLabel))
270                    .addPreferredGap(RELATED)
271                    .addGroup(layout.createParallelGroup(BASELINE)
272                        .addComponent(portLine)
273                        .addComponent(portLabel))
274                    .addPreferredGap(RELATED, DEFAULT_SIZE, Short.MAX_VALUE)
275                    .addGroup(layout.createParallelGroup(BASELINE)
276                        .addComponent(statusLabelLabel)
277                        .addComponent(statusLabel))
278                    .addPreferredGap(UNRELATED)
279                    .addGroup(layout.createParallelGroup(BASELINE)
280                        .addComponent(loadButton)
281                        .addComponent(helpButton))
282                    .addContainerGap())
283            );
284            
285            return myPanel;
286            
287        }
288    
289    }