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