001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
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 https://www.gnu.org/licenses/.
027 */
028
029package edu.wisc.ssec.mcidasv.chooser;
030
031
032import static javax.swing.GroupLayout.DEFAULT_SIZE;
033import static javax.swing.GroupLayout.Alignment.BASELINE;
034import static javax.swing.GroupLayout.Alignment.LEADING;
035import static javax.swing.GroupLayout.Alignment.TRAILING;
036import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;
037import static javax.swing.LayoutStyle.ComponentPlacement.UNRELATED;
038
039import java.awt.event.ActionEvent;
040import java.awt.event.ActionListener;
041import java.awt.event.FocusEvent;
042import java.awt.event.FocusListener;
043import java.util.ArrayList;
044import java.util.Hashtable;
045import java.util.List;
046
047import javax.swing.GroupLayout;
048import javax.swing.JButton;
049import javax.swing.JComponent;
050import javax.swing.JLabel;
051import javax.swing.JPanel;
052import javax.swing.JTextField;
053
054import org.w3c.dom.Element;
055
056import ucar.unidata.idv.chooser.IdvChooser;
057import ucar.unidata.idv.chooser.IdvChooserManager;
058import ucar.unidata.util.GuiUtils;
059import ucar.unidata.util.LogUtil;
060
061import edu.wisc.ssec.mcidasv.Constants;
062import edu.wisc.ssec.mcidasv.data.McIdasFrame;
063import edu.wisc.ssec.mcidasv.data.McIdasXInfo;
064import edu.wisc.ssec.mcidasv.util.McVGuiUtils;
065import edu.wisc.ssec.mcidasv.util.McVGuiUtils.Position;
066import edu.wisc.ssec.mcidasv.util.McVGuiUtils.TextColor;
067import edu.wisc.ssec.mcidasv.util.McVGuiUtils.Width;
068
069
070public 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 {@code PropertyChangeEvent}.
154     *
155     */
156    public void doLoadInThread() {
157        showWaitCursor();
158        List frames = getFrameList();
159        if (frames.size() < 1) {
160            LogUtil.userMessage("Connection to McIDAS-X Bridge Listener at " + getHost() + ":" + getPort() + " failed");
161            showNormalCursor();
162            return;
163        }
164
165        Hashtable ht = new Hashtable();
166        ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.FRAME_NUMBERS_KEY, getFrameNumbers());
167        if (getFrameCount() > 1) {
168           ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.DATA_NAME_KEY,"Frame Sequence");
169        } else {
170           ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.DATA_NAME_KEY,"Frame");
171        }
172        ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.REQUEST_HOST, getHost());
173        ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.REQUEST_PORT, getPort());
174        ht.put(edu.wisc.ssec.mcidasv.chooser.FrameChooser.REQUEST_KEY, this.mcidasxInfo.getKeyString());
175        //System.out.println("    ht:  " + ht);
176        makeDataSource("", "MCIDASX", ht);
177        showNormalCursor();
178    }
179
180    private JLabel statusLabel = new JLabel("Status");
181
182    @Override
183    public void setStatus(String statusString, String foo) {
184        if (statusString == null)
185            statusString = "";
186        statusLabel.setText(statusString);
187    }
188
189    /**
190     * Make the GUI.
191     *
192     * @return The GUI.
193     */
194    protected JComponent doMakeContents() {
195        JPanel myPanel = new JPanel();
196        
197        
198        JLabel hostLabel = McVGuiUtils.makeLabelRight("Host:");
199        
200        hostLine.setText(mcidasxInfo.getHostString());
201        McVGuiUtils.setComponentWidth(hostLine, Width.DOUBLE);
202        hostLine.addFocusListener(new FocusListener() {
203            public void focusGained(FocusEvent e) {}
204            public void focusLost(FocusEvent e) { setHost(); }
205        });
206        hostLine.addActionListener(new ActionListener() {
207            public void actionPerformed(ActionEvent ae) { setHost(); }
208        });
209        
210        JLabel portLabel = McVGuiUtils.makeLabelRight("Port:");
211
212        portLine.setText(mcidasxInfo.getPortString());
213        McVGuiUtils.setComponentWidth(portLine, Width.DOUBLE);
214        portLine.addFocusListener(new FocusListener() {
215            public void focusGained(FocusEvent e) {}
216            public void focusLost(FocusEvent e) { setPort(); }
217        });
218        portLine.addActionListener(new ActionListener() {
219            public void actionPerformed(ActionEvent ae) { setPort(); }
220        });
221                
222        JLabel statusLabelLabel = McVGuiUtils.makeLabelRight("");
223
224        statusLabel.setText("Press \"" + getLoadCommandName() + "\" to connect to the McIDAS-X Bridge Listener");
225        McVGuiUtils.setLabelPosition(statusLabel, Position.RIGHT);
226        McVGuiUtils.setComponentColor(statusLabel, TextColor.STATUS);
227                
228        JButton helpButton = McVGuiUtils.makeImageButton(ICON_HELP, "Show help");
229        helpButton.setActionCommand(GuiUtils.CMD_HELP);
230        helpButton.addActionListener(this);
231        
232        McVGuiUtils.setComponentWidth(loadButton, Width.DOUBLE);
233        
234        GroupLayout layout = new GroupLayout(myPanel);
235        myPanel.setLayout(layout);
236        layout.setHorizontalGroup(
237            layout.createParallelGroup(LEADING)
238            .addGroup(layout.createSequentialGroup()
239                .addContainerGap()
240                .addGroup(layout.createParallelGroup(LEADING)
241                    .addGroup(layout.createSequentialGroup()
242                        .addComponent(hostLabel)
243                        .addGap(GAP_RELATED)
244                        .addComponent(hostLine))
245                    .addGroup(layout.createSequentialGroup()
246                        .addComponent(portLabel)
247                        .addGap(GAP_RELATED)
248                        .addComponent(portLine))
249                    .addGroup(TRAILING, layout.createSequentialGroup()
250                        .addComponent(helpButton)
251                        .addPreferredGap(RELATED)
252                        .addComponent(loadButton))
253                    .addGroup(layout.createSequentialGroup()
254                        .addComponent(statusLabelLabel)
255                        .addGap(GAP_RELATED)
256                        .addComponent(statusLabel, DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE)))
257                .addContainerGap())
258        );
259        layout.setVerticalGroup(
260            layout.createParallelGroup(LEADING)
261            .addGroup(TRAILING, layout.createSequentialGroup()
262                .addContainerGap()
263                .addGroup(layout.createParallelGroup(BASELINE)
264                    .addComponent(hostLine)
265                    .addComponent(hostLabel))
266                .addPreferredGap(RELATED)
267                .addGroup(layout.createParallelGroup(BASELINE)
268                    .addComponent(portLine)
269                    .addComponent(portLabel))
270                .addPreferredGap(RELATED, DEFAULT_SIZE, Short.MAX_VALUE)
271                .addGroup(layout.createParallelGroup(BASELINE)
272                    .addComponent(statusLabelLabel)
273                    .addComponent(statusLabel))
274                .addPreferredGap(UNRELATED)
275                .addGroup(layout.createParallelGroup(BASELINE)
276                    .addComponent(loadButton)
277                    .addComponent(helpButton))
278                .addContainerGap())
279        );
280        
281        return myPanel;
282        
283    }
284
285}