3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.ui
.dialog
;
12 import java
.util
.HashMap
;
13 import java
.util
.List
;
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
;
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
;
60 public class RemotingLoginDialog
extends Dialog
{
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
;
70 private final Map
<String
, CdmServerInfo
> csiiMap
= new HashMap
<String
, CdmServerInfo
>();
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";
81 private final static String LOGIN_NODE
= "login";
82 private final static String USERNAME_SUFFIX
= "_username";
83 private final static String PASSWORD_SUFFIX
= "_password";
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";
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
;
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
;
120 private String serverName
, instanceName
;
121 private boolean autoConnect
= false;
122 private boolean loadLoginPrefs
= true;
123 private boolean isDevRemoteSource
= false;
130 public RemotingLoginDialog(Shell parent
, int style
) {
131 super(parent
, style
);
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
);
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
;
156 public Object
open() {
157 // ICdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
158 // if(devRemoteSource != null) {
159 // connect(devRemoteSource);
162 if(serverName
== null && instanceName
== null) {
163 readPrefLastServerInstance();
166 populateCdmServerCombo();
170 xpndblcmpstAdvanced
.setExpanded(false);
172 Display display
= getParent().getDisplay();
174 while (!shlConnect
.isDisposed()) {
175 if (!display
.readAndDispatch()) {
184 * Create contents of the dialog.
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
));
193 remotingComposite
= new Composite(shlConnect
, SWT
.NONE
);
194 remotingComposite
.setLayout(new GridLayout(1, false));
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));
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));
207 comboCdmServer
= new Combo(cdmServerComposite
, SWT
.READ_ONLY
);
208 comboCdmServer
.addSelectionListener(new SelectionAdapter() {
210 public void widgetSelected(SelectionEvent e
) {
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);
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
);
227 btnCdmServerRefresh
= new Button(cdmServerComposite
, SWT
.NONE
);
228 btnCdmServerRefresh
.addSelectionListener(new SelectionAdapter() {
230 public void widgetSelected(SelectionEvent e
) {
234 btnCdmServerRefresh
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 1, 1));
235 btnCdmServerRefresh
.setText("Refresh");
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
));
244 comboCdmInstance
= new Combo(cdmServerComposite
, SWT
.READ_ONLY
);
245 comboCdmInstance
.addSelectionListener(new SelectionAdapter() {
247 public void widgetSelected(SelectionEvent e
) {
248 updateSelectedCdmInstance();
249 checkSelectedCdmInstance();
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);
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
);
264 Button btnCdmInstanceRefresh
= new Button(cdmServerComposite
, SWT
.FLAT
);
265 btnCdmInstanceRefresh
.addSelectionListener(new SelectionAdapter() {
267 public void widgetSelected(SelectionEvent e
) {
268 refreshCdmInstance();
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");
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
);
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
));
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
);
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
));
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
);
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() {
317 public void mouseUp(MouseEvent e
) {
321 btnConnect
.setText("Connect");
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
);
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
);
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() {
354 public void expansionStateChanged(ExpansionEvent e
) {
355 GridData gridData
= (GridData
) xpndblcmpstAdvanced
.getLayoutData();
357 shlConnect
.setSize(MIN_WIDTH
, MIN_EXP_HEIGHT
);
359 shlConnect
.setSize(MIN_WIDTH
, MIN_HEIGHT
);
364 public void expansionStateChanging(ExpansionEvent e
) {
367 xpndblcmpstAdvanced
.setText("advanced");
368 xpndblcmpstAdvanced
.setExpanded(true);
370 compAdvanced
= new Composite(xpndblcmpstAdvanced
, SWT
.NONE
);
371 xpndblcmpstAdvanced
.setClient(compAdvanced
);
372 compAdvanced
.setLayout(new GridLayout(4, false));
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
));
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
);
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
));
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
);
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
));
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
);
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
));
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
);
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
));
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));
436 private void populateCdmServerCombo() {
437 Job job
= new Job("Retrieve Server Instances") {
439 protected IStatus
run(IProgressMonitor monitor
) {
440 Display
.getDefault().syncExec(new Runnable() {
443 for(CdmServerInfo csii
: CdmServerInfo
.getCdmServers()) {
444 csiiMap
.put(csii
.getName(), csii
);
445 comboCdmServer
.add(csii
.getName());
447 int serverIndex
= -1;
448 if(serverName
!= null) {
449 serverIndex
= comboCdmServer
.indexOf(serverName
);
451 if(serverIndex
== -1) {
452 comboCdmServer
.select(0);
455 comboCdmServer
.select(serverIndex
);
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);
471 return Status
.OK_STATUS
;
478 private void refreshCdmServer() {
479 txtCdmServerStatus
.setText(STATUS_CHECKING_AVAILABILITY
);
480 updateSelectedCdmServer();
481 checkSelectedCdmServer();
484 private void updateSelectedCdmServer() {
485 int selIndex
= comboCdmServer
.getSelectionIndex();
487 selectedCsii
= csiiMap
.get(comboCdmServer
.getItem(selIndex
));
491 private void updatePort() {
493 if(selectedCsii
!= null) {
494 txtPort
.setText(String
.valueOf(selectedCsii
.getPort()));
498 private int getPort() {
501 port
= Integer
.valueOf(txtPort
.getText());
502 } catch (NumberFormatException nfe
) {
503 setMessage("Port should be an integer");
508 private void checkSelectedCdmServer() {
510 txtCdmInstanceStatus
.setText("");
511 txtPort
.setEditable(false);
512 txtPort
.setEnabled(false);
515 if(selectedCsii
!= null) {
516 if(selectedCsii
.isLocalhost()) {
517 txtPort
.setEditable(true);
518 txtPort
.setEnabled(true);
520 if(selectedCsii
.pingServer()) {
521 txtCdmServerStatus
.setText(STATUS_AVAILABLE
);
522 populateCdmInstanceCombo(true);
525 txtCdmServerStatus
.setText(STATUS_NOT_AVAILABLE
);
526 comboCdmInstance
.removeAll();
532 private void populateCdmInstanceCombo(final boolean forceRefresh
) {
533 comboCdmInstance
.removeAll();
534 comboCdmInstance
.setEnabled(false);
535 btnConnect
.setEnabled(false);
536 txtCdmInstanceStatus
.setText(STATUS_RETRIEVING
);
538 Job job
= new Job("Retrieve Server Instances") {
540 protected IStatus
run(IProgressMonitor monitor
) {
542 if(selectedCsii
!= null) {
544 selectedCsii
.refreshInstances();
546 final List
<CdmInstanceInfo
> instances
= selectedCsii
.getInstances();
547 Display
.getDefault().asyncExec(new Runnable() {
550 if(!instances
.isEmpty()) {
551 for(CdmInstanceInfo cdmInstance
: instances
) {
552 comboCdmInstance
.add(cdmInstance
.getName());
554 int instanceIndex
= -1;
555 if(instanceName
!= null) {
556 instanceIndex
= comboCdmInstance
.indexOf(instanceName
);
558 if(instanceIndex
== -1) {
559 comboCdmInstance
.select(0);
562 comboCdmInstance
.select(instanceIndex
);
564 updateSelectedCdmInstance();
565 checkSelectedCdmInstance();
566 comboCdmInstance
.setEnabled(true);
572 txtCdmInstanceStatus
.setText(STATUS_NO_INSTANCES
);
573 btnConnect
.setEnabled(false);
578 } catch (CDMServerException e
) {
579 MessagingUtils
.warn(getClass(), e
);
580 Display
.getDefault().asyncExec(new Runnable() {
583 txtCdmInstanceStatus
.setText(STATUS_REMOTING_NOT_ACTIVATED
);
584 comboCdmInstance
.setEnabled(false);
585 btnConnect
.setEnabled(false);
589 return Status
.OK_STATUS
;
593 if(txtCdmServerStatus
.getText().equals(STATUS_AVAILABLE
)) {
600 private void refreshCdmInstance() {
601 txtCdmInstanceStatus
.setText(STATUS_CHECKING_AVAILABILITY
);
602 updateSelectedCdmInstance();
603 checkSelectedCdmInstance();
606 private void updateSelectedCdmInstance() {
607 int selIndex
= comboCdmInstance
.getSelectionIndex();
609 selectedCdmInstance
= selectedCsii
.getInstanceFromName(comboCdmInstance
.getItem(selIndex
));
611 readPrefCredentials();
616 private void checkSelectedCdmInstance() {
617 if(txtCdmServerStatus
.getText().equals(STATUS_AVAILABLE
)) {
619 if(selectedCsii
.pingInstance(selectedCdmInstance
, getPort())) {
620 txtCdmInstanceStatus
.setText(STATUS_AVAILABLE
);
621 btnConnect
.setEnabled(true);
623 txtCdmInstanceStatus
.setText(STATUS_NOT_AVAILABLE
);
624 btnConnect
.setEnabled(false);
626 } catch (Exception e
) {
627 txtCdmInstanceStatus
.setText(STATUS_NOT_AVAILABLE
);
628 txtCdmInstanceStatus
.setToolTipText(e
.getMessage());
634 private void connect() {
635 checkSelectedCdmInstance();
637 if(!txtCdmInstanceStatus
.getText().equals(STATUS_AVAILABLE
)) {
641 ICdmRemoteSource source
= selectedCsii
.getCdmRemoteSource(selectedCdmInstance
, getPort());
643 if(!validateLogin()) {
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
652 throw new RuntimeException(e
);
658 public boolean isRememberMe() {
659 return btnRememberMe
.getSelection();
662 private void persistPrefLastServerInstance() {
663 IEclipsePreferences preferences
= ConfigurationScope
.INSTANCE
.getNode(STORE_PREFERENCES_NODE
);
664 Preferences lastServerInstancePrefs
= preferences
.node(LAST_SERVER_INSTANCE_NODE
);
666 lastServerInstancePrefs
.put(LAST_SERVER_KEY
, selectedCsii
.getName());
667 lastServerInstancePrefs
.put(LAST_INSTANCE_KEY
, selectedCdmInstance
.getName());
669 flushPreferences(lastServerInstancePrefs
);
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
);
680 private void flushPreferences(Preferences prefs
) {
683 } catch (BackingStoreException bse
) {
684 setMessage(bse
.getMessage());
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(), txtLogin
.getText());
693 txtLogin
.setText(username
);
694 password
= credentialsPrefs
.get(getPasswordPrefKey(),txtPassword
.getText());
695 txtPassword
.setText(password
);
696 if(username
.isEmpty() || password
.isEmpty()) {
701 private void readPrefLastServerInstance() {
702 IEclipsePreferences preferences
= ConfigurationScope
.INSTANCE
.getNode(STORE_PREFERENCES_NODE
);
703 Preferences lastServerInstancePrefs
= preferences
.node(LAST_SERVER_INSTANCE_NODE
);
705 serverName
= lastServerInstancePrefs
.get(LAST_SERVER_KEY
, null);
706 instanceName
= lastServerInstancePrefs
.get(LAST_INSTANCE_KEY
, null);
709 private void emptyCredentials() {
710 txtLogin
.setText("");
711 txtPassword
.setText("");
714 private String
getUsernamePrefKey() {
715 return selectedCsii
.toString(selectedCdmInstance
.getName(), getPort()) + USERNAME_SUFFIX
;
718 private String
getPasswordPrefKey() {
719 return selectedCsii
.toString(selectedCdmInstance
.getName(), getPort()) + PASSWORD_SUFFIX
;
722 private boolean validateLogin() {
723 if(getUsername() == null || getUsername().isEmpty()) {
724 setMessage("User login cannot be empty");
727 if(getPassword() == null || getPassword().isEmpty()) {
728 setMessage("Password cannot be empty");
733 public String
getUsername() {
734 return txtLogin
.getText();
737 public String
getPassword() {
738 return txtPassword
.getText();
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) + "...";
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
);
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
);
759 remotingComposite
.layout();
764 public void hide(boolean isHidden
) {
765 if(shlConnect
!= null) {
766 shlConnect
.setVisible(!isHidden
);
769 public void dispose() {
770 if(shlConnect
!= null) {
771 shlConnect
.dispose();
775 public void onComplete() {
776 Display
.getDefault().asyncExec(new Runnable() {
780 persistPrefCredentials();
782 persistPrefLastServerInstance();