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
                    txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
604
                    MessagingUtils.warningDialog(Messages.RemotingLoginDialog_CONNECTION_FAILED_TITLE, this,
605
                            Messages.RemotingLoginDialog_CONNECTION_FAILED_MESSAGE);
606
                    logger.warn(Messages.RemotingLoginDialog_CONNECTION_FAILED_TITLE, e);
607
                }
608
            }
609
            updatePort();
610
        }
611
        catch(SWTException e){
612
            //catch widget is disposed exception which may occurr if
613
            //dialog is closed but the runnable tries to update
614
        }
615
    }
616

    
617

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

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

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

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

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

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

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

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

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

    
721

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

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

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

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

    
754

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
912
    }
913

    
914

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

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

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

    
926
        flushPreferences(lastServerInstancePrefs);
927
    }
928

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

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

    
959

    
960

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

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

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

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

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

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

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

    
1005

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

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

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

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

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

    
1060

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

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

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

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

    
1100
        return cdmlibLastModifiedTimestamp;
1101
    }
1102

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

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

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

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

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

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

    
1140

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

    
1151

    
1152
}
(5-5/7)