Project

General

Profile

Download (35.6 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.text.SimpleDateFormat;
13
import java.util.Date;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17

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

    
50
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
51
import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
52
import eu.etaxonomy.taxeditor.model.MessagingUtils;
53
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
54
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
55
import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
56
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
57
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
58
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo;
59
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
60
import eu.etaxonomy.taxeditor.store.CdmStore;
61

    
62
/**
63
 * @author cmathew
64
 * @date 20 Jan 2015
65
 *
66
 */
67
public class RemotingLoginDialog extends Dialog {
68

    
69
    protected Object result;
70
    protected Shell shlConnect;
71
    private Text txtCdmServerStatus;
72
    private Text txtCdmInstanceStatus;
73
    private Combo comboCdmServer;
74
    private Combo comboCdmInstance;
75
    private Button btnConnect;
76

    
77
    private final Map<String, CdmServerInfo> csiiMap = new HashMap<String, CdmServerInfo>();
78

    
79
    private final static String STATUS_AVAILABLE = "Available";
80
    private final static String STATUS_NOT_AVAILABLE = "Not Available";
81
    private final static String STATUS_RETRIEVING = "Retrieving ...";
82
    private final static String STATUS_CHECKING_AVAILABILITY = "Checking ...";
83
    private final static String STATUS_NO_INSTANCES = "No Instances Found";
84
    private final static String STATUS_ERROR = "Error";
85
    private final static String STATUS_REMOTING_NOT_ACTIVATED = "Remoting not activated";
86
    private final static String STATUS_NOT_COMPATIBLE = "Not Compatible";
87

    
88
    private final static String MESG_COMPATIBLE_EDITOR_OLD = "Please update the Taxonomic Editor (Help->Check for Updates) or choose a compatible cdm-server";
89
    private final static String MESG_COMPATIBLE_SERVER_OLD = "Please choose a compatible cdm-server or update the chosen cdm-server";
90

    
91
    private final static String STORE_PREFERENCES_NODE = "eu.etaxonomy.taxeditor.store";
92

    
93
    private final static String LOGIN_NODE = "login";
94
    private final static String USERNAME_SUFFIX = "_username";
95
    private final static String PASSWORD_SUFFIX = "_password";
96

    
97
    private final static String LAST_SERVER_INSTANCE_NODE = "lastServerInstance";
98
    private final static String LAST_SERVER_KEY = "lastServerKey";
99
    private final static String LAST_INSTANCE_KEY = "lastInstanceKey";
100

    
101

    
102
    private Composite remotingComposite;
103
    private CdmServerInfo selectedCsii;
104
    private CdmInstanceInfo selectedCdmInstance;
105
    private Button btnCdmServerRefresh;
106
    private Composite loginComposite;
107
    private Label lblLogin;
108
    private Text txtLogin;
109
    private Label lblPassword;
110
    private Text txtPassword;
111
    private Button btnRememberMe;
112
    private Composite compAdvanced;
113
    private Label lblPort;
114
    private Text txtPort;
115
    private Label lblServerVersion;
116
    private Text txtServerVersion;
117
    private ExpandableComposite xpndblcmpstAdvanced;
118
    private StyledText styledTxtMessage;
119

    
120

    
121
    private final int MIN_WIDTH = 530;
122
    private final int MIN_HEIGHT = 220;
123
    private final int MIN_EXP_HEIGHT = 380;
124
    private final int MESSAGE_HEIGHT = 25;
125
    private Label lblEditorVersion;
126
    private Text txtEditorVersion;
127
    private Label lblServerCDMVersion;
128
    private Text txtServerCDMVersion;
129
    private Label lblEditorCDMVersion;
130
    private Text txtEditorCDMVersion;
131

    
132
    private String serverName, instanceName;
133
    private boolean autoConnect = false;
134
    private boolean loadLoginPrefs = true;
135
    private boolean isDevRemoteSource = false;
136

    
137
    /**
138
     * Create the dialog.
139
     * @param parent
140
     * @param style
141
     */
142
    public RemotingLoginDialog(Shell parent, int style) {
143
        super(parent, style);
144
        setText("Login");
145
    }
146

    
147
    public Object open(CdmRemoteSource source, boolean loadLoginPrefs, boolean autoConnect) {
148
        this.loadLoginPrefs = loadLoginPrefs;
149
        this.serverName = source.getName();
150
        String contextPath = source.getContextPath();
151
        this.instanceName = contextPath.substring(contextPath.lastIndexOf("/") + 1);
152
        return open(serverName, instanceName, loadLoginPrefs, autoConnect);
153
    }
154

    
155

    
156
    public Object open(String serverName, String instanceName, boolean loadLoginPrefs, boolean autoConnect) {
157
        this.serverName = serverName;
158
        this.instanceName = instanceName;
159
        this.loadLoginPrefs = loadLoginPrefs;
160
        this.autoConnect = autoConnect;
161
        return open();
162
    }
163

    
164
    /**
165
     * Open the dialog.
166
     * @return the result
167
     */
168
    public Object open() {
169
        //        ICdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
170
        //        if(devRemoteSource != null) {
171
        //            connect(devRemoteSource);
172
        //        } else {
173
        createContents();
174
        if(serverName == null && instanceName == null) {
175
            readPrefLastServerInstance();
176
        }
177

    
178
        setEditorInfo();
179
        populateCdmServerCombo();
180
        shlConnect.open();
181
        shlConnect.layout();
182

    
183
        xpndblcmpstAdvanced.setExpanded(false);
184

    
185
        Display display = getParent().getDisplay();
186

    
187
        while (!shlConnect.isDisposed()) {
188
            if (!display.readAndDispatch()) {
189
                display.sleep();
190
            }
191
        }
192
        //}
193
        return result;
194
    }
195

    
196
    /**
197
     * Create contents of the dialog.
198
     */
199
    private void createContents() {
200
        shlConnect = new Shell(getParent(), SWT.DIALOG_TRIM);
201
        shlConnect.setMinimumSize(new Point(MIN_WIDTH, MIN_HEIGHT));
202
        shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
203
        shlConnect.setText("Connect");
204
        shlConnect.setLayout(new FillLayout(SWT.HORIZONTAL));
205

    
206
        remotingComposite = new Composite(shlConnect, SWT.NONE);
207
        remotingComposite.setLayout(new GridLayout(1, false));
208

    
209
        Composite cdmServerComposite = new Composite(remotingComposite, SWT.NONE);
210
        GridData gd_cdmServerComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
211
        gd_cdmServerComposite.heightHint = 68;
212
        cdmServerComposite.setLayoutData(gd_cdmServerComposite);
213
        cdmServerComposite.setLayout(new GridLayout(4, false));
214

    
215
        Label lblCdmServer = new Label(cdmServerComposite, SWT.NONE);
216
        lblCdmServer.setText("CDM Server : ");
217
        lblCdmServer.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
218
        lblCdmServer.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
219

    
220
        comboCdmServer = new Combo(cdmServerComposite, SWT.READ_ONLY);
221
        comboCdmServer.addSelectionListener(new SelectionAdapter() {
222
            @Override
223
            public void widgetSelected(SelectionEvent e) {
224
                refreshCdmServer();
225
                updatePort();
226
            }
227
        });
228
        GridData gd_comboCdmServer = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
229
        gd_comboCdmServer.widthHint = 150;
230
        comboCdmServer.setLayoutData(gd_comboCdmServer);
231
        comboCdmServer.select(0);
232

    
233
        txtCdmServerStatus = new Text(cdmServerComposite, SWT.BORDER);
234
        txtCdmServerStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
235
        txtCdmServerStatus.setEditable(false);
236
        GridData gd_txtCdmServerStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
237
        gd_txtCdmServerStatus.widthHint = 100;
238
        txtCdmServerStatus.setLayoutData(gd_txtCdmServerStatus);
239

    
240
        btnCdmServerRefresh = new Button(cdmServerComposite, SWT.NONE);
241
        btnCdmServerRefresh.addSelectionListener(new SelectionAdapter() {
242
            @Override
243
            public void widgetSelected(SelectionEvent e) {
244
                refreshCdmServer();
245
            }
246
        });
247
        btnCdmServerRefresh.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
248
        btnCdmServerRefresh.setText("Refresh");
249

    
250
        Label lblCdmInstance = new Label(cdmServerComposite, SWT.NONE);
251
        GridData gd_lblCdmInstance = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
252
        gd_lblCdmInstance.heightHint = 30;
253
        lblCdmInstance.setLayoutData(gd_lblCdmInstance);
254
        lblCdmInstance.setText("CDM Instance : ");
255
        lblCdmInstance.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
256

    
257
        comboCdmInstance = new Combo(cdmServerComposite, SWT.READ_ONLY);
258
        comboCdmInstance.addSelectionListener(new SelectionAdapter() {
259
            @Override
260
            public void widgetSelected(SelectionEvent e) {
261
                updateSelectedCdmInstance();
262
                checkSelectedCdmInstance();
263
            }
264
        });
265
        GridData gd_comboCdmInstance = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
266
        gd_comboCdmInstance.widthHint = 150;
267
        comboCdmInstance.setLayoutData(gd_comboCdmInstance);
268
        comboCdmInstance.select(0);
269

    
270
        txtCdmInstanceStatus = new Text(cdmServerComposite, SWT.BORDER);
271
        txtCdmInstanceStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
272
        txtCdmInstanceStatus.setEditable(false);
273
        GridData gd_txtCdmInstanceStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
274
        gd_txtCdmInstanceStatus.widthHint = 100;
275
        txtCdmInstanceStatus.setLayoutData(gd_txtCdmInstanceStatus);
276

    
277
        Button btnCdmInstanceRefresh = new Button(cdmServerComposite, SWT.FLAT);
278
        btnCdmInstanceRefresh.addSelectionListener(new SelectionAdapter() {
279
            @Override
280
            public void widgetSelected(SelectionEvent e) {
281
                refreshCdmInstance();
282
            }
283
        });
284
        GridData gd_btnCdmInstanceRefresh = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
285
        gd_btnCdmInstanceRefresh.widthHint = 110;
286
        gd_btnCdmInstanceRefresh.heightHint = 30;
287
        btnCdmInstanceRefresh.setLayoutData(gd_btnCdmInstanceRefresh);
288
        btnCdmInstanceRefresh.setText("Refresh");
289

    
290
        loginComposite = new Composite(remotingComposite, SWT.NONE);
291
        GridData gd_loginComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
292
        gd_loginComposite.widthHint = 487;
293
        gd_loginComposite.heightHint = 70;
294
        loginComposite.setLayoutData(gd_loginComposite);
295
        GridLayout gl_loginComposite = new GridLayout(6, false);
296
        gl_loginComposite.marginTop = 5;
297
        loginComposite.setLayout(gl_loginComposite);
298

    
299
        lblLogin = new Label(loginComposite, SWT.CENTER);
300
        GridData gd_lblLogin = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
301
        gd_lblLogin.widthHint = 50;
302
        lblLogin.setLayoutData(gd_lblLogin);
303
        lblLogin.setText("Login : ");
304
        lblLogin.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
305

    
306
        txtLogin = new Text(loginComposite, SWT.BORDER);
307
        GridData gd_txtLogin = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
308
        gd_txtLogin.minimumWidth = 80;
309
        gd_txtLogin.widthHint = 80;
310
        gd_txtLogin.heightHint = 15;
311
        txtLogin.setLayoutData(gd_txtLogin);
312

    
313
        lblPassword = new Label(loginComposite, SWT.CENTER);
314
        lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
315
        lblPassword.setText("Password : ");
316
        lblPassword.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
317

    
318
        txtPassword = new Text(loginComposite, SWT.BORDER | SWT.PASSWORD);
319
        GridData gd_txtPassword = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
320
        gd_txtPassword.minimumWidth = 80;
321
        gd_txtPassword.widthHint = 80;
322
        gd_txtPassword.heightHint = 15;
323
        txtPassword.setLayoutData(gd_txtPassword);
324
        new Label(loginComposite, SWT.NONE);
325

    
326
        btnConnect = new Button(loginComposite, SWT.FLAT);
327
        btnConnect.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
328
        btnConnect.addMouseListener(new MouseAdapter() {
329
            @Override
330
            public void mouseUp(MouseEvent e) {
331
                connect();
332
            }
333
        });
334
        btnConnect.setText("Connect");
335

    
336
        btnRememberMe = new Button(loginComposite, SWT.CHECK);
337
        btnRememberMe.setSelection(true);
338
        GridData gd_btnRememberMe = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
339
        gd_btnRememberMe.widthHint = 107;
340
        btnRememberMe.setLayoutData(gd_btnRememberMe);
341
        btnRememberMe.setText("Remember Me");
342
        new Label(loginComposite, SWT.NONE);
343
        new Label(loginComposite, SWT.NONE);
344
        new Label(loginComposite, SWT.NONE);
345
        new Label(loginComposite, SWT.NONE);
346

    
347
        styledTxtMessage = new StyledText(remotingComposite, SWT.NONE);
348
        styledTxtMessage.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
349
        styledTxtMessage.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
350
        styledTxtMessage.setFont(SWTResourceManager.getFont("Ubuntu", 12, SWT.BOLD));
351
        styledTxtMessage.setSelectionBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION_TEXT));
352
        styledTxtMessage.setSelectionForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
353
        styledTxtMessage.setDoubleClickEnabled(false);
354
        styledTxtMessage.setEditable(false);
355
        GridData gd_styledTxtMessage = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
356
        gd_styledTxtMessage.exclude = true;
357
        gd_styledTxtMessage.minimumHeight = MESSAGE_HEIGHT;
358
        gd_styledTxtMessage.heightHint = MESSAGE_HEIGHT;
359
        styledTxtMessage.setLayoutData(gd_styledTxtMessage);
360

    
361
        xpndblcmpstAdvanced = new ExpandableComposite(remotingComposite, SWT.NONE, ExpandableComposite.TWISTIE);
362
        GridData gd_xpndblcmpstAdvanced = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
363
        gd_xpndblcmpstAdvanced.heightHint = 19;
364
        xpndblcmpstAdvanced.setLayoutData(gd_xpndblcmpstAdvanced);
365
        xpndblcmpstAdvanced.addExpansionListener(new IExpansionListener() {
366
            @Override
367
            public void expansionStateChanged(ExpansionEvent e) {
368
                GridData gridData = (GridData) xpndblcmpstAdvanced.getLayoutData();
369
                if(e.getState()) {
370
                    shlConnect.setSize(MIN_WIDTH, MIN_EXP_HEIGHT);
371
                } else {
372
                    shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
373
                }
374

    
375
            }
376
            @Override
377
            public void expansionStateChanging(ExpansionEvent e) {
378
            }
379
        });
380
        xpndblcmpstAdvanced.setText("advanced");
381
        xpndblcmpstAdvanced.setExpanded(true);
382

    
383
        compAdvanced = new Composite(xpndblcmpstAdvanced, SWT.NONE);
384
        xpndblcmpstAdvanced.setClient(compAdvanced);
385
        compAdvanced.setLayout(new GridLayout(4, false));
386

    
387
        lblPort = new Label(compAdvanced, SWT.CENTER);
388
        lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
389
        lblPort.setSize(0, 0);
390
        lblPort.setText("Port : ");
391
        lblPort.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
392

    
393
        txtPort = new Text(compAdvanced, SWT.BORDER);
394
        GridData gd_txtPort = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
395
        gd_txtPort.minimumWidth = 50;
396
        gd_txtPort.widthHint = 50;
397
        txtPort.setLayoutData(gd_txtPort);
398

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

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

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

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

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

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

    
432
        lblEditorCDMVersion = new Label(compAdvanced, SWT.CENTER);
433
        lblEditorCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
434
        lblEditorCDMVersion.setText("Editor CDM Version :");
435
        lblEditorCDMVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
436

    
437
        txtEditorCDMVersion = new Text(compAdvanced, SWT.BORDER);
438
        txtEditorCDMVersion.setEditable(false);
439
        txtEditorCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
440

    
441
    }
442

    
443

    
444

    
445
    private void populateCdmServerCombo() {
446
        Job job = new Job("Retrieve Server Instances") {
447
            @Override
448
            protected IStatus run(IProgressMonitor monitor) {
449
                Display.getDefault().syncExec(new Runnable() {
450
                    @Override
451
                    public void run() {
452
                        for(CdmServerInfo csii : CdmServerInfo.getCdmServers()) {
453
                            csiiMap.put(csii.getName(), csii);
454
                            comboCdmServer.add(csii.getName());
455
                        }
456
                        int serverIndex = -1;
457
                        if(serverName != null) {
458
                            serverIndex = comboCdmServer.indexOf(serverName);
459
                        }
460
                        if(serverIndex == -1) {
461
                            comboCdmServer.select(0);
462
                            autoConnect = false;
463
                        } else {
464
                            comboCdmServer.select(serverIndex);
465
                        }
466
                        CdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
467
                        if(devRemoteSource != null) {
468
                            isDevRemoteSource = true;
469
                        }
470
                        refreshCdmServer();
471
                        updatePort();
472

    
473
                        if(devRemoteSource != null) {
474
                            String username = System.getProperty("cdm.server.dev.username");
475
                            String password = System.getProperty("cdm.server.dev.password");
476
                            if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
477
                                txtLogin.setText(username);
478
                                txtPassword.setText(password);
479
                                CdmStore.connect(devRemoteSource, RemotingLoginDialog.this);
480
                            }
481
                        }
482
                    }
483
                });
484
                return Status.OK_STATUS;
485
            }
486
        };
487
        job.schedule();
488
    }
489

    
490

    
491
    private void refreshCdmServer() {
492
        txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
493
        updateSelectedCdmServer();
494
        checkSelectedCdmServer();
495
    }
496

    
497
    private void updateSelectedCdmServer() {
498
        int selIndex = comboCdmServer.getSelectionIndex();
499
        if(selIndex != -1) {
500
            selectedCsii = csiiMap.get(comboCdmServer.getItem(selIndex));
501
        }
502
    }
503

    
504
    private void updatePort() {
505
        txtPort.setText("");
506
        if(selectedCsii != null) {
507
            txtPort.setText(String.valueOf(selectedCsii.getPort()));
508
        }
509
    }
510

    
511
    private int getPort() {
512
        int port = 0;
513
        try {
514
            port = Integer.valueOf(txtPort.getText());
515
        } catch (NumberFormatException nfe) {
516
            setMessage("Port should be an integer");
517
        }
518
        return port;
519
    }
520

    
521
    private void checkSelectedCdmServer() {
522

    
523
        clearOnServerChange();
524
        emptyCredentials();
525

    
526
        if(selectedCsii != null) {
527
            if(selectedCsii.isLocalhost()) {
528
                txtPort.setEditable(true);
529
                txtPort.setEnabled(true);
530
            }
531
            if(selectedCsii.pingServer()) {
532
                txtCdmServerStatus.setText(STATUS_AVAILABLE);
533
                populateCdmInstanceCombo(true);
534
                txtServerVersion.setText(selectedCsii.getCdmlibServicesVersion());
535
                txtServerVersion.setToolTipText(generateLastModifiedTooltip(selectedCsii.getCdmlibLastModified()));
536

    
537
            } else {
538
                txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
539
                comboCdmInstance.removeAll();
540
            }
541
        }
542
    }
543

    
544

    
545
    private void populateCdmInstanceCombo(final boolean forceRefresh) {
546
        comboCdmInstance.removeAll();
547
        comboCdmInstance.setEnabled(false);
548
        btnConnect.setEnabled(false);
549
        txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
550
        txtCdmInstanceStatus.setToolTipText("");
551

    
552
        Job job = new Job("Retrieve Server Instances") {
553
            @Override
554
            protected IStatus run(IProgressMonitor monitor) {
555
                try {
556
                    if(selectedCsii != null) {
557
                        if(forceRefresh) {
558
                            selectedCsii.refreshInstances();
559
                        }
560
                        final List<CdmInstanceInfo> instances = selectedCsii.getInstances();
561
                        Display.getDefault().asyncExec(new Runnable() {
562
                            @Override
563
                            public void run() {
564
                                if(!instances.isEmpty()) {
565
                                    for(CdmInstanceInfo cdmInstance : instances) {
566
                                        comboCdmInstance.add(cdmInstance.getName());
567
                                    }
568
                                    int instanceIndex = -1;
569
                                    if(instanceName != null) {
570
                                        instanceIndex = comboCdmInstance.indexOf(instanceName);
571
                                    }
572
                                    if(instanceIndex == -1) {
573
                                        comboCdmInstance.select(0);
574
                                        autoConnect = false;
575
                                    } else {
576
                                        comboCdmInstance.select(instanceIndex);
577
                                    }
578
                                    updateSelectedCdmInstance();
579
                                    checkSelectedCdmInstance();
580
                                    comboCdmInstance.setEnabled(true);
581
                                    if(autoConnect) {
582
                                        connect();
583
                                    }
584

    
585
                                } else {
586
                                    txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
587
                                    btnConnect.setEnabled(false);
588
                                }
589
                            }
590
                        });
591
                    }
592
                } catch (final CDMServerException e) {
593
                    MessagingUtils.warn(getClass(), e);
594
                    Display.getDefault().asyncExec(new Runnable() {
595
                        @Override
596
                        public void run() {
597
                            txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
598
                            txtCdmInstanceStatus.setToolTipText(e.getMessage());
599
                            comboCdmInstance.setEnabled(false);
600
                            btnConnect.setEnabled(false);
601
                        }
602
                    });
603
                }
604
                return Status.OK_STATUS;
605
            }
606
        };
607

    
608
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE) && !isDevRemoteSource) {
609
            // Start the Job
610
            job.schedule();
611
        }
612

    
613
    }
614

    
615
    private void refreshCdmInstance() {
616
        txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
617
        clearOnInstanceChange();
618
        updateSelectedCdmInstance();
619
        checkSelectedCdmInstance();
620
    }
621

    
622
    private void updateSelectedCdmInstance() {
623
        int selIndex = comboCdmInstance.getSelectionIndex();
624
        if(selIndex != -1) {
625
            selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
626
            if(loadLoginPrefs && !isDevRemoteSource) {
627
                readPrefCredentials();
628
            }
629
        }
630
    }
631

    
632
    private void checkSelectedCdmInstance() {
633
        boolean available = false;
634
        String status = STATUS_NOT_AVAILABLE;
635
        String message = null;
636

    
637
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
638
            try {
639
                if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
640
                    status = STATUS_AVAILABLE;
641
                    available = true;
642
                } else {
643
                    status = STATUS_NOT_AVAILABLE;
644
                    available = false;
645
                }
646

    
647
                if(available) {
648
                    txtServerCDMVersion.setText(selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort()).getDbSchemaVersion());
649
                    int compareDbSchemaVersion = selectedCsii.compareDbSchemaVersion(selectedCdmInstance, getPort());
650
                    int compareCdmlibServicesVersion = 0;
651
                    boolean disableServicesApiTimestampCheck =
652
                            PreferencesUtil.getPreferenceStore().getBoolean((IPreferenceKeys.DISABLE_SERVICES_API_TIMESTAMP_CHECK));
653
                    if(!disableServicesApiTimestampCheck) {
654
                        compareCdmlibServicesVersion = selectedCsii.compareCdmlibServicesVersion();
655
                    }
656
                    if(compareDbSchemaVersion > 0 || compareCdmlibServicesVersion > 0) {
657
                        status =  STATUS_NOT_COMPATIBLE;
658
                        available = false;
659
                        message = MESG_COMPATIBLE_EDITOR_OLD;
660
                    } else if(compareDbSchemaVersion < 0 || compareCdmlibServicesVersion < 0) {
661
                        status = STATUS_NOT_COMPATIBLE;
662
                        available = false;
663
                        message = MESG_COMPATIBLE_SERVER_OLD;
664
                    } else {
665
                        status =  STATUS_AVAILABLE;
666
                        available = true;
667
                        message = "";
668
                    }
669
                }
670
            } catch (Exception e) {
671
                txtCdmInstanceStatus.setToolTipText(e.getMessage());
672
            } finally {
673
                btnConnect.setEnabled(available);
674
                txtCdmInstanceStatus.setText(status);
675
                if(!StringUtils.isBlank(message)) {
676
                    setMessage(message);
677
                }
678
            }
679

    
680
        }
681

    
682
    }
683

    
684
    private void connect() {
685
        checkSelectedCdmInstance();
686

    
687
        if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
688
            return;
689
        }
690

    
691
        ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
692

    
693
        if(!validateLogin()) {
694
            return;
695
        }
696

    
697
        try {
698
            CdmStore.connect(source, this);
699
        } catch (Exception e) {
700
            // Do not expect anything to go wrong at this point, so we throw a runtime exception
701
            // if any problems
702
            throw new RuntimeException(e);
703
        }
704

    
705
    }
706

    
707

    
708
    public boolean isRememberMe() {
709
        return btnRememberMe.getSelection();
710
    }
711

    
712
    private void persistPrefLastServerInstance() {
713
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
714
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
715

    
716
        lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
717
        lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
718

    
719
        flushPreferences(lastServerInstancePrefs);
720
    }
721

    
722
    private void persistPrefCredentials() {
723
         IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
724
         Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
725
         credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
726
         credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
727
         flushPreferences(credentialsPrefs);
728
    }
729

    
730
    private void flushPreferences(Preferences prefs) {
731
        try {
732
            prefs.flush();
733
        } catch (BackingStoreException bse) {
734
            setMessage(bse.getMessage());
735
        }
736
    }
737

    
738
    private void readPrefCredentials() {
739
        String username, password;
740
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
741
        Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
742
        username = credentialsPrefs.get(getUsernamePrefKey(), "");
743
        txtLogin.setText(username);
744
        password = credentialsPrefs.get(getPasswordPrefKey(),"");
745
        txtPassword.setText(password);
746
        if(username.isEmpty() || password.isEmpty()) {
747
            autoConnect = false;
748
        }
749
    }
750

    
751
    private void readPrefLastServerInstance() {
752
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
753
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
754

    
755
        serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
756
        instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
757
    }
758

    
759
    private void emptyCredentials() {
760
        txtLogin.setText("");
761
        txtPassword.setText("");
762
    }
763

    
764
    private String getUsernamePrefKey() {
765
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
766
    }
767

    
768
    private String getPasswordPrefKey() {
769
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
770
    }
771

    
772
    private boolean validateLogin() {
773
        if(getUsername() == null || getUsername().isEmpty()) {
774
            setMessage("User login cannot be empty");
775
            return false;
776
        }
777
        if(getPassword() == null || getPassword().isEmpty()) {
778
            setMessage("Password cannot be empty");
779
            return false;
780
        }
781
        return true;
782
    }
783
    public String getUsername() {
784
        return txtLogin.getText();
785
    }
786

    
787
    public String getPassword() {
788
        return txtPassword.getText();
789
    }
790

    
791
    public void setMessage(String message) {
792
        if(message != null && !message.isEmpty()) {
793
            if(message.length() > 60) {
794
                styledTxtMessage.setToolTipText(message);
795
                message = message.substring(0, 60) + "...";
796
            }
797
            styledTxtMessage.setText(message);
798
            styledTxtMessage.setVisible(true);
799
            ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
800
            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
801
            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
802
        } else {
803
            styledTxtMessage.setText("");
804
            styledTxtMessage.setVisible(false);
805
            ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
806
            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage());
807
            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage());
808
        }
809
        remotingComposite.layout();
810
    }
811

    
812
    private int getHeightWithoutMessage() {
813
        if(xpndblcmpstAdvanced.isExpanded()) {
814
            return MIN_EXP_HEIGHT;
815
        } else {
816
            return MIN_HEIGHT;
817
        }
818
    }
819

    
820

    
821
    public void hide(boolean isHidden) {
822
        if(shlConnect != null && shlConnect.getDisplay() != null) {
823
            shlConnect.setVisible(!isHidden);
824
        }
825
    }
826
    public void dispose() {
827
        if(shlConnect != null && shlConnect.getDisplay() != null) {
828
            shlConnect.dispose();
829
        }
830
    }
831

    
832
    public void onComplete() {
833
        Display.getDefault().asyncExec(new Runnable() {
834
            @Override
835
            public void run() {
836
                if(!isDevRemoteSource) {
837
                    if(isRememberMe()) {
838
                        persistPrefCredentials();
839
                    }
840
                    persistPrefLastServerInstance();
841
                }
842
                dispose();
843
            }
844
        });
845
    }
846

    
847
    private String generateLastModifiedTooltip(String cdmlibLastModified) {
848
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z");
849
        Date cdmlibLastModifiedDate;
850
        String cdmlibLastModifiedTimestamp = "";
851

    
852
        cdmlibLastModifiedDate = new Date(Long.valueOf(cdmlibLastModified));
853
        cdmlibLastModifiedTimestamp = sdf.format(cdmlibLastModifiedDate);
854

    
855
        return "last modified : " + cdmlibLastModifiedTimestamp;
856
    }
857

    
858
    private void setEditorInfo() {
859
        txtEditorCDMVersion.setText(CdmMetaData.getDbSchemaVersion());
860
        txtEditorVersion.setText(CdmApplicationState.getCdmlibVersion());
861
        txtEditorVersion.setToolTipText(generateLastModifiedTooltip(CdmApplicationState.getCdmlibLastModified()));
862
    }
863

    
864
    private void clearOnServerChange() {
865
        setMessage("");
866
        txtServerCDMVersion.setText("");
867
        txtServerVersion.setText("");
868
        txtServerVersion.setToolTipText("");
869
        txtServerCDMVersion.setText("");
870
        txtPort.setEditable(false);
871
        txtPort.setEnabled(false);
872
    }
873

    
874
    private void clearOnInstanceChange() {
875
        setMessage("");
876
        txtServerCDMVersion.setText("");
877
    }
878

    
879
    private void updateOnServerChange(String serverVersion, String serverVersionTooltip) {
880

    
881
    }
882

    
883
}
(3-3/5)