aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/dictaccregwizz/FirstWizardPage.java
blob: 59cfd277fd637fd0a8bbe6ab09f2d4ae9397932a (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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.plugin.dictaccregwizz;

import java.awt.*;
import java.awt.event.*;
import java.util.List;

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

import net.java.dict4j.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.protocol.*;

/**
 * The <tt>FirstWizardPage</tt> is the page, where user could enter the host,
 * port and the strategy of the account.
 *
 * @author ROTH Damien
 * @author LITZELMANN Cedric
 */
public class FirstWizardPage
    extends TransparentPanel
    implements WizardPage, DocumentListener, ActionListener
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    public static final String FIRST_PAGE_IDENTIFIER = "FirstPageIdentifier";

    private JPanel hostPortPanel = new TransparentPanel(new BorderLayout(10, 10));

    private JPanel labelsPanel = new TransparentPanel();

    private JPanel valuesPanel = new TransparentPanel();

    private JLabel hostLabel
        = new JLabel(Resources.getString("plugin.dictaccregwizz.HOST"));

    private JPanel emptyPanel = new TransparentPanel();

    private JLabel hostExampleLabel = new JLabel("Ex: dict.org");

    private JLabel portLabel
        = new JLabel(Resources.getString("service.gui.PORT"));

    private JTextField hostField = new JTextField();

    private JTextField portField = new JTextField("2628");

    private JPanel strategyPanel = new TransparentPanel(new BorderLayout(10, 10));

    private JPanel strategyTitleBloc = new TransparentPanel(new BorderLayout());

    private JLabel strategyTitle = new JLabel(Resources.getString(
            "plugin.dictaccregwizz.STRATEGY_LIST"));

    private JButton strategyLoader
        = new JButton(Resources.getString(
            "plugin.dictaccregwizz.SEARCH_STRATEGIES"));

    private StrategiesList strategiesList;

    private JTextArea strategyDescription
        = new JTextArea(Resources.getString(
            "plugin.dictaccregwizz.STRATEGY_DESCRIPTION"));

    private ProgressPanel searchProgressPanel;

    private JPanel mainPanel = new TransparentPanel(new BorderLayout());

    private Object nextPageIdentifier = WizardPage.SUMMARY_PAGE_IDENTIFIER;

    private DictAccountRegistrationWizard wizard;

    private String initstrategy = "";

    private ThreadManager searchThread = null;

    private boolean firstAccount = false;

    private boolean isPageCommitted = false;

    /**
     * Initial AccountID (null if new account)
     * Used to check if there are modifications to the account
     */
    private AccountID initAccountID = null;

    /**
     * Creates an instance of <tt>FirstWizardPage</tt>.
     *
     * @param wizard the parent wizard
     */
    public FirstWizardPage(DictAccountRegistrationWizard wizard)
    {
        super(new BorderLayout());

        this.wizard = wizard;

        this.setPreferredSize(new Dimension(300, 150));

        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        this.searchThread = new ThreadManager(this);
        this.searchProgressPanel = new ProgressPanel(this.searchThread);

        this.firstAccount = !this.hasAccount();

        if (this.firstAccount)
        {
            this.initFirstAccount();
        }
        else
        {
            this.init();
        }

        this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    }

    /**
     * Initializes all panels, buttons, etc.
     */
    private void init()
    {
        // Host and port Field
        this.hostField = new JTextField();
        this.portField = new JTextField("2628");

        this.hostField.getDocument().addDocumentListener(this);
        this.portField.getDocument().addDocumentListener(this);

        this.hostExampleLabel.setForeground(Color.GRAY);
        this.hostExampleLabel.setFont(hostExampleLabel.getFont().deriveFont(8));
        this.emptyPanel.setMaximumSize(new Dimension(40, 35));
        this.hostExampleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 8,
            0));

        labelsPanel.add(hostLabel);
        labelsPanel.add(emptyPanel);
        labelsPanel.add(portLabel);

        valuesPanel.add(hostField);
        valuesPanel.add(hostExampleLabel);
        valuesPanel.add(portField);

        hostPortPanel.add(labelsPanel, BorderLayout.WEST);
        hostPortPanel.add(valuesPanel, BorderLayout.CENTER);

        hostPortPanel.setBorder(BorderFactory.createTitledBorder(
                Resources.getString("plugin.dictaccregwizz.SERVER_INFO")));

        this.labelsPanel.setLayout(new BoxLayout(labelsPanel, BoxLayout.Y_AXIS));
        this.valuesPanel.setLayout(new BoxLayout(valuesPanel, BoxLayout.Y_AXIS));

        mainPanel.add(hostPortPanel);

        this.portField.addKeyListener(new KeyListener() {
            public void keyTyped(KeyEvent evt)
            {
                // If evt isn't a digit, we don't add it
                if (!Character.isDigit(evt.getKeyChar()))
                {
                    evt.consume();
                }
            }

            // Not used
            public void keyPressed(KeyEvent evt) {;}
            public void keyReleased(KeyEvent evt) {;}
        });

        // Strategies list
        this.strategiesList = new StrategiesList();

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.getViewport().add(this.strategiesList);
        this.strategyPanel.add(scrollPane);

        // Strategy title + button
        this.strategyTitleBloc.add(this.strategyTitle, BorderLayout.WEST);
        this.strategyTitleBloc.add(this.strategyLoader, BorderLayout.EAST);

        // Button action listener
        this.strategyLoader.setActionCommand("populateList");
        this.strategyLoader.addActionListener(this);

        // South Panel
        JPanel sSouthPanel = new TransparentPanel(new BorderLayout());

        // Description
        this.strategyDescription.setLineWrap(true);
        this.strategyDescription.setLineWrap(true);
        this.strategyDescription.setRows(4);
        this.strategyDescription.setWrapStyleWord(true);
        this.strategyDescription.setAutoscrolls(false);
        sSouthPanel.add(this.strategyDescription);

        // Message
        sSouthPanel.add(this.searchProgressPanel, BorderLayout.SOUTH);

        this.strategyPanel.add(sSouthPanel, BorderLayout.SOUTH);

        this.strategyPanel.add(this.strategyTitleBloc, BorderLayout.NORTH);
        this.strategyPanel.setBorder(BorderFactory.createTitledBorder(
                Resources.getString("plugin.dictaccregwizz.STRATEGY_SELECTION")));
        mainPanel.add(this.strategyPanel);

        this.add(mainPanel, BorderLayout.NORTH);
    }

    /**
     * Initialize the UI for the first account
     */
    private void initFirstAccount()
    {
        // Data init
        this.hostField = new JTextField("dict.org");
        this.portField = new JTextField("2628");

        // Init strategies list
        this.strategiesList = new StrategiesList();

        this.mainPanel = new TransparentPanel(new BorderLayout());

        JPanel infoTitlePanel
            = new TransparentPanel(new FlowLayout(FlowLayout.CENTER));
        JTextArea firstDescription
            = new JTextArea(Resources.getString(
                "plugin.dictaccregwizz.FIRST_ACCOUNT"));
        JLabel title
            = new JLabel(Resources.getString(
                "plugin.dictaccregwizz.ACCOUNT_INFO_TITLE"));

        // Title
        title.setFont(title.getFont().deriveFont(Font.BOLD, 14.0f));
        infoTitlePanel.add(title);
        this.mainPanel.add(infoTitlePanel, BorderLayout.NORTH);
        this.mainPanel.add(this.searchProgressPanel, BorderLayout.SOUTH);

        // Description
        firstDescription.setLineWrap(true);
        firstDescription.setEditable(false);
        firstDescription.setOpaque(false);
        firstDescription.setRows(6);
        firstDescription.setWrapStyleWord(true);
        firstDescription.setAutoscrolls(false);
        this.mainPanel.add(firstDescription);
    }

    /**
     * Implements the <code>WizardPage.getIdentifier</code> to return this
     * page identifier.
     *
     * @return Returns the identifier of the current (the first) page of the
     * wizard.
     */
    public Object getIdentifier()
    {
        return FIRST_PAGE_IDENTIFIER;
    }

    /**
     * Implements the <code>WizardPage.getNextPageIdentifier</code> to return
     * the next page identifier - the summary page.
     *
     * @return Returns the identifier of the next page of the wizard.
     */
    public Object getNextPageIdentifier()
    {
        return nextPageIdentifier;
    }

    /**
     * Implements the <code>WizardPage.getBackPageIdentifier</code> to return
     * the back identifier, which is null as this is the first wizard page.
     *
     * @return the identifier of the previous page of the wizard.
     */
    public Object getBackPageIdentifier()
    {
        return null;
    }

    /**
     * Implements the <code>WizardPage.getWizardForm</code> to return this
     * panel.
     * @return Returns this form of the wizard.
     */
    public Object getWizardForm()
    {
        return this;
    }

    /**
     * Before this page is displayed enables or disables the "Next" wizard
     * button according to whether the UIN field is empty.
     */
    public void pageShowing()
    {
        this.setNextButtonEnabled();
    }

    /**
     * Saves the user input when the "Next" wizard buttons is clicked.
     */
    public void commitPage()
    {
        String host = hostField.getText();
        int port = Integer.parseInt(portField.getText());
        boolean isModified = false;

        if (this.initAccountID != null)
        { // We check if there are modifications to the server
            String accHost =
                this.initAccountID.getAccountPropertyString(
                    ProtocolProviderFactory.SERVER_ADDRESS);
            int accPort =
                Integer.parseInt(this.initAccountID
                    .getAccountPropertyString(ProtocolProviderFactory.SERVER_PORT));

            if (((accHost == null) ? (host != null) : !accHost.equals(host))
                    || (accPort != port))
            {
                isModified = true;
            }
        }

        // We check if a strategy has been selected
        if (this.strategiesList.getModel().getSize() == 0)
        {   // No Strategy, we get them
            this.populateStrategies();

            if (!this.searchThread.waitThread())
            {
                // TODO error dialog : thread interrupted ? no thread ?
                this.strategiesList.clear();
            }
        }

        if (this.strategiesList.getModel().getSize() == 0)
        {
            // No strategy, maybe not connected
            // Information message is already on the wizard
            nextPageIdentifier = FIRST_PAGE_IDENTIFIER;
            this.revalidate();
        }
        else
        {
            nextPageIdentifier = SUMMARY_PAGE_IDENTIFIER;

            DictAccountRegistration registration = wizard.getRegistration();

            registration.setHost(host);
            registration.setPort(port);
            registration.setStrategy(
                (Strategy) this.strategiesList.getSelectedValue());
        }

        isPageCommitted = true;
    }

    /**
     * Enables or disables the "Next" wizard button according to whether the UIN
     * field is empty.
     */
    private void setNextButtonEnabled()
    {
        boolean hostOK = DictConnection.isUrl(hostField.getText());
        boolean portOK = (this.portField.getText().length() != 0)
            && Integer.parseInt(this.portField.getText()) > 10;

        if (this.firstAccount)
        {
            wizard.getWizardContainer().setNextFinishButtonEnabled(true);
        }
        else if (hostOK && portOK)
        {
            this.strategyLoader.setEnabled(true);
            wizard.getWizardContainer().setNextFinishButtonEnabled(true);
        }
        else
        {
            // Disable the finish button
            wizard.getWizardContainer().setNextFinishButtonEnabled(false);

            // Clear the list and disable the button
            this.strategiesList.clear();
            this.strategyLoader.setEnabled(false);
        }
    }

    /**
     * Handles the <tt>DocumentEvent</tt> triggered when user types in the UIN
     * field. Enables or disables the "Next" wizard button according to whether
     * the UIN field is empty.
     *
     * @param e the <tt>DocumentEvent</tt> triggered when user types in the UIN
     * field.
     */
    public void insertUpdate(DocumentEvent e)
    {
        this.setNextButtonEnabled();
    }

    /**
     * Handles the <tt>DocumentEvent</tt> triggered when user deletes letters
     * from the UIN field. Enables or disables the "Next" wizard button
     * according to whether the UIN field is empty.
     *
     * @param e The <tt>DocumentEvent</tt> triggered when user deletes letters
     * from the UIN field.
     */
    public void removeUpdate(DocumentEvent e)
    {
        this.setNextButtonEnabled();
    }

    /**
     * Handles the <tt>DocumentEvent</tt> triggered when user changes an
     * attribute or set of attributes from the UIN field.
     * Currently this notification has no effect and is just here to implement
     * the DocumentListener interface.
     *
     * @param e The <tt>DocumentEvent</tt> triggered when an attribute or set of
     * attributes changed from the UIN field.
     */
    public void changedUpdate(DocumentEvent e)
    {
    }

    /**
     * Invoked when this WizardPage will be hidden eighter because the user has
     * clicked "Back" or "Next".
     * This function has no effect.
     */
    public void pageHiding()
    {
    }

    /**
     * Invoked when this WizardPage will be shown eighter because the user has
     * clicked "Back" on the next wizard page or "Next" on the previous one.
     * This function has no effect.
     */
    public void pageShown()
    {
    }

    /**
     * Invoked when user clicks on the "Back" wizard button.
     * This function has no effect.
     */
    public void pageBack()
    {
    }

    /**
     * Fills the Host, Port and Strategy fields in this panel with the data comming
     * from the given protocolProvider.
     *
     * @param protocolProvider The <tt>ProtocolProviderService</tt> to load
     *            the data from.
     */
    public void loadAccount(ProtocolProviderService protocolProvider)
    {
        AccountID accountID = protocolProvider.getAccountID();
        String host =
            accountID
                .getAccountPropertyString(ProtocolProviderFactory.SERVER_ADDRESS);
        String port =
            accountID
                .getAccountPropertyString(ProtocolProviderFactory.SERVER_PORT);
        String strategy =
            accountID
                .getAccountPropertyString(ProtocolProviderFactory.STRATEGY);

        this.initAccountID = accountID;

        // Host field
        this.hostField.setText(host);

        // Port Field
        this.portField.setText(port);

        // Load strategies
        this.initstrategy = strategy;
        this.populateStrategies();
    }

    /**
     * Handles the action of the button.
     *
     * @param e The event generated when the button is pressed.
     */
    public void actionPerformed(ActionEvent e)
    {
        // Button action -> populate the list
        if (e.getActionCommand().equals("populateList"))
        {
            this.populateStrategies();
        }
    }

    /**
     * Checks if an account is stored for this protocol
     * @return TRUE, if an account is stored - FALSE otherwise
     */
    private boolean hasAccount()
    {
        ProtocolProviderFactory factory =
            DictAccRegWizzActivator.getDictProtocolProviderFactory();

        return !factory.getRegisteredAccounts().isEmpty();
    }

    /**
     * Start the thread which will populate the Strategies List
     */
    public void populateStrategies()
    {
        // Clear ArrayList
        this.strategiesList.clear();

        boolean ok = this.searchThread.submitRequest(this.hostField.getText(),
                                    Integer.parseInt(this.portField.getText()));

        if (!ok)
        {
            // TODO Display error
        }
    }

    /**
     * Automatic selection of a strategy
     */
    public void autoSelectStrategy()
    {
        this.strategiesList.autoSelectStrategy(this.initstrategy);
    }

    /**
     *
     * @param strategies
     */
    public void setStrategies(List<Strategy> strategies)
    {
        this.strategiesList.setStrategies(strategies);
    }

    /**
     * Informs the user of the current status of the search
     * Should only be called by the thread
     * @param message Search status
     */
    public void progressMessage(String message)
    {
        this.searchProgressPanel.nextStep(message);
    }

    /**
     * Informs the wizard that the search of the strategies is complete.
     * Should only be called by the thread
     */
    public void strategiesSearchComplete()
    {
        setStrategyButtonEnable(true);
        this.searchProgressPanel.finish();
    }

    /**
     * Informs the wizard that the search of the strategies is a failure
     * Should only be called by the thread
     * @param reason Reason message
     * @param de Exception thrown
     */
    public void strategiesSearchFailure(String reason, DictException de)
    {
        strategiesSearchComplete();
        // TODO SHOW ERROR MESSAGE
    }

    /**
     * Enables or disable the Next Button and the Strategy Button
     * @param e TRUE enables - FALSE disables
     */
    public void setStrategyButtonEnable(boolean e)
    {
        // During all the process the buttons and the fieldsset are in the same state

        this.hostField.setEnabled(e);
        this.portField.setEnabled(e);

        this.strategyLoader.setEnabled(e);
        wizard.getWizardContainer().setNextFinishButtonEnabled(e);
    }

    public Object getSimpleForm()
    {
        return mainPanel;
    }

    /**
     * Indicates if this is the first dict account
     *
     * @return TRUE if this is the first dict account - FALSE otherwise
     */
    public boolean isFirstAccount()
    {
        return this.firstAccount;
    }

    public boolean isCommitted()
    {
        return isPageCommitted;
    }
}