Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6115048dbf0d7a7eefb9cea6cf908e04b5c443f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*******************************************************************************
 * Copyright (c) 2016 EfficiOS Inc., Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/

package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow.filters;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.regex.Pattern;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;

import com.google.common.collect.Range;

/**
 * The Dynamic Filters configuration dialog.
 *
 * @author Jonathan Rajotte Julien
 */
public class DynamicFilterDialog extends TitleAreaDialog {

    /** Pattern for CPUS ranges e.g.: 1,1-200,2,3 */
    private static final Pattern CPU_RANGE = Pattern.compile("^((\\d+(\\-\\d+)?, ?)*(\\d+(\\-\\d+)?))+$"); //$NON-NLS-1$
    private static final @NonNull String INTERNAL_RANGE_SEPARATOR = "-"; //$NON-NLS-1$
    private static final @NonNull String RANGES_DELIMITER = ","; //$NON-NLS-1$

    /** The internal ActiveThreadsFilter result */
    private @NonNull ActiveThreadsFilter fInternalActiveThreadsFilter;
    private final @Nullable ITmfTrace fTrace;

    private Button fActiveThreadEnabledButton;
    private Button fAllActiveThreadsRadionButton;
    private Button fCpuRangesRadioButton;
    private Text fCpuRangesField;

    /**
     * Constructor to create a DynamicFilterDialog
     *
     * @param parentShell
     *            The parent shell
     * @param filter
     *            An
     *            {@link org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow.filters.ActiveThreadsFilter
     *            ActiveThreadFilter} instance.
     * @param trace
     *            The relevant trace
     */
    public DynamicFilterDialog(Shell parentShell, @NonNull ActiveThreadsFilter filter, @Nullable ITmfTrace trace) {
        super(parentShell);
        fInternalActiveThreadsFilter = filter;
        fTrace = trace;
    }

    @Override
    public void create() {
        super.create();
        setTitle(Messages.DynamicFilterDialog_Title);
        this.getShell().setText(Messages.DynamicFilterDialog_Title);
    }

    private static boolean validateCpuRange(final String newString) {
        return CPU_RANGE.matcher(newString).matches();
    }

    private void createActiveThreadSection(Composite parent) {
        boolean usesCpuRanges = false;
        boolean filterActive = false;

        ActiveThreadsFilter filter = fInternalActiveThreadsFilter;

        filterActive = fInternalActiveThreadsFilter.isEnabled();
        usesCpuRanges = filter.isCpuRangesBased();

        GridData gd;
        GridLayout gl;
        Group activeThreadGroup = new Group(parent, SWT.SHADOW_NONE | SWT.BORDER);
        activeThreadGroup.setText(Messages.DynamicFilterDialog_ActiveThreadsFilterName);

        activeThreadGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        gl = new GridLayout(1, true);
        gl.marginLeft = gl.marginRight = 0;
        activeThreadGroup.setLayout(gl);

        fActiveThreadEnabledButton = new Button(activeThreadGroup, SWT.CHECK);
        fActiveThreadEnabledButton.setText(Messages.DynamicFilterDialog_ActiveThreadsFilterName);
        fActiveThreadEnabledButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        /* Cpu selection */
        Group cpuSelectionGroup = new Group(activeThreadGroup, SWT.SHADOW_NONE);
        cpuSelectionGroup.setText(Messages.DynamicFilterDialog_OptionsGroupLabel);
        cpuSelectionGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        cpuSelectionGroup.setLayout(new GridLayout(2, false));

        fAllActiveThreadsRadionButton = new Button(cpuSelectionGroup, SWT.RADIO);
        fAllActiveThreadsRadionButton.setText(Messages.DynamicFilterDialog_RadioButtonAllActiveThreads);
        fAllActiveThreadsRadionButton.setToolTipText(Messages.DynamicFilterDialog_RadioButtonAllActiveThreadsToolTip);

        fAllActiveThreadsRadionButton.setSelection(!usesCpuRanges);
        gd = new GridData(SWT.FILL, SWT.FILL, true, true);
        gd.horizontalSpan = 2;
        fAllActiveThreadsRadionButton.setLayoutData(gd);

        fCpuRangesRadioButton = new Button(cpuSelectionGroup, SWT.RADIO);
        fCpuRangesRadioButton.setText(Messages.DynamicFilterDialog_CpuRangesLabel);
        fCpuRangesRadioButton.setToolTipText(Messages.DynamicFilterDialog_CpuRangesTooltip);

        fCpuRangesRadioButton.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true));

        fCpuRangesField = new Text(cpuSelectionGroup, SWT.SINGLE | SWT.BORDER);
        fCpuRangesField.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, true));
        fCpuRangesField.setMessage(Messages.DynamicFilterDialog_CpuRangesExamples);

        fCpuRangesRadioButton.setToolTipText(Messages.DynamicFilterDialog_CpuRangesTooltip);

        /* Attach an automatic validation to the field */
        fCpuRangesField.addVerifyListener(new VerifyListener() {
            @Override
            public void verifyText(VerifyEvent e) {
                /* Reconstruct the string */
                final String oldString = fCpuRangesField.getText();
                final String newString = oldString.substring(0, e.start) + e.text + oldString.substring(e.end);

                /* Validate the string */
                boolean valid = validateCpuRange(newString);

                Button okButton = getButton(IDialogConstants.OK_ID);
                if (okButton != null) {
                    getButton(IDialogConstants.OK_ID).setEnabled(valid);
                }
                if (valid) {
                    setErrorMessage(null);
                } else {
                    setErrorMessage(Messages.DynamicFilterDialog_InvalidRangesErrorMsg);
                }
            }
        });

        fAllActiveThreadsRadionButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);
                boolean selected = ((Button) e.widget).getSelection();
                if (!selected) {
                    return;
                }
                setErrorMessage(null);
                getButton(IDialogConstants.OK_ID).setEnabled(true);
            }
        });

        fCpuRangesRadioButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);
                boolean selected = ((Button) e.widget).getSelection();
                fCpuRangesField.setEnabled(selected);
                validateCpuRange(fCpuRangesField.getText());
            }
        });

        fActiveThreadEnabledButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);
                boolean selected = ((Button) e.widget).getSelection();
                cpuSelectionGroup.setEnabled(selected);
                fAllActiveThreadsRadionButton.setEnabled(selected);
                fCpuRangesRadioButton.setEnabled(selected);
                fCpuRangesField.setEnabled(selected && fCpuRangesRadioButton.getSelection());
            }
        });

        /* Set the base state for the ui control */
        fActiveThreadEnabledButton.setSelection(filterActive);
        cpuSelectionGroup.setEnabled(filterActive);
        fCpuRangesRadioButton.setEnabled(filterActive);
        fAllActiveThreadsRadionButton.setEnabled(filterActive);

        fAllActiveThreadsRadionButton.setSelection(!usesCpuRanges);
        fCpuRangesRadioButton.setSelection(usesCpuRanges);

        fCpuRangesField.setEnabled(filterActive && usesCpuRanges);

        /* Populate the CPU ranges fields */
        if (!filter.getCpuRanges().isEmpty()) {
            StringJoiner joiner = new StringJoiner(RANGES_DELIMITER);
            for (Range<Long> range : filter.getCpuRanges()) {
                String rangeString = range.lowerEndpoint().toString();
                if (range.lowerEndpoint() != range.upperEndpoint()) {
                    rangeString = rangeString.concat(INTERNAL_RANGE_SEPARATOR + range.upperEndpoint());
                }
                joiner.add(rangeString);
            }
            fCpuRangesField.setText(joiner.toString());
        }
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite area = (Composite) super.createDialogArea(parent);
        Composite container = new Composite(area, SWT.NONE);
        container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        GridLayout layout = new GridLayout(1, true);
        container.setLayout(layout);
        createActiveThreadSection(container);
        return area;
    }

    @Override
    protected boolean isResizable() {
        return true;
    }

    private void saveInput() {
        if (!(fAllActiveThreadsRadionButton.getSelection() || fCpuRangesRadioButton.getSelection())
                || (fAllActiveThreadsRadionButton.getSelection() && fCpuRangesRadioButton.getSelection())) {
            throw new IllegalStateException(Messages.DynamicFilterDialog_InvalidRadioButtonState);
        }

        List<Range<Long>> ranges = null;
        ranges = parseCpuRangesText(fCpuRangesField.getText());

        fInternalActiveThreadsFilter = new ActiveThreadsFilter(ranges, fCpuRangesRadioButton.getSelection(), fTrace);
        fInternalActiveThreadsFilter.setEnabled(fActiveThreadEnabledButton.getSelection());
    }

    private static List<Range<Long>> parseCpuRangesText(final String string) {
        List<Range<Long>> results = new ArrayList<>();
        if (validateCpuRange(string)) {
            string.split(RANGES_DELIMITER);
            for (String range : string.split(RANGES_DELIMITER)) {
                if (range.contains(INTERNAL_RANGE_SEPARATOR)) {
                    /* Parse as a range */
                    String[] split = range.split(INTERNAL_RANGE_SEPARATOR);
                    if (split.length != 2) {
                        /* Invalid range */
                        continue;
                    }

                    long[] sorted = new long[split.length];
                    Arrays.setAll(sorted, i -> Long.parseLong(split[i]));
                    Arrays.sort(sorted);
                    results.add(Range.closed(sorted[0], sorted[1]));
                } else {
                    /* Parse as an individual number */
                    Long value = Long.parseLong(range);
                    results.add(Range.closed(value, value));
                }
            }
        }
        return results;
    }

    @Override
    protected void okPressed() {
        saveInput();
        super.okPressed();
    }

    @Override
    public boolean isHelpAvailable() {
        return false;
    }

    /**
     * Get the resulting ActiveThreadsFilter
     *
     * @return The configured
     *         {@link org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow.filters.ActiveThreadsFilter
     *         ActiveThreadFilter} instance.
     */
    public @NonNull ActiveThreadsFilter getActiveThreadsResult() {
        return fInternalActiveThreadsFilter;
    }
}

Back to the top