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 */
028package edu.wisc.ssec.mcidasv.servermanager;
029
030import static edu.wisc.ssec.mcidasv.util.McVGuiUtils.safeGetText;
031
032import java.awt.BorderLayout;
033import java.awt.FlowLayout;
034import java.awt.event.ActionListener;
035import java.awt.event.ActionEvent;
036
037import javax.swing.JButton;
038import javax.swing.JCheckBox;
039import javax.swing.JDialog;
040import javax.swing.JLabel;
041import javax.swing.JPanel;
042import javax.swing.JTextField;
043import javax.swing.SwingUtilities;
044import javax.swing.WindowConstants;
045import javax.swing.border.EmptyBorder;
046import javax.swing.event.DocumentListener;
047import javax.swing.event.DocumentEvent;
048
049import org.slf4j.Logger;
050import org.slf4j.LoggerFactory;
051
052import net.miginfocom.swing.MigLayout;
053
054/**
055 * Class used to import from a remote {@literal "MCTABLE.TXT"} file.
056 */
057public class ImportUrl extends JDialog implements ActionListener {
058    
059    protected static final String CMD_DEFAULT_ACCOUNTING =
060        "use_default_accounting";
061
062    protected static final String CMD_IMPORT = "import";
063
064    protected static final String CMD_CANCEL = "cancel";
065    
066    private TabbedAddeManager serverManagerGui;
067    private EntryStore serverManager;
068    
069    private static final Logger logger =
070        LoggerFactory.getLogger(ImportUrl.class);
071    
072    private JLabel userLabel;
073    private JLabel projLabel;
074    private JCheckBox acctBox;
075    private JTextField mctableField;
076    private JTextField userField;
077    private JTextField projField;
078    private JButton okButton;
079    private JButton cancelButton;
080    
081    private final JPanel contentPanel = new JPanel();
082    
083    
084    /**
085     * Create the dialog.
086     */
087    public ImportUrl() {
088        initComponents();
089    }
090    
091    public ImportUrl(final EntryStore serverManager,
092                     final TabbedAddeManager serverManagerGui)
093    {
094        this.serverManager = serverManager;
095        this.serverManagerGui = serverManagerGui;
096        initComponents();
097    }
098
099    public void initComponents() {
100        setTitle("Import from URL");
101        setBounds(100, 100, 450, 215);
102        getContentPane().setLayout(new BorderLayout());
103        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
104        getContentPane().add(contentPanel, BorderLayout.CENTER);
105        contentPanel.setLayout(new MigLayout("", "[][grow]", "[][][][]"));
106        
107        JLabel mctableLabel = new JLabel("MCTABLE.TXT URL:");
108        contentPanel.add(mctableLabel, "cell 0 0,alignx trailing");
109        
110        mctableField = new JTextField();
111        contentPanel.add(mctableField, "cell 1 0,growx");
112        mctableField.getDocument().addDocumentListener(new DocumentListener() {
113            public void changedUpdate(final DocumentEvent e) {
114                int len = safeGetText(mctableField).trim().length();
115//                okButton.setEnabled(mctableField.getText().trim().length() > 0);
116                okButton.setEnabled(len > 0);
117                logger.trace("len={}", len);
118            }
119            public void insertUpdate(final DocumentEvent e) {}
120            public void removeUpdate(final DocumentEvent e) {}
121        });
122        
123        acctBox = new JCheckBox("Use ADDE accounting?");
124        acctBox.addActionListener(e -> {
125            boolean selected = acctBox.isSelected();
126            userLabel.setEnabled(selected);
127            userField.setEnabled(selected);
128            projLabel.setEnabled(selected);
129            projField.setEnabled(selected);
130        });
131        acctBox.setSelected(false);
132        contentPanel.add(acctBox, "cell 1 1");
133        
134        userLabel = new JLabel("Username:");
135        userLabel.setEnabled(acctBox.isSelected());
136        contentPanel.add(userLabel, "cell 0 2,alignx trailing");
137        
138        userField = new JTextField();
139        contentPanel.add(userField, "cell 1 2,growx");
140        userField.setColumns(4);
141        userField.setEnabled(acctBox.isSelected());
142        
143        projLabel = new JLabel("Project #:");
144        projLabel.setEnabled(acctBox.isSelected());
145        contentPanel.add(projLabel, "cell 0 3,alignx trailing");
146        
147        projField = new JTextField();
148        contentPanel.add(projField, "cell 1 3,growx");
149        projField.setColumns(4);
150        projField.setEnabled(acctBox.isSelected());
151        
152        {
153            JPanel buttonPane = new JPanel();
154            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
155            getContentPane().add(buttonPane, BorderLayout.SOUTH);
156            {
157                okButton = new JButton("Import MCTABLE.TXT");
158                okButton.setActionCommand(CMD_IMPORT);
159                okButton.addActionListener(this);
160                buttonPane.add(okButton);
161//                getRootPane().setDefaultButton(okButton);
162            }
163            {
164                cancelButton = new JButton("Cancel");
165                cancelButton.setActionCommand(CMD_CANCEL);
166                cancelButton.addActionListener(this);
167                buttonPane.add(cancelButton);
168            }
169        }
170        
171    }
172
173    public void actionPerformed(final ActionEvent e) {
174        String cmd = e.getActionCommand();
175        if (CMD_CANCEL.equals(cmd)) {
176            dispose();
177        } else if (CMD_IMPORT.equals(cmd)) {
178            
179            String path = safeGetText(mctableField).trim();
180            String user = AddeEntry.DEFAULT_ACCOUNT.getUsername();
181            String proj = AddeEntry.DEFAULT_ACCOUNT.getProject();
182
183            if (acctBox.isSelected()) {
184                user = safeGetText(userField).trim();
185                proj = safeGetText(projField).trim();
186            }
187
188            logger.trace("importing: path={} user={} proj={}", path, user, proj);
189
190            if (serverManagerGui != null) {
191                serverManagerGui.importMctable(path, user, proj);
192            }
193            dispose();
194        }
195    }
196    
197    /**
198     * Launch the application.
199     *
200     * @param args Ignored.
201     */
202    public static void main(String[] args) {
203        SwingUtilities.invokeLater(() -> {
204            try {
205                ImportUrl tmp = new ImportUrl();
206                tmp.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
207                tmp.setVisible(true);
208            } catch (Exception e) {
209                logger.error("Error creating ImportUrl", e);
210            }
211        });
212
213    }
214}