0a6b84b3227e8a074ab213156fa04e37ea7e2579
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / RemotingLoginDialog.java
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.text.SimpleDateFormat;
13 import java.util.Date;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.core.runtime.preferences.ConfigurationScope;
24 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.custom.StyledText;
27 import org.eclipse.swt.events.MouseAdapter;
28 import org.eclipse.swt.events.MouseEvent;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.swt.layout.FillLayout;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Combo;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Dialog;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.swt.widgets.Text;
43 import org.eclipse.ui.forms.events.ExpansionEvent;
44 import org.eclipse.ui.forms.events.IExpansionListener;
45 import org.eclipse.ui.forms.widgets.ExpandableComposite;
46 import org.eclipse.wb.swt.SWTResourceManager;
47 import org.osgi.service.prefs.BackingStoreException;
48 import org.osgi.service.prefs.Preferences;
49
50 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
51 import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
52 import eu.etaxonomy.taxeditor.model.MessagingUtils;
53 import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
54 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
55 import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
56 import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo;
57 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
58 import eu.etaxonomy.taxeditor.store.CdmStore;
59
60 /**
61 * @author cmathew
62 * @date 20 Jan 2015
63 *
64 */
65 public class RemotingLoginDialog extends Dialog {
66
67 protected Object result;
68 protected Shell shlConnect;
69 private Text txtCdmServerStatus;
70 private Text txtCdmInstanceStatus;
71 private Combo comboCdmServer;
72 private Combo comboCdmInstance;
73 private Button btnConnect;
74
75 private final Map<String, CdmServerInfo> csiiMap = new HashMap<String, CdmServerInfo>();
76
77 private final static String STATUS_AVAILABLE = "Available";
78 private final static String STATUS_NOT_AVAILABLE = "Not Available";
79 private final static String STATUS_RETRIEVING = "Retrieving ...";
80 private final static String STATUS_CHECKING_AVAILABILITY = "Checking ...";
81 private final static String STATUS_NO_INSTANCES = "No Instances Found";
82 private final static String STATUS_ERROR = "Error";
83 private final static String STATUS_REMOTING_NOT_ACTIVATED = "Remoting not activated";
84 private final static String STATUS_NOT_COMPATIBLE = "Not Compatible";
85
86 private final static String MESG_COMPATIBLE_EDITOR_OLD = "Please update the Taxonomic Editor (Help->Check for Updates) or choose a compatible cdm-server";
87 private final static String MESG_COMPATIBLE_SERVER_OLD = "Please update the chosen cdm-server or choose a compatible cdm-server";
88
89 private final static String STORE_PREFERENCES_NODE = "eu.etaxonomy.taxeditor.store";
90
91 private final static String LOGIN_NODE = "login";
92 private final static String USERNAME_SUFFIX = "_username";
93 private final static String PASSWORD_SUFFIX = "_password";
94
95 private final static String LAST_SERVER_INSTANCE_NODE = "lastServerInstance";
96 private final static String LAST_SERVER_KEY = "lastServerKey";
97 private final static String LAST_INSTANCE_KEY = "lastInstanceKey";
98
99
100 private Composite remotingComposite;
101 private CdmServerInfo selectedCsii;
102 private CdmInstanceInfo selectedCdmInstance;
103 private Button btnCdmServerRefresh;
104 private Composite loginComposite;
105 private Label lblLogin;
106 private Text txtLogin;
107 private Label lblPassword;
108 private Text txtPassword;
109 private Button btnRememberMe;
110 private Composite compAdvanced;
111 private Label lblPort;
112 private Text txtPort;
113 private Label lblServerVersion;
114 private Text txtServerVersion;
115 private ExpandableComposite xpndblcmpstAdvanced;
116 private StyledText styledTxtMessage;
117
118
119 private final int MIN_WIDTH = 530;
120 private final int MIN_HEIGHT = 220;
121 private final int MIN_EXP_HEIGHT = 380;
122 private final int MESSAGE_HEIGHT = 25;
123 private Label lblEditorVersion;
124 private Text txtEditorVersion;
125 private Label lblServerCDMVersion;
126 private Text txtServerCDMVersion;
127 private Label lblEditorCDMVersion;
128 private Text txtEditorCDMVersion;
129
130 private String serverName, instanceName;
131 private boolean autoConnect = false;
132 private boolean loadLoginPrefs = true;
133 private boolean isDevRemoteSource = false;
134
135 /**
136 * Create the dialog.
137 * @param parent
138 * @param style
139 */
140 public RemotingLoginDialog(Shell parent, int style) {
141 super(parent, style);
142 setText("Login");
143 }
144
145 public Object open(CdmRemoteSource source, boolean loadLoginPrefs, boolean autoConnect) {
146 this.loadLoginPrefs = loadLoginPrefs;
147 this.serverName = source.getName();
148 String contextPath = source.getContextPath();
149 this.instanceName = contextPath.substring(contextPath.lastIndexOf("/") + 1);
150 return open(serverName, instanceName, loadLoginPrefs, autoConnect);
151 }
152
153
154 public Object open(String serverName, String instanceName, boolean loadLoginPrefs, boolean autoConnect) {
155 this.serverName = serverName;
156 this.instanceName = instanceName;
157 this.loadLoginPrefs = loadLoginPrefs;
158 this.autoConnect = autoConnect;
159 return open();
160 }
161
162 /**
163 * Open the dialog.
164 * @return the result
165 */
166 public Object open() {
167 // ICdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
168 // if(devRemoteSource != null) {
169 // connect(devRemoteSource);
170 // } else {
171 createContents();
172 if(serverName == null && instanceName == null) {
173 readPrefLastServerInstance();
174 }
175
176 setEditorInfo();
177 populateCdmServerCombo();
178 shlConnect.open();
179 shlConnect.layout();
180
181 xpndblcmpstAdvanced.setExpanded(false);
182
183 Display display = getParent().getDisplay();
184
185 while (!shlConnect.isDisposed()) {
186 if (!display.readAndDispatch()) {
187 display.sleep();
188 }
189 }
190 //}
191 return result;
192 }
193
194 /**
195 * Create contents of the dialog.
196 */
197 private void createContents() {
198 shlConnect = new Shell(getParent(), SWT.DIALOG_TRIM);
199 shlConnect.setMinimumSize(new Point(MIN_WIDTH, MIN_HEIGHT));
200 shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
201 shlConnect.setText("Connect");
202 shlConnect.setLayout(new FillLayout(SWT.HORIZONTAL));
203
204 remotingComposite = new Composite(shlConnect, SWT.NONE);
205 remotingComposite.setLayout(new GridLayout(1, false));
206
207 Composite cdmServerComposite = new Composite(remotingComposite, SWT.NONE);
208 GridData gd_cdmServerComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
209 gd_cdmServerComposite.heightHint = 68;
210 cdmServerComposite.setLayoutData(gd_cdmServerComposite);
211 cdmServerComposite.setLayout(new GridLayout(4, false));
212
213 Label lblCdmServer = new Label(cdmServerComposite, SWT.NONE);
214 lblCdmServer.setText("CDM Server : ");
215 lblCdmServer.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
216 lblCdmServer.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
217
218 comboCdmServer = new Combo(cdmServerComposite, SWT.READ_ONLY);
219 comboCdmServer.addSelectionListener(new SelectionAdapter() {
220 @Override
221 public void widgetSelected(SelectionEvent e) {
222 refreshCdmServer();
223 updatePort();
224 }
225 });
226 GridData gd_comboCdmServer = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
227 gd_comboCdmServer.widthHint = 150;
228 comboCdmServer.setLayoutData(gd_comboCdmServer);
229 comboCdmServer.select(0);
230
231 txtCdmServerStatus = new Text(cdmServerComposite, SWT.BORDER);
232 txtCdmServerStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
233 txtCdmServerStatus.setEditable(false);
234 GridData gd_txtCdmServerStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
235 gd_txtCdmServerStatus.widthHint = 100;
236 txtCdmServerStatus.setLayoutData(gd_txtCdmServerStatus);
237
238 btnCdmServerRefresh = new Button(cdmServerComposite, SWT.NONE);
239 btnCdmServerRefresh.addSelectionListener(new SelectionAdapter() {
240 @Override
241 public void widgetSelected(SelectionEvent e) {
242 refreshCdmServer();
243 }
244 });
245 btnCdmServerRefresh.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
246 btnCdmServerRefresh.setText("Refresh");
247
248 Label lblCdmInstance = new Label(cdmServerComposite, SWT.NONE);
249 GridData gd_lblCdmInstance = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
250 gd_lblCdmInstance.heightHint = 30;
251 lblCdmInstance.setLayoutData(gd_lblCdmInstance);
252 lblCdmInstance.setText("CDM Instance : ");
253 lblCdmInstance.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
254
255 comboCdmInstance = new Combo(cdmServerComposite, SWT.READ_ONLY);
256 comboCdmInstance.addSelectionListener(new SelectionAdapter() {
257 @Override
258 public void widgetSelected(SelectionEvent e) {
259 updateSelectedCdmInstance();
260 checkSelectedCdmInstance();
261 }
262 });
263 GridData gd_comboCdmInstance = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
264 gd_comboCdmInstance.widthHint = 150;
265 comboCdmInstance.setLayoutData(gd_comboCdmInstance);
266 comboCdmInstance.select(0);
267
268 txtCdmInstanceStatus = new Text(cdmServerComposite, SWT.BORDER);
269 txtCdmInstanceStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
270 txtCdmInstanceStatus.setEditable(false);
271 GridData gd_txtCdmInstanceStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
272 gd_txtCdmInstanceStatus.widthHint = 100;
273 txtCdmInstanceStatus.setLayoutData(gd_txtCdmInstanceStatus);
274
275 Button btnCdmInstanceRefresh = new Button(cdmServerComposite, SWT.FLAT);
276 btnCdmInstanceRefresh.addSelectionListener(new SelectionAdapter() {
277 @Override
278 public void widgetSelected(SelectionEvent e) {
279 refreshCdmInstance();
280 }
281 });
282 GridData gd_btnCdmInstanceRefresh = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
283 gd_btnCdmInstanceRefresh.widthHint = 110;
284 gd_btnCdmInstanceRefresh.heightHint = 30;
285 btnCdmInstanceRefresh.setLayoutData(gd_btnCdmInstanceRefresh);
286 btnCdmInstanceRefresh.setText("Refresh");
287
288 loginComposite = new Composite(remotingComposite, SWT.NONE);
289 GridData gd_loginComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
290 gd_loginComposite.widthHint = 487;
291 gd_loginComposite.heightHint = 70;
292 loginComposite.setLayoutData(gd_loginComposite);
293 GridLayout gl_loginComposite = new GridLayout(6, false);
294 gl_loginComposite.marginTop = 5;
295 loginComposite.setLayout(gl_loginComposite);
296
297 lblLogin = new Label(loginComposite, SWT.CENTER);
298 GridData gd_lblLogin = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
299 gd_lblLogin.widthHint = 50;
300 lblLogin.setLayoutData(gd_lblLogin);
301 lblLogin.setText("Login : ");
302 lblLogin.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
303
304 txtLogin = new Text(loginComposite, SWT.BORDER);
305 GridData gd_txtLogin = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
306 gd_txtLogin.minimumWidth = 80;
307 gd_txtLogin.widthHint = 80;
308 gd_txtLogin.heightHint = 15;
309 txtLogin.setLayoutData(gd_txtLogin);
310
311 lblPassword = new Label(loginComposite, SWT.CENTER);
312 lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
313 lblPassword.setText("Password : ");
314 lblPassword.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
315
316 txtPassword = new Text(loginComposite, SWT.BORDER | SWT.PASSWORD);
317 GridData gd_txtPassword = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
318 gd_txtPassword.minimumWidth = 80;
319 gd_txtPassword.widthHint = 80;
320 gd_txtPassword.heightHint = 15;
321 txtPassword.setLayoutData(gd_txtPassword);
322 new Label(loginComposite, SWT.NONE);
323
324 btnConnect = new Button(loginComposite, SWT.FLAT);
325 btnConnect.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
326 btnConnect.addMouseListener(new MouseAdapter() {
327 @Override
328 public void mouseUp(MouseEvent e) {
329 connect();
330 }
331 });
332 btnConnect.setText("Connect");
333
334 btnRememberMe = new Button(loginComposite, SWT.CHECK);
335 btnRememberMe.setSelection(true);
336 GridData gd_btnRememberMe = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
337 gd_btnRememberMe.widthHint = 107;
338 btnRememberMe.setLayoutData(gd_btnRememberMe);
339 btnRememberMe.setText("Remember Me");
340 new Label(loginComposite, SWT.NONE);
341 new Label(loginComposite, SWT.NONE);
342 new Label(loginComposite, SWT.NONE);
343 new Label(loginComposite, SWT.NONE);
344
345 styledTxtMessage = new StyledText(remotingComposite, SWT.NONE);
346 styledTxtMessage.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
347 styledTxtMessage.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
348 styledTxtMessage.setFont(SWTResourceManager.getFont("Ubuntu", 12, SWT.BOLD));
349 styledTxtMessage.setSelectionBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION_TEXT));
350 styledTxtMessage.setSelectionForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
351 styledTxtMessage.setDoubleClickEnabled(false);
352 styledTxtMessage.setEditable(false);
353 GridData gd_styledTxtMessage = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
354 gd_styledTxtMessage.exclude = true;
355 gd_styledTxtMessage.minimumHeight = MESSAGE_HEIGHT;
356 gd_styledTxtMessage.heightHint = MESSAGE_HEIGHT;
357 styledTxtMessage.setLayoutData(gd_styledTxtMessage);
358
359 xpndblcmpstAdvanced = new ExpandableComposite(remotingComposite, SWT.NONE, ExpandableComposite.TWISTIE);
360 GridData gd_xpndblcmpstAdvanced = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
361 gd_xpndblcmpstAdvanced.heightHint = 19;
362 xpndblcmpstAdvanced.setLayoutData(gd_xpndblcmpstAdvanced);
363 xpndblcmpstAdvanced.addExpansionListener(new IExpansionListener() {
364 @Override
365 public void expansionStateChanged(ExpansionEvent e) {
366 GridData gridData = (GridData) xpndblcmpstAdvanced.getLayoutData();
367 if(e.getState()) {
368 shlConnect.setSize(MIN_WIDTH, MIN_EXP_HEIGHT);
369 } else {
370 shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
371 }
372
373 }
374 @Override
375 public void expansionStateChanging(ExpansionEvent e) {
376 }
377 });
378 xpndblcmpstAdvanced.setText("advanced");
379 xpndblcmpstAdvanced.setExpanded(true);
380
381 compAdvanced = new Composite(xpndblcmpstAdvanced, SWT.NONE);
382 xpndblcmpstAdvanced.setClient(compAdvanced);
383 compAdvanced.setLayout(new GridLayout(4, false));
384
385 lblPort = new Label(compAdvanced, SWT.CENTER);
386 lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
387 lblPort.setSize(0, 0);
388 lblPort.setText("Port : ");
389 lblPort.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
390
391 txtPort = new Text(compAdvanced, SWT.BORDER);
392 GridData gd_txtPort = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
393 gd_txtPort.minimumWidth = 50;
394 gd_txtPort.widthHint = 50;
395 txtPort.setLayoutData(gd_txtPort);
396
397 lblServerVersion = new Label(compAdvanced, SWT.CENTER);
398 lblServerVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
399 lblServerVersion.setText("Server Cdmlib Version :");
400 lblServerVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
401
402 txtServerVersion = new Text(compAdvanced, SWT.BORDER);
403 txtServerVersion.setEditable(false);
404 txtServerVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
405 new Label(compAdvanced, SWT.NONE);
406 new Label(compAdvanced, SWT.NONE);
407
408 lblEditorVersion = new Label(compAdvanced, SWT.CENTER);
409 lblEditorVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
410 lblEditorVersion.setText("Editor Cdmlib Version :");
411 lblEditorVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
412
413 txtEditorVersion = new Text(compAdvanced, SWT.BORDER);
414 txtEditorVersion.setEditable(false);
415 txtEditorVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
416 new Label(compAdvanced, SWT.NONE);
417 new Label(compAdvanced, SWT.NONE);
418
419 lblServerCDMVersion = new Label(compAdvanced, SWT.CENTER);
420 lblServerCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
421 lblServerCDMVersion.setText("Server CDM Version :");
422 lblServerCDMVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
423
424 txtServerCDMVersion = new Text(compAdvanced, SWT.BORDER);
425 txtServerCDMVersion.setEditable(false);
426 txtServerCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
427 new Label(compAdvanced, SWT.NONE);
428 new Label(compAdvanced, SWT.NONE);
429
430 lblEditorCDMVersion = new Label(compAdvanced, SWT.CENTER);
431 lblEditorCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
432 lblEditorCDMVersion.setText("Editor CDM Version :");
433 lblEditorCDMVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
434
435 txtEditorCDMVersion = new Text(compAdvanced, SWT.BORDER);
436 txtEditorCDMVersion.setEditable(false);
437 txtEditorCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
438
439 }
440
441
442
443 private void populateCdmServerCombo() {
444 Job job = new Job("Retrieve Server Instances") {
445 @Override
446 protected IStatus run(IProgressMonitor monitor) {
447 Display.getDefault().syncExec(new Runnable() {
448 @Override
449 public void run() {
450 for(CdmServerInfo csii : CdmServerInfo.getCdmServers()) {
451 csiiMap.put(csii.getName(), csii);
452 comboCdmServer.add(csii.getName());
453 }
454 int serverIndex = -1;
455 if(serverName != null) {
456 serverIndex = comboCdmServer.indexOf(serverName);
457 }
458 if(serverIndex == -1) {
459 comboCdmServer.select(0);
460 autoConnect = false;
461 } else {
462 comboCdmServer.select(serverIndex);
463 }
464 CdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
465 if(devRemoteSource != null) {
466 isDevRemoteSource = true;
467 }
468 refreshCdmServer();
469 updatePort();
470
471 if(devRemoteSource != null) {
472 String username = System.getProperty("cdm.server.dev.username");
473 String password = System.getProperty("cdm.server.dev.password");
474 if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
475 txtLogin.setText(username);
476 txtPassword.setText(password);
477 CdmStore.connect(devRemoteSource, RemotingLoginDialog.this);
478 }
479 }
480 }
481 });
482 return Status.OK_STATUS;
483 }
484 };
485 job.schedule();
486 }
487
488
489 private void refreshCdmServer() {
490 txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
491 updateSelectedCdmServer();
492 checkSelectedCdmServer();
493 }
494
495 private void updateSelectedCdmServer() {
496 int selIndex = comboCdmServer.getSelectionIndex();
497 if(selIndex != -1) {
498 selectedCsii = csiiMap.get(comboCdmServer.getItem(selIndex));
499 }
500 }
501
502 private void updatePort() {
503 txtPort.setText("");
504 if(selectedCsii != null) {
505 txtPort.setText(String.valueOf(selectedCsii.getPort()));
506 }
507 }
508
509 private int getPort() {
510 int port = 0;
511 try {
512 port = Integer.valueOf(txtPort.getText());
513 } catch (NumberFormatException nfe) {
514 setMessage("Port should be an integer");
515 }
516 return port;
517 }
518
519 private void checkSelectedCdmServer() {
520
521 clearOnServerChange();
522 emptyCredentials();
523
524 if(selectedCsii != null) {
525 if(selectedCsii.isLocalhost()) {
526 txtPort.setEditable(true);
527 txtPort.setEnabled(true);
528 }
529 if(selectedCsii.pingServer()) {
530 txtCdmServerStatus.setText(STATUS_AVAILABLE);
531 populateCdmInstanceCombo(true);
532 txtServerVersion.setText(selectedCsii.getCdmlibServicesVersion());
533 txtServerVersion.setToolTipText(generateLastModifiedTooltip(selectedCsii.getCdmlibLastModified()));
534
535 } else {
536 txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
537 comboCdmInstance.removeAll();
538 }
539 }
540 }
541
542
543 private void populateCdmInstanceCombo(final boolean forceRefresh) {
544 comboCdmInstance.removeAll();
545 comboCdmInstance.setEnabled(false);
546 btnConnect.setEnabled(false);
547 txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
548 txtCdmInstanceStatus.setToolTipText("");
549
550 Job job = new Job("Retrieve Server Instances") {
551 @Override
552 protected IStatus run(IProgressMonitor monitor) {
553 try {
554 if(selectedCsii != null) {
555 if(forceRefresh) {
556 selectedCsii.refreshInstances();
557 }
558 final List<CdmInstanceInfo> instances = selectedCsii.getInstances();
559 Display.getDefault().asyncExec(new Runnable() {
560 @Override
561 public void run() {
562 if(!instances.isEmpty()) {
563 for(CdmInstanceInfo cdmInstance : instances) {
564 comboCdmInstance.add(cdmInstance.getName());
565 }
566 int instanceIndex = -1;
567 if(instanceName != null) {
568 instanceIndex = comboCdmInstance.indexOf(instanceName);
569 }
570 if(instanceIndex == -1) {
571 comboCdmInstance.select(0);
572 autoConnect = false;
573 } else {
574 comboCdmInstance.select(instanceIndex);
575 }
576 updateSelectedCdmInstance();
577 checkSelectedCdmInstance();
578 comboCdmInstance.setEnabled(true);
579 if(autoConnect) {
580 connect();
581 }
582
583 } else {
584 txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
585 btnConnect.setEnabled(false);
586 }
587 }
588 });
589 }
590 } catch (final CDMServerException e) {
591 MessagingUtils.warn(getClass(), e);
592 Display.getDefault().asyncExec(new Runnable() {
593 @Override
594 public void run() {
595 txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
596 txtCdmInstanceStatus.setToolTipText(e.getMessage());
597 comboCdmInstance.setEnabled(false);
598 btnConnect.setEnabled(false);
599 }
600 });
601 }
602 return Status.OK_STATUS;
603 }
604 };
605
606 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE) && !isDevRemoteSource) {
607 // Start the Job
608 job.schedule();
609 }
610
611 }
612
613 private void refreshCdmInstance() {
614 txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
615 clearOnInstanceChange();
616 updateSelectedCdmInstance();
617 checkSelectedCdmInstance();
618 }
619
620 private void updateSelectedCdmInstance() {
621 int selIndex = comboCdmInstance.getSelectionIndex();
622 if(selIndex != -1) {
623 selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
624 if(loadLoginPrefs && !isDevRemoteSource) {
625 readPrefCredentials();
626 }
627 }
628 }
629
630 private void checkSelectedCdmInstance() {
631 boolean available = false;
632 String status = STATUS_NOT_AVAILABLE;
633 String message = null;
634
635 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
636 try {
637 if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
638 status = STATUS_AVAILABLE;
639 available = true;
640 } else {
641 status = STATUS_NOT_AVAILABLE;
642 available = false;
643 }
644
645 if(available) {
646 txtServerCDMVersion.setText(selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort()).getDbSchemaVersion());
647 int compareDbSchemaVersion = selectedCsii.compareDbSchemaVersion(selectedCdmInstance, getPort());
648 int compareCdmlibServicesVersion = selectedCsii.compareCdmlibServicesVersion();
649
650 if(compareDbSchemaVersion > 0 || compareCdmlibServicesVersion > 0) {
651 status = STATUS_NOT_COMPATIBLE;
652 available = false;
653 message = MESG_COMPATIBLE_EDITOR_OLD;
654 } else if(compareDbSchemaVersion < 0 || compareCdmlibServicesVersion < 0) {
655 status = STATUS_NOT_COMPATIBLE;
656 available = false;
657 message = MESG_COMPATIBLE_SERVER_OLD;
658 } else {
659 status = STATUS_AVAILABLE;
660 available = true;
661 message = "";
662 }
663
664
665 }
666
667 } catch (Exception e) {
668 txtCdmInstanceStatus.setToolTipText(e.getMessage());
669 } finally {
670 btnConnect.setEnabled(available);
671 txtCdmInstanceStatus.setText(status);
672 if(!StringUtils.isBlank(message)) {
673 setMessage(message);
674 }
675 }
676
677 }
678
679 }
680
681 private void connect() {
682 checkSelectedCdmInstance();
683
684 if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
685 return;
686 }
687
688 ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
689
690 if(!validateLogin()) {
691 return;
692 }
693
694 try {
695 CdmStore.connect(source, this);
696 } catch (Exception e) {
697 // Do not expect anything to go wrong at this point, so we throw a runtime exception
698 // if any problems
699 throw new RuntimeException(e);
700 }
701
702 }
703
704
705 public boolean isRememberMe() {
706 return btnRememberMe.getSelection();
707 }
708
709 private void persistPrefLastServerInstance() {
710 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
711 Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
712
713 lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
714 lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
715
716 flushPreferences(lastServerInstancePrefs);
717 }
718
719 private void persistPrefCredentials() {
720 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
721 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
722 credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
723 credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
724 flushPreferences(credentialsPrefs);
725 }
726
727 private void flushPreferences(Preferences prefs) {
728 try {
729 prefs.flush();
730 } catch (BackingStoreException bse) {
731 setMessage(bse.getMessage());
732 }
733 }
734
735 private void readPrefCredentials() {
736 String username, password;
737 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
738 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
739 username = credentialsPrefs.get(getUsernamePrefKey(), "");
740 txtLogin.setText(username);
741 password = credentialsPrefs.get(getPasswordPrefKey(),"");
742 txtPassword.setText(password);
743 if(username.isEmpty() || password.isEmpty()) {
744 autoConnect = false;
745 }
746 }
747
748 private void readPrefLastServerInstance() {
749 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
750 Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
751
752 serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
753 instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
754 }
755
756 private void emptyCredentials() {
757 txtLogin.setText("");
758 txtPassword.setText("");
759 }
760
761 private String getUsernamePrefKey() {
762 return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
763 }
764
765 private String getPasswordPrefKey() {
766 return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
767 }
768
769 private boolean validateLogin() {
770 if(getUsername() == null || getUsername().isEmpty()) {
771 setMessage("User login cannot be empty");
772 return false;
773 }
774 if(getPassword() == null || getPassword().isEmpty()) {
775 setMessage("Password cannot be empty");
776 return false;
777 }
778 return true;
779 }
780 public String getUsername() {
781 return txtLogin.getText();
782 }
783
784 public String getPassword() {
785 return txtPassword.getText();
786 }
787
788 public void setMessage(String message) {
789 if(message != null && !message.isEmpty()) {
790 if(message.length() > 60) {
791 styledTxtMessage.setToolTipText(message);
792 message = message.substring(0, 60) + "...";
793 }
794 styledTxtMessage.setText(message);
795 styledTxtMessage.setVisible(true);
796 ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
797 shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
798 shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
799 } else {
800 styledTxtMessage.setText("");
801 styledTxtMessage.setVisible(false);
802 ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
803 shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage());
804 shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage());
805 }
806 remotingComposite.layout();
807 }
808
809 private int getHeightWithoutMessage() {
810 if(xpndblcmpstAdvanced.isExpanded()) {
811 return MIN_EXP_HEIGHT;
812 } else {
813 return MIN_HEIGHT;
814 }
815 }
816
817
818 public void hide(boolean isHidden) {
819 if(shlConnect != null && shlConnect.getDisplay() != null) {
820 shlConnect.setVisible(!isHidden);
821 }
822 }
823 public void dispose() {
824 if(shlConnect != null && shlConnect.getDisplay() != null) {
825 shlConnect.dispose();
826 }
827 }
828
829 public void onComplete() {
830 Display.getDefault().asyncExec(new Runnable() {
831 @Override
832 public void run() {
833 if(!isDevRemoteSource) {
834 if(isRememberMe()) {
835 persistPrefCredentials();
836 }
837 persistPrefLastServerInstance();
838 }
839 dispose();
840 }
841 });
842 }
843
844 private String generateLastModifiedTooltip(String cdmlibLastModified) {
845 SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z");
846 Date cdmlibLastModifiedDate;
847 String cdmlibLastModifiedTimestamp = "";
848
849 cdmlibLastModifiedDate = new Date(Long.valueOf(cdmlibLastModified));
850 cdmlibLastModifiedTimestamp = sdf.format(cdmlibLastModifiedDate);
851
852 return "last modified : " + cdmlibLastModifiedTimestamp;
853 }
854
855 private void setEditorInfo() {
856 txtEditorCDMVersion.setText(CdmMetaData.getDbSchemaVersion());
857 txtEditorVersion.setText(CdmApplicationState.getCdmlibVersion());
858 txtEditorVersion.setToolTipText(generateLastModifiedTooltip(CdmApplicationState.getCdmlibLastModified()));
859 }
860
861 private void clearOnServerChange() {
862 setMessage("");
863 txtServerCDMVersion.setText("");
864 txtServerVersion.setText("");
865 txtServerVersion.setToolTipText("");
866 txtServerCDMVersion.setText("");
867 txtPort.setEditable(false);
868 txtPort.setEnabled(false);
869 }
870
871 private void clearOnInstanceChange() {
872 setMessage("");
873 txtServerCDMVersion.setText("");
874 }
875
876 private void updateOnServerChange(String serverVersion, String serverVersionTooltip) {
877
878 }
879
880 }