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