-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIMainWindow.java
More file actions
464 lines (386 loc) · 18.5 KB
/
UIMainWindow.java
File metadata and controls
464 lines (386 loc) · 18.5 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import java.io.*;
import java.util.*;
import javafx.application.Application;
import javafx.beans.binding.*;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.stage.*;
public class UIMainWindow {
private DoubleProperty previousPasScrollbar = new SimpleDoubleProperty();
public final double getPreviousPasScrollbar() { return previousPasScrollbar.get(); }
public final void setPreviousPasScrollbar(double value) { previousPasScrollbar.set(value); }
public DoubleProperty previousPasScrollbarProperty() { return previousPasScrollbar; }
public CoordinatorMaster coordinator;
public List<UIDetachedTab> detachedTabs = new ArrayList<>();
public SensitiveDataVariable sdVariableToView = null;
public Stage window;
private Map<String, Node> contentPool;
private FileChooser fileChooser;
private double fontSize = 1.0;
private BorderPane layout;
private Scene scene;
private TabPane tabPane;
private RadioMenuItem menuNovice;
private RadioMenuItem menuIntermediate;
private RadioMenuItem menuAdvanced;
private RadioMenuItem menuExpert;
private ScrollPane scrollPanePAS;
private ScrollPane scrollPaneSD;
private Canvas canvasCG;
// TODO: other scrollpanes
public UIMainWindow(CoordinatorMaster coordinatorIn) {
contentPool = new HashMap<>();
coordinator = coordinatorIn;
fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter =
new FileChooser.ExtensionFilter("Program run files (*.vaccs)", "*.vaccs");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.setTitle("Load Program Run");
}
public void display() {
window = new Stage();
window.setTitle("SecureCvisual");
window.setMinWidth(250);
// Exit callback
window.setOnCloseRequest( e -> {
e.consume();
coordinator.closeProgram();
});
// File menu
Menu fileMenu = new Menu("File");
MenuItem menuOpen = new MenuItem("Open Program Run...");
menuOpen.setOnAction(e -> {
File file = fileChooser.showOpenDialog(window);
if (file != null) {
String absolutePath = file.getAbsolutePath();
coordinator.getRun().loadRun(absolutePath, 100, canvasCG);
// Parse source
Node sourceCodeLayout = UISourceCode.parseSourceCode(coordinator.getRun().cSource);
setTabContent(SubProgram.toString(SubProgram.SC), sourceCodeLayout);
// Load source file together with program run -- DISABLED, boss didn't like it
// String possibleSourceFile = absolutePath.substring(0, absolutePath.length() - 13) + ".c";
// try {
// Pane sourceCodeLayout = UISourceCode.loadSourceFile(possibleSourceFile);
// setTabContent(SubProgram.toString(SubProgram.SC), sourceCodeLayout);
// } catch (IOException ex) {
// System.err.println("Could not find source file " + possibleSourceFile);
// }
coordinator.queryProcessRunAndUpdateUI();
}
});
MenuItem menuExit = new MenuItem("Exit...");
menuExit.setOnAction(e -> coordinator.closeProgram());
fileMenu.getItems().addAll(menuOpen, menuExit);
// View menu
Menu viewMenu = new Menu("View");
// Detail mode
Menu detailMenu = new Menu("Detail Mode");
ToggleGroup detailToggle = new ToggleGroup();
menuNovice = new RadioMenuItem(DetailLevel.toString(DetailLevel.NOVICE));
menuNovice.setOnAction(e -> {
coordinator.runFilter.setDetailLevel(DetailLevel.NOVICE);
coordinator.queryProcessRunAndUpdateUI();
});
menuIntermediate = new RadioMenuItem(DetailLevel.toString(DetailLevel.INTERMEDIATE));
menuIntermediate.setOnAction(e -> {
coordinator.runFilter.setDetailLevel(DetailLevel.INTERMEDIATE);
coordinator.queryProcessRunAndUpdateUI();
});
menuAdvanced = new RadioMenuItem(DetailLevel.toString(DetailLevel.ADVANCED));
menuAdvanced.setOnAction(e -> {
coordinator.runFilter.setDetailLevel(DetailLevel.ADVANCED);
coordinator.queryProcessRunAndUpdateUI();
});
menuExpert = new RadioMenuItem(DetailLevel.toString(DetailLevel.EXPERT));
menuExpert.setOnAction(e -> {
coordinator.runFilter.setDetailLevel(DetailLevel.EXPERT);
coordinator.queryProcessRunAndUpdateUI();
});
menuNovice.setToggleGroup(detailToggle);
menuIntermediate.setToggleGroup(detailToggle);
menuAdvanced.setToggleGroup(detailToggle);
menuExpert.setToggleGroup(detailToggle);
coordinator.runFilter.detailLevelProperty().addListener((obs, oldv, newv) -> {
switch (newv) {
case NOVICE: menuNovice.setSelected(true); break;
case INTERMEDIATE: menuIntermediate.setSelected(true); break;
case ADVANCED: menuAdvanced.setSelected(true); break;
case EXPERT: menuExpert.setSelected(true); break;
}
});
detailMenu.getItems().addAll(menuNovice, menuIntermediate, menuAdvanced, menuExpert);
viewMenu.getItems().add(detailMenu);
// View elements
Menu viewElementsMenu = new Menu("View Elements");
CheckMenuItem menuSimplifiedPas = new CheckMenuItem("Simplified PAS");
menuSimplifiedPas.selectedProperty().bindBidirectional(coordinator.runFilter.simplifiedPasProperty());
menuSimplifiedPas.setOnAction(e -> setCustomDetailLevel());
CheckMenuItem menuShowHiddenFunctions = new CheckMenuItem("Hidden Functions");
menuShowHiddenFunctions.selectedProperty().bindBidirectional(coordinator.runFilter.showAllFunctionsProperty());
menuShowHiddenFunctions.setOnAction(
e -> {
if (coordinator.runFilter.getShowAllFunctions()) coordinator.runFilter.setSimplifiedPas(false);
setCustomDetailLevel();
}
);
CheckMenuItem menuShowRegisters = new CheckMenuItem("CPU Registers");
menuShowRegisters.selectedProperty().bindBidirectional(coordinator.runFilter.showRegistersProperty());
menuShowRegisters.setOnAction(
e -> {
if (coordinator.runFilter.getShowRegisters()) coordinator.runFilter.setSimplifiedPas(false);
setCustomDetailLevel();
}
);
CheckMenuItem menuShowProgramSections = new CheckMenuItem("All Program Sections");
menuShowProgramSections.selectedProperty().bindBidirectional
(coordinator.runFilter.showAllSectionsByDefaultProperty());
menuShowProgramSections.setOnAction(
e -> {
if (coordinator.runFilter.getShowAllSectionsByDefault()) coordinator.runFilter.setSimplifiedPas(false);
setCustomDetailLevel();
}
);
MenuItem menuShowTextData = new MenuItem("Show .text, .(ro)data");
menuShowTextData.setOnAction(
e -> {
coordinator.runFilter.setSimplifiedPas(false);
coordinator.runFilter.resetSectionFilter();
setCustomDetailLevel();
}
);
CheckMenuItem menuShowAssemblyCode = new CheckMenuItem("Assembly Code");
menuShowAssemblyCode.selectedProperty().bindBidirectional(coordinator.runFilter.showAssemblyProperty());
menuShowAssemblyCode.setOnAction(e -> setCustomDetailLevel());
CheckMenuItem menuShowProgramOutput = new CheckMenuItem("Program Output");
menuShowProgramOutput.selectedProperty().bindBidirectional(coordinator.runFilter.showOutputProperty());
CheckMenuItem menuShowOffsets = new CheckMenuItem("Offsets");
menuShowOffsets.selectedProperty().bindBidirectional(coordinator.runFilter.showOffsetsProperty());
// CheckMenuItem menuShowMemoryLayout = new CheckMenuItem("Memory Layout"); // TODO
viewElementsMenu.getItems().addAll(menuSimplifiedPas, menuShowHiddenFunctions, menuShowRegisters,
menuShowProgramSections, menuShowTextData, menuShowAssemblyCode,
menuShowProgramOutput, menuShowOffsets /*menuShowMemoryLayout*/);
viewMenu.getItems().add(viewElementsMenu);
// Filters
Menu filtersMenu = new Menu("Filters");
MenuItem menuClearRegisterFilters = new MenuItem("Reset Register Filters");
menuClearRegisterFilters.setOnAction(e -> {
coordinator.runFilter.clearRegisterFilter();
coordinator.queryProcessRunAndUpdateUI();
});
MenuItem menuClearSectionFilters = new MenuItem("Reset Section Filters");
menuClearSectionFilters.setOnAction(e -> {
coordinator.runFilter.clearSectionFilter();
coordinator.queryProcessRunAndUpdateUI();
});
filtersMenu.getItems().addAll(menuClearRegisterFilters, menuClearSectionFilters);
viewMenu.getItems().add(filtersMenu);
MenuItem menuIncreaseFontSize = new MenuItem("Increase Font Size");
menuIncreaseFontSize.setOnAction(e -> {
if (fontSize < 5.0) fontSize += 0.1;
tabPane.setStyle("-fx-font-size: " + UIUtils.calculateFontSize(fontSize, scene.getWidth(), scene.getHeight()));
});
menuIncreaseFontSize.setAccelerator(new KeyCodeCombination(KeyCode.EQUALS, KeyCombination.CONTROL_DOWN));
viewMenu.getItems().add(menuIncreaseFontSize);
MenuItem menuDecreaseFontSize = new MenuItem("Decrease Font Size");
menuDecreaseFontSize.setOnAction(e -> {
if (fontSize > 0.1) fontSize -= 0.1;
tabPane.setStyle("-fx-font-size: " + UIUtils.calculateFontSize(fontSize, scene.getWidth(), scene.getHeight()));
});
menuDecreaseFontSize.setAccelerator(new KeyCodeCombination(KeyCode.MINUS, KeyCombination.CONTROL_DOWN));
viewMenu.getItems().add(menuDecreaseFontSize);
// Help menu TODO
Menu helpMenu = new Menu("Help");
MenuItem menuDocumentation = new MenuItem("Documentation...");
MenuItem menuAbout = new MenuItem("About...");
MenuItem menuVersion = new MenuItem("Alpha v18.05.02");
helpMenu.getItems().addAll(menuDocumentation, menuAbout, menuVersion);
//Main menu bar
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu, viewMenu, helpMenu);
//Tabs
Group root = new Group();
scene = new Scene(root, 400, 250, Color.WHITE);
tabPane = new TabPane();
BorderPane borderPane = new BorderPane();
// Tabs
String tabName = "";
for (int i = 0; i < 5; ++i) {
switch (i) {
case 0: tabName = SubProgram.toString(SubProgram.PAS); break;
case 1: tabName = SubProgram.toString(SubProgram.CG); break;
case 2: tabName = SubProgram.toString(SubProgram.FO); break;
case 3: tabName = SubProgram.toString(SubProgram.SD); break;
case 4: tabName = SubProgram.toString(SubProgram.SC); break;
default:
System.err.println("Error populating tabs");
System.exit(1);
}
// initialize
Tab tab = new Tab();
tab.setClosable(false);
tab.setText(tabName);
tab.setId(tabName);
// content
HBox content = new HBox();
content.getChildren().add(new Label(tabName));
content.setAlignment(Pos.CENTER);
// make detachable
ContextMenu contextMenu = new ContextMenu();
MenuItem detach = new MenuItem("Detach " + tabName);
detach.setOnAction(e -> detachTab(tab.getText()));
contextMenu.getItems().add(detach);
tab.setContextMenu(contextMenu);
contextMenu.show(tabPane.lookup("#" + tabName), Side.RIGHT, 0, 0);
// finalize
contentPool.put(tabName, content);
tab.setContent(contentPool.get(tabName));
tabPane.getTabs().add(tab);
}
// bind to take available space
borderPane.prefHeightProperty().bind(scene.heightProperty());
borderPane.prefWidthProperty().bind(scene.widthProperty());
// scene size change listeners
scene.widthProperty().addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth, Number newSceneWidth) {
tabPane.setStyle("-fx-font-size: " + UIUtils.calculateFontSize(fontSize, scene.getWidth(), scene.getHeight()));
}
});
scene.heightProperty().addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
tabPane.setStyle("-fx-font-size: " + UIUtils.calculateFontSize(fontSize, scene.getWidth(), scene.getHeight()));
}
});
// Set up canvas for call graph
canvasCG = new Canvas(getTabWindow(SubProgram.toString(SubProgram.CG)).getWidth(),
getTabWindow(SubProgram.toString(SubProgram.CG)).getHeight());
canvasCG.heightProperty().bind(getTabWindow(SubProgram.toString(SubProgram.CG)).heightProperty());
canvasCG.widthProperty().bind(getTabWindow(SubProgram.toString(SubProgram.CG)).widthProperty());
borderPane.setCenter(tabPane);
borderPane.setTop(menuBar);
root.getChildren().add(borderPane);
window.setScene(scene);
window.show();
}
public void setCustomDetailLevel() {
menuNovice.setSelected(false);
menuIntermediate.setSelected(false);
menuAdvanced.setSelected(false);
menuExpert.setSelected(false);
coordinator.runFilter.setDetailLevel(DetailLevel.CUSTOM);
coordinator.queryProcessRunAndUpdateUI();
UISourceCode.toggleAssemblyWindow(coordinator.runFilter.getShowAssembly());
}
public void detachTab(String title) {
Tab tab = tabPane.getTabs().stream().filter(t -> t.getId().equals(title)).findAny().orElse(null);
assert tab != null;
UIDetachedTab detachedTab = new UIDetachedTab(this, coordinator, contentPool.get(title), title);
detachedTabs.add(detachedTab);
detachedTab.display();
tabPane.getTabs().remove(tab);
coordinator.queryProcessRunAndUpdateUI();
}
public void reattachTab(UIDetachedTab dtab) {
detachedTabs.remove(dtab);
Tab tab = new Tab();
tab.setClosable(false);
tab.setText(dtab.title);
tab.setId(dtab.title);
// tab.setContent(contentPool.get(title));
// Must be made detachable again
ContextMenu contextMenu = new ContextMenu();
MenuItem detach = new MenuItem("Detach " + dtab.title);
detach.setOnAction(e -> detachTab(dtab.title));
contextMenu.getItems().add(detach);
tab.setContextMenu(contextMenu);
contextMenu.show(tabPane.lookup("#" + dtab.title), Side.RIGHT, 0, 0);
tabPane.getTabs().add(tab);
coordinator.queryProcessRunAndUpdateUI();
}
private void setTabContent(String title, Node content) {
// contentPool.put(title, content);
Tab tab = tabPane.getTabs().stream().filter(t -> t.getId().equals(title)).findAny().orElse(null);
if (tab != null) {
tab.setContent(content);
return;
}
UIDetachedTab dtab = detachedTabs.stream().filter(t -> t.title.equals(title)).findAny().orElse(null);
if (dtab != null) {
dtab.setContent(content);
return;
}
assert false; /* */
}
public Stage getTabWindow(String title) {
Tab tab = tabPane.getTabs().stream().filter(t -> t.getId().equals(title)).findAny().orElse(null);
if (tab != null) return window;
UIDetachedTab dtab = detachedTabs.stream().filter(t -> t.title.equals(title)).findAny().orElse(null);
if (dtab != null) return dtab.window;
assert false; /* */
return null;
}
public void updateUI(int sourceLine,
String assembly,
List<ActivationRecord> stack,
TreeMap<String, String> registers,
TreeMap<String, VariableDelta> variables,
ArrayList<ProgramSection> sections)
{
// Update PAS
scrollPanePAS = UIProgramAddressSpace.buildPAS(this,
scene,
coordinator.runFilter.getDetailLevel(),
stack,
registers,
variables,
sections);
scrollPanePAS.setVvalue(getPreviousPasScrollbar());
previousPasScrollbarProperty().bind(scrollPanePAS.vvalueProperty());
setTabContent(SubProgram.toString(SubProgram.PAS), scrollPanePAS);
// Update source code
setTabContent(SubProgram.toString(SubProgram.SC),
UISourceCode.buildSC(scene, sourceLine, assembly));
// Update SD
scrollPaneSD = UISensitiveData.buildSD(this,
scene,
coordinator.runFilter.getAllSensitiveDataStates(coordinator.getRun()),
coordinator.runFilter.getLastSensitiveDataState(coordinator.getRun()),
sdVariableToView);
setTabContent(SubProgram.toString(SubProgram.SD), scrollPaneSD);
// TODO: other tabs
}
public String saveConfig() {
String config = "";
config += "MainWindowX:" + Double.toString(window.getX()) + System.lineSeparator();
config += "MainWindowY:" + Double.toString(window.getY()) + System.lineSeparator();
config += "MainWindowWidth:" + Double.toString(window.getWidth()) + System.lineSeparator();
config += "MainWindowHeight:" + Double.toString(window.getHeight()) + System.lineSeparator();
config += "FontSize:" + Double.toString(fontSize) + System.lineSeparator();
config += "VarRepFontSize:" + Double.toString(UIVariableRepresentation.fontSize) + System.lineSeparator();
for (UIDetachedTab tab : detachedTabs) config += "DetachedTab:" + tab.title + System.lineSeparator();
for (UIDetachedTab tab : detachedTabs) config += tab.saveConfig();
return config;
}
public void loadConfig(List<String> config) {
for (String line : config) {
String[] parameters = line.trim().split(":");
if (parameters.length > 1) {
if (parameters[0].equals("MainWindowX")) window.setX(Double.parseDouble(parameters[1]));
else if (parameters[0].equals("MainWindowY")) window.setY(Double.parseDouble(parameters[1]));
else if (parameters[0].equals("MainWindowWidth")) window.setWidth(Double.parseDouble(parameters[1]));
else if (parameters[0].equals("MainWindowHeight")) window.setHeight(Double.parseDouble(parameters[1]));
else if (parameters[0].equals("DetachedTab")) detachTab(parameters[1]);
else if (parameters[0].equals("FontSize")) fontSize = Double.parseDouble(parameters[1]);
else if (parameters[0].equals("VarRepFontSize")) UIVariableRepresentation.fontSize =
Double.parseDouble(parameters[1]);
}
}
for (UIDetachedTab tab : detachedTabs) tab.loadConfig(config);
}
}