001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.preferences;
020
021import java.awt.GridBagConstraints;
022import java.awt.GridBagLayout;
023import java.awt.Insets;
024import java.awt.event.ActionEvent;
025import java.awt.event.ActionListener;
026
027import javax.swing.JButton;
028import javax.swing.JCheckBox;
029import javax.swing.JLabel;
030import javax.swing.JPasswordField;
031
032import icy.gui.component.IcyTextField;
033import icy.preferences.ChatPreferences;
034
035public class ChatPreferencePanel extends PreferencePanel
036{
037    /**
038     * 
039     */
040    private static final long serialVersionUID = 2856629717614258089L;
041
042    public static final String NODE_NAME = "Chat";
043
044    private IcyTextField realNameField;
045    private JPasswordField passwordField;
046    IcyTextField extraChannelsField;
047    private JCheckBox connectAtStartCheckBox;
048    private JCheckBox enableDesktopOverlayCheckBox;
049    IcyTextField desktopChannelsField;
050    private JCheckBox statusMessageCheckBox;
051
052    /**
053     * Create the panel.
054     */
055    public ChatPreferencePanel(PreferenceFrame parent)
056    {
057        super(parent, NODE_NAME, PreferenceFrame.NODE_NAME);
058
059        initialize();
060        validate();
061
062        load();
063    }
064
065    void initialize()
066    {
067        GridBagLayout gridBagLayout = new GridBagLayout();
068        gridBagLayout.columnWidths = new int[] {46, 97, 0, 0, 18, 60, 0, 0};
069        gridBagLayout.rowHeights = new int[] {0, 0, 0, 0, 0, 0, 0};
070        gridBagLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
071        gridBagLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
072        mainPanel.setLayout(gridBagLayout);
073
074        connectAtStartCheckBox = new JCheckBox("Connect at start up");
075        connectAtStartCheckBox.setToolTipText("Automatically connect when application starts");
076        GridBagConstraints gbc_connectAtStartCheckBox = new GridBagConstraints();
077        gbc_connectAtStartCheckBox.anchor = GridBagConstraints.WEST;
078        gbc_connectAtStartCheckBox.gridwidth = 4;
079        gbc_connectAtStartCheckBox.insets = new Insets(0, 0, 5, 5);
080        gbc_connectAtStartCheckBox.gridx = 0;
081        gbc_connectAtStartCheckBox.gridy = 0;
082        mainPanel.add(connectAtStartCheckBox, gbc_connectAtStartCheckBox);
083
084        statusMessageCheckBox = new JCheckBox("Show status messages");
085        statusMessageCheckBox.setToolTipText("Show chat status change messages");
086        GridBagConstraints gbc_statusMessageCheckBox = new GridBagConstraints();
087        gbc_statusMessageCheckBox.anchor = GridBagConstraints.WEST;
088        gbc_statusMessageCheckBox.gridwidth = 4;
089        gbc_statusMessageCheckBox.insets = new Insets(0, 0, 5, 5);
090        gbc_statusMessageCheckBox.gridx = 0;
091        gbc_statusMessageCheckBox.gridy = 1;
092        mainPanel.add(statusMessageCheckBox, gbc_statusMessageCheckBox);
093
094        enableDesktopOverlayCheckBox = new JCheckBox("Enable desktop chat");
095        enableDesktopOverlayCheckBox.setToolTipText("Display chat in the application desktop");
096        GridBagConstraints gbc_enableDesktopOverlayCheckBox = new GridBagConstraints();
097        gbc_enableDesktopOverlayCheckBox.anchor = GridBagConstraints.WEST;
098        gbc_enableDesktopOverlayCheckBox.gridwidth = 2;
099        gbc_enableDesktopOverlayCheckBox.insets = new Insets(0, 0, 5, 5);
100        gbc_enableDesktopOverlayCheckBox.gridx = 0;
101        gbc_enableDesktopOverlayCheckBox.gridy = 2;
102        mainPanel.add(enableDesktopOverlayCheckBox, gbc_enableDesktopOverlayCheckBox);
103
104        desktopChannelsField = new IcyTextField();
105        desktopChannelsField.setToolTipText(
106                "Channel(s) to display on dekstop chat. You can enter severals channels (ex : \"icy;icy-support\")");
107        desktopChannelsField.setText("icy");
108        GridBagConstraints gbc_desktopChannelsField = new GridBagConstraints();
109        gbc_desktopChannelsField.gridwidth = 2;
110        gbc_desktopChannelsField.insets = new Insets(0, 0, 5, 5);
111        gbc_desktopChannelsField.fill = GridBagConstraints.HORIZONTAL;
112        gbc_desktopChannelsField.gridx = 2;
113        gbc_desktopChannelsField.gridy = 2;
114        mainPanel.add(desktopChannelsField, gbc_desktopChannelsField);
115        desktopChannelsField.setColumns(10);
116
117        JButton btnDefault_1 = new JButton("Default");
118        btnDefault_1.addActionListener(new ActionListener()
119        {
120            @Override
121            public void actionPerformed(ActionEvent e)
122            {
123                desktopChannelsField.setText(ChatPreferences.getDefaultDesktopChannels());
124            }
125        });
126        GridBagConstraints gbc_btnDefault_1 = new GridBagConstraints();
127        gbc_btnDefault_1.insets = new Insets(0, 0, 5, 5);
128        gbc_btnDefault_1.gridx = 5;
129        gbc_btnDefault_1.gridy = 2;
130        mainPanel.add(btnDefault_1, gbc_btnDefault_1);
131
132        JLabel lblNewLabel_2 = new JLabel("Real name");
133        lblNewLabel_2.setToolTipText("Real name (give more information about user)");
134        GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
135        gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
136        gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
137        gbc_lblNewLabel_2.gridx = 0;
138        gbc_lblNewLabel_2.gridy = 3;
139        mainPanel.add(lblNewLabel_2, gbc_lblNewLabel_2);
140
141        realNameField = new IcyTextField();
142        realNameField.setToolTipText("Real name (give more information about user)");
143        GridBagConstraints gbc_realNameField = new GridBagConstraints();
144        gbc_realNameField.fill = GridBagConstraints.HORIZONTAL;
145        gbc_realNameField.gridwidth = 3;
146        gbc_realNameField.insets = new Insets(0, 0, 5, 5);
147        gbc_realNameField.gridx = 1;
148        gbc_realNameField.gridy = 3;
149        mainPanel.add(realNameField, gbc_realNameField);
150        realNameField.setColumns(10);
151
152        JLabel lblPassword = new JLabel("Password");
153        lblPassword.setToolTipText("Password for registered nickname only");
154        GridBagConstraints gbc_lblPassword = new GridBagConstraints();
155        gbc_lblPassword.anchor = GridBagConstraints.EAST;
156        gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
157        gbc_lblPassword.gridx = 0;
158        gbc_lblPassword.gridy = 4;
159        mainPanel.add(lblPassword, gbc_lblPassword);
160
161        passwordField = new JPasswordField();
162        passwordField.setColumns(12);
163        passwordField.setToolTipText("Password for registered nickname only");
164        GridBagConstraints gbc_passwordField = new GridBagConstraints();
165        gbc_passwordField.gridwidth = 3;
166        gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
167        gbc_passwordField.insets = new Insets(0, 0, 5, 5);
168        gbc_passwordField.gridx = 1;
169        gbc_passwordField.gridy = 4;
170        mainPanel.add(passwordField, gbc_passwordField);
171
172        JLabel lblChannels = new JLabel("Extra channels");
173        lblChannels.setToolTipText(
174                "Extra channels to join at start up. You can enter severals channels (ex : \"icy-news;icy-support;others\")");
175        GridBagConstraints gbc_lblChannels = new GridBagConstraints();
176        gbc_lblChannels.anchor = GridBagConstraints.EAST;
177        gbc_lblChannels.insets = new Insets(0, 0, 0, 5);
178        gbc_lblChannels.gridx = 0;
179        gbc_lblChannels.gridy = 5;
180        mainPanel.add(lblChannels, gbc_lblChannels);
181
182        extraChannelsField = new IcyTextField();
183        extraChannelsField.setToolTipText(
184                "Extra channel(s) to join at start up. You can enter severals channels (ex : \"icy-news;icy-support;...\")");
185        GridBagConstraints gbc_channelsField = new GridBagConstraints();
186        gbc_channelsField.gridwidth = 3;
187        gbc_channelsField.insets = new Insets(0, 0, 0, 5);
188        gbc_channelsField.fill = GridBagConstraints.HORIZONTAL;
189        gbc_channelsField.gridx = 1;
190        gbc_channelsField.gridy = 5;
191        mainPanel.add(extraChannelsField, gbc_channelsField);
192        extraChannelsField.setColumns(10);
193
194        JButton btnDefault = new JButton("Default");
195        btnDefault.addActionListener(new ActionListener()
196        {
197            @Override
198            public void actionPerformed(ActionEvent e)
199            {
200                extraChannelsField.setText(ChatPreferences.getDefaultExtraChannels());
201            }
202        });
203        GridBagConstraints gbc_btnDefault = new GridBagConstraints();
204        gbc_btnDefault.insets = new Insets(0, 0, 0, 5);
205        gbc_btnDefault.gridx = 5;
206        gbc_btnDefault.gridy = 5;
207        mainPanel.add(btnDefault, gbc_btnDefault);
208    }
209
210    @Override
211    protected void load()
212    {
213        realNameField.setText(ChatPreferences.getRealname());
214        passwordField.setText(ChatPreferences.getUserPassword());
215        extraChannelsField.setText(ChatPreferences.getExtraChannels());
216        connectAtStartCheckBox.setSelected(ChatPreferences.getAutoConnect());
217        statusMessageCheckBox.setSelected(ChatPreferences.getShowStatusMessages());
218        enableDesktopOverlayCheckBox.setSelected(ChatPreferences.getDesktopOverlay());
219        desktopChannelsField.setText(ChatPreferences.getDesktopChannels());
220    }
221
222    @Override
223    protected void save()
224    {
225        ChatPreferences.setRealname(realNameField.getText());
226        ChatPreferences.setUserPassword(new String(passwordField.getPassword()));
227        ChatPreferences.setExtraChannels(extraChannelsField.getText());
228        ChatPreferences.setAutoConnect(connectAtStartCheckBox.isSelected());
229        ChatPreferences.setShowStatusMessages(statusMessageCheckBox.isSelected());
230        ChatPreferences.setDesktopOverlay(enableDesktopOverlayCheckBox.isSelected());
231        ChatPreferences.setDesktopChannels(desktopChannelsField.getText());
232
233        // final MainFrame mainFrame = Icy.getMainInterface().getMainFrame();
234        //
235        // repaint desktop pane for desktop overlay change
236        // if (mainFrame != null)
237        // mainFrame.getChat().refreshDesktopOverlayState();
238    }
239
240}