Merge branch 'develop' of wp5.e-taxonomy.eu:/var/git/taxeditor 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.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;
37
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;
45
46 /**
47 * @author cmathew
48 * @date 20 Jan 2015
49 *
50 */
51 public class RemotingLoginDialog extends Dialog {
52
53 protected Object result;
54 protected Shell shlConnect;
55 private Text txtPort;
56 private Text txtCdmServerStatus;
57 private Text txtCdmInstanceStatus;
58 private Combo comboCdmServer;
59 private Combo comboCdmInstance;
60 private Button btnConnect;
61
62 private final Map<String, CdmServerInfo> csiiMap = new HashMap<String, CdmServerInfo>();
63
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";
71
72 private CdmServerInfo selectedCsii;
73 private CdmInstanceInfo selectedCdmInstance;
74 private Button btnCdmServerRefresh;
75
76
77 /**
78 * Create the dialog.
79 * @param parent
80 * @param style
81 */
82 public RemotingLoginDialog(Shell parent, int style) {
83 super(parent, style);
84 setText("Login");
85 }
86
87 /**
88 * Open the dialog.
89 * @return the result
90 */
91 public Object open() {
92 ICdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
93 if(devRemoteSource != null) {
94 connect(devRemoteSource);
95 } else {
96 createContents();
97 populateCdmServerCombo();
98 shlConnect.open();
99 shlConnect.layout();
100
101 Display display = getParent().getDisplay();
102
103 while (!shlConnect.isDisposed()) {
104 if (!display.readAndDispatch()) {
105 display.sleep();
106 }
107 }
108 }
109 return result;
110 }
111
112 /**
113 * Create contents of the dialog.
114 */
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));
120
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);
125
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);
130
131 Composite cdmServerComposite = new Composite(remotingComposite, SWT.NONE);
132 cdmServerComposite.setLayout(new GridLayout(6, false));
133
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));
138
139 comboCdmServer = new Combo(cdmServerComposite, SWT.READ_ONLY);
140 comboCdmServer.addSelectionListener(new SelectionAdapter() {
141 @Override
142 public void widgetSelected(SelectionEvent e) {
143 refreshCdmServer();
144 }
145 });
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);
150
151 Label lblPortSeparator = new Label(cdmServerComposite, SWT.NONE);
152 lblPortSeparator.setText(" : ");
153
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);
158
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);
165
166 btnCdmServerRefresh = new Button(cdmServerComposite, SWT.NONE);
167 btnCdmServerRefresh.addSelectionListener(new SelectionAdapter() {
168 @Override
169 public void widgetSelected(SelectionEvent e) {
170 refreshCdmServer();
171 }
172 });
173 btnCdmServerRefresh.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
174 btnCdmServerRefresh.setText("Refresh");
175
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));
182
183 comboCdmInstance = new Combo(cdmServerComposite, SWT.READ_ONLY);
184 comboCdmInstance.addSelectionListener(new SelectionAdapter() {
185 @Override
186 public void widgetSelected(SelectionEvent e) {
187 updateSelectedCdmInstance();
188 checkSelectedCdmServerInstance();
189 }
190 });
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);
195
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);
202
203 Button btnCdmInstanceRefresh = new Button(cdmServerComposite, SWT.FLAT);
204 btnCdmInstanceRefresh.addSelectionListener(new SelectionAdapter() {
205 @Override
206 public void widgetSelected(SelectionEvent e) {
207 refreshCdmInstance();
208 }
209 });
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);
216
217 btnConnect = new Button(cdmServerComposite, SWT.FLAT);
218 btnConnect.addMouseListener(new MouseAdapter() {
219 @Override
220 public void mouseUp(MouseEvent e) {
221 int selInstanceIndex = comboCdmInstance.getSelectionIndex();
222 String instance = comboCdmInstance.getItem(selInstanceIndex);
223 checkSelectedCdmServerInstance();
224 connect(selectedCsii.getCdmRemoteSource(selectedCdmInstance));
225 }
226 });
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);
235
236 }
237
238 private void populateCdmServerCombo() {
239 Job job = new Job("Retrieve Server Instances") {
240 @Override
241 protected IStatus run(IProgressMonitor monitor) {
242 Display.getDefault().asyncExec(new Runnable() {
243 @Override
244 public void run() {
245 for(CdmServerInfo csii : CdmServerInfo.getCdmServers()) {
246 csiiMap.put(csii.getName(), csii);
247 comboCdmServer.add(csii.getName());
248 }
249 comboCdmServer.select(0);
250 refreshCdmServer();
251 }
252 });
253 return Status.OK_STATUS;
254 }
255 };
256 job.schedule();
257 }
258
259
260 private void refreshCdmServer() {
261 txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
262 updateSelectedCdmServer();
263 updatePort();
264 checkSelectedCdmServer();
265 }
266
267 private void updateSelectedCdmServer() {
268 int selIndex = comboCdmServer.getSelectionIndex();
269 if(selIndex != -1) {
270 selectedCsii = csiiMap.get(comboCdmServer.getItem(selIndex));
271 }
272 }
273
274 private void updatePort() {
275 txtPort.setText("");
276 if(selectedCsii != null) {
277 txtPort.setText(String.valueOf(selectedCsii.getPort()));
278 }
279 }
280
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);
288 } else {
289 txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
290 comboCdmInstance.removeAll();
291 }
292 }
293 }
294
295
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") {
302 @Override
303 protected IStatus run(IProgressMonitor monitor) {
304 try {
305 if(selectedCsii != null) {
306 if(forceRefresh) {
307 selectedCsii.refreshInstances();
308 }
309 final List<CdmInstanceInfo> instances = selectedCsii.getInstances();
310 Display.getDefault().asyncExec(new Runnable() {
311 @Override
312 public void run() {
313 if(!instances.isEmpty()) {
314 for(CdmInstanceInfo cdmInstance : instances) {
315 comboCdmInstance.add(cdmInstance.getName());
316 }
317 comboCdmInstance.select(0);
318 updateSelectedCdmInstance();
319 checkSelectedCdmServerInstance();
320 comboCdmInstance.setEnabled(true);
321 } else {
322 txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
323 btnConnect.setEnabled(false);
324 }
325 }
326 });
327 }
328 } catch (CDMServerException e) {
329 MessagingUtils.warn(getClass(), e);
330 Display.getDefault().asyncExec(new Runnable() {
331 @Override
332 public void run() {
333 txtCdmInstanceStatus.setText(STATUS_REMOTING_NOT_ACTIVATED);
334 comboCdmInstance.setEnabled(false);
335 btnConnect.setEnabled(false);
336 }
337 });
338 }
339 return Status.OK_STATUS;
340 }
341 };
342
343 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
344 // Start the Job
345 job.schedule();
346 }
347
348 }
349
350 private void refreshCdmInstance() {
351 txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
352 updateSelectedCdmInstance();
353 checkSelectedCdmServerInstance();
354 }
355
356 private void updateSelectedCdmInstance() {
357 int selIndex = comboCdmInstance.getSelectionIndex();
358 if(selIndex != -1) {
359 selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
360 }
361 }
362
363 private void checkSelectedCdmServerInstance() {
364 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
365 try {
366 if(selectedCsii.pingInstance(selectedCdmInstance)) {
367 txtCdmInstanceStatus.setText(STATUS_AVAILABLE);
368 btnConnect.setEnabled(true);
369 } else {
370 txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
371 btnConnect.setEnabled(false);
372 }
373 } catch (Exception e) {
374 txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
375 MessagingUtils.warn(this.getClass(), e);
376 }
377 }
378
379 }
380
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.");
384 return;
385 }
386
387 try {
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
391 // if any problems
392 throw new RuntimeException(e);
393 }
394
395 if(shlConnect != null) {
396 shlConnect.dispose();
397 }
398 }
399
400
401 }