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