Commit | Line | Data |
---|---|---|
6151d86c PT |
1 | package org.eclipse.linuxtools.internal.tmf.ui.parsers.wizards; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.FileWriter; | |
5 | import java.io.IOException; | |
6 | import java.util.ArrayList; | |
7 | import java.util.Iterator; | |
8 | import java.util.List; | |
9 | ||
10 | import org.eclipse.jface.wizard.WizardPage; | |
11 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
12 | import org.eclipse.linuxtools.internal.tmf.ui.Messages; | |
13 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomEventsTable; | |
14 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTraceDefinition.OutputColumn; | |
15 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace; | |
16 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition; | |
17 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
18 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
19 | import org.eclipse.swt.SWT; | |
20 | import org.eclipse.swt.custom.SashForm; | |
21 | import org.eclipse.swt.custom.ScrolledComposite; | |
22 | import org.eclipse.swt.events.SelectionAdapter; | |
23 | import org.eclipse.swt.events.SelectionEvent; | |
24 | import org.eclipse.swt.graphics.Image; | |
25 | import org.eclipse.swt.layout.GridData; | |
26 | import org.eclipse.swt.layout.GridLayout; | |
27 | import org.eclipse.swt.widgets.Button; | |
28 | import org.eclipse.swt.widgets.Composite; | |
29 | import org.eclipse.swt.widgets.Text; | |
30 | ||
31 | public class CustomTxtParserOutputWizardPage extends WizardPage { | |
32 | ||
33 | private static final Image upImage = Activator.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$ | |
34 | private static final Image downImage = Activator.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$ | |
35 | private final CustomTxtParserWizard wizard; | |
36 | private CustomTxtTraceDefinition definition; | |
37 | List<Output> outputs = new ArrayList<Output>(); | |
38 | // Output messageOutput; | |
39 | Composite container; | |
40 | SashForm sash; | |
41 | // Text timestampFormatText; | |
42 | // Text timestampPreviewText; | |
43 | ScrolledComposite outputsScrolledComposite; | |
44 | Composite outputsContainer; | |
45 | // ScrolledComposite inputScrolledComposite; | |
46 | Composite tableContainer; | |
47 | CustomEventsTable previewTable; | |
48 | File tmpFile; | |
49 | ||
50 | protected CustomTxtParserOutputWizardPage(final CustomTxtParserWizard wizard) { | |
51 | super("CustomParserOutputWizardPage"); //$NON-NLS-1$ | |
52 | setTitle(wizard.inputPage.getTitle()); | |
53 | setDescription(Messages.CustomTxtParserOutputWizardPage_description); | |
54 | this.wizard = wizard; | |
55 | setPageComplete(false); | |
56 | } | |
57 | ||
58 | @Override | |
59 | public void createControl(final Composite parent) { | |
60 | container = new Composite(parent, SWT.NULL); | |
61 | container.setLayout(new GridLayout()); | |
62 | ||
63 | sash = new SashForm(container, SWT.VERTICAL); | |
64 | sash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
65 | sash.setBackground(sash.getDisplay().getSystemColor(SWT.COLOR_GRAY)); | |
66 | ||
67 | outputsScrolledComposite = new ScrolledComposite(sash, SWT.V_SCROLL); | |
68 | outputsScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
69 | outputsContainer = new Composite(outputsScrolledComposite, SWT.NONE); | |
70 | final GridLayout outputsLayout = new GridLayout(4, false); | |
71 | outputsLayout.marginHeight = 10; | |
72 | outputsLayout.marginWidth = 0; | |
73 | outputsContainer.setLayout(outputsLayout); | |
74 | outputsScrolledComposite.setContent(outputsContainer); | |
75 | outputsScrolledComposite.setExpandHorizontal(true); | |
76 | outputsScrolledComposite.setExpandVertical(true); | |
77 | ||
78 | outputsContainer.layout(); | |
79 | ||
80 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
81 | ||
82 | tableContainer = new Composite(sash, SWT.NONE); | |
83 | final GridLayout tableLayout = new GridLayout(); | |
84 | tableLayout.marginHeight = 0; | |
85 | tableLayout.marginWidth = 0; | |
86 | tableContainer.setLayout(tableLayout); | |
87 | previewTable = new CustomEventsTable(new CustomTxtTraceDefinition(), tableContainer, 0); | |
88 | previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
89 | ||
90 | if (wizard.definition != null) { | |
91 | loadDefinition(wizard.definition); | |
92 | } | |
93 | setControl(container); | |
94 | ||
95 | } | |
96 | ||
97 | @Override | |
98 | public void dispose() { | |
99 | previewTable.dispose(); | |
100 | super.dispose(); | |
101 | } | |
102 | ||
103 | private void loadDefinition(final CustomTxtTraceDefinition definition) { | |
104 | for (final OutputColumn outputColumn : definition.outputs) { | |
105 | final Output output = new Output(outputsContainer, outputColumn.name); | |
106 | outputs.add(output); | |
107 | } | |
108 | } | |
109 | ||
110 | /* (non-Javadoc) | |
111 | * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean) | |
112 | */ | |
113 | @Override | |
114 | public void setVisible(final boolean visible) { | |
115 | if (visible) { | |
116 | this.definition = wizard.inputPage.getDefinition(); | |
117 | final List<String> outputNames = wizard.inputPage.getInputNames(); | |
118 | ||
119 | // dispose outputs that have been removed in the input page | |
120 | final Iterator<Output> iter = outputs.iterator(); | |
121 | while (iter.hasNext()) { | |
122 | final Output output = iter.next(); | |
123 | boolean found = false; | |
124 | for (final String name : outputNames) { | |
125 | if (output.name.equals(name)) { | |
126 | found = true; | |
127 | break; | |
128 | } | |
129 | } | |
130 | if (!found) { | |
131 | output.dispose(); | |
132 | iter.remove(); | |
133 | } | |
134 | } | |
135 | ||
136 | // create outputs that have been added in the input page | |
137 | for (final String name : outputNames) { | |
138 | boolean found = false; | |
139 | for (final Output output : outputs) { | |
140 | if (output.name.equals(name)) { | |
141 | found = true; | |
142 | break; | |
143 | } | |
144 | } | |
145 | if (!found) { | |
146 | outputs.add(new Output(outputsContainer, name)); | |
147 | } | |
148 | } | |
149 | ||
150 | outputsContainer.layout(); | |
151 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
152 | updatePreviewTable(); | |
153 | if (sash.getSize().y > outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y + previewTable.getTable().getItemHeight()) { | |
154 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sash.getSize().y - outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y}); | |
155 | } else { | |
156 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, previewTable.getTable().getItemHeight()}); | |
157 | } | |
158 | setPageComplete(true); | |
159 | } else { | |
160 | setPageComplete(false); | |
161 | } | |
162 | super.setVisible(visible); | |
163 | } | |
164 | ||
165 | private void moveBefore(final Output moved) { | |
166 | final int i = outputs.indexOf(moved); | |
167 | if (i > 0) { | |
168 | final Output previous = outputs.get(i-1); | |
169 | moved.enabledButton.moveAbove(previous.enabledButton); | |
170 | moved.nameLabel.moveBelow(moved.enabledButton); | |
171 | moved.upButton.moveBelow(moved.nameLabel); | |
172 | moved.downButton.moveBelow(moved.upButton); | |
173 | outputs.add(i-1, outputs.remove(i)); | |
174 | outputsContainer.layout(); | |
175 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
176 | container.layout(); | |
177 | updatePreviewTable(); | |
178 | } | |
179 | } | |
180 | ||
181 | private void moveAfter(final Output moved) { | |
182 | final int i = outputs.indexOf(moved); | |
183 | if (i+1 < outputs.size()) { | |
184 | final Output next = outputs.get(i+1); | |
185 | moved.enabledButton.moveBelow(next.downButton); | |
186 | moved.nameLabel.moveBelow(moved.enabledButton); | |
187 | moved.upButton.moveBelow(moved.nameLabel); | |
188 | moved.downButton.moveBelow(moved.upButton); | |
189 | outputs.add(i+1, outputs.remove(i)); | |
190 | outputsContainer.layout(); | |
191 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
192 | container.layout(); | |
193 | updatePreviewTable(); | |
194 | } | |
195 | } | |
196 | ||
197 | private void updatePreviewTable() { | |
198 | final int CACHE_SIZE = 50; | |
199 | definition.outputs = extractOutputs(); | |
200 | ||
201 | try { | |
202 | tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$ | |
203 | final FileWriter writer = new FileWriter(tmpFile); | |
204 | writer.write(wizard.inputPage.getInputText()); | |
205 | writer.close(); | |
206 | ||
207 | final ITmfTrace trace = new CustomTxtTrace(null, definition, tmpFile.getAbsolutePath(), CACHE_SIZE); | |
208 | previewTable.dispose(); | |
209 | previewTable = new CustomEventsTable(definition, tableContainer, CACHE_SIZE); | |
210 | previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
211 | previewTable.setTrace(trace, true); | |
212 | } catch (final TmfTraceException e) { | |
213 | Activator.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ | |
214 | } catch (final IOException e) { | |
215 | Activator.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ | |
216 | } | |
217 | ||
218 | tableContainer.layout(); | |
219 | container.layout(); | |
220 | } | |
221 | ||
222 | public List<OutputColumn> extractOutputs() { | |
223 | int numColumns = 0; | |
224 | for (int i = 0; i < outputs.size(); i++) { | |
225 | if (outputs.get(i).enabledButton.getSelection()) { | |
226 | numColumns++; | |
227 | } | |
228 | } | |
229 | final List<OutputColumn> outputColumns = new ArrayList<OutputColumn>(numColumns); | |
230 | numColumns = 0; | |
231 | for (int i = 0; i < outputs.size(); i++) { | |
232 | final Output output = outputs.get(i); | |
233 | if (output.enabledButton.getSelection()) { | |
234 | final OutputColumn column = new OutputColumn(); | |
235 | column.name = output.nameLabel.getText(); | |
236 | outputColumns.add(column); | |
237 | } | |
238 | } | |
239 | return outputColumns; | |
240 | } | |
241 | ||
242 | private class Output { | |
243 | String name; | |
244 | Button enabledButton; | |
245 | Text nameLabel; | |
246 | Button upButton; | |
247 | Button downButton; | |
248 | ||
249 | public Output(final Composite parent, final String name) { | |
250 | this.name = name; | |
251 | ||
252 | enabledButton = new Button(parent, SWT.CHECK); | |
253 | enabledButton.setToolTipText(Messages.CustomTxtParserOutputWizardPage_visible); | |
254 | enabledButton.setSelection(true); | |
255 | enabledButton.addSelectionListener(new SelectionAdapter() { | |
256 | @Override | |
257 | public void widgetSelected(final SelectionEvent e) { | |
258 | updatePreviewTable(); | |
259 | } | |
260 | }); | |
261 | // if (messageOutput != null) { | |
262 | // enabledButton.moveAbove(messageOutput.enabledButton); | |
263 | // } | |
264 | ||
265 | nameLabel = new Text(parent, SWT.BORDER | SWT.READ_ONLY | SWT.SINGLE); | |
266 | nameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
267 | nameLabel.setText(name); | |
268 | nameLabel.moveBelow(enabledButton); | |
269 | ||
270 | upButton = new Button(parent, SWT.PUSH); | |
271 | upButton.setImage(upImage); | |
272 | upButton.setToolTipText(Messages.CustomTxtParserOutputWizardPage_moveBefore); | |
273 | upButton.addSelectionListener(new SelectionAdapter() { | |
274 | @Override | |
275 | public void widgetSelected(final SelectionEvent e) { | |
276 | moveBefore(Output.this); | |
277 | } | |
278 | }); | |
279 | upButton.moveBelow(nameLabel); | |
280 | ||
281 | downButton = new Button(parent, SWT.PUSH); | |
282 | downButton.setImage(downImage); | |
283 | downButton.setToolTipText(Messages.CustomTxtParserOutputWizardPage_moveAfter); | |
284 | downButton.addSelectionListener(new SelectionAdapter() { | |
285 | @Override | |
286 | public void widgetSelected(final SelectionEvent e) { | |
287 | moveAfter(Output.this); | |
288 | } | |
289 | }); | |
290 | downButton.moveBelow(upButton); | |
291 | } | |
292 | ||
293 | private void dispose() { | |
294 | enabledButton.dispose(); | |
295 | nameLabel.dispose(); | |
296 | upButton.dispose(); | |
297 | downButton.dispose(); | |
298 | } | |
299 | } | |
300 | ||
301 | public CustomTxtTraceDefinition getDefinition() { | |
302 | return definition; | |
303 | } | |
304 | ||
305 | } |