|
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 * Connection Creator based on preferences |
|
17 * |
|
18 */ |
|
19 package com.nokia.traceviewer.engine; |
|
20 |
|
21 import org.eclipse.jface.preference.IPreferenceStore; |
|
22 |
|
23 import com.nokia.traceviewer.TraceViewerPlugin; |
|
24 import com.nokia.traceviewer.engine.preferences.PreferenceConstants; |
|
25 |
|
26 /** |
|
27 * Creates and returns a connection based on preferences |
|
28 * |
|
29 */ |
|
30 public final class ConnectionCreator { |
|
31 |
|
32 /** |
|
33 * Trace file path |
|
34 */ |
|
35 private final String traceFilePath; |
|
36 |
|
37 /** |
|
38 * Constructor |
|
39 * |
|
40 * @param traceFilePath |
|
41 * trace file path |
|
42 */ |
|
43 public ConnectionCreator(String traceFilePath) { |
|
44 this.traceFilePath = traceFilePath; |
|
45 } |
|
46 |
|
47 /** |
|
48 * Creates and returns a connection |
|
49 * |
|
50 * @return created connection |
|
51 */ |
|
52 public Connection getConnection() { |
|
53 Connection connection = null; |
|
54 IPreferenceStore store = TraceViewerPlugin.getDefault() |
|
55 .getPreferenceStore(); |
|
56 |
|
57 // TCP connection |
|
58 if (store.getString(PreferenceConstants.CONNECTION_TYPE).equals( |
|
59 PreferenceConstants.CONNECTION_TYPE_TCPIP)) { |
|
60 |
|
61 connection = createTcpConnection(store); |
|
62 |
|
63 // USB Serial connection |
|
64 } else if (store.getString(PreferenceConstants.CONNECTION_TYPE).equals( |
|
65 PreferenceConstants.CONNECTION_TYPE_USB_SERIAL)) { |
|
66 connection = createUsbSerialConnection(store); |
|
67 } |
|
68 |
|
69 return connection; |
|
70 } |
|
71 |
|
72 /** |
|
73 * Creates TCP connection |
|
74 * |
|
75 * @param store |
|
76 * preference store |
|
77 * @return created connection |
|
78 */ |
|
79 private Connection createTcpConnection(IPreferenceStore store) { |
|
80 Connection connection; |
|
81 |
|
82 // Close the old file from file handler |
|
83 TraceViewerGlobals.getTraceViewer().getFileHandler().closeFile(); |
|
84 |
|
85 // Set parameters to array |
|
86 String[] parameters = new String[3]; |
|
87 parameters[0] = store.getString(PreferenceConstants.IP_ADDRESS); |
|
88 parameters[1] = store.getString(PreferenceConstants.TCPIP_PORT); |
|
89 parameters[2] = store.getString(PreferenceConstants.TCPIP_CHANNEL); |
|
90 |
|
91 // Create connection |
|
92 connection = TraceViewerGlobals.getTraceProvider() |
|
93 .getConnectionHandler(); |
|
94 connection.createConnection(PreferenceConstants.CONNECTION_TYPE_TCPIP, |
|
95 parameters, traceFilePath); |
|
96 |
|
97 // Set TCF connection as file handler |
|
98 TraceViewerGlobals.getTraceViewer().setFileHandler( |
|
99 (TraceFileHandler) connection); |
|
100 |
|
101 return connection; |
|
102 } |
|
103 |
|
104 /** |
|
105 * Creates Serial connection |
|
106 * |
|
107 * @param store |
|
108 * preference store |
|
109 * @return created connection |
|
110 */ |
|
111 private Connection createUsbSerialConnection(IPreferenceStore store) { |
|
112 Connection connection = null; |
|
113 |
|
114 // Close the old file from file handler |
|
115 TraceViewerGlobals.getTraceViewer().getFileHandler().closeFile(); |
|
116 |
|
117 // Set parameters to array |
|
118 String[] parameters = new String[1]; |
|
119 parameters[0] = store |
|
120 .getString(PreferenceConstants.USB_SERIAL_COM_PORT); |
|
121 |
|
122 // Create connection |
|
123 connection = TraceViewerGlobals.getTraceProvider() |
|
124 .getConnectionHandler(); |
|
125 connection.createConnection( |
|
126 PreferenceConstants.CONNECTION_TYPE_USB_SERIAL, parameters, |
|
127 traceFilePath); |
|
128 |
|
129 // Set TCF connection as file handler |
|
130 TraceViewerGlobals.getTraceViewer().setFileHandler( |
|
131 (TraceFileHandler) connection); |
|
132 |
|
133 return connection; |
|
134 } |
|
135 } |