001package icy.gui.preferences;
002
003import icy.gui.component.IcyTextField;
004import icy.gui.dialog.ActionDialog;
005import icy.gui.util.ComponentUtil;
006import icy.preferences.RepositoryPreferences.RepositoryInfo;
007
008import java.awt.BorderLayout;
009import java.awt.Dimension;
010import java.awt.event.ActionEvent;
011import java.awt.event.ActionListener;
012
013import javax.swing.BorderFactory;
014import javax.swing.Box;
015import javax.swing.BoxLayout;
016import javax.swing.JCheckBox;
017import javax.swing.JLabel;
018import javax.swing.JPanel;
019import javax.swing.JPasswordField;
020import javax.swing.event.ChangeEvent;
021import javax.swing.event.ChangeListener;
022
023public class EditRepositoryDialog extends ActionDialog
024{
025    /**
026     * 
027     */
028    private static final long serialVersionUID = 893945926064333575L;
029
030    // GUI
031    IcyTextField nameField;
032    IcyTextField locationField;
033    JCheckBox supportParamCheckBox;
034    JCheckBox authCheckBox;
035    IcyTextField loginField;
036    JPasswordField passwordField;
037
038    JLabel nameLabel;
039    JLabel locationLabel;
040    JLabel supportParamLabel;
041    JLabel authLabel;
042    JLabel loginLabel;
043    JLabel passwordLabel;
044
045    // internal
046    final RepositoryInfo reposInf;
047
048    /**
049     * Create the dialog.
050     */
051    public EditRepositoryDialog(String title, RepositoryInfo reposInf)
052    {
053        super(title);
054
055        this.reposInf = reposInf;
056
057        initialize();
058
059        updateAuthFields();
060
061        // center on screen and make it visible
062        pack();
063        ComponentUtil.center(this);
064        setVisible(true);
065    }
066
067    private void initialize()
068    {
069        setMinimumSize(new Dimension(400, 200));
070        setBounds(100, 100, 450, 300);
071
072        // setPreferredSize(new Dimension(600, 200));
073
074        nameField = new IcyTextField();
075        nameField.setText(reposInf.getName());
076        ComponentUtil.setFixedHeight(nameField, 24);
077        locationField = new IcyTextField();
078        locationField.setText(reposInf.getLocation());
079        ComponentUtil.setFixedHeight(locationField, 24);
080        authCheckBox = new JCheckBox("", reposInf.isAuthenticationEnabled());
081        ComponentUtil.setFixedHeight(authCheckBox, 24);
082        loginField = new IcyTextField(reposInf.getLogin());
083        loginField.setText(reposInf.getLogin());
084        ComponentUtil.setFixedHeight(loginField, 24);
085        passwordField = new JPasswordField(reposInf.getPassword());
086        ComponentUtil.setFixedHeight(passwordField, 24);
087        supportParamCheckBox = new JCheckBox("", reposInf.getSupportParam());
088        ComponentUtil.setFixedHeight(supportParamCheckBox, 24);
089
090        authCheckBox.addChangeListener(new ChangeListener()
091        {
092            @Override
093            public void stateChanged(ChangeEvent e)
094            {
095                updateAuthFields();
096            }
097        });
098
099        // save changes on validation
100        setOkAction(new ActionListener()
101        {
102            @Override
103            public void actionPerformed(ActionEvent e)
104            {
105                reposInf.setName(nameField.getText());
106                reposInf.setLocation(locationField.getText());
107                reposInf.setSupportParam(supportParamCheckBox.isSelected());
108                reposInf.setAuthenticationEnabled(authCheckBox.isSelected());
109                reposInf.setLogin(loginField.getText());
110                reposInf.setPassword(new String(passwordField.getPassword()));
111            }
112        });
113
114        mainPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
115        mainPanel.setLayout(new BorderLayout(8, 8));
116
117        final JPanel labelPanel = new JPanel();
118        labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.PAGE_AXIS));
119
120        nameLabel = new JLabel("Name");
121        nameLabel.setToolTipText("Repository name");
122        ComponentUtil.setFixedHeight(nameLabel, 24);
123        locationLabel = new JLabel("Location");
124        locationLabel.setToolTipText("Repository address");
125        ComponentUtil.setFixedHeight(locationLabel, 24);
126        supportParamLabel = new JLabel("Enable extra parameters");
127        supportParamLabel.setToolTipText("Enable extra parameters when querying the XML file (only for online repository)");
128        ComponentUtil.setFixedHeight(supportParamLabel, 24);
129        authLabel = new JLabel("Use authentication");
130        authLabel.setToolTipText("Enable authentication to access the repository");
131        ComponentUtil.setFixedHeight(authLabel, 24);
132        loginLabel = new JLabel("Login");
133        ComponentUtil.setFixedHeight(loginLabel, 24);
134        passwordLabel = new JLabel("Password");
135        ComponentUtil.setFixedHeight(passwordLabel, 24);
136
137        labelPanel.add(nameLabel);
138        labelPanel.add(Box.createVerticalStrut(4));
139        labelPanel.add(locationLabel);
140        labelPanel.add(Box.createVerticalStrut(4));
141        labelPanel.add(supportParamLabel);
142        labelPanel.add(Box.createVerticalStrut(4));
143        labelPanel.add(authLabel);
144        labelPanel.add(Box.createVerticalStrut(4));
145        labelPanel.add(loginLabel);
146        labelPanel.add(Box.createVerticalStrut(4));
147        labelPanel.add(passwordLabel);
148        labelPanel.add(Box.createVerticalGlue());
149
150        final JPanel fieldPanel = new JPanel();
151        fieldPanel.setLayout(new BoxLayout(fieldPanel, BoxLayout.PAGE_AXIS));
152
153        fieldPanel.add(nameField);
154        fieldPanel.add(Box.createVerticalStrut(4));
155        fieldPanel.add(locationField);
156        fieldPanel.add(Box.createVerticalStrut(4));
157        fieldPanel.add(supportParamCheckBox);
158        fieldPanel.add(Box.createVerticalStrut(4));
159        fieldPanel.add(authCheckBox);
160        fieldPanel.add(Box.createVerticalStrut(4));
161        fieldPanel.add(loginField);
162        fieldPanel.add(Box.createVerticalStrut(4));
163        fieldPanel.add(passwordField);
164        fieldPanel.add(Box.createVerticalGlue());
165
166        mainPanel.add(labelPanel, BorderLayout.WEST);
167        mainPanel.add(fieldPanel, BorderLayout.CENTER);
168    }
169
170    void updateAuthFields()
171    {
172        final boolean enabled = authCheckBox.isSelected();
173
174        loginLabel.setEnabled(enabled);
175        loginField.setEnabled(enabled);
176        passwordLabel.setEnabled(enabled);
177        passwordField.setEnabled(enabled);
178    }
179}