Commit | Line | Data |
---|---|---|
d90ae2a5 RK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 Movidius Inc. and others | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | *******************************************************************************/ | |
10 | ||
11 | package org.eclipse.tracecompass.internal.tmf.ui.symbols; | |
12 | ||
13 | import java.io.File; | |
14 | import java.text.MessageFormat; | |
15 | ||
16 | import org.eclipse.jdt.annotation.NonNull; | |
17 | import org.eclipse.swt.SWT; | |
18 | import org.eclipse.swt.events.SelectionAdapter; | |
19 | import org.eclipse.swt.events.SelectionEvent; | |
20 | import org.eclipse.swt.layout.GridData; | |
21 | import org.eclipse.swt.layout.GridLayout; | |
22 | import org.eclipse.swt.widgets.Button; | |
23 | import org.eclipse.swt.widgets.Composite; | |
24 | import org.eclipse.swt.widgets.Control; | |
25 | import org.eclipse.swt.widgets.FileDialog; | |
26 | import org.eclipse.swt.widgets.Text; | |
d90ae2a5 RK |
27 | import org.eclipse.tracecompass.internal.tmf.ui.symbols.BasicSymbolProvider.SourceKind; |
28 | import org.eclipse.tracecompass.tmf.ui.symbols.AbstractSymbolProviderPreferencePage; | |
29 | ||
30 | /** | |
31 | * Preference page that allows the user to configure a | |
32 | * {@link BasicSymbolProvider} | |
33 | * | |
34 | * @author Robert Kiss | |
35 | * | |
36 | */ | |
37 | public class BasicSymbolProviderPreferencePage extends AbstractSymbolProviderPreferencePage { | |
38 | ||
39 | private Button fRadioBinaryFile; | |
40 | private Text fTextBinaryFile; | |
41 | private Button fButtonBrowseBinary; | |
42 | private Button fRadioMappingFile; | |
43 | private Text fTextMappingFile; | |
44 | private Button fButtonBrowseMapping; | |
45 | ||
46 | /** | |
47 | * Creates a new object for the given provider | |
48 | * | |
49 | * @param provider | |
50 | * a non-null provider | |
51 | */ | |
52 | public BasicSymbolProviderPreferencePage(@NonNull BasicSymbolProvider provider) { | |
53 | super(provider); | |
54 | setDescription(MessageFormat.format(Messages.BasicSymbolProviderPrefPage_description, provider.getTrace().getName())); | |
55 | setValid(true); | |
56 | } | |
57 | ||
58 | @Override | |
59 | protected Control createContents(Composite parent) { | |
60 | Composite composite = new Composite(parent, SWT.NONE); | |
61 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
62 | composite.setLayout(new GridLayout(3, false)); | |
63 | ||
64 | fRadioBinaryFile = new Button(composite, SWT.RADIO); | |
65 | fRadioBinaryFile.setText(Messages.BasicSymbolProviderPrefPage_radioBinaryFile_text); | |
66 | fRadioBinaryFile.setToolTipText(Messages.BasicSymbolProviderPrefPage_radioBinaryFile_tooltip); | |
67 | fRadioBinaryFile.setSelection(true); | |
68 | fRadioBinaryFile.addSelectionListener(new SelectionAdapter() { | |
69 | @Override | |
70 | public void widgetSelected(SelectionEvent e) { | |
71 | switchToSourceKind(SourceKind.BINARY, true); | |
72 | } | |
73 | }); | |
74 | ||
75 | fTextBinaryFile = new Text(composite, SWT.BORDER); | |
76 | fTextBinaryFile.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
77 | fTextBinaryFile.setEditable(false); | |
78 | ||
79 | fButtonBrowseBinary = new Button(composite, SWT.NONE); | |
80 | fButtonBrowseBinary.setText(Messages.BasicSymbolProviderPrefPage_btnBrowse); | |
81 | fButtonBrowseBinary.addSelectionListener(new SelectionAdapter() { | |
82 | ||
83 | @Override | |
84 | public void widgetSelected(SelectionEvent e) { | |
85 | browseForFile(fTextBinaryFile, Messages.BasicSymbolProviderPrefPage_ImportBinaryFileDialogTitle); | |
86 | } | |
87 | }); | |
88 | ||
89 | fRadioMappingFile = new Button(composite, SWT.RADIO); | |
90 | fRadioMappingFile.setText(Messages.BasicSymbolProviderPrefPage_radioMappingFile_text); | |
91 | fRadioMappingFile.setToolTipText(Messages.BasicSymbolProviderPrefPage_radioMappingFile_tooltip); | |
92 | fRadioMappingFile.setSelection(false); | |
93 | fRadioMappingFile.addSelectionListener(new SelectionAdapter() { | |
94 | @Override | |
95 | public void widgetSelected(SelectionEvent e) { | |
96 | switchToSourceKind(SourceKind.MAPPING, true); | |
97 | } | |
98 | }); | |
99 | ||
100 | fTextMappingFile = new Text(composite, SWT.BORDER); | |
101 | fTextMappingFile.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
102 | fTextMappingFile.setEnabled(false); | |
103 | fTextMappingFile.setEditable(false); | |
104 | ||
105 | fButtonBrowseMapping = new Button(composite, SWT.NONE); | |
106 | fButtonBrowseMapping.setText(Messages.BasicSymbolProviderPrefPage_btnBrowse); | |
107 | fButtonBrowseMapping.setEnabled(false); | |
108 | fButtonBrowseMapping.addSelectionListener(new SelectionAdapter() { | |
109 | ||
110 | @Override | |
111 | public void widgetSelected(SelectionEvent e) { | |
112 | browseForFile(fTextMappingFile, Messages.BasicSymbolProviderPrefPage_ImportMappingDialogTitle); | |
113 | } | |
114 | }); | |
115 | ||
116 | loadProviderSettings(); | |
117 | ||
118 | return composite; | |
119 | } | |
120 | ||
121 | private void browseForFile(Text fileField, String dialogTitle) { | |
122 | FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN); | |
123 | fileDialog.setText(dialogTitle); | |
124 | String filePath = fileDialog.open(); | |
125 | if (filePath != null) { | |
126 | fileField.setText(filePath); | |
127 | broadcastChanges(); | |
128 | } | |
129 | ||
130 | } | |
131 | ||
132 | private void loadProviderSettings() { | |
133 | BasicSymbolProvider provider = (BasicSymbolProvider) getSymbolProvider(); | |
134 | String source = provider.getConfiguredSource(); | |
135 | SourceKind sourceKind = provider.getConfiguredSourceKind(); | |
136 | if (source != null) { | |
137 | if (sourceKind == SourceKind.BINARY) { | |
138 | fTextBinaryFile.setText(source); | |
139 | } else { | |
140 | fTextMappingFile.setText(source); | |
141 | } | |
142 | } | |
143 | ||
144 | switchToSourceKind(sourceKind, false); | |
145 | broadcastChanges(); | |
146 | } | |
147 | ||
148 | private void switchToSourceKind(@NonNull SourceKind kind, boolean broadcastChanges) { | |
149 | fRadioBinaryFile.setSelection(kind == SourceKind.BINARY); | |
150 | fTextBinaryFile.setEnabled(kind == SourceKind.BINARY); | |
151 | fButtonBrowseBinary.setEnabled(kind == SourceKind.BINARY); | |
152 | ||
153 | fRadioMappingFile.setSelection(kind == SourceKind.MAPPING); | |
154 | fTextMappingFile.setEnabled(kind == SourceKind.MAPPING); | |
155 | fButtonBrowseMapping.setEnabled(kind == SourceKind.MAPPING); | |
156 | if (broadcastChanges) { | |
157 | broadcastChanges(); | |
158 | } | |
159 | } | |
160 | ||
161 | @Override | |
162 | public void saveConfiguration() { | |
163 | BasicSymbolProvider provider = (BasicSymbolProvider) getSymbolProvider(); | |
164 | provider.setConfiguredSource(getCurrentSource(), getCurrentSourceKind()); | |
165 | } | |
166 | ||
167 | private @NonNull SourceKind getCurrentSourceKind() { | |
168 | if (fRadioBinaryFile.getSelection()) { | |
169 | return SourceKind.BINARY; | |
170 | } | |
171 | return SourceKind.MAPPING; | |
172 | } | |
173 | ||
174 | private String getCurrentSource() { | |
175 | if (fRadioBinaryFile.getSelection()) { | |
176 | return fTextBinaryFile.getText(); | |
177 | } | |
178 | return fTextMappingFile.getText(); | |
179 | } | |
180 | ||
181 | private void broadcastChanges() { | |
182 | String filePath = getCurrentSource(); | |
183 | String errorMessage = null; | |
184 | if (filePath != null && filePath.length() > 0) { | |
185 | File file = new File(filePath); | |
186 | if (!file.isFile()) { | |
187 | errorMessage = Messages.BasicSymbolProviderPrefPage_errorFileDoesNotExists; | |
188 | } | |
189 | } else { | |
190 | errorMessage = Messages.BasicSymbolProviderPrefPage_errorSpecifyFile; | |
191 | } | |
192 | setErrorMessage(errorMessage); | |
193 | setValid(errorMessage == null); | |
194 | } | |
195 | ||
196 | } |