-
Notifications
You must be signed in to change notification settings - Fork 11
/
InAndOutConfigDialog.java
262 lines (240 loc) · 10.6 KB
/
InAndOutConfigDialog.java
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
/*******************************************************************************
* Copyright (c) 2022 Ericsson
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.incubator.internal.inandout.ui;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Shell;
import org.eclipse.tracecompass.incubator.internal.inandout.core.analysis.InAndOutAnalysisModule;
import org.eclipse.tracecompass.incubator.internal.inandout.core.analysis.SegmentSpecifier;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
/**
* Configuration dialog
*
* @author Matthew Khouzam
*/
public class InAndOutConfigDialog extends Dialog {
private final List<@NonNull SegmentSpecifier> fData = new ArrayList<>();
private ListViewer fSpecifiers = null;
private Button fAdd = null;
private File fPath = null;
private ITmfTrace fTrace;
private boolean fChanged = false;
/**
* Constructor
*
* @param parentShell
* the parent shell, or null to create a top-level shell
* @param supplementalPath
* the path of the trace folder
* @param trace
* the trace
*/
protected InAndOutConfigDialog(Shell parentShell, File supplementalPath, ITmfTrace trace) {
super(parentShell);
fPath = supplementalPath;
fTrace = trace;
setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
}
@Override
protected Control createDialogArea(@Nullable Composite parent) {
if (parent == null) {
return null;
}
File file = new File(fPath.getAbsolutePath() + File.separator + InAndOutAnalysisModule.ID + InAndOutAnalysisModule.JSON);
List<@NonNull SegmentSpecifier> read = InAndOutAnalysisModule.read(file);
fData.addAll(read);
Shell shell = getShell();
shell.setText("Configure In and Out analysis"); //$NON-NLS-1$
shell.addControlListener(resizeLayouter(shell));
parent.addControlListener(resizeLayouter(parent));
Composite localParent = (Composite) super.createDialogArea(parent);
localParent.addControlListener(resizeLayouter(localParent));
localParent.setLayout(GridLayoutFactory.fillDefaults().numColumns(2).create());
localParent.setLayoutData(GridDataFactory.fillDefaults().hint(400, 240).grab(true, true).create());
fSpecifiers = new ListViewer(localParent);
fSpecifiers.setLabelProvider(new LabelProvider());
fSpecifiers.setContentProvider(new ArrayContentProvider());
fSpecifiers.setInput(fData);
Control list = fSpecifiers.getControl();
list.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
fSpecifiers.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(@Nullable DoubleClickEvent event) {
edit(fSpecifiers.getSelection());
}
});
Composite controls = new Composite(localParent, SWT.NONE);
controls.setLayout(new GridLayout());
controls.setLayoutData(GridDataFactory.fillDefaults().grab(false, true).create());
fAdd = new Button(controls, SWT.PUSH);
fAdd.setEnabled(!fData.contains(InAndOutAnalysisModule.REFERENCE));
fAdd.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(@Nullable SelectionEvent e) {
add(fSpecifiers);
}
});
fAdd.setText("Add"); //$NON-NLS-1$
Button edit = new Button(controls, SWT.PUSH);
edit.setEnabled(false);
fSpecifiers.addSelectionChangedListener(event -> edit.setEnabled(!fSpecifiers.getSelection().isEmpty()));
edit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(@Nullable SelectionEvent e) {
edit(fSpecifiers.getSelection());
}
});
edit.setText("Edit"); //$NON-NLS-1$
Button remove = new Button(controls, SWT.PUSH);
remove.setEnabled(false);
fSpecifiers.addSelectionChangedListener(event -> remove.setEnabled(!event.getSelection().isEmpty() && fData.size() != 1));
remove.addSelectionListener(specifierAdapter(fSpecifiers, specifier -> {
fData.remove(specifier);
fSpecifiers.refresh();
fAdd.setEnabled(!fData.contains(InAndOutAnalysisModule.REFERENCE));
}));
remove.setText("Remove"); //$NON-NLS-1$
Button reset = new Button(controls, SWT.PUSH);
reset.setEnabled(false);
fSpecifiers.addSelectionChangedListener(event -> reset.setEnabled(fData.size() > 1 || !fData.contains(InAndOutAnalysisModule.REFERENCE)));
reset.addSelectionListener(specifierAdapter(fSpecifiers, specifier -> {
fData.clear();
add(fSpecifiers);
}));
reset.setText("Reset"); //$NON-NLS-1$
Button up = new Button(controls, SWT.PUSH);
up.setEnabled(false);
fSpecifiers.addSelectionChangedListener(event -> up.setEnabled(!event.getSelection().isEmpty() && fData.size() != 1));
up.addSelectionListener(specifierAdapter(fSpecifiers, specifier -> {
int index = fData.indexOf(specifier);
@NonNull
SegmentSpecifier temp = fData.get(index - 1);
fData.set(index - 1, specifier);
fData.set(index, temp);
fSpecifiers.refresh();
}));
up.setText("^"); //$NON-NLS-1$
Button down = new Button(controls, SWT.PUSH);
down.setEnabled(false);
fSpecifiers.addSelectionChangedListener(event -> down.setEnabled(!event.getSelection().isEmpty() && fData.size() != 1));
down.addSelectionListener(specifierAdapter(fSpecifiers, specifier -> {
int index = fData.indexOf(specifier);
@NonNull
SegmentSpecifier temp = fData.get(index + 1);
fData.set(index + 1, specifier);
fData.set(index, temp);
fSpecifiers.refresh();
}));
down.setText("v"); //$NON-NLS-1$
localParent.pack();
return localParent;
}
private static ControlListener resizeLayouter(Composite composite) {
return new ControlListener() {
@Override
public void controlResized(ControlEvent e) {
composite.layout();
}
@Override
public void controlMoved(ControlEvent e) {
composite.layout();
}
};
}
private void add(ListViewer specifiers) {
fData.add(new SegmentSpecifier(InAndOutAnalysisModule.REFERENCE));
specifiers.refresh();
fAdd.setEnabled(false);
}
private void edit(ISelection selection) {
Object first = ((StructuredSelection) selection).getFirstElement();
SegmentSpecifier specifier = (SegmentSpecifier) first;
SegmentSpecifierDialog dialog = new SegmentSpecifierDialog(getParentShell(), specifier, fTrace);
dialog.open();
if (dialog.isUpdated()) {
fSpecifiers.refresh();
fAdd.setEnabled(!fData.contains(InAndOutAnalysisModule.REFERENCE));
}
}
private static SelectionAdapter specifierAdapter(ListViewer specifiers, Consumer<@NonNull SegmentSpecifier> ss) {
return new SelectionAdapter() {
@Override
public void widgetSelected(@Nullable SelectionEvent e) {
ISelection selection = specifiers.getSelection();
Object firstElement = ((StructuredSelection) selection).getFirstElement();
if (firstElement != null) {
ss.accept((SegmentSpecifier) firstElement);
}
}
};
}
/**
* Has the dataset changed
*
* @return has the dataset changed?
*/
public boolean isChanged() {
return fChanged;
}
@Override
protected void buttonPressed(int buttonId) {
if (buttonId == Window.OK) {
File file = new File(fPath.getAbsolutePath() + File.separator + InAndOutAnalysisModule.ID + InAndOutAnalysisModule.JSON);
fChanged = !(Objects.equals(InAndOutAnalysisModule.read(file), fData));
if (fChanged) {
InAndOutAnalysisModule.write(file, fData);
File stateSystemFile = new File(fPath.getAbsolutePath() + File.separator + InAndOutAnalysisModule.ID + ".ht"); //$NON-NLS-1$
if (stateSystemFile.exists()) {
try {
Files.delete(stateSystemFile.toPath());
} catch (IOException e) {
Activator activator = Activator.getDefault();
if (activator == null) {
throw new IllegalStateException("Activator should not be null here, IO exception caught", e); //$NON-NLS-1$
}
activator.getLog().log(new Status(IStatus.ERROR, activator.getBundle().getSymbolicName(),
"Failed to delete file: " + stateSystemFile.getAbsolutePath(), e)); //$NON-NLS-1$
}
}
}
}
super.buttonPressed(buttonId);
}
}