aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/generalconfig/GeneralConfigurationForm.java
blob: 613f85c35f3e463c14fc9e7b273b4d70954c8349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
package net.java.sip.communicator.plugin.generalconfig;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.util.*;

import java.io.*;
import com.izforge.izpack.util.os.*;

/**
 * 
 * @author Yana Stamcheva
 */
public class GeneralConfigurationForm
    extends javax.swing.JPanel
    implements  ConfigurationForm,
                ActionListener
{
    private Logger logger = Logger.getLogger(GeneralConfigurationForm.class);

    private JPanel mainPanel;
    private JCheckBox bringToFrontCheckBox;
    private JCheckBox autoStartCheckBox;
    private JCheckBox groupMessagesCheckBox;
    private JCheckBox logHistoryCheckBox;
    private JPanel sendMessagePanel;
    private JLabel sendMessageLabel;
    private JComboBox sendMessageComboBox;
    private JLabel historySizeLabel;
    private JSpinner historySizeSpinner;
    private JCheckBox enableTypingNotifiCheckBox;
    private JCheckBox showHistoryCheckBox;
    private JPanel logHistoryPanel;

    public GeneralConfigurationForm()
    {
        super();
        initGUI();
        initDefaults();
    }

    private void initGUI()
    {
        BorderLayout borderLayout = new BorderLayout();
        this.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        this.setLayout(borderLayout);
        setPreferredSize(new Dimension(500, 300));
        {
            mainPanel = new JPanel();
            BoxLayout boxLayout = new BoxLayout(
                mainPanel, javax.swing.BoxLayout.Y_AXIS);
            mainPanel.setLayout(boxLayout);
            this.add(mainPanel, BorderLayout.NORTH);

            String osName = System.getProperty("os.name");
            if (osName.startsWith("Windows"))
            {
                autoStartCheckBox = new JCheckBox();
                mainPanel.add(autoStartCheckBox);
                mainPanel.add(Box.createVerticalStrut(10));
                autoStartCheckBox.setText(
                    Resources.getString("autoStartOption"));
                initAutoStartCheckBox();
                autoStartCheckBox.addActionListener(this);
            }
            {
                groupMessagesCheckBox = new JCheckBox();
                mainPanel.add(groupMessagesCheckBox);
                mainPanel.add(Box.createVerticalStrut(10));
                groupMessagesCheckBox.setText(
                    Resources.getString("groupChatMessages"));
                groupMessagesCheckBox.addActionListener(this);
            }
            {
                logHistoryPanel = new JPanel();
                mainPanel.add(logHistoryPanel);
                mainPanel.add(Box.createVerticalStrut(10));
                logHistoryPanel.setLayout(null);
                logHistoryPanel.setPreferredSize(
                    new java.awt.Dimension(380, 57));
                logHistoryPanel.setAlignmentX(0.0f);
                {
                    logHistoryCheckBox = new JCheckBox();
                    logHistoryPanel.add(logHistoryCheckBox);
                    logHistoryCheckBox.setText(
                        Resources.getString("logHistory"));
                    logHistoryCheckBox.setBounds(0, 0, 200, 19);
                    logHistoryCheckBox.addActionListener(this);
                    logHistoryCheckBox.addChangeListener(new ChangeListener()
                    {
                        public void stateChanged(ChangeEvent e)
                        {
                            showHistoryCheckBox.setEnabled(
                                logHistoryCheckBox.isSelected());
                            historySizeSpinner.setEnabled(
                                logHistoryCheckBox.isSelected());
                        }
                    });
                }
                {
                    showHistoryCheckBox = new JCheckBox();
                    logHistoryPanel.add(showHistoryCheckBox);
                    showHistoryCheckBox.setText(
                        Resources.getString("showHistory"));
                    showHistoryCheckBox.setBounds(17, 25, 140, 19);
                    showHistoryCheckBox.addActionListener(this);
                    showHistoryCheckBox.addChangeListener(new ChangeListener()
                    {
                        public void stateChanged(ChangeEvent e)
                        {
                            historySizeSpinner.setEnabled(
                                showHistoryCheckBox.isSelected());
                        }
                    });
                }
                {
                    SpinnerNumberModel historySizeSpinnerModel = 
                        new SpinnerNumberModel(0, 0, 100, 1);
                    historySizeSpinner = new JSpinner();
                    logHistoryPanel.add(historySizeSpinner);
                    historySizeSpinner.setModel(historySizeSpinnerModel);
                    historySizeSpinner.setBounds(150, 23, 47, 22);
                    historySizeSpinnerModel.addChangeListener(
                        new ChangeListener()
                        {
                            public void stateChanged(ChangeEvent e)
                            {
                                ConfigurationManager.setChatHistorySize(
                                    ((Integer) historySizeSpinner
                                        .getValue()).intValue());
                            }
                        });
                }
                {
                    historySizeLabel = new JLabel();
                    logHistoryPanel.add(historySizeLabel);
                    historySizeLabel.setText(
                        Resources.getString("historySize"));
                    historySizeLabel.setBounds(205, 27, 220, 15);
                }
            }
            {
                sendMessagePanel = new JPanel();
                BorderLayout sendMessagePanelLayout
                    = new BorderLayout(10, 10);
                sendMessagePanel.setLayout(sendMessagePanelLayout);
                mainPanel.add(sendMessagePanel);
                mainPanel.add(Box.createVerticalStrut(10));
                sendMessagePanel.setAlignmentX(0.0f);
                sendMessagePanel.setPreferredSize(
                    new java.awt.Dimension(380, 22));
                {
                    sendMessageLabel = new JLabel();
                    sendMessagePanel.add(
                        sendMessageLabel, BorderLayout.WEST);
                    sendMessageLabel.setText(
                        Resources.getString("sendMessagesWith"));
                }
                {
                    ComboBoxModel sendMessageComboBoxModel = 
                        new DefaultComboBoxModel(
                            new String[] {
                                ConfigurationManager.ENTER_COMMAND,
                                ConfigurationManager.CTRL_ENTER_COMMAND });
                    sendMessageComboBox = new JComboBox();
                    sendMessagePanel.add(
                        sendMessageComboBox, BorderLayout.CENTER);
                    sendMessageComboBox.setModel(sendMessageComboBoxModel);
                    sendMessageComboBox.addItemListener(new ItemListener()
                    {
                        public void itemStateChanged(ItemEvent arg0)
                        {
                            ConfigurationManager.setSendMessageCommand(
                                (String)sendMessageComboBox.getSelectedItem());
                        }
                    });
                }
            }
            {
                enableTypingNotifiCheckBox = new JCheckBox();
                enableTypingNotifiCheckBox.setLayout(null);
                mainPanel.add(enableTypingNotifiCheckBox);
                mainPanel.add(Box.createVerticalStrut(10));
                enableTypingNotifiCheckBox.setText(
                    Resources.getString("enableTypingNotifications"));
                enableTypingNotifiCheckBox.setPreferredSize(
                    new java.awt.Dimension(253, 20));
                enableTypingNotifiCheckBox.setAlignmentY(0.0f);
                enableTypingNotifiCheckBox.addActionListener(this);
            }
            {
                bringToFrontCheckBox = new JCheckBox();
                mainPanel.add(bringToFrontCheckBox);
                mainPanel.add(Box.createVerticalStrut(10));
                bringToFrontCheckBox.setText(
                    Resources.getString("bringWindowToFront"));
                bringToFrontCheckBox.addActionListener(this);
            }
        }
    }

    private void initDefaults()
    {
        groupMessagesCheckBox.setSelected(
            ConfigurationManager.isMultiChatWindowEnabled());
        
        logHistoryCheckBox.setSelected(
            ConfigurationManager.isHistoryLoggingEnabled());
        
        showHistoryCheckBox.setSelected(
            ConfigurationManager.isHistoryShown());
        
        historySizeSpinner.setValue(
            ConfigurationManager.getChatHistorySize());
        
        if (!ConfigurationManager.isHistoryLoggingEnabled())
        {
            showHistoryCheckBox.setEnabled(false);
            historySizeSpinner.setEnabled(false);
        }

        if (!ConfigurationManager.isHistoryShown())
        {
            historySizeSpinner.setEnabled(false);
        }

        sendMessageComboBox.setSelectedItem(
            ConfigurationManager.getSendMessageCommand());
        
        enableTypingNotifiCheckBox.setSelected(
            ConfigurationManager.isSendTypingNotifications());
        
        bringToFrontCheckBox.setSelected(
            ConfigurationManager.isAutoPopupNewMessage());
    }

    public Object getForm()
    {
        return this;
    }

    public byte[] getIcon()
    {
        return Resources.getImage("pluginIconGeneralConfig");
    }

    public String getTitle()
    {
        return Resources.getString("general");
    }

    public int getIndex()
    {
        return 0;
    }

    public void actionPerformed(ActionEvent event)
    {
        Object sourceObject = event.getSource();
        
        if (sourceObject.equals(autoStartCheckBox))
        {
            try 
            {
                String workingDir = new File(".").getCanonicalPath();
                
                String appName = 
                        Resources.getApplicationString("applicationName");
                ShellLink shortcut = new ShellLink(ShellLink.STARTUP, appName);
                shortcut.setUserType(ShellLink.CURRENT_USER);
                shortcut.setDescription(
                        "This starts " + appName + " Application");
                shortcut.setIconLocation(
                        workingDir + File.separator + "sc-logo.ico", 0);
                shortcut.setShowCommand(ShellLink.MINNOACTIVE);
                shortcut.setTargetPath(workingDir + File.separator + "run.exe");
                shortcut.setWorkingDirectory(workingDir);
                
                String f1 = shortcut.getcurrentUserLinkPath() + 
                        File.separator + appName + ".lnk";
            
                String f2 = f1.replaceAll(
                        System.getProperty("user.name"), 
                        "All Users");

                if(autoStartCheckBox.isSelected())
                {
                    if(!new File(f1).exists() && 
                       !new File(f2).exists())
                    shortcut.save();
                }
                else
                {
                    boolean isFileDeleted = false;
                    try {
                        isFileDeleted = new File(f1).delete();
                    } catch (Exception e) {}
                    
                    try {
                        new File(f2).delete();
                    } catch (Exception e) 
                    {
                        if(!isFileDeleted)
                            GeneralConfigPluginActivator.getUIService().
                                getPopupDialog().showMessagePopupDialog(
                                    e.getMessage(),
                                    Resources.getString("errorPermission"),
                                    PopupDialog.ERROR_MESSAGE);
                        // cannot delete no permissions
                    }
                }
            } catch (Exception e) 
            {
                logger.error("Cannot create/delete startup shortcut", e);
            }
        }
        if (sourceObject.equals(groupMessagesCheckBox))
        {
            ConfigurationManager.setMultiChatWindowEnabled(
                groupMessagesCheckBox.isSelected());
        }
        else if (sourceObject.equals(logHistoryCheckBox))
        {
            ConfigurationManager.setHistoryLoggingEnabled(
                logHistoryCheckBox.isSelected());
        }
        else if (sourceObject.equals(showHistoryCheckBox))
        {
            ConfigurationManager.setHistoryShown(
                showHistoryCheckBox.isSelected());
        }
        else if (sourceObject.equals(enableTypingNotifiCheckBox))
        {
            ConfigurationManager.setSendTypingNotifications(
                enableTypingNotifiCheckBox.isSelected());
        }
        else if (sourceObject.equals(bringToFrontCheckBox))
        {
            ConfigurationManager.setAutoPopupNewMessage(
                bringToFrontCheckBox.isSelected());
        }
    }
    
    
    private void initAutoStartCheckBox()
    {
        try 
        {
            String appName = 
                        Resources.getApplicationString("applicationName");
            ShellLink shortcut = 
                new ShellLink(
                    ShellLink.STARTUP, 
                    appName);
            shortcut.setUserType(ShellLink.CURRENT_USER);
            
            String f1 = shortcut.getcurrentUserLinkPath() + 
                        File.separator + appName + ".lnk";
            
            String f2 = f1.replaceAll(
                    System.getProperty("user.name"), 
                    "All Users");

            if(new File(f1).exists() || new File(f2).exists())
                autoStartCheckBox.setSelected(true);
            else
                autoStartCheckBox.setSelected(false);
            
        } catch (Exception e) 
        {
            logger.error(e);
        }
    }
}