Project

General

Profile

Download (31.5 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.taxeditor.ui.dialog;
11

    
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15

    
16
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.Status;
19
import org.eclipse.core.runtime.jobs.Job;
20
import org.eclipse.core.runtime.preferences.ConfigurationScope;
21
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.custom.StyledText;
24
import org.eclipse.swt.events.MouseAdapter;
25
import org.eclipse.swt.events.MouseEvent;
26
import org.eclipse.swt.events.SelectionAdapter;
27
import org.eclipse.swt.events.SelectionEvent;
28
import org.eclipse.swt.graphics.Point;
29
import org.eclipse.swt.layout.FillLayout;
30
import org.eclipse.swt.layout.GridData;
31
import org.eclipse.swt.layout.GridLayout;
32
import org.eclipse.swt.widgets.Button;
33
import org.eclipse.swt.widgets.Combo;
34
import org.eclipse.swt.widgets.Composite;
35
import org.eclipse.swt.widgets.Dialog;
36
import org.eclipse.swt.widgets.Display;
37
import org.eclipse.swt.widgets.Label;
38
import org.eclipse.swt.widgets.Shell;
39
import org.eclipse.swt.widgets.Text;
40
import org.eclipse.ui.forms.events.ExpansionEvent;
41
import org.eclipse.ui.forms.events.IExpansionListener;
42
import org.eclipse.ui.forms.widgets.ExpandableComposite;
43
import org.eclipse.wb.swt.SWTResourceManager;
44
import org.osgi.service.prefs.BackingStoreException;
45
import org.osgi.service.prefs.Preferences;
46

    
47
import eu.etaxonomy.taxeditor.model.MessagingUtils;
48
import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
49
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
50
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
51
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo;
52
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
53
import eu.etaxonomy.taxeditor.store.CdmStore;
54

    
55
/**
56
 * @author cmathew
57
 * @date 20 Jan 2015
58
 *
59
 */
60
public class RemotingLoginDialog extends Dialog {
61

    
62
    protected Object result;
63
    protected Shell shlConnect;
64
    private Text txtCdmServerStatus;
65
    private Text txtCdmInstanceStatus;
66
    private Combo comboCdmServer;
67
    private Combo comboCdmInstance;
68
    private Button btnConnect;
69

    
70
    private final Map<String, CdmServerInfo> csiiMap = new HashMap<String, CdmServerInfo>();
71

    
72
    private final static String STATUS_AVAILABLE = "Available";
73
    private final static String STATUS_NOT_AVAILABLE = "Not Available";
74
    private final static String STATUS_RETRIEVING = "Retrieving ...";
75
    private final static String STATUS_CHECKING_AVAILABILITY = "Checking ...";
76
    private final static String STATUS_NO_INSTANCES = "No Instances Found";
77
    private final static String STATUS_ERROR = "Error";
78
    private final static String STATUS_REMOTING_NOT_ACTIVATED = "Remoting not activated";
79
    private final static String STORE_PREFERENCES_NODE = "eu.etaxonomy.taxeditor.store";
80

    
81
    private final static String LOGIN_NODE = "login";
82
    private final static String USERNAME_SUFFIX = "_username";
83
    private final static String PASSWORD_SUFFIX = "_password";
84

    
85
    private final static String LAST_SERVER_INSTANCE_NODE = "lastServerInstance";
86
    private final static String LAST_SERVER_KEY = "lastServerKey";
87
    private final static String LAST_INSTANCE_KEY = "lastInstanceKey";
88

    
89

    
90
    private Composite remotingComposite;
91
    private CdmServerInfo selectedCsii;
92
    private CdmInstanceInfo selectedCdmInstance;
93
    private Button btnCdmServerRefresh;
94
    private Composite loginComposite;
95
    private Label lblLogin;
96
    private Text txtLogin;
97
    private Label lblPassword;
98
    private Text txtPassword;
99
    private Button btnRememberMe;
100
    private Composite compAdvanced;
101
    private Label lblPort;
102
    private Text txtPort;
103
    private Label lblServerVersion;
104
    private Text txtServerVersion;
105
    private ExpandableComposite xpndblcmpstAdvanced;
106
    private StyledText styledTxtMessage;
107

    
108

    
109
    private final int MIN_WIDTH = 530;
110
    private final int MIN_HEIGHT = 220;
111
    private final int MIN_EXP_HEIGHT = 350;
112
    private final int MESSAGE_HEIGHT = 25;
113
    private Label lblEditorVersion;
114
    private Text txtEditorVersion;
115
    private Label lblServerCDMVersion;
116
    private Text txtServerCDMVersion;
117
    private Label lblEditorCDMVersion;
118
    private Text txtEditorCDMVersion;
119

    
120
    private String serverName, instanceName;
121
    private boolean autoConnect = false;
122
    private boolean loadLoginPrefs = true;
123
    private boolean isDevRemoteSource = false;
124

    
125
    /**
126
     * Create the dialog.
127
     * @param parent
128
     * @param style
129
     */
130
    public RemotingLoginDialog(Shell parent, int style) {
131
        super(parent, style);
132
        setText("Login");
133
    }
134

    
135
    public Object open(CdmRemoteSource source, boolean loadLoginPrefs, boolean autoConnect) {
136
        this.loadLoginPrefs = loadLoginPrefs;
137
        this.serverName = source.getName();
138
        String contextPath = source.getContextPath();
139
        this.instanceName = contextPath.substring(contextPath.lastIndexOf("/") + 1);
140
        return open(serverName, instanceName, loadLoginPrefs, autoConnect);
141
    }
142

    
143

    
144
    public Object open(String serverName, String instanceName, boolean loadLoginPrefs, boolean autoConnect) {
145
        this.serverName = serverName;
146
        this.instanceName = instanceName;
147
        this.loadLoginPrefs = loadLoginPrefs;
148
        this.autoConnect = autoConnect;
149
        return open();
150
    }
151

    
152
    /**
153
     * Open the dialog.
154
     * @return the result
155
     */
156
    public Object open() {
157
        //        ICdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
158
        //        if(devRemoteSource != null) {
159
        //            connect(devRemoteSource);
160
        //        } else {
161
        createContents();
162
        if(serverName == null && instanceName == null) {
163
            readPrefLastServerInstance();
164
        }
165

    
166
        populateCdmServerCombo();
167
        shlConnect.open();
168
        shlConnect.layout();
169

    
170
        xpndblcmpstAdvanced.setExpanded(false);
171

    
172
        Display display = getParent().getDisplay();
173

    
174
        while (!shlConnect.isDisposed()) {
175
            if (!display.readAndDispatch()) {
176
                display.sleep();
177
            }
178
        }
179
        //}
180
        return result;
181
    }
182

    
183
    /**
184
     * Create contents of the dialog.
185
     */
186
    private void createContents() {
187
        shlConnect = new Shell(getParent(), SWT.DIALOG_TRIM);
188
        shlConnect.setMinimumSize(new Point(MIN_WIDTH, MIN_HEIGHT));
189
        shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
190
        shlConnect.setText("Connect");
191
        shlConnect.setLayout(new FillLayout(SWT.HORIZONTAL));
192

    
193
        remotingComposite = new Composite(shlConnect, SWT.NONE);
194
        remotingComposite.setLayout(new GridLayout(1, false));
195

    
196
        Composite cdmServerComposite = new Composite(remotingComposite, SWT.NONE);
197
        GridData gd_cdmServerComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
198
        gd_cdmServerComposite.heightHint = 68;
199
        cdmServerComposite.setLayoutData(gd_cdmServerComposite);
200
        cdmServerComposite.setLayout(new GridLayout(4, false));
201

    
202
        Label lblCdmServer = new Label(cdmServerComposite, SWT.NONE);
203
        lblCdmServer.setText("CDM Server : ");
204
        lblCdmServer.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
205
        lblCdmServer.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
206

    
207
        comboCdmServer = new Combo(cdmServerComposite, SWT.READ_ONLY);
208
        comboCdmServer.addSelectionListener(new SelectionAdapter() {
209
            @Override
210
            public void widgetSelected(SelectionEvent e) {
211
                refreshCdmServer();
212
                updatePort();
213
            }
214
        });
215
        GridData gd_comboCdmServer = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
216
        gd_comboCdmServer.widthHint = 150;
217
        comboCdmServer.setLayoutData(gd_comboCdmServer);
218
        comboCdmServer.select(0);
219

    
220
        txtCdmServerStatus = new Text(cdmServerComposite, SWT.BORDER);
221
        txtCdmServerStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
222
        txtCdmServerStatus.setEditable(false);
223
        GridData gd_txtCdmServerStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
224
        gd_txtCdmServerStatus.widthHint = 100;
225
        txtCdmServerStatus.setLayoutData(gd_txtCdmServerStatus);
226

    
227
        btnCdmServerRefresh = new Button(cdmServerComposite, SWT.NONE);
228
        btnCdmServerRefresh.addSelectionListener(new SelectionAdapter() {
229
            @Override
230
            public void widgetSelected(SelectionEvent e) {
231
                refreshCdmServer();
232
            }
233
        });
234
        btnCdmServerRefresh.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
235
        btnCdmServerRefresh.setText("Refresh");
236

    
237
        Label lblCdmInstance = new Label(cdmServerComposite, SWT.NONE);
238
        GridData gd_lblCdmInstance = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
239
        gd_lblCdmInstance.heightHint = 30;
240
        lblCdmInstance.setLayoutData(gd_lblCdmInstance);
241
        lblCdmInstance.setText("CDM Instance : ");
242
        lblCdmInstance.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
243

    
244
        comboCdmInstance = new Combo(cdmServerComposite, SWT.READ_ONLY);
245
        comboCdmInstance.addSelectionListener(new SelectionAdapter() {
246
            @Override
247
            public void widgetSelected(SelectionEvent e) {
248
                updateSelectedCdmInstance();
249
                checkSelectedCdmInstance();
250
            }
251
        });
252
        GridData gd_comboCdmInstance = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
253
        gd_comboCdmInstance.widthHint = 150;
254
        comboCdmInstance.setLayoutData(gd_comboCdmInstance);
255
        comboCdmInstance.select(0);
256

    
257
        txtCdmInstanceStatus = new Text(cdmServerComposite, SWT.BORDER);
258
        txtCdmInstanceStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
259
        txtCdmInstanceStatus.setEditable(false);
260
        GridData gd_txtCdmInstanceStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
261
        gd_txtCdmInstanceStatus.widthHint = 100;
262
        txtCdmInstanceStatus.setLayoutData(gd_txtCdmInstanceStatus);
263

    
264
        Button btnCdmInstanceRefresh = new Button(cdmServerComposite, SWT.FLAT);
265
        btnCdmInstanceRefresh.addSelectionListener(new SelectionAdapter() {
266
            @Override
267
            public void widgetSelected(SelectionEvent e) {
268
                refreshCdmInstance();
269
            }
270
        });
271
        GridData gd_btnCdmInstanceRefresh = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
272
        gd_btnCdmInstanceRefresh.widthHint = 110;
273
        gd_btnCdmInstanceRefresh.heightHint = 30;
274
        btnCdmInstanceRefresh.setLayoutData(gd_btnCdmInstanceRefresh);
275
        btnCdmInstanceRefresh.setText("Refresh");
276

    
277
        loginComposite = new Composite(remotingComposite, SWT.NONE);
278
        GridData gd_loginComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
279
        gd_loginComposite.widthHint = 487;
280
        gd_loginComposite.heightHint = 70;
281
        loginComposite.setLayoutData(gd_loginComposite);
282
        GridLayout gl_loginComposite = new GridLayout(6, false);
283
        gl_loginComposite.marginTop = 5;
284
        loginComposite.setLayout(gl_loginComposite);
285

    
286
        lblLogin = new Label(loginComposite, SWT.CENTER);
287
        GridData gd_lblLogin = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
288
        gd_lblLogin.widthHint = 50;
289
        lblLogin.setLayoutData(gd_lblLogin);
290
        lblLogin.setText("Login : ");
291
        lblLogin.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
292

    
293
        txtLogin = new Text(loginComposite, SWT.BORDER);
294
        GridData gd_txtLogin = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
295
        gd_txtLogin.minimumWidth = 80;
296
        gd_txtLogin.widthHint = 80;
297
        gd_txtLogin.heightHint = 15;
298
        txtLogin.setLayoutData(gd_txtLogin);
299

    
300
        lblPassword = new Label(loginComposite, SWT.CENTER);
301
        lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
302
        lblPassword.setText("Password : ");
303
        lblPassword.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
304

    
305
        txtPassword = new Text(loginComposite, SWT.BORDER | SWT.PASSWORD);
306
        GridData gd_txtPassword = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
307
        gd_txtPassword.minimumWidth = 80;
308
        gd_txtPassword.widthHint = 80;
309
        gd_txtPassword.heightHint = 15;
310
        txtPassword.setLayoutData(gd_txtPassword);
311
        new Label(loginComposite, SWT.NONE);
312

    
313
        btnConnect = new Button(loginComposite, SWT.FLAT);
314
        btnConnect.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
315
        btnConnect.addMouseListener(new MouseAdapter() {
316
            @Override
317
            public void mouseUp(MouseEvent e) {
318
                connect();
319
            }
320
        });
321
        btnConnect.setText("Connect");
322

    
323
        btnRememberMe = new Button(loginComposite, SWT.CHECK);
324
        btnRememberMe.setSelection(true);
325
        GridData gd_btnRememberMe = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
326
        gd_btnRememberMe.widthHint = 107;
327
        btnRememberMe.setLayoutData(gd_btnRememberMe);
328
        btnRememberMe.setText("Remember Me");
329
        new Label(loginComposite, SWT.NONE);
330
        new Label(loginComposite, SWT.NONE);
331
        new Label(loginComposite, SWT.NONE);
332
        new Label(loginComposite, SWT.NONE);
333

    
334
        styledTxtMessage = new StyledText(remotingComposite, SWT.NONE);
335
        styledTxtMessage.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
336
        styledTxtMessage.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
337
        styledTxtMessage.setFont(SWTResourceManager.getFont("Ubuntu", 12, SWT.BOLD));
338
        styledTxtMessage.setSelectionBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION_TEXT));
339
        styledTxtMessage.setSelectionForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
340
        styledTxtMessage.setDoubleClickEnabled(false);
341
        styledTxtMessage.setEditable(false);
342
        GridData gd_styledTxtMessage = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
343
        gd_styledTxtMessage.exclude = true;
344
        gd_styledTxtMessage.minimumHeight = MESSAGE_HEIGHT;
345
        gd_styledTxtMessage.heightHint = MESSAGE_HEIGHT;
346
        styledTxtMessage.setLayoutData(gd_styledTxtMessage);
347

    
348
        xpndblcmpstAdvanced = new ExpandableComposite(remotingComposite, SWT.NONE, ExpandableComposite.TWISTIE);
349
        GridData gd_xpndblcmpstAdvanced = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
350
        gd_xpndblcmpstAdvanced.heightHint = 19;
351
        xpndblcmpstAdvanced.setLayoutData(gd_xpndblcmpstAdvanced);
352
        xpndblcmpstAdvanced.addExpansionListener(new IExpansionListener() {
353
            @Override
354
            public void expansionStateChanged(ExpansionEvent e) {
355
                GridData gridData = (GridData) xpndblcmpstAdvanced.getLayoutData();
356
                if(e.getState()) {
357
                    shlConnect.setSize(MIN_WIDTH, MIN_EXP_HEIGHT);
358
                } else {
359
                    shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
360
                }
361

    
362
            }
363
            @Override
364
            public void expansionStateChanging(ExpansionEvent e) {
365
            }
366
        });
367
        xpndblcmpstAdvanced.setText("advanced");
368
        xpndblcmpstAdvanced.setExpanded(true);
369

    
370
        compAdvanced = new Composite(xpndblcmpstAdvanced, SWT.NONE);
371
        xpndblcmpstAdvanced.setClient(compAdvanced);
372
        compAdvanced.setLayout(new GridLayout(4, false));
373

    
374
        lblPort = new Label(compAdvanced, SWT.CENTER);
375
        lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
376
        lblPort.setSize(0, 0);
377
        lblPort.setText("Port : ");
378
        lblPort.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
379

    
380
        txtPort = new Text(compAdvanced, SWT.BORDER);
381
        GridData gd_txtPort = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
382
        gd_txtPort.minimumWidth = 50;
383
        gd_txtPort.widthHint = 50;
384
        txtPort.setLayoutData(gd_txtPort);
385

    
386
        lblServerVersion = new Label(compAdvanced, SWT.CENTER);
387
        lblServerVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
388
        lblServerVersion.setText("Server Version :");
389
        lblServerVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
390

    
391
        txtServerVersion = new Text(compAdvanced, SWT.BORDER);
392
        txtServerVersion.setEnabled(false);
393
        txtServerVersion.setEditable(false);
394
        txtServerVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
395
        new Label(compAdvanced, SWT.NONE);
396
        new Label(compAdvanced, SWT.NONE);
397

    
398
        lblEditorVersion = new Label(compAdvanced, SWT.CENTER);
399
        lblEditorVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
400
        lblEditorVersion.setText("Editor Version :");
401
        lblEditorVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
402

    
403
        txtEditorVersion = new Text(compAdvanced, SWT.BORDER);
404
        txtEditorVersion.setEnabled(false);
405
        txtEditorVersion.setEditable(false);
406
        txtEditorVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
407
        new Label(compAdvanced, SWT.NONE);
408
        new Label(compAdvanced, SWT.NONE);
409

    
410
        lblServerCDMVersion = new Label(compAdvanced, SWT.CENTER);
411
        lblServerCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
412
        lblServerCDMVersion.setText("Server CDM Version :");
413
        lblServerCDMVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
414

    
415
        txtServerCDMVersion = new Text(compAdvanced, SWT.BORDER);
416
        txtServerCDMVersion.setEnabled(false);
417
        txtServerCDMVersion.setEditable(false);
418
        txtServerCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
419
        new Label(compAdvanced, SWT.NONE);
420
        new Label(compAdvanced, SWT.NONE);
421

    
422
        lblEditorCDMVersion = new Label(compAdvanced, SWT.CENTER);
423
        lblEditorCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
424
        lblEditorCDMVersion.setText("Editor CDM Version :");
425
        lblEditorCDMVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
426

    
427
        txtEditorCDMVersion = new Text(compAdvanced, SWT.BORDER);
428
        txtEditorCDMVersion.setEnabled(false);
429
        txtEditorCDMVersion.setEditable(false);
430
        txtEditorCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
431

    
432
    }
433

    
434

    
435

    
436
    private void populateCdmServerCombo() {
437
        Job job = new Job("Retrieve Server Instances") {
438
            @Override
439
            protected IStatus run(IProgressMonitor monitor) {
440
                Display.getDefault().syncExec(new Runnable() {
441
                    @Override
442
                    public void run() {
443
                        for(CdmServerInfo csii : CdmServerInfo.getCdmServers()) {
444
                            csiiMap.put(csii.getName(), csii);
445
                            comboCdmServer.add(csii.getName());
446
                        }
447
                        int serverIndex = -1;
448
                        if(serverName != null) {
449
                            serverIndex = comboCdmServer.indexOf(serverName);
450
                        }
451
                        if(serverIndex == -1) {
452
                            comboCdmServer.select(0);
453
                            autoConnect = false;
454
                        } else {
455
                            comboCdmServer.select(serverIndex);
456
                        }
457
                        refreshCdmServer();
458
                        updatePort();
459
                        CdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
460
                        if(devRemoteSource != null) {
461
                            String username = System.getProperty("cdm.server.dev.username");
462
                            String password = System.getProperty("cdm.server.dev.password");
463
                            if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
464
                                txtLogin.setText(username);
465
                                txtPassword.setText(password);
466
                                loadLoginPrefs = false;
467
                                CdmStore.connect(devRemoteSource, RemotingLoginDialog.this);
468
                            }
469
                        }
470
                    }
471
                });
472
                return Status.OK_STATUS;
473
            }
474
        };
475
        job.schedule();
476
    }
477

    
478

    
479
    private void refreshCdmServer() {
480
        txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
481
        updateSelectedCdmServer();
482
        checkSelectedCdmServer();
483
    }
484

    
485
    private void updateSelectedCdmServer() {
486
        int selIndex = comboCdmServer.getSelectionIndex();
487
        if(selIndex != -1) {
488
            selectedCsii = csiiMap.get(comboCdmServer.getItem(selIndex));
489
        }
490
    }
491

    
492
    private void updatePort() {
493
        txtPort.setText("");
494
        if(selectedCsii != null) {
495
            txtPort.setText(String.valueOf(selectedCsii.getPort()));
496
        }
497
    }
498

    
499
    private int getPort() {
500
        int port = 0;
501
        try {
502
            port = Integer.valueOf(txtPort.getText());
503
        } catch (NumberFormatException nfe) {
504
            setMessage("Port should be an integer");
505
        }
506
        return port;
507
    }
508

    
509
    private void checkSelectedCdmServer() {
510

    
511
        txtCdmInstanceStatus.setText("");
512
        txtPort.setEditable(false);
513
        txtPort.setEnabled(false);
514
        emptyCredentials();
515

    
516
        if(selectedCsii != null) {
517
            if(selectedCsii.isLocalhost()) {
518
                txtPort.setEditable(true);
519
                txtPort.setEnabled(true);
520
            }
521
            if(selectedCsii.pingServer()) {
522
                txtCdmServerStatus.setText(STATUS_AVAILABLE);
523
                populateCdmInstanceCombo(true);
524

    
525
            } else {
526
                txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
527
                comboCdmInstance.removeAll();
528
            }
529
        }
530
    }
531

    
532

    
533
    private void populateCdmInstanceCombo(final boolean forceRefresh) {
534
        comboCdmInstance.removeAll();
535
        comboCdmInstance.setEnabled(false);
536
        btnConnect.setEnabled(false);
537
        txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
538

    
539
        Job job = new Job("Retrieve Server Instances") {
540
            @Override
541
            protected IStatus run(IProgressMonitor monitor) {
542
                try {
543
                    if(selectedCsii != null) {
544
                        if(forceRefresh) {
545
                            selectedCsii.refreshInstances();
546
                        }
547
                        final List<CdmInstanceInfo> instances = selectedCsii.getInstances();
548
                        Display.getDefault().asyncExec(new Runnable() {
549
                            @Override
550
                            public void run() {
551
                                if(!instances.isEmpty()) {
552
                                    for(CdmInstanceInfo cdmInstance : instances) {
553
                                        comboCdmInstance.add(cdmInstance.getName());
554
                                    }
555
                                    int instanceIndex = -1;
556
                                    if(instanceName != null) {
557
                                        instanceIndex = comboCdmInstance.indexOf(instanceName);
558
                                    }
559
                                    if(instanceIndex == -1) {
560
                                        comboCdmInstance.select(0);
561
                                        autoConnect = false;
562
                                    } else {
563
                                        comboCdmInstance.select(instanceIndex);
564
                                    }
565
                                    updateSelectedCdmInstance();
566
                                    checkSelectedCdmInstance();
567
                                    comboCdmInstance.setEnabled(true);
568
                                    if(autoConnect) {
569
                                        connect();
570
                                    }
571

    
572
                                } else {
573
                                    txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
574
                                    btnConnect.setEnabled(false);
575
                                }
576
                            }
577
                        });
578
                    }
579
                } catch (CDMServerException e) {
580
                    MessagingUtils.warn(getClass(), e);
581
                    Display.getDefault().asyncExec(new Runnable() {
582
                        @Override
583
                        public void run() {
584
                            txtCdmInstanceStatus.setText(STATUS_REMOTING_NOT_ACTIVATED);
585
                            comboCdmInstance.setEnabled(false);
586
                            btnConnect.setEnabled(false);
587
                        }
588
                    });
589
                }
590
                return Status.OK_STATUS;
591
            }
592
        };
593

    
594
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
595
            // Start the Job
596
            job.schedule();
597
        }
598

    
599
    }
600

    
601
    private void refreshCdmInstance() {
602
        txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
603
        updateSelectedCdmInstance();
604
        checkSelectedCdmInstance();
605
    }
606

    
607
    private void updateSelectedCdmInstance() {
608
        int selIndex = comboCdmInstance.getSelectionIndex();
609
        if(selIndex != -1) {
610
            selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
611
            if(loadLoginPrefs) {
612
                readPrefCredentials();
613
            }
614
        }
615
    }
616

    
617
    private void checkSelectedCdmInstance() {
618
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
619
            try {
620
                if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
621
                    txtCdmInstanceStatus.setText(STATUS_AVAILABLE);
622
                    btnConnect.setEnabled(true);
623
                } else {
624
                    txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
625
                    btnConnect.setEnabled(false);
626
                }
627
            } catch (Exception e) {
628
                txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
629
                txtCdmInstanceStatus.setToolTipText(e.getMessage());
630
            }
631
        }
632

    
633
    }
634

    
635
    private void connect() {
636
        checkSelectedCdmInstance();
637

    
638
        if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
639
            return;
640
        }
641

    
642
        ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
643

    
644
        if(!validateLogin()) {
645
            return;
646
        }
647

    
648
        try {
649
            CdmStore.connect(source, this);
650
        } catch (Exception e) {
651
            // Do not expect anything to go wrong at this point, so we throw a runtime exception
652
            // if any problems
653
            throw new RuntimeException(e);
654
        }
655

    
656
    }
657

    
658

    
659
    public boolean isRememberMe() {
660
        return btnRememberMe.getSelection();
661
    }
662

    
663
    private void persistPrefLastServerInstance() {
664
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
665
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
666

    
667
        lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
668
        lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
669

    
670
        flushPreferences(lastServerInstancePrefs);
671
    }
672

    
673
    private void persistPrefCredentials() {
674
         IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
675
         Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
676
         credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
677
         credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
678
         flushPreferences(credentialsPrefs);
679
    }
680

    
681
    private void flushPreferences(Preferences prefs) {
682
        try {
683
            prefs.flush();
684
        } catch (BackingStoreException bse) {
685
            setMessage(bse.getMessage());
686
        }
687
    }
688

    
689
    private void readPrefCredentials() {
690
        String username, password;
691
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
692
        Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
693
        username = credentialsPrefs.get(getUsernamePrefKey(), txtLogin.getText());
694
        txtLogin.setText(username);
695
        password = credentialsPrefs.get(getPasswordPrefKey(),txtPassword.getText());
696
        txtPassword.setText(password);
697
        if(username.isEmpty() || password.isEmpty()) {
698
            autoConnect = false;
699
        }
700
    }
701

    
702
    private void readPrefLastServerInstance() {
703
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
704
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
705

    
706
        serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
707
        instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
708
    }
709

    
710
    private void emptyCredentials() {
711
        txtLogin.setText("");
712
        txtPassword.setText("");
713
    }
714

    
715
    private String getUsernamePrefKey() {
716
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
717
    }
718

    
719
    private String getPasswordPrefKey() {
720
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
721
    }
722

    
723
    private boolean validateLogin() {
724
        if(getUsername() == null || getUsername().isEmpty()) {
725
            setMessage("User login cannot be empty");
726
            return false;
727
        }
728
        if(getPassword() == null || getPassword().isEmpty()) {
729
            setMessage("Password cannot be empty");
730
            return false;
731
        }
732
        return true;
733
    }
734
    public String getUsername() {
735
        return txtLogin.getText();
736
    }
737

    
738
    public String getPassword() {
739
        return txtPassword.getText();
740
    }
741

    
742
    public void setMessage(String message) {
743
        if(message != null && !message.isEmpty()) {
744
            if(message.length() > 60) {
745
                styledTxtMessage.setToolTipText(message);
746
                message = message.substring(0, 60) + "...";
747
            }
748
            styledTxtMessage.setText(message);
749
            styledTxtMessage.setVisible(true);
750
            ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
751
            shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT+MESSAGE_HEIGHT);
752
            shlConnect.setMinimumSize(MIN_WIDTH, MIN_HEIGHT+MESSAGE_HEIGHT);
753
        } else {
754
            styledTxtMessage.setText("");
755
            styledTxtMessage.setVisible(false);
756
            ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
757
            shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
758
            shlConnect.setMinimumSize(MIN_WIDTH, MIN_HEIGHT);
759
        }
760
        remotingComposite.layout();
761
    }
762

    
763

    
764

    
765
    public void hide(boolean isHidden) {
766
        if(shlConnect != null && shlConnect.getDisplay() != null) {
767
            shlConnect.setVisible(!isHidden);
768
        }
769
    }
770
    public void dispose() {
771
        if(shlConnect != null && shlConnect.getDisplay() != null) {
772
            shlConnect.dispose();
773
        }
774
    }
775

    
776
    public void onComplete() {
777
        Display.getDefault().asyncExec(new Runnable() {
778
            @Override
779
            public void run() {
780
                if(isRememberMe()) {
781
                    persistPrefCredentials();
782
                }
783
                persistPrefLastServerInstance();
784
                dispose();
785
            }
786
        });
787
    }
788
}
(3-3/4)