Project

General

Profile

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

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

    
81

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

    
89
    private Logger logger = Logger.getLogger(getClass());
90

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

    
102
    private final Map<String, CdmServerInfo> csiiMap = new HashMap<String, CdmServerInfo>();
103

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

    
113
    private final static String MESG_COMPATIBLE_EDITOR_OLD = Messages.RemotingLoginDialog_UPDATE_EDITOR;
114
    private final static String MESG_COMPATIBLE_SERVER_OLD = Messages.RemotingLoginDialog_CHOOSE_COMPATIBLE_CDM_SERVER;
115

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

    
118
    private final static String LOGIN_NODE = "login"; //$NON-NLS-1$
119
    private final static String USERNAME_SUFFIX = "_username"; //$NON-NLS-1$
120
    private final static String PASSWORD_SUFFIX = "_password"; //$NON-NLS-1$
121

    
122
    private final static String LAST_SERVER_INSTANCE_NODE = "lastServerInstance"; //$NON-NLS-1$
123
    private final static String LAST_SERVER_KEY = "lastServerKey"; //$NON-NLS-1$
124
    private final static String LAST_INSTANCE_KEY = "lastInstanceKey"; //$NON-NLS-1$
125

    
126

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

    
147

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

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

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

    
182

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

    
191
    /**
192
     * Open the dialog.
193
     * @return the result
194
     */
195
    public Object open() {
196

    
197
        createContents();
198
        if(serverName == null && instanceName == null) {
199
            readPrefLastServerInstance();
200
        }
201

    
202
        setEditorInfo();
203
        populateCdmServerCombo();
204
        shlConnect.open();
205
        shlConnect.layout();
206

    
207
        xpndblcmpstAdvanced.setExpanded(false);
208

    
209
        Display display = getParent().getDisplay();
210

    
211
        while (!shlConnect.isDisposed()) {
212
            if (!display.readAndDispatch()) {
213
                display.sleep();
214
            }
215
        }
216

    
217
        return result;
218
    }
219

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

    
230
        remotingComposite = new Composite(shlConnect, SWT.NONE);
231
        remotingComposite.setLayout(new GridLayout(1, false));
232

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
412
        compAdvanced = new Composite(xpndblcmpstAdvanced, SWT.NONE);
413
        xpndblcmpstAdvanced.setClient(compAdvanced);
414
        compAdvanced.setLayout(new GridLayout(4, false));
415

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

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

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

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

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

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

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

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

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

    
473
        txtEditorCDMVersion = new Text(compAdvanced, SWT.BORDER);
474
        txtEditorCDMVersion.setEditable(false);
475
        txtEditorCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
476

    
477
    }
478

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

    
488
		}
489
	}
490

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

    
533

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

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

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

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

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

    
616

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

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

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

    
676
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE) && !isDevRemoteSource) {
677
            // Start the Job
678
            serverJob.schedule();
679
        }
680
    }
681

    
682
    private void disableCdmInstanceControls(String cdmInstanceStatus, String tooltip) {
683
        txtCdmInstanceStatus.setText(cdmInstanceStatus);
684
        txtCdmInstanceStatus.setToolTipText(tooltip);
685
        comboCdmInstance.setEnabled(false);
686
        btnConnect.setEnabled(false);
687

    
688
    }
689
    private void refreshCdmInstance() {
690
        txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
691
        clearOnInstanceChange();
692
        updateSelectedCdmInstance();
693
        checkSelectedCdmInstance();
694
        updateManagedServerControls();
695
    }
696

    
697
    private void updateSelectedCdmInstance() {
698
        int selIndex = comboCdmInstance.getSelectionIndex();
699
        if(selIndex != -1) {
700
            selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
701

    
702
            if(loadLoginPrefs && !isDevRemoteSource) {
703
                readPrefCredentials();
704
            }
705
        }
706
    }
707

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

    
720

    
721
        if(isManagedServerRunning()) {
722
            btnStopServer.setEnabled(true);
723
        } else {
724
            btnStopServer.setEnabled(false);
725
        }
726
    }
727

    
728
    private boolean isManagedServerRunning() {
729
        return CdmStore.getManagedServer() != null && CdmStore.getManagedServer().isAlive();
730
    }
731

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

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

    
753

    
754
        Job job = new Job(Messages.RemotingLoginDialog_JOB_SERVER_LAUNCH) {
755

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

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

    
813
        // configure the job
814
        job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
815
        job.setUser(true);
816
        // schedule job
817
        hide(true);
818
        job.schedule();
819
    }
820

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

    
836
    private int getManagedServerPort() {
837
        return CdmStore.getManagedServer() == null ? CdmServerInfo.NULL_PORT : CdmStore.getManagedServer().getPort();
838
    }
839

    
840
    private void checkSelectedCdmInstance() {
841
        boolean available = false;
842
        String status = STATUS_NOT_AVAILABLE;
843
        String message = null;
844

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

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

    
890
    private void connect() {
891
        checkSelectedCdmInstance();
892

    
893
        if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
894
            return;
895
        }
896

    
897
        ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
898

    
899
        if(!validateLogin(source)) {
900
            return;
901
        }
902

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

    
911
    }
912

    
913

    
914
    public boolean isRememberMe() {
915
        return btnRememberMe.getSelection();
916
    }
917

    
918
    private void persistPrefLastServerInstance() {
919
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
920
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
921

    
922
        lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
923
        lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
924

    
925
        flushPreferences(lastServerInstancePrefs);
926
    }
927

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

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

    
958

    
959

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

    
973
    private void readPrefLastServerInstance() {
974
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
975
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
976

    
977
        serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
978
        instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
979
    }
980

    
981
    private void emptyCredentials() {
982
        txtLogin.setText(""); //$NON-NLS-1$
983
        txtPassword.setText(""); //$NON-NLS-1$
984
    }
985

    
986
    private String getUsernamePrefKey() {
987
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
988
    }
989

    
990
    private String getPasswordPrefKey() {
991
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
992
    }
993

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

    
1004

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

    
1022
    public String getUsername() {
1023
        return txtLogin.getText();
1024
    }
1025

    
1026
    public String getPassword() {
1027
        return txtPassword.getText();
1028
    }
1029

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

    
1051
    private int getHeightWithoutMessage() {
1052
        if(xpndblcmpstAdvanced.isExpanded()) {
1053
            return MIN_EXP_HEIGHT;
1054
        } else {
1055
            return MIN_HEIGHT;
1056
        }
1057
    }
1058

    
1059

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

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

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

    
1096
        cdmlibLastModifiedDate = new Date(Long.valueOf(cdmlibLastModified));
1097
        cdmlibLastModifiedTimestamp = sdf.format(cdmlibLastModifiedDate);
1098

    
1099
        return cdmlibLastModifiedTimestamp;
1100
    }
1101

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

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

    
1120
    private void clearOnInstanceChange() {
1121
        setMessage(""); //$NON-NLS-1$
1122
        txtServerCDMVersion.setText(""); //$NON-NLS-1$
1123
    }
1124

    
1125
    /**
1126
     * {@inheritDoc}
1127
     */
1128
    @Override
1129
    public void handleError(final Throwable t) {
1130

    
1131
        Display.getDefault().syncExec(new Runnable() {
1132
            @Override
1133
            public void run() {
1134
                serverJob.cancel();
1135

    
1136
                String title = Messages.RemotingLoginDialog_SERVER_LAUNCH_ERROR;
1137
                String  message = t.getMessage();
1138

    
1139

    
1140
                MessagingUtils.errorDialog(title,
1141
                        this,
1142
                        message,
1143
                        TaxeditorStorePlugin.PLUGIN_ID,
1144
                        t,
1145
                        true);
1146
            }
1147
        });
1148
    }
1149

    
1150

    
1151
}
(5-5/7)