Project

General

Profile

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

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

    
20
import org.apache.commons.lang.StringUtils;
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.swt.SWT;
28
import org.eclipse.swt.custom.StyledText;
29
import org.eclipse.swt.events.KeyAdapter;
30
import org.eclipse.swt.events.KeyEvent;
31
import org.eclipse.swt.events.MouseAdapter;
32
import org.eclipse.swt.events.MouseEvent;
33
import org.eclipse.swt.events.SelectionAdapter;
34
import org.eclipse.swt.events.SelectionEvent;
35
import org.eclipse.swt.graphics.Point;
36
import org.eclipse.swt.layout.FillLayout;
37
import org.eclipse.swt.layout.GridData;
38
import org.eclipse.swt.layout.GridLayout;
39
import org.eclipse.swt.widgets.Button;
40
import org.eclipse.swt.widgets.Combo;
41
import org.eclipse.swt.widgets.Composite;
42
import org.eclipse.swt.widgets.Dialog;
43
import org.eclipse.swt.widgets.Display;
44
import org.eclipse.swt.widgets.Label;
45
import org.eclipse.swt.widgets.Shell;
46
import org.eclipse.swt.widgets.Text;
47
import org.eclipse.ui.forms.events.ExpansionEvent;
48
import org.eclipse.ui.forms.events.IExpansionListener;
49
import org.eclipse.ui.forms.widgets.ExpandableComposite;
50
import org.eclipse.ui.progress.IProgressConstants;
51
import org.eclipse.wb.swt.SWTResourceManager;
52
import org.osgi.service.prefs.BackingStoreException;
53
import org.osgi.service.prefs.Preferences;
54
import org.springframework.security.authentication.BadCredentialsException;
55
import org.springframework.security.authentication.LockedException;
56
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
57

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

    
79

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

    
87
    private static final String UBUNTU = "Ubuntu"; //$NON-NLS-1$
88
	protected Object result;
89
    protected Shell shlConnect;
90
    private Text txtCdmServerStatus;
91
    private Text txtCdmInstanceStatus;
92
    private Combo comboCdmServer;
93
    private Combo comboCdmInstance;
94
    private Button btnConnect;
95

    
96
    private final Map<String, CdmServerInfo> csiiMap = new HashMap<String, CdmServerInfo>();
97

    
98
    private final static String STATUS_AVAILABLE = Messages.RemotingLoginDialog_STATUS_AVAILABLE;
99
    private final static String STATUS_NOT_AVAILABLE = Messages.RemotingLoginDialog_STATUS_NOT_AVAILABLE;
100
    private final static String STATUS_STARTED = Messages.RemotingLoginDialog_STATUS_STARTED;
101
    private final static String STATUS_NOT_STARTED = Messages.RemotingLoginDialog_STATUS_NOT_STARTED;
102
    private final static String STATUS_RETRIEVING = Messages.RemotingLoginDialog_STATUS_RETRIEVING;
103
    private final static String STATUS_CHECKING_AVAILABILITY = Messages.RemotingLoginDialog_STATUS_CHECKING;
104
    private final static String STATUS_NO_INSTANCES = Messages.RemotingLoginDialog_STATUS_NO_INSTANCES_FOUND;
105
    private final static String STATUS_ERROR = Messages.RemotingLoginDialog_STATUS_ERROR;
106
    private final static String STATUS_REMOTING_NOT_ACTIVATED = Messages.RemotingLoginDialog_STATUS_REMOTING_NOT_ACTIVATED;
107
    private final static String STATUS_NOT_COMPATIBLE = Messages.RemotingLoginDialog_STATUS_NOT_COMPATIBLE;
108

    
109
    private final static String MESG_COMPATIBLE_EDITOR_OLD = Messages.RemotingLoginDialog_UPDATE_EDITOR;
110
    private final static String MESG_COMPATIBLE_SERVER_OLD = Messages.RemotingLoginDialog_CHOOSE_COMPATIBLE_CDM_SERVER;
111

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

    
114
    private final static String LOGIN_NODE = "login"; //$NON-NLS-1$
115
    private final static String USERNAME_SUFFIX = "_username"; //$NON-NLS-1$
116
    private final static String PASSWORD_SUFFIX = "_password"; //$NON-NLS-1$
117

    
118
    private final static String LAST_SERVER_INSTANCE_NODE = "lastServerInstance"; //$NON-NLS-1$
119
    private final static String LAST_SERVER_KEY = "lastServerKey"; //$NON-NLS-1$
120
    private final static String LAST_INSTANCE_KEY = "lastInstanceKey"; //$NON-NLS-1$
121

    
122
    private final static String REFRESH_LABEL = Messages.RemotingLoginDialog_LABEL_REFRESH;
123

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

    
144

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

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

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

    
179

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

    
188
    /**
189
     * Open the dialog.
190
     * @return the result
191
     */
192
    public Object open() {
193

    
194
        createContents();
195
        if(serverName == null && instanceName == null) {
196
            readPrefLastServerInstance();
197
        }
198

    
199
        setEditorInfo();
200
        populateCdmServerCombo();
201
        shlConnect.open();
202
        shlConnect.layout();
203

    
204
        xpndblcmpstAdvanced.setExpanded(false);
205

    
206
        Display display = getParent().getDisplay();
207

    
208
        while (!shlConnect.isDisposed()) {
209
            if (!display.readAndDispatch()) {
210
                display.sleep();
211
            }
212
        }
213

    
214
        return result;
215
    }
216

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

    
227
        remotingComposite = new Composite(shlConnect, SWT.NONE);
228
        remotingComposite.setLayout(new GridLayout(1, false));
229

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
475
    }
476

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

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

    
528

    
529
    private void refreshCdmServer() {
530
        txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
531
        clearOnServerChange();
532
        emptyCredentials();
533
        updateSelectedCdmServer();
534
        Display.getDefault().asyncExec(new Runnable() {
535
            @Override
536
            public void run() {
537
                checkSelectedCdmServer();
538
            }
539
        });
540

    
541
    }
542

    
543
    private void updateSelectedCdmServer() {
544
        int selIndex = comboCdmServer.getSelectionIndex();
545
        if(selIndex != -1) {
546
            selectedCsii = csiiMap.get(comboCdmServer.getItem(selIndex));
547
        }
548
    }
549

    
550
    private void updatePort() {
551
        txtPort.setText("");
552
        if(selectedCsii != null) {
553
            int port = selectedCsii.getPort();
554
            if(port == CdmServerInfo.NULL_PORT) {
555
                txtPort.setText(CdmServerInfo.NULL_PORT_STRING);
556
            } else {
557
                txtPort.setText(String.valueOf(port));
558
            }
559
        }
560
    }
561

    
562
    private int getPort() {
563
        int port = CdmServerInfo.NULL_PORT;
564
        try {
565
            port = Integer.valueOf(txtPort.getText());
566
        } catch (NumberFormatException nfe) {
567
            if(!CdmServerInfo.NULL_PORT_STRING.equals(txtPort.getText())) {
568
                setMessage(Messages.RemotingLoginDialog_MESSAGE_PORT_SHOULD_BE_INTEGER);
569
            }
570
        }
571
        return port;
572
    }
573

    
574
    private void checkSelectedCdmServer() {
575
        if(selectedCsii != null) {
576
            if(selectedCsii.isLocalhost()) {
577
                txtPort.setEditable(true);
578
                txtPort.setEnabled(true);
579
            }
580
            if(selectedCsii.pingServer()) {
581
                txtCdmServerStatus.setText(STATUS_AVAILABLE);
582
                populateCdmInstanceCombo(true);
583
                String serverVersionTimestamp = generateLastModifiedTooltip(selectedCsii.getCdmlibLastModified());
584
                txtServerVersion.setText(selectedCsii.getCdmlibServicesVersion() + ":" + serverVersionTimestamp); //$NON-NLS-1$
585
            } else {
586
                txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
587
                comboCdmInstance.removeAll();
588
                disableCdmInstanceControls("", "");
589
            }
590
        }
591
        updatePort();
592
    }
593

    
594

    
595
    private void populateCdmInstanceCombo(final boolean forceRefresh) {
596
        comboCdmInstance.removeAll();
597
        comboCdmInstance.setEnabled(false);
598
        btnConnect.setEnabled(false);
599
        txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
600
        txtCdmInstanceStatus.setToolTipText("");
601

    
602
        serverJob = new Job(Messages.RemotingLoginDialog_RETRIEVE_SERVER_INSTANCES) {
603
            @Override
604
            protected IStatus run(IProgressMonitor monitor) {
605
                try {
606
                    if(selectedCsii != null) {
607
                        if(forceRefresh) {
608
                            selectedCsii.refreshInstances();
609
                        }
610
                        final List<CdmInstanceInfo> instances = selectedCsii.getInstances();
611
                        Display.getDefault().asyncExec(new Runnable() {
612
                            @Override
613
                            public void run() {
614
                                if(!instances.isEmpty()) {
615
                                    for(CdmInstanceInfo cdmInstance : instances) {
616
                                        comboCdmInstance.add(cdmInstance.getName());
617
                                    }
618
                                    int instanceIndex = -1;
619
                                    if(instanceName != null) {
620
                                        instanceIndex = comboCdmInstance.indexOf(instanceName);
621
                                    }
622
                                    if(instanceIndex == -1) {
623
                                        comboCdmInstance.select(0);
624
                                        autoConnect = false;
625
                                    } else {
626
                                        comboCdmInstance.select(instanceIndex);
627
                                    }
628
                                    refreshCdmInstance();
629
                                    comboCdmInstance.setEnabled(true);
630
                                    if(autoConnect) {
631
                                        connect();
632
                                    }
633

    
634
                                } else {
635
                                    txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
636
                                    btnConnect.setEnabled(false);
637
                                }
638
                            }
639
                        });
640
                    }
641
                } catch (final CDMServerException e) {
642
                    MessagingUtils.warn(getClass(), e);
643
                    Display.getDefault().asyncExec(new Runnable() {
644
                        @Override
645
                        public void run() {
646
                            disableCdmInstanceControls(STATUS_NOT_AVAILABLE, e.getMessage());
647
                        }
648
                    });
649
                }
650
                return Status.OK_STATUS;
651
            }
652
        };
653

    
654
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE) && !isDevRemoteSource) {
655
            // Start the Job
656
            serverJob.schedule();
657
        }
658
    }
659

    
660
    private void disableCdmInstanceControls(String cdmInstanceStatus, String tooltip) {
661
        txtCdmInstanceStatus.setText(cdmInstanceStatus);
662
        txtCdmInstanceStatus.setToolTipText(tooltip);
663
        comboCdmInstance.setEnabled(false);
664
        btnConnect.setEnabled(false);
665

    
666
    }
667
    private void refreshCdmInstance() {
668
        txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
669
        clearOnInstanceChange();
670
        updateSelectedCdmInstance();
671
        checkSelectedCdmInstance();
672
        updateManagedServerControls();
673
    }
674

    
675
    private void updateSelectedCdmInstance() {
676
        int selIndex = comboCdmInstance.getSelectionIndex();
677
        if(selIndex != -1) {
678
            selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
679

    
680
            if(loadLoginPrefs && !isDevRemoteSource) {
681
                readPrefCredentials();
682
            }
683
        }
684
    }
685

    
686
    private void updateManagedServerControls() {
687
        if(selectedCsii.isLocalhostMgd()) {
688
            if(isSelectedCdmInstanceRunningInManagedServer()) {
689
                txtCdmInstanceStatus.setText(STATUS_STARTED);
690
            } else {
691
                txtCdmInstanceStatus.setText(STATUS_NOT_STARTED);
692
            }
693
            btnConnect.setEnabled(true);
694
            selectedCsii.setPort(getManagedServerPort());
695
            updatePort();
696
        }
697

    
698

    
699
        if(isManagedServerRunning()) {
700
            btnStopServer.setEnabled(true);
701
        } else {
702
            btnStopServer.setEnabled(false);
703
        }
704
    }
705

    
706
    private boolean isManagedServerRunning() {
707
        return CdmStore.getManagedServer() != null && CdmStore.getManagedServer().isAlive();
708
    }
709

    
710
    private boolean isSelectedCdmInstanceRunningInManagedServer() {
711
        return CdmStore.getManagedServer() != null &&
712
                CdmStore.getManagedServer().isAlive() &&
713
                selectedCsii.isLocalhostMgd() &&
714
                CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName());
715
    }
716

    
717
    private void startManagedServer() {
718
        if(isManagedServerRunning()) {
719
            if(CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName())) {
720
                return;
721
            } else {
722
                Display.getDefault().syncExec(new Runnable() {
723
                    @Override
724
                    public void run() {
725
                        stopManagedServer();
726
                    }
727
                });
728
            }
729
        }
730

    
731

    
732
        Job job = new Job(Messages.RemotingLoginDialog_JOB_SERVER_LAUNCH) {
733

    
734
            @Override
735
            public IStatus run(IProgressMonitor monitor) {
736
                String mgdServerConfigFileName = "mgd.datasources.xml"; //$NON-NLS-1$
737
                String config = CDMServerUtils.convertEditorToServerConfig();
738
                File managedServerConfigFile;
739
                int maxUnits = 50;
740
                monitor.beginTask(Messages.RemotingLoginDialog_TASK_LAUNCHING_SERVER, maxUnits);
741
                try {
742
                    monitor.subTask(String.format(Messages.RemotingLoginDialog_GENERATING_CONFIG_FILE, selectedCdmInstance.getName()));
743
                    managedServerConfigFile = CDMServerUtils.writeManagedServerConfig(config, mgdServerConfigFileName);
744
                    monitor.worked(1);
745
                    CdmStore.setManagedServer(new CDMServer(selectedCdmInstance.getName(), managedServerConfigFile));
746
                    monitor.subTask(Messages.RemotingLoginDialog_STARTING_MGD_SERVER);
747
                    CdmStore.getManagedServer().start(false, RemotingLoginDialog.this);
748
                    int serverUnits = 0;
749

    
750
                    // the following loop is a 'fake' progress monitoring where the progress
751
                    // bar is advanced by one unit every second until maxUnits -2
752
                    while(!CdmStore.getManagedServer().isStarted() && !CdmStore.getManagedServer().isFailed()) {
753
                        if(serverUnits < maxUnits - 2) {
754
                            try {
755
                                Thread.sleep(1000);
756
                            } catch (InterruptedException e) {
757
                            }
758
                            monitor.worked(1);
759
                            serverUnits++;
760
                        }
761
                    }
762
                    Display.getDefault().asyncExec(new Runnable() {
763
                        @Override
764
                        public void run() {
765
                                hide(false);
766
                                updateManagedServerControls();
767
                                connect();
768
                            }
769
                    });
770
                } catch (IOException ioe) {
771
                    MessagingUtils.errorDialog(Messages.RemotingLoginDialog_ERROR_GENERATING_CONFIG_FILE,
772
                            this,
773
                            ioe.getMessage(),
774
                            TaxeditorStorePlugin.PLUGIN_ID,
775
                            ioe,
776
                            true);
777
                } catch (CDMEmbeddedServerException cse) {
778
                    MessagingUtils.errorDialog(Messages.RemotingLoginDialog_ERROR_STARTING_SERVER,
779
                            this,
780
                            cse.getMessage(),
781
                            TaxeditorStorePlugin.PLUGIN_ID,
782
                            cse,
783
                            true);
784
                } finally {
785
                    monitor.done();
786
                }
787
                return Status.OK_STATUS;
788
            }
789
        };
790

    
791
        // configure the job
792
        job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
793
        job.setUser(true);
794
        // schedule job
795
        hide(true);
796
        job.schedule();
797
    }
798

    
799
    private void stopManagedServer() {
800
        try {
801
            CdmStore.getManagedServer().stop();
802
        } catch (Exception e) {
803
            MessagingUtils.errorDialog(Messages.RemotingLoginDialog_ERROR_STOPPING_SERVER,
804
                    this,
805
                    String.format(Messages.RemotingLoginDialog_COULD_NOT_STOP_SERVER, CdmStore.getManagedServer().getPort()),
806
                    TaxeditorStorePlugin.PLUGIN_ID,
807
                    e,
808
                    true);
809
        }
810
        CdmStore.setManagedServer(null);
811
        updateManagedServerControls();
812
    }
813

    
814
    private int getManagedServerPort() {
815
        return CdmStore.getManagedServer() == null ? CdmServerInfo.NULL_PORT : CdmStore.getManagedServer().getPort();
816
    }
817

    
818
    private void checkSelectedCdmInstance() {
819
        boolean available = false;
820
        String status = STATUS_NOT_AVAILABLE;
821
        String message = null;
822

    
823
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
824
            try {
825
                if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
826
                    status = STATUS_AVAILABLE;
827
                    available = true;
828
                } else {
829
                    status = STATUS_NOT_AVAILABLE;
830
                    available = false;
831
                }
832

    
833
                if(available) {
834
                    txtServerCDMVersion.setText(selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort()).getDbSchemaVersion());
835
                    int compareDbSchemaVersion = selectedCsii.compareDbSchemaVersion(selectedCdmInstance, getPort());
836
                    int compareCdmlibServicesVersion = 0;
837
                    boolean disableServicesApiTimestampCheck =
838
                            PreferencesUtil.getPreferenceStore().getBoolean((IPreferenceKeys.DISABLE_SERVICES_API_TIMESTAMP_CHECK));
839
                    if(!disableServicesApiTimestampCheck) {
840
                        compareCdmlibServicesVersion = selectedCsii.compareCdmlibServicesVersion();
841
                    }
842
                    if(compareDbSchemaVersion > 0 || compareCdmlibServicesVersion > 0) {
843
                        status =  STATUS_NOT_COMPATIBLE;
844
                        available = false;
845
                        message = MESG_COMPATIBLE_EDITOR_OLD;
846
                    } else if(compareDbSchemaVersion < 0 || compareCdmlibServicesVersion < 0) {
847
                        status = STATUS_NOT_COMPATIBLE;
848
                        available = false;
849
                        message = MESG_COMPATIBLE_SERVER_OLD;
850
                    } else {
851
                        status =  STATUS_AVAILABLE;
852
                        available = true;
853
                        message = ""; //$NON-NLS-1$
854
                    }
855
                }
856
            } catch (Exception e) {
857
                txtCdmInstanceStatus.setToolTipText(e.getMessage());
858
            } finally {
859
                btnConnect.setEnabled(available);
860
                txtCdmInstanceStatus.setText(status);
861
                if(!StringUtils.isBlank(message)) {
862
                    setMessage(message);
863
                }
864
            }
865
        }
866
    }
867

    
868
    private void connect() {
869
        checkSelectedCdmInstance();
870

    
871
        if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
872
            return;
873
        }
874

    
875
        ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
876

    
877
        if(!validateLogin(source)) {
878
            return;
879
        }
880

    
881
        try {
882
            CdmStore.connect(source, this);
883
        } catch (Exception e) {
884
            // Do not expect anything to go wrong at this point, so we throw a runtime exception
885
            // if any problems
886
            throw new RuntimeException(e);
887
        }
888

    
889
    }
890

    
891

    
892
    public boolean isRememberMe() {
893
        return btnRememberMe.getSelection();
894
    }
895

    
896
    private void persistPrefLastServerInstance() {
897
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
898
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
899

    
900
        lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
901
        lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
902

    
903
        flushPreferences(lastServerInstancePrefs);
904
    }
905

    
906
    private void persistPrefCredentials() {
907
         IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
908
         Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
909
         credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
910
         credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
911
         flushPreferences(credentialsPrefs);
912
    }
913

    
914
    private void flushPreferences(Preferences prefs) {
915
        try {
916
            prefs.flush();
917
        } catch (BackingStoreException bse) {
918
            setMessage(bse.getMessage());
919
        }
920
    }
921

    
922
    private void readPrefCredentials() {
923
        String username, password;
924
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
925
        Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
926
        username = credentialsPrefs.get(getUsernamePrefKey(), ""); //$NON-NLS-1$
927
        txtLogin.setText(username);
928
        password = credentialsPrefs.get(getPasswordPrefKey(),""); //$NON-NLS-1$
929
        txtPassword.setText(password);
930
        if(username.isEmpty() || password.isEmpty()) {
931
            autoConnect = false;
932
        }
933
    }
934

    
935
    private void readPrefLastServerInstance() {
936
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
937
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
938

    
939
        serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
940
        instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
941
    }
942

    
943
    private void emptyCredentials() {
944
        txtLogin.setText("");
945
        txtPassword.setText("");
946
    }
947

    
948
    private String getUsernamePrefKey() {
949
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
950
    }
951

    
952
    private String getPasswordPrefKey() {
953
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
954
    }
955

    
956
    private boolean validateLogin(ICdmRemoteSource remoteSource) {
957
        if(getUsername() == null || getUsername().isEmpty()) {
958
            setMessage(Messages.RemotingLoginDialog_LOGIN_CANNOT_BE_EMPTY);
959
            return false;
960
        }
961
        if(getPassword() == null || getPassword().isEmpty()) {
962
            setMessage(Messages.RemotingLoginDialog_PASSWORD_CANNOT_BE_EMPTY);
963
            return false;
964
        }
965

    
966

    
967
        try {
968
            IUserService userService = CdmApplicationRemoteConfiguration.getUserService(remoteSource);
969
            UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(getUsername(), getPassword());
970
            CdmApplicationRemoteConfiguration.getAuthenticationManager(userService).authenticate(token);
971
        } catch(BadCredentialsException e){
972
            setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
973
            return false;
974
        } catch(LockedException e){
975
            setMessage(LoginManager.ACCOUNT_LOCKED_MESSAGE);
976
            return false;
977
        } catch(IllegalArgumentException e){
978
            setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
979
            return false;
980
        }
981
        return true;
982
    }
983

    
984
    public String getUsername() {
985
        return txtLogin.getText();
986
    }
987

    
988
    public String getPassword() {
989
        return txtPassword.getText();
990
    }
991

    
992
    public void setMessage(String message) {
993
        if(message != null && !message.isEmpty()) {
994
            if(message.length() > 60) {
995
                styledTxtMessage.setToolTipText(message);
996
                message = message.substring(0, 60) + "..."; //$NON-NLS-1$
997
            }
998
            styledTxtMessage.setText(message);
999
            styledTxtMessage.setVisible(true);
1000
            ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
1001
            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
1002
            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
1003
        } else {
1004
            styledTxtMessage.setText(""); //$NON-NLS-1$
1005
            styledTxtMessage.setVisible(false);
1006
            ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
1007
            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage());
1008
            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage());
1009
        }
1010
        remotingComposite.layout();
1011
    }
1012

    
1013
    private int getHeightWithoutMessage() {
1014
        if(xpndblcmpstAdvanced.isExpanded()) {
1015
            return MIN_EXP_HEIGHT;
1016
        } else {
1017
            return MIN_HEIGHT;
1018
        }
1019
    }
1020

    
1021

    
1022
    public void hide(boolean isHidden) {
1023
        if(shlConnect != null && shlConnect.getDisplay() != null) {
1024
            shlConnect.setVisible(!isHidden);
1025
        }
1026
    }
1027
    public void dispose() {
1028
        if(shlConnect != null && shlConnect.getDisplay() != null) {
1029
            shlConnect.dispose();
1030
        }
1031
    }
1032

    
1033
    public void onComplete() {
1034
        Display.getDefault().asyncExec(new Runnable() {
1035
            @Override
1036
            public void run() {
1037
                if(!isDevRemoteSource) {
1038
                    if(isRememberMe()) {
1039
                        persistPrefCredentials();
1040
                    }
1041
                    persistPrefLastServerInstance();
1042
                }
1043
                dispose();
1044
            }
1045
        });
1046
    }
1047

    
1048
    private String generateLastModifiedTooltip(String cdmlibLastModified) {
1049
        if(StringUtils.isBlank(cdmlibLastModified)) {
1050
            return ""; //$NON-NLS-1$
1051
        }
1052
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.z"); //$NON-NLS-1$
1053
        Date cdmlibLastModifiedDate;
1054
        String cdmlibLastModifiedTimestamp = ""; //$NON-NLS-1$
1055

    
1056
        cdmlibLastModifiedDate = new Date(Long.valueOf(cdmlibLastModified));
1057
        cdmlibLastModifiedTimestamp = sdf.format(cdmlibLastModifiedDate);
1058

    
1059
        return cdmlibLastModifiedTimestamp;
1060
    }
1061

    
1062
    private void setEditorInfo() {
1063
        txtEditorCDMVersion.setText(CdmMetaData.getDbSchemaVersion());
1064
        String editorVersionTimestamp = generateLastModifiedTooltip(CdmApplicationState.getCdmlibLastModified());
1065
        txtEditorVersion.setText(CdmApplicationState.getCdmlibVersion() + ":" + editorVersionTimestamp); //$NON-NLS-1$
1066
    }
1067

    
1068
    private void clearOnServerChange() {
1069
        setMessage(""); //$NON-NLS-1$
1070
        txtServerCDMVersion.setText(""); //$NON-NLS-1$
1071
        txtServerVersion.setText(""); //$NON-NLS-1$
1072
        txtServerVersion.setToolTipText(""); //$NON-NLS-1$
1073
        txtServerCDMVersion.setText(""); //$NON-NLS-1$
1074
        comboCdmInstance.removeAll();
1075
        txtCdmInstanceStatus.setText(""); //$NON-NLS-1$
1076
        txtPort.setEditable(false);
1077
        txtPort.setEnabled(false);
1078
    }
1079

    
1080
    private void clearOnInstanceChange() {
1081
        setMessage(""); //$NON-NLS-1$
1082
        txtServerCDMVersion.setText(""); //$NON-NLS-1$
1083
    }
1084

    
1085
    /**
1086
     * {@inheritDoc}
1087
     */
1088
    @Override
1089
    public void handleError(final Throwable t) {
1090

    
1091
        Display.getDefault().syncExec(new Runnable() {
1092
            @Override
1093
            public void run() {
1094
                serverJob.cancel();
1095

    
1096
                String title = Messages.RemotingLoginDialog_SERVER_LAUNCH_ERROR;
1097
                String  message = t.getMessage();
1098

    
1099

    
1100
                MessagingUtils.errorDialog(title,
1101
                        this,
1102
                        message,
1103
                        TaxeditorStorePlugin.PLUGIN_ID,
1104
                        t,
1105
                        true);
1106
            }
1107
        });
1108
    }
1109

    
1110

    
1111
}
(5-5/7)