ee7107e088d9dbf088922fc1f260a860e698bbae
[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 refreshCdmServer();
458 updatePort();
459 CdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
460 if(devRemoteSource != null) {
461 String username = System.getProperty("cdm.server.dev.username");
462 String password = System.getProperty("cdm.server.dev.password");
463 if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
464 txtLogin.setText(username);
465 txtPassword.setText(password);
466 CdmStore.connect(devRemoteSource, RemotingLoginDialog.this);
467 }
468 }
469 }
470 });
471 return Status.OK_STATUS;
472 }
473 };
474 job.schedule();
475 }
476
477
478 private void refreshCdmServer() {
479 txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
480 updateSelectedCdmServer();
481 checkSelectedCdmServer();
482 }
483
484 private void updateSelectedCdmServer() {
485 int selIndex = comboCdmServer.getSelectionIndex();
486 if(selIndex != -1) {
487 selectedCsii = csiiMap.get(comboCdmServer.getItem(selIndex));
488 }
489 }
490
491 private void updatePort() {
492 txtPort.setText("");
493 if(selectedCsii != null) {
494 txtPort.setText(String.valueOf(selectedCsii.getPort()));
495 }
496 }
497
498 private int getPort() {
499 int port = 0;
500 try {
501 port = Integer.valueOf(txtPort.getText());
502 } catch (NumberFormatException nfe) {
503 setMessage("Port should be an integer");
504 }
505 return port;
506 }
507
508 private void checkSelectedCdmServer() {
509
510 txtCdmInstanceStatus.setText("");
511 txtPort.setEditable(false);
512 txtPort.setEnabled(false);
513 emptyCredentials();
514
515 if(selectedCsii != null) {
516 if(selectedCsii.isLocalhost()) {
517 txtPort.setEditable(true);
518 txtPort.setEnabled(true);
519 }
520 if(selectedCsii.pingServer()) {
521 txtCdmServerStatus.setText(STATUS_AVAILABLE);
522 populateCdmInstanceCombo(true);
523
524 } else {
525 txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
526 comboCdmInstance.removeAll();
527 }
528 }
529 }
530
531
532 private void populateCdmInstanceCombo(final boolean forceRefresh) {
533 comboCdmInstance.removeAll();
534 comboCdmInstance.setEnabled(false);
535 btnConnect.setEnabled(false);
536 txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
537
538 Job job = new Job("Retrieve Server Instances") {
539 @Override
540 protected IStatus run(IProgressMonitor monitor) {
541 try {
542 if(selectedCsii != null) {
543 if(forceRefresh) {
544 selectedCsii.refreshInstances();
545 }
546 final List<CdmInstanceInfo> instances = selectedCsii.getInstances();
547 Display.getDefault().asyncExec(new Runnable() {
548 @Override
549 public void run() {
550 if(!instances.isEmpty()) {
551 for(CdmInstanceInfo cdmInstance : instances) {
552 comboCdmInstance.add(cdmInstance.getName());
553 }
554 int instanceIndex = -1;
555 if(instanceName != null) {
556 instanceIndex = comboCdmInstance.indexOf(instanceName);
557 }
558 if(instanceIndex == -1) {
559 comboCdmInstance.select(0);
560 autoConnect = false;
561 } else {
562 comboCdmInstance.select(instanceIndex);
563 }
564 updateSelectedCdmInstance();
565 checkSelectedCdmInstance();
566 comboCdmInstance.setEnabled(true);
567 if(autoConnect) {
568 connect();
569 }
570
571 } else {
572 txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
573 btnConnect.setEnabled(false);
574 }
575 }
576 });
577 }
578 } catch (CDMServerException e) {
579 MessagingUtils.warn(getClass(), e);
580 Display.getDefault().asyncExec(new Runnable() {
581 @Override
582 public void run() {
583 txtCdmInstanceStatus.setText(STATUS_REMOTING_NOT_ACTIVATED);
584 comboCdmInstance.setEnabled(false);
585 btnConnect.setEnabled(false);
586 }
587 });
588 }
589 return Status.OK_STATUS;
590 }
591 };
592
593 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
594 // Start the Job
595 job.schedule();
596 }
597
598 }
599
600 private void refreshCdmInstance() {
601 txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
602 updateSelectedCdmInstance();
603 checkSelectedCdmInstance();
604 }
605
606 private void updateSelectedCdmInstance() {
607 int selIndex = comboCdmInstance.getSelectionIndex();
608 if(selIndex != -1) {
609 selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
610 if(loadLoginPrefs) {
611 readPrefCredentials();
612 }
613 }
614 }
615
616 private void checkSelectedCdmInstance() {
617 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
618 try {
619 if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
620 txtCdmInstanceStatus.setText(STATUS_AVAILABLE);
621 btnConnect.setEnabled(true);
622 } else {
623 txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
624 btnConnect.setEnabled(false);
625 }
626 } catch (Exception e) {
627 txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
628 txtCdmInstanceStatus.setToolTipText(e.getMessage());
629 }
630 }
631
632 }
633
634 private void connect() {
635 checkSelectedCdmInstance();
636
637 if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
638 return;
639 }
640
641 ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
642
643 if(!validateLogin()) {
644 return;
645 }
646
647 try {
648 CdmStore.connect(source, this);
649 } catch (Exception e) {
650 // Do not expect anything to go wrong at this point, so we throw a runtime exception
651 // if any problems
652 throw new RuntimeException(e);
653 }
654
655 }
656
657
658 public boolean isRememberMe() {
659 return btnRememberMe.getSelection();
660 }
661
662 private void persistPrefLastServerInstance() {
663 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
664 Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
665
666 lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
667 lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
668
669 flushPreferences(lastServerInstancePrefs);
670 }
671
672 private void persistPrefCredentials() {
673 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
674 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
675 credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
676 credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
677 flushPreferences(credentialsPrefs);
678 }
679
680 private void flushPreferences(Preferences prefs) {
681 try {
682 prefs.flush();
683 } catch (BackingStoreException bse) {
684 setMessage(bse.getMessage());
685 }
686 }
687
688 private void readPrefCredentials() {
689 String username, password;
690 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
691 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
692 username = credentialsPrefs.get(getUsernamePrefKey(), "");
693 txtLogin.setText(username);
694 password = credentialsPrefs.get(getPasswordPrefKey(), "");
695 txtPassword.setText(password);
696 if(username.isEmpty() || password.isEmpty()) {
697 autoConnect = false;
698 }
699 }
700
701 private void readPrefLastServerInstance() {
702 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
703 Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
704
705 serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
706 instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
707 }
708
709 private void emptyCredentials() {
710 txtLogin.setText("");
711 txtPassword.setText("");
712 }
713
714 private String getUsernamePrefKey() {
715 return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
716 }
717
718 private String getPasswordPrefKey() {
719 return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
720 }
721
722 private boolean validateLogin() {
723 if(getUsername() == null || getUsername().isEmpty()) {
724 setMessage("User login cannot be empty");
725 return false;
726 }
727 if(getPassword() == null || getPassword().isEmpty()) {
728 setMessage("Password cannot be empty");
729 return false;
730 }
731 return true;
732 }
733 public String getUsername() {
734 return txtLogin.getText();
735 }
736
737 public String getPassword() {
738 return txtPassword.getText();
739 }
740
741 public void setMessage(String message) {
742 if(message != null && !message.isEmpty()) {
743 if(message.length() > 60) {
744 styledTxtMessage.setToolTipText(message);
745 message = message.substring(0, 60) + "...";
746 }
747 styledTxtMessage.setText(message);
748 styledTxtMessage.setVisible(true);
749 ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
750 shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT+MESSAGE_HEIGHT);
751 shlConnect.setMinimumSize(MIN_WIDTH, MIN_HEIGHT+MESSAGE_HEIGHT);
752 } else {
753 styledTxtMessage.setText("");
754 styledTxtMessage.setVisible(false);
755 ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
756 shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
757 shlConnect.setMinimumSize(MIN_WIDTH, MIN_HEIGHT);
758 }
759 remotingComposite.layout();
760 }
761
762
763
764 public void hide(boolean isHidden) {
765 if(shlConnect != null) {
766 shlConnect.setVisible(!isHidden);
767 }
768 }
769 public void dispose() {
770 if(shlConnect != null) {
771 shlConnect.dispose();
772 }
773 }
774
775 public void onComplete() {
776 Display.getDefault().asyncExec(new Runnable() {
777 @Override
778 public void run() {
779 if(isRememberMe()) {
780 persistPrefCredentials();
781 }
782 persistPrefLastServerInstance();
783 dispose();
784 }
785 });
786 }
787 }