Project

General

Profile

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

    
11
import java.io.File;
12
import java.io.IOException;
13
import java.text.SimpleDateFormat;
14
import java.util.Date;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18

    
19
import org.apache.commons.lang.StringUtils;
20
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.Status;
23
import org.eclipse.core.runtime.jobs.Job;
24
import org.eclipse.core.runtime.preferences.ConfigurationScope;
25
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
26
import org.eclipse.jface.layout.GridDataFactory;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.SWTException;
29
import org.eclipse.swt.custom.StyledText;
30
import org.eclipse.swt.events.KeyAdapter;
31
import org.eclipse.swt.events.KeyEvent;
32
import org.eclipse.swt.events.MouseAdapter;
33
import org.eclipse.swt.events.MouseEvent;
34
import org.eclipse.swt.events.SelectionAdapter;
35
import org.eclipse.swt.events.SelectionEvent;
36
import org.eclipse.swt.graphics.Point;
37
import org.eclipse.swt.layout.FillLayout;
38
import org.eclipse.swt.layout.GridData;
39
import org.eclipse.swt.layout.GridLayout;
40
import org.eclipse.swt.widgets.Button;
41
import org.eclipse.swt.widgets.Combo;
42
import org.eclipse.swt.widgets.Composite;
43
import org.eclipse.swt.widgets.Dialog;
44
import org.eclipse.swt.widgets.Display;
45
import org.eclipse.swt.widgets.Label;
46
import org.eclipse.swt.widgets.Shell;
47
import org.eclipse.swt.widgets.Text;
48
import org.eclipse.ui.forms.events.ExpansionEvent;
49
import org.eclipse.ui.forms.events.IExpansionListener;
50
import org.eclipse.ui.forms.widgets.ExpandableComposite;
51
import org.eclipse.ui.progress.IProgressConstants;
52
import org.eclipse.wb.swt.SWTResourceManager;
53
import org.osgi.service.prefs.BackingStoreException;
54
import org.osgi.service.prefs.Preferences;
55
import org.springframework.security.authentication.BadCredentialsException;
56
import org.springframework.security.authentication.LockedException;
57
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
58

    
59
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteConfiguration;
60
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
61
import eu.etaxonomy.cdm.api.service.IUserService;
62
import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
63
import eu.etaxonomy.taxeditor.l10n.Messages;
64
import eu.etaxonomy.taxeditor.model.MessagingUtils;
65
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
66
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
67
import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
68
import eu.etaxonomy.taxeditor.remoting.server.CDMServerUtils;
69
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
70
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
71
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo;
72
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
73
import eu.etaxonomy.taxeditor.store.CdmStore;
74
import eu.etaxonomy.taxeditor.store.LoginManager;
75
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
76
import eu.etaxonomy.taxeditor.webapp.CDMEmbeddedServerException;
77
import eu.etaxonomy.taxeditor.webapp.CDMServer;
78
import eu.etaxonomy.taxeditor.webapp.ICDMServerError;
79

    
80

    
81
/**
82
 * @author cmathew
83
 * @date 20 Jan 2015
84
 *
85
 */
86
public class RemotingLoginDialog extends Dialog implements ICDMServerError {
87

    
88
    /**
89
     *
90
     */
91
    private static final String DEFAULT_PASS = "00000";
92
    /**
93
     *
94
     */
95
    private static final String DEFAULT_USER = "admin";
96
    private static final String UBUNTU = "Ubuntu"; //$NON-NLS-1$
97
	protected Object result;
98
    protected Shell shlConnect;
99
    private Text txtCdmServerStatus;
100
    private Text txtCdmInstanceStatus;
101
    private Combo comboCdmServer;
102
    private Combo comboCdmInstance;
103
    private Button btnConnect;
104

    
105
    private final Map<String, CdmServerInfo> csiiMap = new HashMap<String, CdmServerInfo>();
106

    
107
    private final static String STATUS_AVAILABLE = Messages.RemotingLoginDialog_STATUS_AVAILABLE;
108
    private final static String STATUS_NOT_AVAILABLE = Messages.RemotingLoginDialog_STATUS_NOT_AVAILABLE;
109
    private final static String STATUS_STARTED = Messages.RemotingLoginDialog_STATUS_STARTED;
110
    private final static String STATUS_NOT_STARTED = Messages.RemotingLoginDialog_STATUS_NOT_STARTED;
111
    private final static String STATUS_RETRIEVING = Messages.RemotingLoginDialog_STATUS_RETRIEVING;
112
    private final static String STATUS_CHECKING_AVAILABILITY = Messages.RemotingLoginDialog_STATUS_CHECKING;
113
    private final static String STATUS_NO_INSTANCES = Messages.RemotingLoginDialog_STATUS_NO_INSTANCES_FOUND;
114
    private final static String STATUS_NOT_COMPATIBLE = Messages.RemotingLoginDialog_STATUS_NOT_COMPATIBLE;
115

    
116
    private final static String MESG_COMPATIBLE_EDITOR_OLD = Messages.RemotingLoginDialog_UPDATE_EDITOR;
117
    private final static String MESG_COMPATIBLE_SERVER_OLD = Messages.RemotingLoginDialog_CHOOSE_COMPATIBLE_CDM_SERVER;
118

    
119
    private final static String STORE_PREFERENCES_NODE = "eu.etaxonomy.taxeditor.store"; //$NON-NLS-1$
120

    
121
    private final static String LOGIN_NODE = "login"; //$NON-NLS-1$
122
    private final static String USERNAME_SUFFIX = "_username"; //$NON-NLS-1$
123
    private final static String PASSWORD_SUFFIX = "_password"; //$NON-NLS-1$
124

    
125
    private final static String LAST_SERVER_INSTANCE_NODE = "lastServerInstance"; //$NON-NLS-1$
126
    private final static String LAST_SERVER_KEY = "lastServerKey"; //$NON-NLS-1$
127
    private final static String LAST_INSTANCE_KEY = "lastInstanceKey"; //$NON-NLS-1$
128

    
129

    
130
    private Composite remotingComposite;
131
    private CdmServerInfo selectedCsii;
132
    private CdmInstanceInfo selectedCdmInstance;
133
    private Button btnCdmServerRefresh;
134
    private Button btnCdmInstanceRefresh;
135
    private Button btnStopServer;
136
    private Composite loginComposite;
137
    private Label lblLogin;
138
    private Text txtLogin;
139
    private Label lblPassword;
140
    private Text txtPassword;
141
    private Button btnRememberMe;
142
    private Composite compAdvanced;
143
    private Label lblPort;
144
    private Text txtPort;
145
    private Label lblServerVersion;
146
    private Text txtServerVersion;
147
    private ExpandableComposite xpndblcmpstAdvanced;
148
    private StyledText styledTxtMessage;
149

    
150

    
151
    private final int MIN_WIDTH = 530;
152
    private final int MIN_HEIGHT = 220;
153
    private final int MIN_EXP_HEIGHT = 380;
154
    private final int MESSAGE_HEIGHT = 25;
155
    private Label lblEditorVersion;
156
    private Text txtEditorVersion;
157
    private Label lblServerCDMVersion;
158
    private Text txtServerCDMVersion;
159
    private Label lblEditorCDMVersion;
160
    private Text txtEditorCDMVersion;
161

    
162
    private String serverName, instanceName;
163
    private boolean autoConnect = false;
164
    private boolean loadLoginPrefs = true;
165
    private boolean isDevRemoteSource = false;
166
    private Job serverJob;
167
    /**
168
     * Create the dialog.
169
     * @param parent
170
     * @param style
171
     */
172
    public RemotingLoginDialog(Shell parent, int style) {
173
        super(parent, style);
174
        setText(Messages.RemotingLoginDialog_LABEL_LOGIN);
175
    }
176

    
177
    public Object open(CdmRemoteSource source, boolean loadLoginPrefs, boolean autoConnect) {
178
        this.loadLoginPrefs = loadLoginPrefs;
179
        this.serverName = source.getName();
180
        String contextPath = source.getContextPath();
181
        this.instanceName = contextPath.substring(contextPath.lastIndexOf("/") + 1); //$NON-NLS-1$
182
        return open(serverName, instanceName, loadLoginPrefs, autoConnect);
183
    }
184

    
185

    
186
    public Object open(String serverName, String instanceName, boolean loadLoginPrefs, boolean autoConnect) {
187
        this.serverName = serverName;
188
        this.instanceName = instanceName;
189
        this.loadLoginPrefs = loadLoginPrefs;
190
        this.autoConnect = autoConnect;
191
        return open();
192
    }
193

    
194
    /**
195
     * Open the dialog.
196
     * @return the result
197
     */
198
    public Object open() {
199

    
200
        createContents();
201
        if(serverName == null && instanceName == null) {
202
            readPrefLastServerInstance();
203
        }
204

    
205
        setEditorInfo();
206
        populateCdmServerCombo();
207
        shlConnect.open();
208
        shlConnect.layout();
209

    
210
        xpndblcmpstAdvanced.setExpanded(false);
211

    
212
        Display display = getParent().getDisplay();
213

    
214
        while (!shlConnect.isDisposed()) {
215
            if (!display.readAndDispatch()) {
216
                display.sleep();
217
            }
218
        }
219

    
220
        return result;
221
    }
222

    
223
    /**
224
     * Create contents of the dialog.
225
     */
226
    private void createContents() {
227
        shlConnect = new Shell(getParent(), SWT.DIALOG_TRIM);
228
        shlConnect.setMinimumSize(new Point(MIN_WIDTH, MIN_HEIGHT));
229
        shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
230
        shlConnect.setText(Messages.RemotingLoginDialog_LABEL_CONNECT);
231
        shlConnect.setLayout(new FillLayout(SWT.HORIZONTAL));
232

    
233
        remotingComposite = new Composite(shlConnect, SWT.NONE);
234
        remotingComposite.setLayout(new GridLayout(1, false));
235

    
236
        Composite cdmServerComposite = new Composite(remotingComposite, SWT.NONE);
237
        GridData gd_cdmServerComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
238
        gd_cdmServerComposite.heightHint = 68;
239
        cdmServerComposite.setLayoutData(gd_cdmServerComposite);
240
        cdmServerComposite.setLayout(new GridLayout(4, false));
241

    
242
        Label lblCdmServer = new Label(cdmServerComposite, SWT.NONE);
243
        lblCdmServer.setText(Messages.RemotingLoginDialog_LABEL_CDM_SERVER);
244
        lblCdmServer.setFont(SWTResourceManager.getFont(UBUNTU, 9, SWT.NORMAL));
245
        lblCdmServer.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
246

    
247
        comboCdmServer = new Combo(cdmServerComposite, SWT.READ_ONLY);
248
        comboCdmServer.addSelectionListener(new SelectionAdapter() {
249
            @Override
250
            public void widgetSelected(SelectionEvent e) {
251
                refreshCdmServer();
252

    
253
            }
254
        });
255
        GridData gd_comboCdmServer = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
256
        gd_comboCdmServer.widthHint = 150;
257
        comboCdmServer.setLayoutData(gd_comboCdmServer);
258
        comboCdmServer.select(0);
259

    
260
        txtCdmServerStatus = new Text(cdmServerComposite, SWT.BORDER);
261
        txtCdmServerStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
262
        txtCdmServerStatus.setEditable(false);
263
        GridData gd_txtCdmServerStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
264
        gd_txtCdmServerStatus.widthHint = 100;
265
        txtCdmServerStatus.setLayoutData(gd_txtCdmServerStatus);
266

    
267
        btnCdmServerRefresh = new Button(cdmServerComposite, SWT.NONE);
268
        btnCdmServerRefresh.addSelectionListener(new SelectionAdapter() {
269
            @Override
270
            public void widgetSelected(SelectionEvent e) {
271
                refreshCdmServer();
272
            }
273
        });
274
        btnCdmServerRefresh.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
275
        btnCdmServerRefresh.setText(Messages.RemotingLoginDialog_LABEL_REFRESH);
276

    
277
        Label lblCdmInstance = new Label(cdmServerComposite, SWT.NONE);
278
        GridData gd_lblCdmInstance = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
279
        gd_lblCdmInstance.heightHint = 30;
280
        lblCdmInstance.setLayoutData(gd_lblCdmInstance);
281
        lblCdmInstance.setText(Messages.RemotingLoginDialog_LABEL_CDM_INSTANCE);
282
        lblCdmInstance.setFont(SWTResourceManager.getFont(UBUNTU, 9, SWT.NORMAL));
283

    
284
        comboCdmInstance = new Combo(cdmServerComposite, SWT.READ_ONLY);
285
        comboCdmInstance.addSelectionListener(new SelectionAdapter() {
286
            @Override
287
            public void widgetSelected(SelectionEvent e) {
288
                refreshCdmInstance();
289
            }
290
        });
291
        GridData gd_comboCdmInstance = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
292
        gd_comboCdmInstance.widthHint = 150;
293
        comboCdmInstance.setLayoutData(gd_comboCdmInstance);
294
        comboCdmInstance.select(0);
295

    
296
        txtCdmInstanceStatus = new Text(cdmServerComposite, SWT.BORDER);
297
        txtCdmInstanceStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
298
        txtCdmInstanceStatus.setEditable(false);
299
        GridData gd_txtCdmInstanceStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
300
        gd_txtCdmInstanceStatus.widthHint = 100;
301
        txtCdmInstanceStatus.setLayoutData(gd_txtCdmInstanceStatus);
302

    
303
        btnCdmInstanceRefresh = new Button(cdmServerComposite, SWT.FLAT);
304
        btnCdmInstanceRefresh.addSelectionListener(new SelectionAdapter() {
305
            @Override
306
            public void widgetSelected(SelectionEvent e) {
307
                refreshCdmInstance();
308
            }
309
        });
310
        GridData gd_btnCdmInstanceRefresh = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
311
        gd_btnCdmInstanceRefresh.widthHint = 110;
312
        gd_btnCdmInstanceRefresh.heightHint = 30;
313
        btnCdmInstanceRefresh.setLayoutData(gd_btnCdmInstanceRefresh);
314
        btnCdmInstanceRefresh.setText(Messages.RemotingLoginDialog_LABEL_REFRESH);
315

    
316
        loginComposite = new Composite(remotingComposite, SWT.NONE);
317
        GridData gd_loginComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
318
        gd_loginComposite.widthHint = 487;
319
        gd_loginComposite.heightHint = 70;
320
        loginComposite.setLayoutData(gd_loginComposite);
321
        GridLayout gl_loginComposite = new GridLayout(6, false);
322
        gl_loginComposite.marginTop = 5;
323
        loginComposite.setLayout(gl_loginComposite);
324

    
325
        lblLogin = new Label(loginComposite, SWT.CENTER);
326
        GridData gd_lblLogin = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
327
        gd_lblLogin.widthHint = 50;
328
        lblLogin.setLayoutData(gd_lblLogin);
329
        lblLogin.setText(Messages.RemotingLoginDialog_LABEL_LOGIN_COLON);
330
        lblLogin.setFont(SWTResourceManager.getFont(UBUNTU, 9, SWT.NORMAL));
331

    
332
        txtLogin = new Text(loginComposite, SWT.BORDER);
333
        GridData gd_txtLogin = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
334
        gd_txtLogin.minimumWidth = 80;
335
        gd_txtLogin.widthHint = 80;
336
        gd_txtLogin.heightHint = 15;
337
        txtLogin.setLayoutData(gd_txtLogin);
338

    
339
        lblPassword = new Label(loginComposite, SWT.CENTER);
340
        lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
341
        lblPassword.setText(Messages.RemotingLoginDialog_LABEL_PASSWORD);
342
        lblPassword.setFont(SWTResourceManager.getFont(UBUNTU, 9, SWT.NORMAL));
343

    
344
        txtPassword = new Text(loginComposite, SWT.BORDER | SWT.PASSWORD);
345
        GridData gd_txtPassword = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
346
        gd_txtPassword.minimumWidth = 80;
347
        gd_txtPassword.widthHint = 80;
348
        gd_txtPassword.heightHint = 15;
349
        txtPassword.setLayoutData(gd_txtPassword);
350
        new Label(loginComposite, SWT.NONE);
351
        txtPassword.addKeyListener(new KeyAdapter() {
352
        	@Override
353
        	public void keyPressed(KeyEvent e) {
354
        		if(e.character==SWT.CR){
355
        			connectButtonPressed();
356
        		}
357
        	}
358
        });
359

    
360
        btnConnect = new Button(loginComposite, SWT.FLAT);
361
        btnConnect.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
362
        btnConnect.addSelectionListener(new SelectionAdapter() {
363
			@Override
364
			public void widgetSelected(SelectionEvent e) {
365
				connectButtonPressed();
366
			}
367
		});
368
        btnConnect.setText(Messages.RemotingLoginDialog_LABEL_CONNECT);
369

    
370
        btnRememberMe = new Button(loginComposite, SWT.CHECK);
371
        btnRememberMe.setSelection(true);
372
        GridData gd_btnRememberMe = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
373
        btnRememberMe.setLayoutData(gd_btnRememberMe);
374
        btnRememberMe.setText(Messages.RemotingLoginDialog_LABEL_REMEMBER_ME);
375

    
376
        Label lblDefaultLogin = new Label(loginComposite, SWT.NONE);
377
        GridDataFactory.fillDefaults().span(4, 1).align(SWT.TRAIL, SWT.CENTER).applyTo(lblDefaultLogin);
378
        lblDefaultLogin.setText(String.format(Messages.RemotingLoginDialog_DEFAULT_LOGIN, DEFAULT_USER, DEFAULT_PASS));
379

    
380
        styledTxtMessage = new StyledText(remotingComposite, SWT.NONE);
381
        styledTxtMessage.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
382
        styledTxtMessage.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
383
        styledTxtMessage.setFont(SWTResourceManager.getFont(UBUNTU, 12, SWT.BOLD));
384
        styledTxtMessage.setSelectionBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION_TEXT));
385
        styledTxtMessage.setSelectionForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
386
        styledTxtMessage.setDoubleClickEnabled(false);
387
        styledTxtMessage.setEditable(false);
388
        GridData gd_styledTxtMessage = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
389
        gd_styledTxtMessage.exclude = true;
390
        gd_styledTxtMessage.minimumHeight = MESSAGE_HEIGHT;
391
        gd_styledTxtMessage.heightHint = MESSAGE_HEIGHT;
392
        styledTxtMessage.setLayoutData(gd_styledTxtMessage);
393

    
394
        xpndblcmpstAdvanced = new ExpandableComposite(remotingComposite, SWT.NONE, ExpandableComposite.TWISTIE);
395
        GridData gd_xpndblcmpstAdvanced = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
396
        gd_xpndblcmpstAdvanced.heightHint = 19;
397
        xpndblcmpstAdvanced.setLayoutData(gd_xpndblcmpstAdvanced);
398
        xpndblcmpstAdvanced.addExpansionListener(new IExpansionListener() {
399
            @Override
400
            public void expansionStateChanged(ExpansionEvent e) {
401
                if(e.getState()) {
402
                    shlConnect.setSize(MIN_WIDTH, MIN_EXP_HEIGHT);
403
                } else {
404
                    shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
405
                }
406

    
407
            }
408
            @Override
409
            public void expansionStateChanging(ExpansionEvent e) {
410
            }
411
        });
412
        xpndblcmpstAdvanced.setText(Messages.RemotingLoginDialog_LABEL_ADVANCED);
413
        xpndblcmpstAdvanced.setExpanded(true);
414

    
415
        compAdvanced = new Composite(xpndblcmpstAdvanced, SWT.NONE);
416
        xpndblcmpstAdvanced.setClient(compAdvanced);
417
        compAdvanced.setLayout(new GridLayout(4, false));
418

    
419
        lblPort = new Label(compAdvanced, SWT.CENTER);
420
        lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
421
        lblPort.setSize(0, 0);
422
        lblPort.setText(Messages.RemotingLoginDialog_LABEL_PORT);
423
        lblPort.setFont(SWTResourceManager.getFont(UBUNTU, 9, SWT.NORMAL));
424

    
425
        txtPort = new Text(compAdvanced, SWT.BORDER);
426
        GridData gd_txtPort = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
427
        gd_txtPort.minimumWidth = 50;
428
        gd_txtPort.widthHint = 50;
429
        txtPort.setLayoutData(gd_txtPort);
430

    
431
        lblServerVersion = new Label(compAdvanced, SWT.CENTER);
432
        lblServerVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
433
        lblServerVersion.setText(Messages.RemotingLoginDialog_LABEL_SERVER_CDMLIB_VERSION);
434
        lblServerVersion.setFont(SWTResourceManager.getFont(UBUNTU, 9, SWT.NORMAL));
435

    
436
        txtServerVersion = new Text(compAdvanced, SWT.BORDER);
437
        txtServerVersion.setEditable(false);
438
        txtServerVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
439
        btnStopServer = new Button(compAdvanced, SWT.FLAT);
440
        btnStopServer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
441
        btnStopServer.addMouseListener(new MouseAdapter() {
442
            @Override
443
            public void mouseUp(MouseEvent e) {
444
                stopManagedServer();
445
            }
446
        });
447
        btnStopServer.setText(Messages.RemotingLoginDialog_LABEL_STOP_MANAGED_SERVER);
448

    
449
        lblEditorVersion = new Label(compAdvanced, SWT.CENTER);
450
        lblEditorVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
451
        lblEditorVersion.setText(Messages.RemotingLoginDialog_LABEL_EDITOR_CDMLIB_VERSION);
452
        lblEditorVersion.setFont(SWTResourceManager.getFont(UBUNTU, 9, SWT.NORMAL));
453

    
454
        txtEditorVersion = new Text(compAdvanced, SWT.BORDER);
455
        txtEditorVersion.setEditable(false);
456
        txtEditorVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
457
        new Label(compAdvanced, SWT.NONE);
458
        new Label(compAdvanced, SWT.NONE);
459

    
460
        lblServerCDMVersion = new Label(compAdvanced, SWT.CENTER);
461
        lblServerCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
462
        lblServerCDMVersion.setText(Messages.RemotingLoginDialog_LABEL_SERVER_CDM_VERSION);
463
        lblServerCDMVersion.setFont(SWTResourceManager.getFont(UBUNTU, 9, SWT.NORMAL));
464

    
465
        txtServerCDMVersion = new Text(compAdvanced, SWT.BORDER);
466
        txtServerCDMVersion.setEditable(false);
467
        txtServerCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
468
        new Label(compAdvanced, SWT.NONE);
469
        new Label(compAdvanced, SWT.NONE);
470

    
471
        lblEditorCDMVersion = new Label(compAdvanced, SWT.CENTER);
472
        lblEditorCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
473
        lblEditorCDMVersion.setText(Messages.RemotingLoginDialog_LABEL_EDITOR_CDM_VERSION);
474
        lblEditorCDMVersion.setFont(SWTResourceManager.getFont(UBUNTU, 9, SWT.NORMAL));
475

    
476
        txtEditorCDMVersion = new Text(compAdvanced, SWT.BORDER);
477
        txtEditorCDMVersion.setEditable(false);
478
        txtEditorCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
479

    
480
    }
481

    
482
	private void connectButtonPressed() {
483
		if(selectedCsii.isLocalhostMgd() && !isSelectedCdmInstanceRunningInManagedServer()) {
484
			startManagedServer();
485
		} else {
486
			connect();
487
			if (CdmStore.isActive()){
488
			    PreferencesUtil.setNomenclaturalCodePreferences();
489
			}
490

    
491
		}
492
	}
493

    
494
    private void populateCdmServerCombo() {
495
        Job job = new Job(Messages.RemotingLoginDialog_RETRIEVE_SERVER_INSTANCES) {
496
            @Override
497
            protected IStatus run(IProgressMonitor monitor) {
498
                Display.getDefault().syncExec(new Runnable() {
499
                    @Override
500
                    public void run() {
501
                        for(CdmServerInfo csii : CdmServerInfo.getCdmServers()) {
502
                            csiiMap.put(csii.getName(), csii);
503
                            comboCdmServer.add(csii.getName());
504
                        }
505
                        int serverIndex = -1;
506
                        if(serverName != null) {
507
                            serverIndex = comboCdmServer.indexOf(serverName);
508
                        }
509
                        if(serverIndex == -1) {
510
                            comboCdmServer.select(0);
511
                            autoConnect = false;
512
                        } else {
513
                            comboCdmServer.select(serverIndex);
514
                        }
515
                        CdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
516
                        if(devRemoteSource != null) {
517
                            isDevRemoteSource = true;
518
                            String username = System.getProperty("cdm.server.dev.username"); //$NON-NLS-1$
519
                            String password = System.getProperty("cdm.server.dev.password"); //$NON-NLS-1$
520
                            if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
521
                                txtLogin.setText(username);
522
                                txtPassword.setText(password);
523
                                CdmStore.connect(devRemoteSource, RemotingLoginDialog.this);
524
                            }
525
                        } else {
526
                            refreshCdmServer();
527
                        }
528
                    }
529
                });
530
                return Status.OK_STATUS;
531
            }
532
        };
533
        job.schedule();
534
    }
535

    
536

    
537
    private void refreshCdmServer() {
538
        try{
539
            txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
540
            clearOnServerChange();
541
            emptyCredentials();
542
            updateSelectedCdmServer();
543
            Display.getDefault().asyncExec(new Runnable() {
544
                @Override
545
                public void run() {
546
                    checkSelectedCdmServer();
547
                }
548
            });
549
        }
550
        catch(SWTException e){
551
            //catch widget is disposed exception which may occurr if
552
            //dialog is closed but the runnable tries to update
553
        }
554
    }
555

    
556
    private void updateSelectedCdmServer() {
557
        int selIndex = comboCdmServer.getSelectionIndex();
558
        if(selIndex != -1) {
559
            selectedCsii = csiiMap.get(comboCdmServer.getItem(selIndex));
560
        }
561
    }
562

    
563
    private void updatePort() {
564
        txtPort.setText(""); //$NON-NLS-1$
565
        if(selectedCsii != null) {
566
            int port = selectedCsii.getPort();
567
            if(port == CdmServerInfo.NULL_PORT) {
568
                txtPort.setText(CdmServerInfo.NULL_PORT_STRING);
569
            } else {
570
                txtPort.setText(String.valueOf(port));
571
            }
572
        }
573
    }
574

    
575
    private int getPort() {
576
        int port = CdmServerInfo.NULL_PORT;
577
        try {
578
            port = Integer.valueOf(txtPort.getText());
579
        } catch (NumberFormatException nfe) {
580
            if(!CdmServerInfo.NULL_PORT_STRING.equals(txtPort.getText())) {
581
                setMessage(Messages.RemotingLoginDialog_MESSAGE_PORT_SHOULD_BE_INTEGER);
582
            }
583
        }
584
        return port;
585
    }
586

    
587
    private void checkSelectedCdmServer() {
588
        try{
589
            if(selectedCsii != null) {
590
                if(selectedCsii.isLocalhost()) {
591
                    txtPort.setEditable(true);
592
                    txtPort.setEnabled(true);
593
                }
594
                if(selectedCsii.pingServer()) {
595
                    txtCdmServerStatus.setText(STATUS_AVAILABLE);
596
                    populateCdmInstanceCombo(true);
597
                    String serverVersionTimestamp = generateLastModifiedTooltip(selectedCsii.getCdmlibLastModified());
598
                    txtServerVersion.setText(selectedCsii.getCdmlibServicesVersion() + ":" + serverVersionTimestamp); //$NON-NLS-1$
599
                } else {
600
                    txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
601
                    comboCdmInstance.removeAll();
602
                    disableCdmInstanceControls("", "");
603
                }
604
            }
605
            updatePort();
606
        }
607
        catch(SWTException e){
608
            //catch widget is disposed exception which may occurr if
609
            //dialog is closed but the runnable tries to update
610
        }
611
    }
612

    
613

    
614
    private void populateCdmInstanceCombo(final boolean forceRefresh) {
615
        comboCdmInstance.removeAll();
616
        comboCdmInstance.setEnabled(false);
617
        btnConnect.setEnabled(false);
618
        txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
619
        txtCdmInstanceStatus.setToolTipText(""); //$NON-NLS-1$
620

    
621
        serverJob = new Job(Messages.RemotingLoginDialog_RETRIEVE_SERVER_INSTANCES) {
622
            @Override
623
            protected IStatus run(IProgressMonitor monitor) {
624
                try {
625
                    if(selectedCsii != null) {
626
                        if(forceRefresh) {
627
                            selectedCsii.refreshInstances();
628
                        }
629
                        final List<CdmInstanceInfo> instances = selectedCsii.getInstances();
630
                        Display.getDefault().asyncExec(new Runnable() {
631
                            @Override
632
                            public void run() {
633
                                if(!instances.isEmpty()) {
634
                                    for(CdmInstanceInfo cdmInstance : instances) {
635
                                        comboCdmInstance.add(cdmInstance.getName());
636
                                    }
637
                                    int instanceIndex = -1;
638
                                    if(instanceName != null) {
639
                                        instanceIndex = comboCdmInstance.indexOf(instanceName);
640
                                    }
641
                                    if(instanceIndex == -1) {
642
                                        comboCdmInstance.select(0);
643
                                        autoConnect = false;
644
                                    } else {
645
                                        comboCdmInstance.select(instanceIndex);
646
                                    }
647
                                    refreshCdmInstance();
648
                                    comboCdmInstance.setEnabled(true);
649
                                    if(autoConnect) {
650
                                        connect();
651
                                    }
652

    
653
                                } else {
654
                                    txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
655
                                    btnConnect.setEnabled(false);
656
                                }
657
                            }
658
                        });
659
                    }
660
                } catch (final CDMServerException e) {
661
                    MessagingUtils.warn(getClass(), e);
662
                    Display.getDefault().asyncExec(new Runnable() {
663
                        @Override
664
                        public void run() {
665
                            disableCdmInstanceControls(STATUS_NOT_AVAILABLE, e.getMessage());
666
                        }
667
                    });
668
                }
669
                return Status.OK_STATUS;
670
            }
671
        };
672

    
673
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE) && !isDevRemoteSource) {
674
            // Start the Job
675
            serverJob.schedule();
676
        }
677
    }
678

    
679
    private void disableCdmInstanceControls(String cdmInstanceStatus, String tooltip) {
680
        txtCdmInstanceStatus.setText(cdmInstanceStatus);
681
        txtCdmInstanceStatus.setToolTipText(tooltip);
682
        comboCdmInstance.setEnabled(false);
683
        btnConnect.setEnabled(false);
684

    
685
    }
686
    private void refreshCdmInstance() {
687
        txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
688
        clearOnInstanceChange();
689
        updateSelectedCdmInstance();
690
        checkSelectedCdmInstance();
691
        updateManagedServerControls();
692
    }
693

    
694
    private void updateSelectedCdmInstance() {
695
        int selIndex = comboCdmInstance.getSelectionIndex();
696
        if(selIndex != -1) {
697
            selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
698

    
699
            if(loadLoginPrefs && !isDevRemoteSource) {
700
                readPrefCredentials();
701
            }
702
        }
703
    }
704

    
705
    private void updateManagedServerControls() {
706
        if(selectedCsii.isLocalhostMgd()) {
707
            if(isSelectedCdmInstanceRunningInManagedServer()) {
708
                txtCdmInstanceStatus.setText(STATUS_STARTED);
709
            } else {
710
                txtCdmInstanceStatus.setText(STATUS_NOT_STARTED);
711
            }
712
            btnConnect.setEnabled(true);
713
            selectedCsii.setPort(getManagedServerPort());
714
            updatePort();
715
        }
716

    
717

    
718
        if(isManagedServerRunning()) {
719
            btnStopServer.setEnabled(true);
720
        } else {
721
            btnStopServer.setEnabled(false);
722
        }
723
    }
724

    
725
    private boolean isManagedServerRunning() {
726
        return CdmStore.getManagedServer() != null && CdmStore.getManagedServer().isAlive();
727
    }
728

    
729
    private boolean isSelectedCdmInstanceRunningInManagedServer() {
730
        return CdmStore.getManagedServer() != null &&
731
                CdmStore.getManagedServer().isAlive() &&
732
                selectedCsii.isLocalhostMgd() &&
733
                CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName());
734
    }
735

    
736
    private void startManagedServer() {
737
        if(isManagedServerRunning()) {
738
            if(CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName())) {
739
                return;
740
            } else {
741
                Display.getDefault().syncExec(new Runnable() {
742
                    @Override
743
                    public void run() {
744
                        stopManagedServer();
745
                    }
746
                });
747
            }
748
        }
749

    
750

    
751
        Job job = new Job(Messages.RemotingLoginDialog_JOB_SERVER_LAUNCH) {
752

    
753
            @Override
754
            public IStatus run(IProgressMonitor monitor) {
755
                String mgdServerConfigFileName = "mgd.datasources.xml"; //$NON-NLS-1$
756
                String config = CDMServerUtils.convertEditorToServerConfig();
757
                File managedServerConfigFile;
758
                int maxUnits = 50;
759
                monitor.beginTask(Messages.RemotingLoginDialog_TASK_LAUNCHING_SERVER, maxUnits);
760
                try {
761
                    monitor.subTask(String.format(Messages.RemotingLoginDialog_GENERATING_CONFIG_FILE, selectedCdmInstance.getName()));
762
                    managedServerConfigFile = CDMServerUtils.writeManagedServerConfig(config, mgdServerConfigFileName);
763
                    monitor.worked(1);
764
                    CdmStore.setManagedServer(new CDMServer(selectedCdmInstance.getName(), managedServerConfigFile));
765
                    monitor.subTask(Messages.RemotingLoginDialog_STARTING_MGD_SERVER);
766
                    CdmStore.getManagedServer().start(false, RemotingLoginDialog.this);
767
                    int serverUnits = 0;
768

    
769
                    // the following loop is a 'fake' progress monitoring where the progress
770
                    // bar is advanced by one unit every second until maxUnits -2
771
                    while(!CdmStore.getManagedServer().isStarted() && !CdmStore.getManagedServer().isFailed()) {
772
                        if(serverUnits < maxUnits - 2) {
773
                            try {
774
                                Thread.sleep(1000);
775
                            } catch (InterruptedException e) {
776
                            }
777
                            monitor.worked(1);
778
                            serverUnits++;
779
                        }
780
                    }
781
                    Display.getDefault().asyncExec(new Runnable() {
782
                        @Override
783
                        public void run() {
784
                                hide(false);
785
                                updateManagedServerControls();
786
                                connect();
787
                            }
788
                    });
789
                } catch (IOException ioe) {
790
                    MessagingUtils.errorDialog(Messages.RemotingLoginDialog_ERROR_GENERATING_CONFIG_FILE,
791
                            this,
792
                            ioe.getMessage(),
793
                            TaxeditorStorePlugin.PLUGIN_ID,
794
                            ioe,
795
                            true);
796
                } catch (CDMEmbeddedServerException cse) {
797
                    MessagingUtils.errorDialog(Messages.RemotingLoginDialog_ERROR_STARTING_SERVER,
798
                            this,
799
                            cse.getMessage(),
800
                            TaxeditorStorePlugin.PLUGIN_ID,
801
                            cse,
802
                            true);
803
                } finally {
804
                    monitor.done();
805
                }
806
                return Status.OK_STATUS;
807
            }
808
        };
809

    
810
        // configure the job
811
        job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
812
        job.setUser(true);
813
        // schedule job
814
        hide(true);
815
        job.schedule();
816
    }
817

    
818
    private void stopManagedServer() {
819
        try {
820
            CdmStore.getManagedServer().stop();
821
        } catch (Exception e) {
822
            MessagingUtils.errorDialog(Messages.RemotingLoginDialog_ERROR_STOPPING_SERVER,
823
                    this,
824
                    String.format(Messages.RemotingLoginDialog_COULD_NOT_STOP_SERVER, CdmStore.getManagedServer().getPort()),
825
                    TaxeditorStorePlugin.PLUGIN_ID,
826
                    e,
827
                    true);
828
        }
829
        CdmStore.setManagedServer(null);
830
        updateManagedServerControls();
831
    }
832

    
833
    private int getManagedServerPort() {
834
        return CdmStore.getManagedServer() == null ? CdmServerInfo.NULL_PORT : CdmStore.getManagedServer().getPort();
835
    }
836

    
837
    private void checkSelectedCdmInstance() {
838
        boolean available = false;
839
        String status = STATUS_NOT_AVAILABLE;
840
        String message = null;
841

    
842
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
843
            try {
844
                if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
845
                    status = STATUS_AVAILABLE;
846
                    available = true;
847
                } else {
848
                    status = STATUS_NOT_AVAILABLE;
849
                    available = false;
850
                }
851

    
852
                if(available) {
853
                    txtServerCDMVersion.setText(selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort()).getDbSchemaVersion());
854
                    int compareDbSchemaVersion = selectedCsii.compareDbSchemaVersion(selectedCdmInstance, getPort());
855
                    int compareCdmlibServicesVersion = 0;
856
                    boolean disableServicesApiTimestampCheck =
857
                            PreferencesUtil.getPreferenceStore().getBoolean((IPreferenceKeys.DISABLE_SERVICES_API_TIMESTAMP_CHECK));
858
                    if(!disableServicesApiTimestampCheck) {
859
                        compareCdmlibServicesVersion = selectedCsii.compareCdmlibServicesVersion();
860
                    }
861
                    if(compareDbSchemaVersion > 0 || compareCdmlibServicesVersion > 0) {
862
                        status =  STATUS_NOT_COMPATIBLE;
863
                        available = false;
864
                        message = MESG_COMPATIBLE_EDITOR_OLD;
865
                    } else if(compareDbSchemaVersion < 0 || compareCdmlibServicesVersion < 0) {
866
                        status = STATUS_NOT_COMPATIBLE;
867
                        available = false;
868
                        message = MESG_COMPATIBLE_SERVER_OLD;
869
                    } else {
870
                        status =  STATUS_AVAILABLE;
871
                        available = true;
872
                        message = ""; //$NON-NLS-1$
873
                    }
874
                }
875
            } catch (Exception e) {
876
                txtCdmInstanceStatus.setToolTipText(e.getMessage());
877
            } finally {
878
                btnConnect.setEnabled(available);
879
                txtCdmInstanceStatus.setText(status);
880
                if(!StringUtils.isBlank(message)) {
881
                    setMessage(message);
882
                }
883
            }
884
        }
885
    }
886

    
887
    private void connect() {
888
        checkSelectedCdmInstance();
889

    
890
        if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
891
            return;
892
        }
893

    
894
        ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
895

    
896
        if(!validateLogin(source)) {
897
            return;
898
        }
899

    
900
        try {
901
            CdmStore.connect(source, this);
902
        } catch (Exception e) {
903
            // Do not expect anything to go wrong at this point, so we throw a runtime exception
904
            // if any problems
905
            throw new RuntimeException(e);
906
        }
907

    
908
    }
909

    
910

    
911
    public boolean isRememberMe() {
912
        return btnRememberMe.getSelection();
913
    }
914

    
915
    private void persistPrefLastServerInstance() {
916
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
917
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
918

    
919
        lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
920
        lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
921

    
922
        flushPreferences(lastServerInstancePrefs);
923
    }
924

    
925
    private void persistPrefCredentials() {
926
         IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
927
         Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
928
         credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
929
         credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
930
         flushPreferences(credentialsPrefs);
931
    }
932

    
933
    private void removePrefCredentials() {
934
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
935
        Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
936
        credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
937
        credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
938
        removePreferences(credentialsPrefs);
939
   }
940
    private void removePreferences(Preferences prefs) {
941
        try {
942
            prefs.removeNode();
943
        } catch (BackingStoreException bse) {
944
            setMessage(bse.getMessage());
945
        }
946
    }
947
    private void flushPreferences(Preferences prefs) {
948
        try {
949
            prefs.flush();
950
        } catch (BackingStoreException bse) {
951
            setMessage(bse.getMessage());
952
        }
953
    }
954

    
955

    
956

    
957
    private void readPrefCredentials() {
958
        String username, password;
959
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
960
        Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
961
        username = credentialsPrefs.get(getUsernamePrefKey(), ""); //$NON-NLS-1$
962
        txtLogin.setText(username);
963
        password = credentialsPrefs.get(getPasswordPrefKey(),""); //$NON-NLS-1$
964
        txtPassword.setText(password);
965
        if(username.isEmpty() || password.isEmpty()) {
966
            autoConnect = false;
967
        }
968
    }
969

    
970
    private void readPrefLastServerInstance() {
971
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
972
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
973

    
974
        serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
975
        instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
976
    }
977

    
978
    private void emptyCredentials() {
979
        txtLogin.setText(""); //$NON-NLS-1$
980
        txtPassword.setText(""); //$NON-NLS-1$
981
    }
982

    
983
    private String getUsernamePrefKey() {
984
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
985
    }
986

    
987
    private String getPasswordPrefKey() {
988
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
989
    }
990

    
991
    private boolean validateLogin(ICdmRemoteSource remoteSource) {
992
        if(getUsername() == null || getUsername().isEmpty()) {
993
            setMessage(Messages.RemotingLoginDialog_LOGIN_CANNOT_BE_EMPTY);
994
            return false;
995
        }
996
        if(getPassword() == null || getPassword().isEmpty()) {
997
            setMessage(Messages.RemotingLoginDialog_PASSWORD_CANNOT_BE_EMPTY);
998
            return false;
999
        }
1000

    
1001

    
1002
        try {
1003
            IUserService userService = CdmApplicationRemoteConfiguration.getUserService(remoteSource);
1004
            UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(getUsername(), getPassword());
1005
            CdmApplicationRemoteConfiguration.getAuthenticationManager(userService).authenticate(token);
1006
        } catch(BadCredentialsException e){
1007
            setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
1008
            return false;
1009
        } catch(LockedException e){
1010
            setMessage(LoginManager.ACCOUNT_LOCKED_MESSAGE);
1011
            return false;
1012
        } catch(IllegalArgumentException e){
1013
            setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
1014
            return false;
1015
        }
1016
        return true;
1017
    }
1018

    
1019
    public String getUsername() {
1020
        return txtLogin.getText();
1021
    }
1022

    
1023
    public String getPassword() {
1024
        return txtPassword.getText();
1025
    }
1026

    
1027
    public void setMessage(String message) {
1028
        if(message != null && !message.isEmpty()) {
1029
            if(message.length() > 60) {
1030
                styledTxtMessage.setToolTipText(message);
1031
                message = message.substring(0, 60) + "..."; //$NON-NLS-1$
1032
            }
1033
            styledTxtMessage.setText(message);
1034
            styledTxtMessage.setVisible(true);
1035
            ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
1036
            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
1037
            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
1038
        } else {
1039
            styledTxtMessage.setText(""); //$NON-NLS-1$
1040
            styledTxtMessage.setVisible(false);
1041
            ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
1042
            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage());
1043
            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage());
1044
        }
1045
        remotingComposite.layout();
1046
    }
1047

    
1048
    private int getHeightWithoutMessage() {
1049
        if(xpndblcmpstAdvanced.isExpanded()) {
1050
            return MIN_EXP_HEIGHT;
1051
        } else {
1052
            return MIN_HEIGHT;
1053
        }
1054
    }
1055

    
1056

    
1057
    public void hide(boolean isHidden) {
1058
        if(shlConnect != null && shlConnect.getDisplay() != null) {
1059
            shlConnect.setVisible(!isHidden);
1060
        }
1061
    }
1062
    public void dispose() {
1063
        if(shlConnect != null && shlConnect.getDisplay() != null) {
1064
            shlConnect.dispose();
1065
        }
1066
    }
1067

    
1068
    public void onComplete() {
1069
        Display.getDefault().asyncExec(new Runnable() {
1070
            @Override
1071
            public void run() {
1072
                if(!isDevRemoteSource) {
1073
                    if(isRememberMe()) {
1074
                        persistPrefCredentials();
1075
                    }else{
1076
                    	removePrefCredentials();
1077
                    }
1078
                    persistPrefLastServerInstance();
1079
                }
1080
                dispose();
1081
            }
1082
        });
1083
    }
1084

    
1085
    private String generateLastModifiedTooltip(String cdmlibLastModified) {
1086
        if(StringUtils.isBlank(cdmlibLastModified)) {
1087
            return ""; //$NON-NLS-1$
1088
        }
1089
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.z"); //$NON-NLS-1$
1090
        Date cdmlibLastModifiedDate;
1091
        String cdmlibLastModifiedTimestamp = ""; //$NON-NLS-1$
1092

    
1093
        cdmlibLastModifiedDate = new Date(Long.valueOf(cdmlibLastModified));
1094
        cdmlibLastModifiedTimestamp = sdf.format(cdmlibLastModifiedDate);
1095

    
1096
        return cdmlibLastModifiedTimestamp;
1097
    }
1098

    
1099
    private void setEditorInfo() {
1100
        txtEditorCDMVersion.setText(CdmMetaData.getDbSchemaVersion());
1101
        String editorVersionTimestamp = generateLastModifiedTooltip(CdmApplicationState.getCdmlibLastModified());
1102
        txtEditorVersion.setText(CdmApplicationState.getCdmlibVersion() + ":" + editorVersionTimestamp); //$NON-NLS-1$
1103
    }
1104

    
1105
    private void clearOnServerChange() {
1106
        setMessage(""); //$NON-NLS-1$
1107
        txtServerCDMVersion.setText(""); //$NON-NLS-1$
1108
        txtServerVersion.setText(""); //$NON-NLS-1$
1109
        txtServerVersion.setToolTipText(""); //$NON-NLS-1$
1110
        txtServerCDMVersion.setText(""); //$NON-NLS-1$
1111
        comboCdmInstance.removeAll();
1112
        txtCdmInstanceStatus.setText(""); //$NON-NLS-1$
1113
        txtPort.setEditable(false);
1114
        txtPort.setEnabled(false);
1115
    }
1116

    
1117
    private void clearOnInstanceChange() {
1118
        setMessage(""); //$NON-NLS-1$
1119
        txtServerCDMVersion.setText(""); //$NON-NLS-1$
1120
    }
1121

    
1122
    /**
1123
     * {@inheritDoc}
1124
     */
1125
    @Override
1126
    public void handleError(final Throwable t) {
1127

    
1128
        Display.getDefault().syncExec(new Runnable() {
1129
            @Override
1130
            public void run() {
1131
                serverJob.cancel();
1132

    
1133
                String title = Messages.RemotingLoginDialog_SERVER_LAUNCH_ERROR;
1134
                String  message = t.getMessage();
1135

    
1136

    
1137
                MessagingUtils.errorDialog(title,
1138
                        this,
1139
                        message,
1140
                        TaxeditorStorePlugin.PLUGIN_ID,
1141
                        t,
1142
                        true);
1143
            }
1144
        });
1145
    }
1146

    
1147

    
1148
}
(5-5/7)