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