Revision 1b626d51
Added by Cherian Mathew over 8 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/RemotingLoginDialog.java | ||
---|---|---|
17 | 17 |
import org.eclipse.core.runtime.IStatus; |
18 | 18 |
import org.eclipse.core.runtime.Status; |
19 | 19 |
import org.eclipse.core.runtime.jobs.Job; |
20 |
import org.eclipse.core.runtime.preferences.ConfigurationScope; |
|
21 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
|
20 | 22 |
import org.eclipse.swt.SWT; |
21 | 23 |
import org.eclipse.swt.custom.StyledText; |
22 | 24 |
import org.eclipse.swt.events.MouseAdapter; |
... | ... | |
39 | 41 |
import org.eclipse.ui.forms.events.IExpansionListener; |
40 | 42 |
import org.eclipse.ui.forms.widgets.ExpandableComposite; |
41 | 43 |
import org.eclipse.wb.swt.SWTResourceManager; |
44 |
import org.osgi.service.prefs.BackingStoreException; |
|
45 |
import org.osgi.service.prefs.Preferences; |
|
42 | 46 |
|
43 | 47 |
import eu.etaxonomy.taxeditor.model.MessagingUtils; |
44 | 48 |
import eu.etaxonomy.taxeditor.remoting.server.CDMServerException; |
49 |
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource; |
|
45 | 50 |
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo; |
46 | 51 |
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo; |
47 | 52 |
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource; |
... | ... | |
64 | 69 |
|
65 | 70 |
private final Map<String, CdmServerInfo> csiiMap = new HashMap<String, CdmServerInfo>(); |
66 | 71 |
|
67 |
private final String STATUS_AVAILABLE = "Available"; |
|
68 |
private final String STATUS_NOT_AVAILABLE = "Not Available"; |
|
69 |
private final String STATUS_RETRIEVING = "Retrieving ..."; |
|
70 |
private final String STATUS_CHECKING_AVAILABILITY = "Checking ..."; |
|
71 |
private final String STATUS_NO_INSTANCES = "No Instances Found"; |
|
72 |
private final String STATUS_ERROR = "Error"; |
|
73 |
private final String STATUS_REMOTING_NOT_ACTIVATED = "Remoting not activated"; |
|
72 |
private final static String STATUS_AVAILABLE = "Available"; |
|
73 |
private final static String STATUS_NOT_AVAILABLE = "Not Available"; |
|
74 |
private final static String STATUS_RETRIEVING = "Retrieving ..."; |
|
75 |
private final static String STATUS_CHECKING_AVAILABILITY = "Checking ..."; |
|
76 |
private final static String STATUS_NO_INSTANCES = "No Instances Found"; |
|
77 |
private final static String STATUS_ERROR = "Error"; |
|
78 |
private final static String STATUS_REMOTING_NOT_ACTIVATED = "Remoting not activated"; |
|
79 |
private final static String STORE_PREFERENCES_NODE = "eu.etaxonomy.taxeditor.store"; |
|
80 |
|
|
81 |
private final static String LOGIN_NODE = "login"; |
|
82 |
private final static String USERNAME_SUFFIX = "_username"; |
|
83 |
private final static String PASSWORD_SUFFIX = "_password"; |
|
84 |
|
|
85 |
private final static String LAST_SERVER_INSTANCE_NODE = "lastServerInstance"; |
|
86 |
private final static String LAST_SERVER_KEY = "lastServerKey"; |
|
87 |
private final static String LAST_INSTANCE_KEY = "lastInstanceKey"; |
|
88 |
|
|
74 | 89 |
|
75 | 90 |
private Composite remotingComposite; |
76 | 91 |
private CdmServerInfo selectedCsii; |
... | ... | |
102 | 117 |
private Label lblEditorCDMVersion; |
103 | 118 |
private Text txtEditorCDMVersion; |
104 | 119 |
|
120 |
private String serverName, instanceName; |
|
121 |
private boolean autoConnect = false; |
|
122 |
private boolean loadLoginPrefs = true; |
|
123 |
|
|
105 | 124 |
/** |
106 | 125 |
* Create the dialog. |
107 | 126 |
* @param parent |
... | ... | |
112 | 131 |
setText("Login"); |
113 | 132 |
} |
114 | 133 |
|
134 |
public Object open(CdmRemoteSource source, boolean loadLoginPrefs, boolean autoConnect) { |
|
135 |
this.loadLoginPrefs = loadLoginPrefs; |
|
136 |
this.serverName = source.getName(); |
|
137 |
String contextPath = source.getContextPath(); |
|
138 |
this.instanceName = contextPath.substring(contextPath.lastIndexOf("/") + 1); |
|
139 |
return open(serverName, instanceName, loadLoginPrefs, autoConnect); |
|
140 |
} |
|
141 |
|
|
142 |
|
|
143 |
public Object open(String serverName, String instanceName, boolean loadLoginPrefs, boolean autoConnect) { |
|
144 |
this.serverName = serverName; |
|
145 |
this.instanceName = instanceName; |
|
146 |
this.loadLoginPrefs = loadLoginPrefs; |
|
147 |
this.autoConnect = autoConnect; |
|
148 |
return open(); |
|
149 |
} |
|
150 |
|
|
115 | 151 |
/** |
116 | 152 |
* Open the dialog. |
117 | 153 |
* @return the result |
... | ... | |
208 | 244 |
@Override |
209 | 245 |
public void widgetSelected(SelectionEvent e) { |
210 | 246 |
updateSelectedCdmInstance(); |
211 |
checkSelectedCdmServerInstance();
|
|
247 |
checkSelectedCdmInstance(); |
|
212 | 248 |
} |
213 | 249 |
}); |
214 | 250 |
GridData gd_comboCdmInstance = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1); |
... | ... | |
277 | 313 |
btnConnect.addMouseListener(new MouseAdapter() { |
278 | 314 |
@Override |
279 | 315 |
public void mouseUp(MouseEvent e) { |
280 |
int selInstanceIndex = comboCdmInstance.getSelectionIndex(); |
|
281 |
String instance = comboCdmInstance.getItem(selInstanceIndex); |
|
282 |
checkSelectedCdmServerInstance(); |
|
283 |
connect(selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort())); |
|
316 |
connect(); |
|
284 | 317 |
} |
285 | 318 |
}); |
286 | 319 |
btnConnect.setText("Connect"); |
287 | 320 |
|
288 | 321 |
btnRememberMe = new Button(loginComposite, SWT.CHECK); |
322 |
btnRememberMe.setSelection(true); |
|
289 | 323 |
GridData gd_btnRememberMe = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1); |
290 | 324 |
gd_btnRememberMe.widthHint = 107; |
291 | 325 |
btnRememberMe.setLayoutData(gd_btnRememberMe); |
292 |
btnRememberMe.setEnabled(false); |
|
293 | 326 |
btnRememberMe.setText("Remember Me"); |
294 | 327 |
new Label(loginComposite, SWT.NONE); |
295 | 328 |
new Label(loginComposite, SWT.NONE); |
... | ... | |
396 | 429 |
|
397 | 430 |
} |
398 | 431 |
|
432 |
|
|
433 |
|
|
399 | 434 |
private void populateCdmServerCombo() { |
400 | 435 |
Job job = new Job("Retrieve Server Instances") { |
401 | 436 |
@Override |
402 | 437 |
protected IStatus run(IProgressMonitor monitor) { |
403 |
Display.getDefault().asyncExec(new Runnable() {
|
|
438 |
Display.getDefault().syncExec(new Runnable() { |
|
404 | 439 |
@Override |
405 | 440 |
public void run() { |
406 | 441 |
for(CdmServerInfo csii : CdmServerInfo.getCdmServers()) { |
407 | 442 |
csiiMap.put(csii.getName(), csii); |
408 | 443 |
comboCdmServer.add(csii.getName()); |
409 | 444 |
} |
410 |
comboCdmServer.select(0); |
|
445 |
int serverIndex = -1; |
|
446 |
if(serverName != null) { |
|
447 |
serverIndex = comboCdmServer.indexOf(serverName); |
|
448 |
} |
|
449 |
if(serverIndex == -1) { |
|
450 |
comboCdmServer.select(0); |
|
451 |
autoConnect = false; |
|
452 |
} else { |
|
453 |
comboCdmServer.select(serverIndex); |
|
454 |
} |
|
411 | 455 |
refreshCdmServer(); |
412 | 456 |
updatePort(); |
413 | 457 |
} |
... | ... | |
450 | 494 |
} |
451 | 495 |
|
452 | 496 |
private void checkSelectedCdmServer() { |
453 |
int selIndex = comboCdmServer.getSelectionIndex(); |
|
497 |
|
|
454 | 498 |
txtCdmInstanceStatus.setText(""); |
455 | 499 |
txtPort.setEditable(false); |
456 | 500 |
txtPort.setEnabled(false); |
501 |
emptyCredentials(); |
|
502 |
|
|
457 | 503 |
if(selectedCsii != null) { |
458 | 504 |
if(selectedCsii.isLocalhost()) { |
459 | 505 |
txtPort.setEditable(true); |
460 | 506 |
txtPort.setEnabled(true); |
461 |
} else { |
|
462 |
txtPort.setEditable(false); |
|
463 |
txtPort.setEnabled(false); |
|
464 | 507 |
} |
465 | 508 |
if(selectedCsii.pingServer()) { |
466 | 509 |
txtCdmServerStatus.setText(STATUS_AVAILABLE); |
... | ... | |
479 | 522 |
comboCdmInstance.setEnabled(false); |
480 | 523 |
btnConnect.setEnabled(false); |
481 | 524 |
txtCdmInstanceStatus.setText(STATUS_RETRIEVING); |
525 |
|
|
482 | 526 |
Job job = new Job("Retrieve Server Instances") { |
483 | 527 |
@Override |
484 | 528 |
protected IStatus run(IProgressMonitor monitor) { |
... | ... | |
495 | 539 |
for(CdmInstanceInfo cdmInstance : instances) { |
496 | 540 |
comboCdmInstance.add(cdmInstance.getName()); |
497 | 541 |
} |
498 |
comboCdmInstance.select(0); |
|
542 |
int instanceIndex = -1; |
|
543 |
if(instanceName != null) { |
|
544 |
instanceIndex = comboCdmInstance.indexOf(instanceName); |
|
545 |
} |
|
546 |
if(instanceIndex == -1) { |
|
547 |
comboCdmInstance.select(0); |
|
548 |
autoConnect = false; |
|
549 |
} else { |
|
550 |
comboCdmInstance.select(instanceIndex); |
|
551 |
} |
|
499 | 552 |
updateSelectedCdmInstance(); |
500 |
checkSelectedCdmServerInstance();
|
|
553 |
checkSelectedCdmInstance(); |
|
501 | 554 |
comboCdmInstance.setEnabled(true); |
555 |
if(autoConnect) { |
|
556 |
connect(); |
|
557 |
} |
|
502 | 558 |
|
503 | 559 |
} else { |
504 | 560 |
txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES); |
... | ... | |
532 | 588 |
private void refreshCdmInstance() { |
533 | 589 |
txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY); |
534 | 590 |
updateSelectedCdmInstance(); |
535 |
checkSelectedCdmServerInstance();
|
|
591 |
checkSelectedCdmInstance(); |
|
536 | 592 |
} |
537 | 593 |
|
538 | 594 |
private void updateSelectedCdmInstance() { |
539 | 595 |
int selIndex = comboCdmInstance.getSelectionIndex(); |
540 | 596 |
if(selIndex != -1) { |
541 | 597 |
selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex)); |
598 |
if(loadLoginPrefs) { |
|
599 |
readPrefCredentials(); |
|
600 |
} |
|
542 | 601 |
} |
543 | 602 |
} |
544 | 603 |
|
545 |
private void checkSelectedCdmServerInstance() {
|
|
604 |
private void checkSelectedCdmInstance() { |
|
546 | 605 |
if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) { |
547 | 606 |
try { |
548 | 607 |
if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) { |
... | ... | |
554 | 613 |
} |
555 | 614 |
} catch (Exception e) { |
556 | 615 |
txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE); |
557 |
//MessagingUtils.warn(this.getClass(), e);
|
|
616 |
txtCdmInstanceStatus.setToolTipText(e.getMessage());
|
|
558 | 617 |
} |
559 | 618 |
} |
560 | 619 |
|
561 | 620 |
} |
562 | 621 |
|
563 |
private void connect(ICdmRemoteSource source) {
|
|
564 |
|
|
622 |
private void connect() { |
|
623 |
checkSelectedCdmInstance(); |
|
565 | 624 |
|
566 |
if(!validateLogin()) {
|
|
625 |
if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
|
|
567 | 626 |
return; |
568 | 627 |
} |
569 | 628 |
|
570 |
if(CdmStore.isConnecting()){ |
|
571 |
MessagingUtils.warningDialog("Already connecting", this, "You are currently connecting to a different CDM Instance already."); |
|
629 |
ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort()); |
|
630 |
|
|
631 |
if(!validateLogin()) { |
|
572 | 632 |
return; |
573 | 633 |
} |
634 |
// |
|
635 |
// if(CdmStore.isConnecting()){ |
|
636 |
// MessagingUtils.warningDialog("Already connecting", this, "You are currently connecting to a different CDM Instance already."); |
|
637 |
// return; |
|
638 |
// } |
|
574 | 639 |
|
575 | 640 |
try { |
576 | 641 |
CdmStore.connect(source, this); |
... | ... | |
582 | 647 |
|
583 | 648 |
} |
584 | 649 |
|
650 |
public boolean isRememberMe() { |
|
651 |
return btnRememberMe.getSelection(); |
|
652 |
} |
|
653 |
|
|
654 |
private void persistPrefLastServerInstance() { |
|
655 |
IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE); |
|
656 |
Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE); |
|
657 |
|
|
658 |
lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName()); |
|
659 |
lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName()); |
|
660 |
|
|
661 |
flushPreferences(lastServerInstancePrefs); |
|
662 |
} |
|
663 |
|
|
664 |
private void persistPrefCredentials() { |
|
665 |
IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE); |
|
666 |
Preferences credentialsPrefs = preferences.node(LOGIN_NODE); |
|
667 |
credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText()); |
|
668 |
credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText()); |
|
669 |
flushPreferences(credentialsPrefs); |
|
670 |
} |
|
671 |
|
|
672 |
private void flushPreferences(Preferences prefs) { |
|
673 |
try { |
|
674 |
prefs.flush(); |
|
675 |
} catch (BackingStoreException bse) { |
|
676 |
setMessage(bse.getMessage()); |
|
677 |
} |
|
678 |
} |
|
679 |
|
|
680 |
private void readPrefCredentials() { |
|
681 |
IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE); |
|
682 |
Preferences credentialsPrefs = preferences.node(LOGIN_NODE); |
|
683 |
String username = credentialsPrefs.get(getUsernamePrefKey(), ""); |
|
684 |
txtLogin.setText(username); |
|
685 |
String password = credentialsPrefs.get(getPasswordPrefKey(), ""); |
|
686 |
txtPassword.setText(password); |
|
687 |
if(username.isEmpty() || password.isEmpty()) { |
|
688 |
autoConnect = false; |
|
689 |
} |
|
690 |
} |
|
691 |
|
|
692 |
private void emptyCredentials() { |
|
693 |
txtLogin.setText(""); |
|
694 |
txtLogin.setText(""); |
|
695 |
} |
|
696 |
|
|
697 |
private String getUsernamePrefKey() { |
|
698 |
return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX; |
|
699 |
} |
|
700 |
|
|
701 |
private String getPasswordPrefKey() { |
|
702 |
return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX; |
|
703 |
} |
|
704 |
|
|
585 | 705 |
private boolean validateLogin() { |
586 | 706 |
if(getUsername() == null || getUsername().isEmpty()) { |
587 | 707 |
setMessage("User login cannot be empty"); |
... | ... | |
603 | 723 |
|
604 | 724 |
public void setMessage(String message) { |
605 | 725 |
if(message != null && !message.isEmpty()) { |
726 |
if(message.length() > 60) { |
|
727 |
styledTxtMessage.setToolTipText(message); |
|
728 |
message = message.substring(0, 60) + "..."; |
|
729 |
} |
|
606 | 730 |
styledTxtMessage.setText(message); |
607 | 731 |
styledTxtMessage.setVisible(true); |
608 | 732 |
((GridData)styledTxtMessage.getLayoutData()).exclude = false; |
... | ... | |
618 | 742 |
remotingComposite.layout(); |
619 | 743 |
} |
620 | 744 |
|
745 |
|
|
746 |
|
|
621 | 747 |
public void hide(boolean isHidden) { |
622 | 748 |
if(shlConnect != null) { |
623 | 749 |
shlConnect.setVisible(!isHidden); |
... | ... | |
628 | 754 |
shlConnect.dispose(); |
629 | 755 |
} |
630 | 756 |
} |
757 |
|
|
758 |
public void onComplete() { |
|
759 |
Display.getDefault().asyncExec(new Runnable() { |
|
760 |
@Override |
|
761 |
public void run() { |
|
762 |
if(isRememberMe()) { |
|
763 |
persistPrefCredentials(); |
|
764 |
} |
|
765 |
persistPrefLastServerInstance(); |
|
766 |
dispose(); |
|
767 |
} |
|
768 |
}); |
|
769 |
} |
|
631 | 770 |
} |
Also available in: Unified diff
#5029 Add functionality for login to remember credentials, reconnect and switch user