|
1 /* |
|
2 * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Handler for connect command |
|
17 * |
|
18 */ |
|
19 package com.nokia.traceviewer.action; |
|
20 |
|
21 import java.net.URL; |
|
22 import java.util.Iterator; |
|
23 import java.util.List; |
|
24 |
|
25 import org.eclipse.jface.preference.IPreferenceStore; |
|
26 import org.eclipse.jface.resource.ImageDescriptor; |
|
27 import org.eclipse.swt.widgets.Display; |
|
28 import org.eclipse.ui.PlatformUI; |
|
29 |
|
30 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator; |
|
31 import com.nokia.carbide.remoteconnections.interfaces.IConnection; |
|
32 import com.nokia.traceviewer.TraceViewerHelpContextIDs; |
|
33 import com.nokia.traceviewer.TraceViewerPlugin; |
|
34 import com.nokia.traceviewer.api.TraceViewerAPI.TVAPIError; |
|
35 import com.nokia.traceviewer.engine.ConnectionHelper; |
|
36 import com.nokia.traceviewer.engine.TraceProvider; |
|
37 import com.nokia.traceviewer.engine.TraceViewerGlobals; |
|
38 import com.nokia.traceviewer.engine.TraceViewerDialogInterface.TVPreferencePage; |
|
39 import com.nokia.traceviewer.engine.preferences.PreferenceConstants; |
|
40 |
|
41 /** |
|
42 * Handler for connect command |
|
43 */ |
|
44 public final class ConnectAction extends TraceViewerAction { |
|
45 |
|
46 /** |
|
47 * Connect image |
|
48 */ |
|
49 private static ImageDescriptor connectImage; |
|
50 |
|
51 /** |
|
52 * Disconnect image |
|
53 */ |
|
54 private static ImageDescriptor disConnectImage; |
|
55 |
|
56 /** |
|
57 * When set to false, don't show protocol change dialog anymore |
|
58 */ |
|
59 private boolean showProtocolChangeDialog = true; |
|
60 |
|
61 /** |
|
62 * Last operation's error code |
|
63 */ |
|
64 private TVAPIError operationErrorCode; |
|
65 |
|
66 /** |
|
67 * Show errors when connecting |
|
68 */ |
|
69 private boolean showErrors = true; |
|
70 |
|
71 /** |
|
72 * When set to false, don't check if given preferences match the one's in |
|
73 * Remote connections |
|
74 */ |
|
75 private boolean checkPreferences = true; |
|
76 |
|
77 /** |
|
78 * Check preferences error |
|
79 */ |
|
80 private static final String CHECK_PREFERENCES_ERROR = com.nokia.traceviewer.action.Messages |
|
81 .getString("ConnectAction.ConnectionPreferencesError"); //$NON-NLS-1$ |
|
82 |
|
83 /** |
|
84 * Connection not supported error |
|
85 */ |
|
86 private static final String CONNECTION_NOT_SUPPORTED_ERROR = Messages |
|
87 .getString("ConnectAction.ConnectionNotSupported"); //$NON-NLS-1$ |
|
88 |
|
89 static { |
|
90 URL url = null; |
|
91 URL url2 = null; |
|
92 url = TraceViewerPlugin.getDefault().getBundle().getEntry( |
|
93 "/icons/connect.gif"); //$NON-NLS-1$ |
|
94 connectImage = ImageDescriptor.createFromURL(url); |
|
95 url2 = TraceViewerPlugin.getDefault().getBundle().getEntry( |
|
96 "/icons/disconnect.gif"); //$NON-NLS-1$ |
|
97 disConnectImage = ImageDescriptor.createFromURL(url2); |
|
98 } |
|
99 |
|
100 /** |
|
101 * Constructor |
|
102 */ |
|
103 ConnectAction() { |
|
104 |
|
105 // Check which action to create |
|
106 if (TraceViewerGlobals.getTraceViewer().getConnection() != null |
|
107 && TraceViewerGlobals.getTraceViewer().getConnection() |
|
108 .isConnected()) { |
|
109 changeToDisconnectAction(); |
|
110 } else { |
|
111 changeToConnectAction(); |
|
112 } |
|
113 |
|
114 // Set help |
|
115 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, |
|
116 TraceViewerHelpContextIDs.ACTIONS); |
|
117 } |
|
118 |
|
119 /** |
|
120 * Connects to the target |
|
121 * |
|
122 * @param showErrors |
|
123 * if true, show errors if there are any |
|
124 * @param checkPreferences |
|
125 * if true, check if preferences match the ones in the selected |
|
126 * Remote Connection |
|
127 * @return TVAPI error code |
|
128 */ |
|
129 public TVAPIError connect(boolean showErrors, boolean checkPreferences) { |
|
130 this.showErrors = showErrors; |
|
131 this.checkPreferences = checkPreferences; |
|
132 |
|
133 // Call doRun instead of run() to not care if TraceProvider is |
|
134 // registered yet or not. |
|
135 doRun(); |
|
136 this.showErrors = true; |
|
137 this.checkPreferences = true; |
|
138 |
|
139 return operationErrorCode; |
|
140 } |
|
141 |
|
142 /** |
|
143 * Disconnects from the target |
|
144 * |
|
145 * @return true if disconnection succeeded, false otherwise |
|
146 */ |
|
147 public boolean disconnect() { |
|
148 |
|
149 // Call doRun instead of run() to not care if TraceProvider is |
|
150 // registered yet or not |
|
151 doRun(); |
|
152 boolean succeed = false; |
|
153 if (operationErrorCode == TVAPIError.NONE) { |
|
154 succeed = true; |
|
155 } |
|
156 return succeed; |
|
157 } |
|
158 |
|
159 /* |
|
160 * (non-Javadoc) |
|
161 * |
|
162 * @see com.nokia.traceviewer.action.TraceViewerAction#doRun() |
|
163 */ |
|
164 @Override |
|
165 protected void doRun() { |
|
166 TraceViewerGlobals.postUiEvent("ConnectButton", "1"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
167 TVAPIError errorCode; |
|
168 |
|
169 boolean connected = (TraceViewerGlobals.getTraceViewer() |
|
170 .getConnection() != null && TraceViewerGlobals.getTraceViewer() |
|
171 .getConnection().isConnected()); |
|
172 // Connect |
|
173 if (!connected) { |
|
174 errorCode = doConnect(); |
|
175 // Disconnect |
|
176 } else { |
|
177 errorCode = doDisconnect(); |
|
178 } |
|
179 operationErrorCode = errorCode; |
|
180 TraceViewerGlobals.postUiEvent("ConnectButton", "0"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
181 } |
|
182 |
|
183 /** |
|
184 * Do connect. Must be called from an UI thread! |
|
185 * |
|
186 * @return TVAPI error code |
|
187 */ |
|
188 private TVAPIError doConnect() { |
|
189 TVAPIError errorCode = TVAPIError.INVALID_CONNECTION_PARAMETERS; |
|
190 |
|
191 boolean canConnect = true; |
|
192 |
|
193 // Check that a connection preferences are set. If not, open preferences |
|
194 // page. Doesn't open preferences page if "current connection" is |
|
195 // selected. |
|
196 canConnect = checkConnectionPreferences(); |
|
197 |
|
198 // Ensure connection before connecting |
|
199 ConnectionHelper.saveConnectionSettingsToPreferenceStore(true); |
|
200 |
|
201 // Check used protocol if user want's to change it |
|
202 if (canConnect) { |
|
203 canConnect = checkUsedProtocol(); |
|
204 |
|
205 // If the main DataReader isn't active, ask to create it again |
|
206 // and erase old data |
|
207 if (canConnect |
|
208 && TraceViewerGlobals.getTraceViewer() |
|
209 .getDataProcessorAccess().getLogger() |
|
210 .isLogFileOpened()) { |
|
211 canConnect = handleCreatingNewDataReader(); |
|
212 } |
|
213 } |
|
214 |
|
215 // Try to connect |
|
216 if (canConnect) { |
|
217 boolean success = TraceViewerGlobals.getTraceViewer().connect(); |
|
218 if (success) { |
|
219 changeToDisconnectAction(); |
|
220 errorCode = TVAPIError.NONE; |
|
221 |
|
222 } else if (!success && showErrors) { |
|
223 TraceViewerGlobals.getTraceViewer().getDialogs() |
|
224 .showErrorMessage(CHECK_PREFERENCES_ERROR); |
|
225 } |
|
226 |
|
227 // User canceled the connection |
|
228 } else { |
|
229 errorCode = TVAPIError.USER_CANCELED; |
|
230 } |
|
231 return errorCode; |
|
232 } |
|
233 |
|
234 /** |
|
235 * Checks connection preferences |
|
236 * |
|
237 * @return true if connection is selected, false otherwise |
|
238 */ |
|
239 private boolean checkConnectionPreferences() { |
|
240 boolean connectionSettingsDefined = true; |
|
241 IPreferenceStore store = TraceViewerPlugin.getDefault() |
|
242 .getPreferenceStore(); |
|
243 |
|
244 // Open preferences page |
|
245 if (store.getString(PreferenceConstants.CONNECTION_TYPE).equals("") || !preferencesMatchSelectedConnection(store)) { //$NON-NLS-1$ |
|
246 connectionSettingsDefined = TraceViewerGlobals.getTraceViewer() |
|
247 .getDialogs().openPreferencePage( |
|
248 TVPreferencePage.CONNECTION); |
|
249 |
|
250 // If user clicked OK, check that some connection is selected |
|
251 if (connectionSettingsDefined) { |
|
252 if (store.getString(PreferenceConstants.CONNECTION_TYPE) |
|
253 .equals("")) { //$NON-NLS-1$ |
|
254 connectionSettingsDefined = false; |
|
255 } |
|
256 } |
|
257 } |
|
258 return connectionSettingsDefined; |
|
259 } |
|
260 |
|
261 /** |
|
262 * Checks that set preferences match selected connections preferences |
|
263 * |
|
264 * @param store |
|
265 * preference store |
|
266 * @return true if set preferences match the parameters in selected Remote |
|
267 * Connection |
|
268 */ |
|
269 private boolean preferencesMatchSelectedConnection(IPreferenceStore store) { |
|
270 boolean matches = false; |
|
271 if (!checkPreferences || ConnectionHelper.isCurrentConnectionSelected) { |
|
272 matches = true; |
|
273 } else { |
|
274 String selectedIdentifier = store |
|
275 .getString(PreferenceConstants.SELECTED_CONNECTION_ID); |
|
276 |
|
277 // Get and compare the parameters from currently selected connection |
|
278 Iterator<IConnection> connections = RemoteConnectionsActivator |
|
279 .getConnectionsManager().getConnections().iterator(); |
|
280 while (connections.hasNext()) { |
|
281 IConnection conn = connections.next(); |
|
282 |
|
283 // First find the right connection with the identifier |
|
284 if (conn.getIdentifier().equals(selectedIdentifier)) { |
|
285 String connectionTypeId = conn.getConnectionType() |
|
286 .getIdentifier(); |
|
287 String connectionType = store |
|
288 .getString(PreferenceConstants.CONNECTION_TYPE); |
|
289 boolean isMusti = connectionTypeId |
|
290 .equals("com.nokia.carbide.trk.support.connection.TCPIPConnectionType"); //$NON-NLS-1$ |
|
291 |
|
292 boolean isPlatsim = connectionTypeId |
|
293 .equals("com.nokia.carbide.trk.support.connection.PlatSimConnectionType"); //$NON-NLS-1$ |
|
294 |
|
295 // TCP / IP connection |
|
296 if (isMusti || isPlatsim) { |
|
297 String address = conn.getSettings().get("ipAddress"); //$NON-NLS-1$ |
|
298 String port = conn.getSettings().get("port"); //$NON-NLS-1$ |
|
299 |
|
300 // Check that connection type, address and port match |
|
301 if (connectionType |
|
302 .equals(PreferenceConstants.CONNECTION_TYPE_TCPIP) |
|
303 && address |
|
304 .equals(store |
|
305 .getString(PreferenceConstants.IP_ADDRESS)) |
|
306 && port |
|
307 .equals(store |
|
308 .getString(PreferenceConstants.TCPIP_PORT))) { |
|
309 matches = true; |
|
310 break; |
|
311 } |
|
312 |
|
313 // USB connection |
|
314 } else if (connectionTypeId |
|
315 .equals("com.nokia.carbide.trk.support.connection.USBConnectionType")) { //$NON-NLS-1$ |
|
316 String portNumStr = conn.getSettings().get("port"); //$NON-NLS-1$ |
|
317 if (connectionType |
|
318 .equals(PreferenceConstants.CONNECTION_TYPE_USB_SERIAL) |
|
319 && portNumStr |
|
320 .equals(store |
|
321 .getString(PreferenceConstants.USB_SERIAL_COM_PORT))) { |
|
322 matches = true; |
|
323 break; |
|
324 } |
|
325 } |
|
326 } |
|
327 } |
|
328 } |
|
329 return matches; |
|
330 } |
|
331 |
|
332 /** |
|
333 * Do disconnect |
|
334 * |
|
335 * @return TVAPI error code |
|
336 */ |
|
337 private TVAPIError doDisconnect() { |
|
338 TVAPIError errorCode; |
|
339 boolean success = TraceViewerGlobals.getTraceViewer().disconnect(); |
|
340 if (success) { |
|
341 errorCode = TVAPIError.NONE; |
|
342 changeToConnectAction(); |
|
343 |
|
344 } else { |
|
345 // Disconnecting failed error |
|
346 errorCode = TVAPIError.DISCONNECTING_FAILED; |
|
347 } |
|
348 return errorCode; |
|
349 } |
|
350 |
|
351 /** |
|
352 * Checks used media protocol |
|
353 * |
|
354 * @return true if TraceViewer can still continue connecting. False if |
|
355 * currently selected TraceProvider doesn't support connecting. |
|
356 */ |
|
357 private boolean checkUsedProtocol() { |
|
358 boolean canConnect = true; |
|
359 |
|
360 // First check there are more than one TraceProviders |
|
361 if (TraceViewerGlobals.getListOfTraceProviders().size() > 1 |
|
362 && showProtocolChangeDialog) { |
|
363 |
|
364 IPreferenceStore store = TraceViewerPlugin.getDefault() |
|
365 .getPreferenceStore(); |
|
366 String selectedConnectionType = store |
|
367 .getString(PreferenceConstants.CONNECTION_TYPE); |
|
368 |
|
369 // Check if current TraceProvider has selected connection type as |
|
370 // preferred |
|
371 String preferredConnectionType = TraceViewerGlobals |
|
372 .getTraceProvider().getPreferredConnectionType(); |
|
373 |
|
374 // If current TraceProvider returns not supported as preferred |
|
375 // connection type, |
|
376 if (preferredConnectionType |
|
377 .equals(PreferenceConstants.CONNECTION_TYPE_NOT_SUPPORTED)) { |
|
378 canConnect = false; |
|
379 } |
|
380 |
|
381 // Connection type doesn't match, find other TraceProvider which has |
|
382 // the selected connection type as preferred |
|
383 if (!preferredConnectionType.equals(selectedConnectionType)) { |
|
384 List<TraceProvider> traceProviders = TraceViewerGlobals |
|
385 .getListOfTraceProviders(); |
|
386 |
|
387 // Go through list of TraceProviders |
|
388 for (int i = 0; i < traceProviders.size(); i++) { |
|
389 TraceProvider newProvider = traceProviders.get(i); |
|
390 if (newProvider.getPreferredConnectionType().equals( |
|
391 selectedConnectionType)) { |
|
392 |
|
393 // Create human readable change message |
|
394 String selectedConnectionHuman = Messages |
|
395 .getString("ConnectAction.USBSerialMsg"); //$NON-NLS-1$ |
|
396 if (selectedConnectionType |
|
397 .equals(PreferenceConstants.CONNECTION_TYPE_TCPIP)) { |
|
398 selectedConnectionHuman = Messages |
|
399 .getString("ConnectAction.TCPIPMsg"); //$NON-NLS-1$ |
|
400 } |
|
401 String changeMsgStart = Messages |
|
402 .getString("ConnectAction.ChangeDataFormatMsg1"); //$NON-NLS-1$ |
|
403 String changeMsgEnd = Messages |
|
404 .getString("ConnectAction.ChangeDataFormatMsg2"); //$NON-NLS-1$ |
|
405 |
|
406 String confirmationMsg = selectedConnectionHuman |
|
407 + changeMsgStart + newProvider.getName() |
|
408 + changeMsgEnd; |
|
409 |
|
410 // Ask the user if the TraceProvider should be changed |
|
411 boolean ret = TraceViewerGlobals.getTraceViewer() |
|
412 .getDialogs().showConfirmationDialog( |
|
413 confirmationMsg); |
|
414 |
|
415 // Change TraceProvider |
|
416 if (ret) { |
|
417 store.setValue(PreferenceConstants.DATA_FORMAT, |
|
418 newProvider.getName()); |
|
419 TraceViewerGlobals.getTraceViewer().clearAllData(); |
|
420 TraceViewerGlobals.setTraceProvider(newProvider, |
|
421 true); |
|
422 canConnect = true; |
|
423 } else { |
|
424 showProtocolChangeDialog = false; |
|
425 } |
|
426 break; |
|
427 } |
|
428 } |
|
429 } |
|
430 } |
|
431 |
|
432 // Current TraceProvider doesn't support connecting |
|
433 if (!canConnect && showErrors) { |
|
434 TraceViewerGlobals.getTraceViewer().getDialogs().showErrorMessage( |
|
435 TraceViewerGlobals.getTraceProvider().getName() |
|
436 + CONNECTION_NOT_SUPPORTED_ERROR); |
|
437 } |
|
438 |
|
439 return canConnect; |
|
440 } |
|
441 |
|
442 /** |
|
443 * Handles creation of new main DataReader |
|
444 * |
|
445 * @return true if new main DataReader was created |
|
446 */ |
|
447 private boolean handleCreatingNewDataReader() { |
|
448 boolean canConnect; |
|
449 String confirmationMsg = Messages |
|
450 .getString("ConnectAction.EraseLogConfirmation"); //$NON-NLS-1$; |
|
451 |
|
452 boolean ret = TraceViewerGlobals.getTraceViewer().getDialogs() |
|
453 .showConfirmationDialog(confirmationMsg); |
|
454 |
|
455 // Ok |
|
456 if (ret) { |
|
457 // Create new main DataReader |
|
458 // Unpause if paused |
|
459 if (TraceViewerGlobals.getTraceViewer().getDataReaderAccess() |
|
460 .getMainDataReader().isPaused()) { |
|
461 TraceViewerGlobals.getTraceViewer().getView() |
|
462 .getActionFactory().getPauseAction().run(); |
|
463 } |
|
464 TraceViewerGlobals.getTraceViewer().clearAllData(); |
|
465 TraceViewerGlobals.getTraceViewer().getView().updateViewName(); |
|
466 canConnect = true; |
|
467 } else { |
|
468 canConnect = false; |
|
469 } |
|
470 return canConnect; |
|
471 } |
|
472 |
|
473 /** |
|
474 * Changes action to connect action |
|
475 */ |
|
476 public void changeToConnectAction() { |
|
477 |
|
478 if (Display.getCurrent() != null) { |
|
479 setImageDescriptor(connectImage); |
|
480 setText(Messages.getString("ConnectAction.Title")); //$NON-NLS-1$ |
|
481 changeConnectToolTip(); |
|
482 } else { |
|
483 Display.getDefault().syncExec(new Runnable() { |
|
484 |
|
485 /* |
|
486 * (non-Javadoc) |
|
487 * |
|
488 * @see java.lang.Runnable#run() |
|
489 */ |
|
490 public void run() { |
|
491 setImageDescriptor(connectImage); |
|
492 setText(Messages.getString("ConnectAction.Title")); //$NON-NLS-1$ |
|
493 changeConnectToolTip(); |
|
494 } |
|
495 |
|
496 }); |
|
497 } |
|
498 } |
|
499 |
|
500 /** |
|
501 * Changes action to disconnect action |
|
502 */ |
|
503 public void changeToDisconnectAction() { |
|
504 if (Display.getCurrent() != null) { |
|
505 setImageDescriptor(disConnectImage); |
|
506 setText(Messages.getString("DisconnectAction.Title")); //$NON-NLS-1$ |
|
507 changeConnectToolTip(); |
|
508 } else { |
|
509 Display.getDefault().syncExec(new Runnable() { |
|
510 |
|
511 /* |
|
512 * (non-Javadoc) |
|
513 * |
|
514 * @see java.lang.Runnable#run() |
|
515 */ |
|
516 public void run() { |
|
517 setImageDescriptor(disConnectImage); |
|
518 setText(Messages.getString("DisconnectAction.Title")); //$NON-NLS-1$ |
|
519 changeConnectToolTip(); |
|
520 } |
|
521 |
|
522 }); |
|
523 } |
|
524 } |
|
525 |
|
526 /** |
|
527 * Changes connect button tooltip if needed |
|
528 */ |
|
529 public void changeConnectToolTip() { |
|
530 IPreferenceStore store = TraceViewerPlugin.getDefault() |
|
531 .getPreferenceStore(); |
|
532 |
|
533 boolean connected = (TraceViewerGlobals.getTraceViewer() |
|
534 .getConnection() != null && TraceViewerGlobals.getTraceViewer() |
|
535 .getConnection().isConnected()); |
|
536 |
|
537 // Start with either connect or disconnect |
|
538 String toolTipText; |
|
539 if (!connected) { |
|
540 toolTipText = Messages.getString("ConnectAction.Tooltip"); //$NON-NLS-1$ |
|
541 } else { |
|
542 toolTipText = Messages.getString("DisconnectAction.Tooltip"); //$NON-NLS-1$ |
|
543 } |
|
544 |
|
545 // Construct the tooltip for connect action |
|
546 String connName = store |
|
547 .getString(PreferenceConstants.SELECTED_CONNECTION_NAME); |
|
548 |
|
549 // "Current connection" selected or already connected |
|
550 if (ConnectionHelper.isCurrentConnectionSelected || connected) { |
|
551 String curText = Messages.getString("ConnectAction.TooltipCurr"); //$NON-NLS-1$ |
|
552 IConnection currentConnection = RemoteConnectionsActivator |
|
553 .getConnectionsManager().getCurrentConnection(); |
|
554 if (currentConnection != null) { |
|
555 curText = currentConnection.getDisplayName(); |
|
556 } else if (connected) { |
|
557 curText = connName; |
|
558 } |
|
559 toolTipText = toolTipText + curText; |
|
560 } |
|
561 |
|
562 // No connection selected |
|
563 else if (connName.equals("")) { //$NON-NLS-1$ |
|
564 toolTipText = toolTipText |
|
565 + Messages.getString("ConnectAction.Tooltip2"); //$NON-NLS-1$ |
|
566 |
|
567 // Static connection selected |
|
568 } else { |
|
569 toolTipText = toolTipText + connName; |
|
570 } |
|
571 setToolTipText(toolTipText); |
|
572 } |
|
573 } |