Commit | Line | Data |
---|---|---|
be222f56 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.Messages; | |
12 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
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 | setControl(container); | |
93 | ||
94 | } | |
95 | ||
96 | @Override | |
97 | public void dispose() { | |
98 | previewTable.dispose(); | |
99 | super.dispose(); | |
100 | } | |
101 | ||
102 | private void loadDefinition(final CustomTxtTraceDefinition definition) { | |
103 | for (final OutputColumn outputColumn : definition.outputs) { | |
104 | final Output output = new Output(outputsContainer, outputColumn.name); | |
105 | outputs.add(output); | |
106 | } | |
107 | } | |
108 | ||
109 | /* (non-Javadoc) | |
110 | * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean) | |
111 | */ | |
112 | @Override | |
113 | public void setVisible(final boolean visible) { | |
114 | if (visible) { | |
115 | this.definition = wizard.inputPage.getDefinition(); | |
116 | final List<String> outputNames = wizard.inputPage.getInputNames(); | |
117 | ||
118 | // dispose outputs that have been removed in the input page | |
119 | final Iterator<Output> iter = outputs.iterator(); | |
120 | while (iter.hasNext()) { | |
121 | final Output output = iter.next(); | |
122 | boolean found = false; | |
123 | for (final String name : outputNames) | |
124 | if (output.name.equals(name)) { | |
125 | found = true; | |
126 | break; | |
127 | } | |
128 | if (!found) { | |
129 | output.dispose(); | |
130 | iter.remove(); | |
131 | } | |
132 | } | |
133 | ||
134 | // create outputs that have been added in the input page | |
135 | for (final String name : outputNames) { | |
136 | boolean found = false; | |
137 | for (final Output output : outputs) | |
138 | if (output.name.equals(name)) { | |
139 | found = true; | |
140 | break; | |
141 | } | |
142 | if (!found) | |
143 | outputs.add(new Output(outputsContainer, name)); | |
144 | } | |
145 | ||
146 | outputsContainer.layout(); | |
147 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
148 | updatePreviewTable(); | |
149 | if (sash.getSize().y > outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y + previewTable.getTable().getItemHeight()) | |
150 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sash.getSize().y - outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y}); | |
151 | else | |
152 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, previewTable.getTable().getItemHeight()}); | |
153 | setPageComplete(true); | |
154 | } else | |
155 | setPageComplete(false); | |
156 | super.setVisible(visible); | |
157 | } | |
158 | ||
159 | private void moveBefore(final Output moved) { | |
160 | final int i = outputs.indexOf(moved); | |
161 | if (i > 0) { | |
162 | final Output previous = outputs.get(i-1); | |
163 | moved.enabledButton.moveAbove(previous.enabledButton); | |
164 | moved.nameLabel.moveBelow(moved.enabledButton); | |
165 | moved.upButton.moveBelow(moved.nameLabel); | |
166 | moved.downButton.moveBelow(moved.upButton); | |
167 | outputs.add(i-1, outputs.remove(i)); | |
168 | outputsContainer.layout(); | |
169 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
170 | container.layout(); | |
171 | updatePreviewTable(); | |
172 | } | |
173 | } | |
174 | ||
175 | private void moveAfter(final Output moved) { | |
176 | final int i = outputs.indexOf(moved); | |
177 | if (i+1 < outputs.size()) { | |
178 | final Output next = outputs.get(i+1); | |
179 | moved.enabledButton.moveBelow(next.downButton); | |
180 | moved.nameLabel.moveBelow(moved.enabledButton); | |
181 | moved.upButton.moveBelow(moved.nameLabel); | |
182 | moved.downButton.moveBelow(moved.upButton); | |
183 | outputs.add(i+1, outputs.remove(i)); | |
184 | outputsContainer.layout(); | |
185 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
186 | container.layout(); | |
187 | updatePreviewTable(); | |
188 | } | |
189 | } | |
190 | ||
191 | private void updatePreviewTable() { | |
192 | final int CACHE_SIZE = 50; | |
193 | definition.outputs = extractOutputs(); | |
194 | ||
195 | try { | |
196 | tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$ | |
197 | final FileWriter writer = new FileWriter(tmpFile); | |
198 | writer.write(wizard.inputPage.getInputText()); | |
199 | writer.close(); | |
200 | ||
201 | final ITmfTrace<?> trace = new CustomTxtTrace(null, definition, tmpFile.getAbsolutePath(), CACHE_SIZE); | |
202 | previewTable.dispose(); | |
203 | previewTable = new CustomEventsTable(definition, tableContainer, CACHE_SIZE); | |
204 | previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
205 | previewTable.setTrace(trace, true); | |
206 | } catch (final TmfTraceException e) { | |
207 | Activator.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ | |
208 | } catch (final IOException e) { | |
209 | Activator.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ | |
210 | } | |
211 | ||
212 | tableContainer.layout(); | |
213 | container.layout(); | |
214 | } | |
215 | ||
216 | public List<OutputColumn> extractOutputs() { | |
217 | int numColumns = 0; | |
218 | for (int i = 0; i < outputs.size(); i++) | |
219 | if (outputs.get(i).enabledButton.getSelection()) | |
220 | numColumns++; | |
221 | final List<OutputColumn> outputColumns = new ArrayList<OutputColumn>(numColumns); | |
222 | numColumns = 0; | |
223 | for (int i = 0; i < outputs.size(); i++) { | |
224 | final Output output = outputs.get(i); | |
225 | if (output.enabledButton.getSelection()) { | |
226 | final OutputColumn column = new OutputColumn(); | |
227 | column.name = output.nameLabel.getText(); | |
228 | outputColumns.add(column); | |
229 | } | |
230 | } | |
231 | return outputColumns; | |
232 | } | |
233 | ||
234 | private class Output { | |
235 | String name; | |
236 | Button enabledButton; | |
237 | Text nameLabel; | |
238 | Button upButton; | |
239 | Button downButton; | |
240 | ||
241 | public Output(final Composite parent, final String name) { | |
242 | this.name = name; | |
243 | ||
244 | enabledButton = new Button(parent, SWT.CHECK); | |
245 | enabledButton.setToolTipText(Messages.CustomTxtParserOutputWizardPage_visible); | |
246 | enabledButton.setSelection(true); | |
247 | enabledButton.addSelectionListener(new SelectionAdapter() { | |
248 | @Override | |
249 | public void widgetSelected(final SelectionEvent e) { | |
250 | updatePreviewTable(); | |
251 | } | |
252 | }); | |
253 | // if (messageOutput != null) { | |
254 | // enabledButton.moveAbove(messageOutput.enabledButton); | |
255 | // } | |
256 | ||
257 | nameLabel = new Text(parent, SWT.BORDER | SWT.READ_ONLY | SWT.SINGLE); | |
258 | nameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
259 | nameLabel.setText(name); | |
260 | nameLabel.moveBelow(enabledButton); | |
261 | ||
262 | upButton = new Button(parent, SWT.PUSH); | |
263 | upButton.setImage(upImage); | |
264 | upButton.setToolTipText(Messages.CustomTxtParserOutputWizardPage_moveBefore); | |
265 | upButton.addSelectionListener(new SelectionAdapter() { | |
266 | @Override | |
267 | public void widgetSelected(final SelectionEvent e) { | |
268 | moveBefore(Output.this); | |
269 | } | |
270 | }); | |
271 | upButton.moveBelow(nameLabel); | |
272 | ||
273 | downButton = new Button(parent, SWT.PUSH); | |
274 | downButton.setImage(downImage); | |
275 | downButton.setToolTipText(Messages.CustomTxtParserOutputWizardPage_moveAfter); | |
276 | downButton.addSelectionListener(new SelectionAdapter() { | |
277 | @Override | |
278 | public void widgetSelected(final SelectionEvent e) { | |
279 | moveAfter(Output.this); | |
280 | } | |
281 | }); | |
282 | downButton.moveBelow(upButton); | |
283 | } | |
284 | ||
285 | private void dispose() { | |
286 | enabledButton.dispose(); | |
287 | nameLabel.dispose(); | |
288 | upButton.dispose(); | |
289 | downButton.dispose(); | |
290 | } | |
291 | } | |
292 | ||
293 | public CustomTxtTraceDefinition getDefinition() { | |
294 | return definition; | |
295 | } | |
296 | ||
297 | } |