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
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.MouseAdapter
;
22 import org
.eclipse
.swt
.events
.MouseEvent
;
23 import org
.eclipse
.swt
.events
.SelectionAdapter
;
24 import org
.eclipse
.swt
.events
.SelectionEvent
;
25 import org
.eclipse
.swt
.layout
.FillLayout
;
26 import org
.eclipse
.swt
.layout
.GridData
;
27 import org
.eclipse
.swt
.layout
.GridLayout
;
28 import org
.eclipse
.swt
.widgets
.Button
;
29 import org
.eclipse
.swt
.widgets
.Combo
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Dialog
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.swt
.widgets
.Label
;
34 import org
.eclipse
.swt
.widgets
.Shell
;
35 import org
.eclipse
.swt
.widgets
.Text
;
36 import org
.eclipse
.wb
.swt
.SWTResourceManager
;
38 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
39 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
40 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerException
;
41 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmServerInfo
;
42 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmServerInfo
.CdmInstanceInfo
;
43 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
44 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
51 public class RemotingLoginDialog
extends Dialog
{
53 protected Object result
;
54 protected Shell shlConnect
;
56 private Text txtCdmServerStatus
;
57 private Text txtCdmInstanceStatus
;
58 private Combo comboCdmServer
;
59 private Combo comboCdmInstance
;
60 private Button btnConnect
;
62 private final Map
<String
, CdmServerInfo
> csiiMap
= new HashMap
<String
, CdmServerInfo
>();
64 private final String STATUS_AVAILABLE
= "Available";
65 private final String STATUS_NOT_AVAILABLE
= "Not Available";
66 private final String STATUS_RETRIEVING
= "Retrieving ...";
67 private final String STATUS_CHECKING_AVAILABILITY
= "Checking ...";
68 private final String STATUS_NO_INSTANCES
= "No Instances Found";
69 private final String STATUS_ERROR
= "Error";
70 private final String STATUS_REMOTING_NOT_ACTIVATED
= "Remoting not activated";
72 private CdmServerInfo selectedCsii
;
73 private CdmInstanceInfo selectedCdmInstance
;
74 private Button btnCdmServerRefresh
;
82 public RemotingLoginDialog(Shell parent
, int style
) {
91 public Object
open() {
92 ICdmRemoteSource devRemoteSource
= CdmServerInfo
.getDevServerRemoteSource();
93 if(devRemoteSource
!= null) {
94 connect(devRemoteSource
);
97 populateCdmServerCombo();
101 Display display
= getParent().getDisplay();
103 while (!shlConnect
.isDisposed()) {
104 if (!display
.readAndDispatch()) {
113 * Create contents of the dialog.
115 private void createContents() {
116 shlConnect
= new Shell(getParent(), getStyle());
117 shlConnect
.setSize(490, 157);
118 shlConnect
.setText("Connect");
119 shlConnect
.setLayout(new FillLayout(SWT
.HORIZONTAL
));
121 Composite composite
= new Composite(shlConnect
, SWT
.NONE
);
122 FillLayout fl_composite
= new FillLayout(SWT
.VERTICAL
);
123 fl_composite
.spacing
= 5;
124 composite
.setLayout(fl_composite
);
126 Composite remotingComposite
= new Composite(composite
, SWT
.NONE
);
127 FillLayout fl_remotingComposite
= new FillLayout(SWT
.VERTICAL
);
128 fl_remotingComposite
.spacing
= 5;
129 remotingComposite
.setLayout(fl_remotingComposite
);
131 Composite cdmServerComposite
= new Composite(remotingComposite
, SWT
.NONE
);
132 cdmServerComposite
.setLayout(new GridLayout(6, false));
134 Label lblCdmServer
= new Label(cdmServerComposite
, SWT
.NONE
);
135 lblCdmServer
.setText("CDM Server : ");
136 lblCdmServer
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
137 lblCdmServer
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
139 comboCdmServer
= new Combo(cdmServerComposite
, SWT
.READ_ONLY
);
140 comboCdmServer
.addSelectionListener(new SelectionAdapter() {
142 public void widgetSelected(SelectionEvent e
) {
146 GridData gd_comboCdmServer
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
147 gd_comboCdmServer
.widthHint
= 150;
148 comboCdmServer
.setLayoutData(gd_comboCdmServer
);
149 comboCdmServer
.select(0);
151 Label lblPortSeparator
= new Label(cdmServerComposite
, SWT
.NONE
);
152 lblPortSeparator
.setText(" : ");
154 txtPort
= new Text(cdmServerComposite
, SWT
.BORDER
);
155 GridData gd_txtPort
= new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 1, 1);
156 gd_txtPort
.widthHint
= 50;
157 txtPort
.setLayoutData(gd_txtPort
);
159 txtCdmServerStatus
= new Text(cdmServerComposite
, SWT
.BORDER
);
160 txtCdmServerStatus
.setBackground(SWTResourceManager
.getColor(SWT
.COLOR_INFO_BACKGROUND
));
161 txtCdmServerStatus
.setEditable(false);
162 GridData gd_txtCdmServerStatus
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1);
163 gd_txtCdmServerStatus
.widthHint
= 100;
164 txtCdmServerStatus
.setLayoutData(gd_txtCdmServerStatus
);
166 btnCdmServerRefresh
= new Button(cdmServerComposite
, SWT
.NONE
);
167 btnCdmServerRefresh
.addSelectionListener(new SelectionAdapter() {
169 public void widgetSelected(SelectionEvent e
) {
173 btnCdmServerRefresh
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 1, 1));
174 btnCdmServerRefresh
.setText("Refresh");
176 Label lblCdmInstance
= new Label(cdmServerComposite
, SWT
.NONE
);
177 GridData gd_lblCdmInstance
= new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1);
178 gd_lblCdmInstance
.heightHint
= 30;
179 lblCdmInstance
.setLayoutData(gd_lblCdmInstance
);
180 lblCdmInstance
.setText("CDM Instance : ");
181 lblCdmInstance
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
183 comboCdmInstance
= new Combo(cdmServerComposite
, SWT
.READ_ONLY
);
184 comboCdmInstance
.addSelectionListener(new SelectionAdapter() {
186 public void widgetSelected(SelectionEvent e
) {
187 updateSelectedCdmInstance();
188 checkSelectedCdmServerInstance();
191 GridData gd_comboCdmInstance
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
192 gd_comboCdmInstance
.widthHint
= 150;
193 comboCdmInstance
.setLayoutData(gd_comboCdmInstance
);
194 comboCdmInstance
.select(0);
196 txtCdmInstanceStatus
= new Text(cdmServerComposite
, SWT
.BORDER
);
197 txtCdmInstanceStatus
.setBackground(SWTResourceManager
.getColor(SWT
.COLOR_INFO_BACKGROUND
));
198 txtCdmInstanceStatus
.setEditable(false);
199 GridData gd_txtCdmInstanceStatus
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 3, 1);
200 gd_txtCdmInstanceStatus
.widthHint
= 100;
201 txtCdmInstanceStatus
.setLayoutData(gd_txtCdmInstanceStatus
);
203 Button btnCdmInstanceRefresh
= new Button(cdmServerComposite
, SWT
.FLAT
);
204 btnCdmInstanceRefresh
.addSelectionListener(new SelectionAdapter() {
206 public void widgetSelected(SelectionEvent e
) {
207 refreshCdmInstance();
210 GridData gd_btnCdmInstanceRefresh
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1);
211 gd_btnCdmInstanceRefresh
.widthHint
= 110;
212 gd_btnCdmInstanceRefresh
.heightHint
= 30;
213 btnCdmInstanceRefresh
.setLayoutData(gd_btnCdmInstanceRefresh
);
214 btnCdmInstanceRefresh
.setText("Refresh");
215 new Label(cdmServerComposite
, SWT
.NONE
);
217 btnConnect
= new Button(cdmServerComposite
, SWT
.FLAT
);
218 btnConnect
.addMouseListener(new MouseAdapter() {
220 public void mouseUp(MouseEvent e
) {
221 int selInstanceIndex
= comboCdmInstance
.getSelectionIndex();
222 String instance
= comboCdmInstance
.getItem(selInstanceIndex
);
223 checkSelectedCdmServerInstance();
224 connect(selectedCsii
.getCdmRemoteSource(selectedCdmInstance
));
227 GridData gd_btnConnect
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1);
228 gd_btnConnect
.heightHint
= 30;
229 btnConnect
.setLayoutData(gd_btnConnect
);
230 btnConnect
.setText("Connect");
231 new Label(cdmServerComposite
, SWT
.NONE
);
232 new Label(cdmServerComposite
, SWT
.NONE
);
233 new Label(cdmServerComposite
, SWT
.NONE
);
234 new Label(cdmServerComposite
, SWT
.NONE
);
238 private void populateCdmServerCombo() {
239 Job job
= new Job("Retrieve Server Instances") {
241 protected IStatus
run(IProgressMonitor monitor
) {
242 Display
.getDefault().asyncExec(new Runnable() {
245 for(CdmServerInfo csii
: CdmServerInfo
.getCdmServers()) {
246 csiiMap
.put(csii
.getName(), csii
);
247 comboCdmServer
.add(csii
.getName());
249 comboCdmServer
.select(0);
253 return Status
.OK_STATUS
;
260 private void refreshCdmServer() {
261 txtCdmServerStatus
.setText(STATUS_CHECKING_AVAILABILITY
);
262 updateSelectedCdmServer();
264 checkSelectedCdmServer();
267 private void updateSelectedCdmServer() {
268 int selIndex
= comboCdmServer
.getSelectionIndex();
270 selectedCsii
= csiiMap
.get(comboCdmServer
.getItem(selIndex
));
274 private void updatePort() {
276 if(selectedCsii
!= null) {
277 txtPort
.setText(String
.valueOf(selectedCsii
.getPort()));
281 private void checkSelectedCdmServer() {
282 int selIndex
= comboCdmServer
.getSelectionIndex();
283 txtCdmInstanceStatus
.setText("");
284 if(selectedCsii
!= null) {
285 if(selectedCsii
.pingServer()) {
286 txtCdmServerStatus
.setText(STATUS_AVAILABLE
);
287 populateCdmInstanceCombo(true);
289 txtCdmServerStatus
.setText(STATUS_NOT_AVAILABLE
);
290 comboCdmInstance
.removeAll();
296 private void populateCdmInstanceCombo(final boolean forceRefresh
) {
297 comboCdmInstance
.removeAll();
298 comboCdmInstance
.setEnabled(false);
299 btnConnect
.setEnabled(false);
300 txtCdmInstanceStatus
.setText(STATUS_RETRIEVING
);
301 Job job
= new Job("Retrieve Server Instances") {
303 protected IStatus
run(IProgressMonitor monitor
) {
305 if(selectedCsii
!= null) {
307 selectedCsii
.refreshInstances();
309 final List
<CdmInstanceInfo
> instances
= selectedCsii
.getInstances();
310 Display
.getDefault().asyncExec(new Runnable() {
313 if(!instances
.isEmpty()) {
314 for(CdmInstanceInfo cdmInstance
: instances
) {
315 comboCdmInstance
.add(cdmInstance
.getName());
317 comboCdmInstance
.select(0);
318 updateSelectedCdmInstance();
319 checkSelectedCdmServerInstance();
320 comboCdmInstance
.setEnabled(true);
322 txtCdmInstanceStatus
.setText(STATUS_NO_INSTANCES
);
323 btnConnect
.setEnabled(false);
328 } catch (CDMServerException e
) {
329 MessagingUtils
.warn(getClass(), e
);
330 Display
.getDefault().asyncExec(new Runnable() {
333 txtCdmInstanceStatus
.setText(STATUS_REMOTING_NOT_ACTIVATED
);
334 comboCdmInstance
.setEnabled(false);
335 btnConnect
.setEnabled(false);
339 return Status
.OK_STATUS
;
343 if(txtCdmServerStatus
.getText().equals(STATUS_AVAILABLE
)) {
350 private void refreshCdmInstance() {
351 txtCdmInstanceStatus
.setText(STATUS_CHECKING_AVAILABILITY
);
352 updateSelectedCdmInstance();
353 checkSelectedCdmServerInstance();
356 private void updateSelectedCdmInstance() {
357 int selIndex
= comboCdmInstance
.getSelectionIndex();
359 selectedCdmInstance
= selectedCsii
.getInstanceFromName(comboCdmInstance
.getItem(selIndex
));
363 private void checkSelectedCdmServerInstance() {
364 if(txtCdmServerStatus
.getText().equals(STATUS_AVAILABLE
)) {
366 if(selectedCsii
.pingInstance(selectedCdmInstance
)) {
367 txtCdmInstanceStatus
.setText(STATUS_AVAILABLE
);
368 btnConnect
.setEnabled(true);
370 txtCdmInstanceStatus
.setText(STATUS_NOT_AVAILABLE
);
371 btnConnect
.setEnabled(false);
373 } catch (Exception e
) {
374 txtCdmInstanceStatus
.setText(STATUS_NOT_AVAILABLE
);
375 MessagingUtils
.warn(this.getClass(), e
);
381 private void connect(ICdmRemoteSource source
) {
382 if(CdmStore
.isConnecting()){
383 MessagingUtils
.warningDialog("Already connecting", this, "You are currently connecting to a different CDM Instance already.");
388 CdmDataSourceRepository
.changeDataSource(source
);
389 } catch (Exception e
) {
390 // Do not expect anything to go wrong at this point, so we throw a runtime exception
392 throw new RuntimeException(e
);
395 if(shlConnect
!= null) {
396 shlConnect
.dispose();