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