Project

General

Profile

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

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

    
19
import org.apache.commons.lang.StringUtils;
20
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.Status;
23
import org.eclipse.core.runtime.jobs.Job;
24
import org.eclipse.core.runtime.preferences.ConfigurationScope;
25
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWTException;
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.l10n.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
			if (CdmStore.isActive()){
483
			    PreferencesUtil.setNomenclaturalCodePreferences();
484
			}
485

    
486
		}
487
	}
488

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

    
531

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

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

    
558
    private void updatePort() {
559
        txtPort.setText("");
560
        if(selectedCsii != null) {
561
            int port = selectedCsii.getPort();
562
            if(port == CdmServerInfo.NULL_PORT) {
563
                txtPort.setText(CdmServerInfo.NULL_PORT_STRING);
564
            } else {
565
                txtPort.setText(String.valueOf(port));
566
            }
567
        }
568
    }
569

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

    
582
    private void checkSelectedCdmServer() {
583
        if(selectedCsii != null) {
584
            if(selectedCsii.isLocalhost()) {
585
                txtPort.setEditable(true);
586
                txtPort.setEnabled(true);
587
            }
588
            if(selectedCsii.pingServer()) {
589
                txtCdmServerStatus.setText(STATUS_AVAILABLE);
590
                populateCdmInstanceCombo(true);
591
                String serverVersionTimestamp = generateLastModifiedTooltip(selectedCsii.getCdmlibLastModified());
592
                txtServerVersion.setText(selectedCsii.getCdmlibServicesVersion() + ":" + serverVersionTimestamp); //$NON-NLS-1$
593
            } else {
594
                txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
595
                comboCdmInstance.removeAll();
596
                disableCdmInstanceControls("", "");
597
            }
598
        }
599
        updatePort();
600
    }
601

    
602

    
603
    private void populateCdmInstanceCombo(final boolean forceRefresh) {
604
        comboCdmInstance.removeAll();
605
        comboCdmInstance.setEnabled(false);
606
        btnConnect.setEnabled(false);
607
        txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
608
        txtCdmInstanceStatus.setToolTipText("");
609

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

    
642
                                } else {
643
                                    txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
644
                                    btnConnect.setEnabled(false);
645
                                }
646
                            }
647
                        });
648
                    }
649
                } catch (final CDMServerException e) {
650
                    MessagingUtils.warn(getClass(), e);
651
                    Display.getDefault().asyncExec(new Runnable() {
652
                        @Override
653
                        public void run() {
654
                            disableCdmInstanceControls(STATUS_NOT_AVAILABLE, e.getMessage());
655
                        }
656
                    });
657
                }
658
                return Status.OK_STATUS;
659
            }
660
        };
661

    
662
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE) && !isDevRemoteSource) {
663
            // Start the Job
664
            serverJob.schedule();
665
        }
666
    }
667

    
668
    private void disableCdmInstanceControls(String cdmInstanceStatus, String tooltip) {
669
        txtCdmInstanceStatus.setText(cdmInstanceStatus);
670
        txtCdmInstanceStatus.setToolTipText(tooltip);
671
        comboCdmInstance.setEnabled(false);
672
        btnConnect.setEnabled(false);
673

    
674
    }
675
    private void refreshCdmInstance() {
676
        txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
677
        clearOnInstanceChange();
678
        updateSelectedCdmInstance();
679
        checkSelectedCdmInstance();
680
        updateManagedServerControls();
681
    }
682

    
683
    private void updateSelectedCdmInstance() {
684
        int selIndex = comboCdmInstance.getSelectionIndex();
685
        if(selIndex != -1) {
686
            selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
687

    
688
            if(loadLoginPrefs && !isDevRemoteSource) {
689
                readPrefCredentials();
690
            }
691
        }
692
    }
693

    
694
    private void updateManagedServerControls() {
695
        if(selectedCsii.isLocalhostMgd()) {
696
            if(isSelectedCdmInstanceRunningInManagedServer()) {
697
                txtCdmInstanceStatus.setText(STATUS_STARTED);
698
            } else {
699
                txtCdmInstanceStatus.setText(STATUS_NOT_STARTED);
700
            }
701
            btnConnect.setEnabled(true);
702
            selectedCsii.setPort(getManagedServerPort());
703
            updatePort();
704
        }
705

    
706

    
707
        if(isManagedServerRunning()) {
708
            btnStopServer.setEnabled(true);
709
        } else {
710
            btnStopServer.setEnabled(false);
711
        }
712
    }
713

    
714
    private boolean isManagedServerRunning() {
715
        return CdmStore.getManagedServer() != null && CdmStore.getManagedServer().isAlive();
716
    }
717

    
718
    private boolean isSelectedCdmInstanceRunningInManagedServer() {
719
        return CdmStore.getManagedServer() != null &&
720
                CdmStore.getManagedServer().isAlive() &&
721
                selectedCsii.isLocalhostMgd() &&
722
                CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName());
723
    }
724

    
725
    private void startManagedServer() {
726
        if(isManagedServerRunning()) {
727
            if(CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName())) {
728
                return;
729
            } else {
730
                Display.getDefault().syncExec(new Runnable() {
731
                    @Override
732
                    public void run() {
733
                        stopManagedServer();
734
                    }
735
                });
736
            }
737
        }
738

    
739

    
740
        Job job = new Job(Messages.RemotingLoginDialog_JOB_SERVER_LAUNCH) {
741

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

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

    
799
        // configure the job
800
        job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
801
        job.setUser(true);
802
        // schedule job
803
        hide(true);
804
        job.schedule();
805
    }
806

    
807
    private void stopManagedServer() {
808
        try {
809
            CdmStore.getManagedServer().stop();
810
        } catch (Exception e) {
811
            MessagingUtils.errorDialog(Messages.RemotingLoginDialog_ERROR_STOPPING_SERVER,
812
                    this,
813
                    String.format(Messages.RemotingLoginDialog_COULD_NOT_STOP_SERVER, CdmStore.getManagedServer().getPort()),
814
                    TaxeditorStorePlugin.PLUGIN_ID,
815
                    e,
816
                    true);
817
        }
818
        CdmStore.setManagedServer(null);
819
        updateManagedServerControls();
820
    }
821

    
822
    private int getManagedServerPort() {
823
        return CdmStore.getManagedServer() == null ? CdmServerInfo.NULL_PORT : CdmStore.getManagedServer().getPort();
824
    }
825

    
826
    private void checkSelectedCdmInstance() {
827
        boolean available = false;
828
        String status = STATUS_NOT_AVAILABLE;
829
        String message = null;
830

    
831
        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
832
            try {
833
                if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
834
                    status = STATUS_AVAILABLE;
835
                    available = true;
836
                } else {
837
                    status = STATUS_NOT_AVAILABLE;
838
                    available = false;
839
                }
840

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

    
876
    private void connect() {
877
        checkSelectedCdmInstance();
878

    
879
        if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
880
            return;
881
        }
882

    
883
        ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
884

    
885
        if(!validateLogin(source)) {
886
            return;
887
        }
888

    
889
        try {
890
            CdmStore.connect(source, this);
891
        } catch (Exception e) {
892
            // Do not expect anything to go wrong at this point, so we throw a runtime exception
893
            // if any problems
894
            throw new RuntimeException(e);
895
        }
896

    
897
    }
898

    
899

    
900
    public boolean isRememberMe() {
901
        return btnRememberMe.getSelection();
902
    }
903

    
904
    private void persistPrefLastServerInstance() {
905
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
906
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
907

    
908
        lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
909
        lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
910

    
911
        flushPreferences(lastServerInstancePrefs);
912
    }
913

    
914
    private void persistPrefCredentials() {
915
         IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
916
         Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
917
         credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
918
         credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
919
         flushPreferences(credentialsPrefs);
920
    }
921

    
922
    private void flushPreferences(Preferences prefs) {
923
        try {
924
            prefs.flush();
925
        } catch (BackingStoreException bse) {
926
            setMessage(bse.getMessage());
927
        }
928
    }
929

    
930
    private void readPrefCredentials() {
931
        String username, password;
932
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
933
        Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
934
        username = credentialsPrefs.get(getUsernamePrefKey(), ""); //$NON-NLS-1$
935
        txtLogin.setText(username);
936
        password = credentialsPrefs.get(getPasswordPrefKey(),""); //$NON-NLS-1$
937
        txtPassword.setText(password);
938
        if(username.isEmpty() || password.isEmpty()) {
939
            autoConnect = false;
940
        }
941
    }
942

    
943
    private void readPrefLastServerInstance() {
944
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
945
        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
946

    
947
        serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
948
        instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
949
    }
950

    
951
    private void emptyCredentials() {
952
        txtLogin.setText("");
953
        txtPassword.setText("");
954
    }
955

    
956
    private String getUsernamePrefKey() {
957
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
958
    }
959

    
960
    private String getPasswordPrefKey() {
961
        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
962
    }
963

    
964
    private boolean validateLogin(ICdmRemoteSource remoteSource) {
965
        if(getUsername() == null || getUsername().isEmpty()) {
966
            setMessage(Messages.RemotingLoginDialog_LOGIN_CANNOT_BE_EMPTY);
967
            return false;
968
        }
969
        if(getPassword() == null || getPassword().isEmpty()) {
970
            setMessage(Messages.RemotingLoginDialog_PASSWORD_CANNOT_BE_EMPTY);
971
            return false;
972
        }
973

    
974

    
975
        try {
976
            IUserService userService = CdmApplicationRemoteConfiguration.getUserService(remoteSource);
977
            UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(getUsername(), getPassword());
978
            CdmApplicationRemoteConfiguration.getAuthenticationManager(userService).authenticate(token);
979
        } catch(BadCredentialsException e){
980
            setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
981
            return false;
982
        } catch(LockedException e){
983
            setMessage(LoginManager.ACCOUNT_LOCKED_MESSAGE);
984
            return false;
985
        } catch(IllegalArgumentException e){
986
            setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
987
            return false;
988
        }
989
        return true;
990
    }
991

    
992
    public String getUsername() {
993
        return txtLogin.getText();
994
    }
995

    
996
    public String getPassword() {
997
        return txtPassword.getText();
998
    }
999

    
1000
    public void setMessage(String message) {
1001
        if(message != null && !message.isEmpty()) {
1002
            if(message.length() > 60) {
1003
                styledTxtMessage.setToolTipText(message);
1004
                message = message.substring(0, 60) + "..."; //$NON-NLS-1$
1005
            }
1006
            styledTxtMessage.setText(message);
1007
            styledTxtMessage.setVisible(true);
1008
            ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
1009
            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
1010
            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
1011
        } else {
1012
            styledTxtMessage.setText(""); //$NON-NLS-1$
1013
            styledTxtMessage.setVisible(false);
1014
            ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
1015
            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage());
1016
            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage());
1017
        }
1018
        remotingComposite.layout();
1019
    }
1020

    
1021
    private int getHeightWithoutMessage() {
1022
        if(xpndblcmpstAdvanced.isExpanded()) {
1023
            return MIN_EXP_HEIGHT;
1024
        } else {
1025
            return MIN_HEIGHT;
1026
        }
1027
    }
1028

    
1029

    
1030
    public void hide(boolean isHidden) {
1031
        if(shlConnect != null && shlConnect.getDisplay() != null) {
1032
            shlConnect.setVisible(!isHidden);
1033
        }
1034
    }
1035
    public void dispose() {
1036
        if(shlConnect != null && shlConnect.getDisplay() != null) {
1037
            shlConnect.dispose();
1038
        }
1039
    }
1040

    
1041
    public void onComplete() {
1042
        Display.getDefault().asyncExec(new Runnable() {
1043
            @Override
1044
            public void run() {
1045
                if(!isDevRemoteSource) {
1046
                    if(isRememberMe()) {
1047
                        persistPrefCredentials();
1048
                    }
1049
                    persistPrefLastServerInstance();
1050
                }
1051
                dispose();
1052
            }
1053
        });
1054
    }
1055

    
1056
    private String generateLastModifiedTooltip(String cdmlibLastModified) {
1057
        if(StringUtils.isBlank(cdmlibLastModified)) {
1058
            return ""; //$NON-NLS-1$
1059
        }
1060
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.z"); //$NON-NLS-1$
1061
        Date cdmlibLastModifiedDate;
1062
        String cdmlibLastModifiedTimestamp = ""; //$NON-NLS-1$
1063

    
1064
        cdmlibLastModifiedDate = new Date(Long.valueOf(cdmlibLastModified));
1065
        cdmlibLastModifiedTimestamp = sdf.format(cdmlibLastModifiedDate);
1066

    
1067
        return cdmlibLastModifiedTimestamp;
1068
    }
1069

    
1070
    private void setEditorInfo() {
1071
        txtEditorCDMVersion.setText(CdmMetaData.getDbSchemaVersion());
1072
        String editorVersionTimestamp = generateLastModifiedTooltip(CdmApplicationState.getCdmlibLastModified());
1073
        txtEditorVersion.setText(CdmApplicationState.getCdmlibVersion() + ":" + editorVersionTimestamp); //$NON-NLS-1$
1074
    }
1075

    
1076
    private void clearOnServerChange() {
1077
        setMessage(""); //$NON-NLS-1$
1078
        txtServerCDMVersion.setText(""); //$NON-NLS-1$
1079
        txtServerVersion.setText(""); //$NON-NLS-1$
1080
        txtServerVersion.setToolTipText(""); //$NON-NLS-1$
1081
        txtServerCDMVersion.setText(""); //$NON-NLS-1$
1082
        comboCdmInstance.removeAll();
1083
        txtCdmInstanceStatus.setText(""); //$NON-NLS-1$
1084
        txtPort.setEditable(false);
1085
        txtPort.setEnabled(false);
1086
    }
1087

    
1088
    private void clearOnInstanceChange() {
1089
        setMessage(""); //$NON-NLS-1$
1090
        txtServerCDMVersion.setText(""); //$NON-NLS-1$
1091
    }
1092

    
1093
    /**
1094
     * {@inheritDoc}
1095
     */
1096
    @Override
1097
    public void handleError(final Throwable t) {
1098

    
1099
        Display.getDefault().syncExec(new Runnable() {
1100
            @Override
1101
            public void run() {
1102
                serverJob.cancel();
1103

    
1104
                String title = Messages.RemotingLoginDialog_SERVER_LAUNCH_ERROR;
1105
                String  message = t.getMessage();
1106

    
1107

    
1108
                MessagingUtils.errorDialog(title,
1109
                        this,
1110
                        message,
1111
                        TaxeditorStorePlugin.PLUGIN_ID,
1112
                        t,
1113
                        true);
1114
            }
1115
        });
1116
    }
1117

    
1118

    
1119
}
(5-5/7)