/*
 * Syncany, www.syncany.org
 * Copyright (C) 2011 Philipp C. Heckel <philipp.heckel@gmail.com> 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.syncany.connection.plugins.webdav;

import java.util.Locale;
import java.util.ResourceBundle;

import org.syncany.Constants;
import org.syncany.config.Config;
import org.syncany.connection.plugins.ConfigPanel;

/**
 *
 * @author Philipp C. Heckel <philipp.heckel@gmail.com>
 */
public class WebdavConfigPanel extends ConfigPanel {

    private ResourceBundle resourceBundle;

    /** Creates new form FtpConnectionPanel */
    public WebdavConfigPanel(WebdavConnection connection) {
        super(connection);
        resourceBundle = Config.getInstance().getResourceBundle();
        initComponents();
    }

    @Override
    public void load() {
        cmbProtocol.setSelectedIndex(getConnection().isSecure() ? 1 : 0);
        txtHost.setText(getConnection().getHost());
        spnPort.setValue(new Integer((getConnection().getPort() == 0) ? 21 : getConnection().getPort()));
        txtUsername.setText(getConnection().getUsername());
        txtPassword.setText(getConnection().getPassword());
        txtFolder.setText(getConnection().getPath());
    }

    @Override
    public void save() {
        getConnection().setSecure(cmbProtocol.getSelectedIndex() == 1);
        getConnection().setHost(txtHost.getText());
        getConnection().setPort((Integer) spnPort.getValue());
        getConnection().setUsername(txtUsername.getText());
        getConnection().setPassword(new String(txtPassword.getPassword()));
        getConnection().setPath(txtFolder.getText());
    }

    @Override
    public WebdavConnection getConnection() {
        return (WebdavConnection) super.getConnection();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                jLabel1 = new javax.swing.JLabel();
                txtHost = new javax.swing.JTextField();
                jLabel2 = new javax.swing.JLabel();
                txtUsername = new javax.swing.JTextField();
                jLabel3 = new javax.swing.JLabel();
                jLabel4 = new javax.swing.JLabel();
                spnPort = new javax.swing.JSpinner();
                txtPassword = new javax.swing.JPasswordField();
                jLabel7 = new javax.swing.JLabel();
                txtFolder = new javax.swing.JTextField();
                cmbProtocol = new javax.swing.JComboBox();

                jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
                jLabel1.setText(resourceBundle.getString("webdav_server_name"));
                jLabel1.setName("jLabel1"); // NOI18N

                txtHost.setName("txtHost"); // NOI18N

                jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
                jLabel2.setText(resourceBundle.getString("webdav_username"));
                jLabel2.setName("jLabel2"); // NOI18N

                txtUsername.setName("txtUsername"); // NOI18N

                jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
                jLabel3.setText(resourceBundle.getString("webdav_password"));
                jLabel3.setName("jLabel3"); // NOI18N

                jLabel4.setText(resourceBundle.getString("webdav_port"));
                jLabel4.setName("jLabel4"); // NOI18N

                spnPort.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(80), Integer.valueOf(1), null, Integer.valueOf(1)));
                spnPort.setName("spnPort"); // NOI18N

                txtPassword.setName("txtPassword"); // NOI18N

                jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
                jLabel7.setText(resourceBundle.getString("webdav_path"));
                jLabel7.setName("jLabel7"); // NOI18N

                txtFolder.setName("txtFolder"); // NOI18N

                cmbProtocol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "http", "https" }));
                cmbProtocol.setName("cmbProtocol"); // NOI18N

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
                this.setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel1)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel7))
                                .addGap(57, 57, 57)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(cmbProtocol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtHost, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel4)
                                                .addGap(18, 18, 18)
                                                .addComponent(spnPort, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                                        .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                                        .addComponent(txtFolder, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE))
                                .addContainerGap())
                );
                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(txtHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(cmbProtocol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel4)
                                        .addComponent(spnPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel3))
                                .addGap(13, 13, 13)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(txtFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel7))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );
        }// </editor-fold>//GEN-END:initComponents
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JComboBox cmbProtocol;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JSpinner spnPort;
        private javax.swing.JTextField txtFolder;
        private javax.swing.JTextField txtHost;
        private javax.swing.JPasswordField txtPassword;
        private javax.swing.JTextField txtUsername;
        // End of variables declaration//GEN-END:variables
}
