/
ButtonsFactory.java
155 lines (135 loc) · 5.94 KB
/
ButtonsFactory.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
package org.example.scelldemo.controls.factory;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.example.scelldemo.controls.FlatIconicButton;
import org.example.scelldemo.controls.ScellWrapper;
import org.example.scelldemo.controls.helper.ButtonsManager;
import org.example.scelldemo.controls.helper.Utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public final class ButtonsFactory {
private static final HashMap<String, String> openButtonsIconsToFilesMap = new HashMap<String, String>() {{
put("dollar.png", "Formats.xlsx");
put("bar_chart.png", "Travel Budget.xlsx");
put("flag_01.png", "Example1.xlsx");
put("line_chart.png", "Climate data for Barcelona.xlsx");
}};
private final ScellWrapper scell;
private final ButtonsManager buttonsManager;
private final Stage primaryStage;
private final FileChooser fileChooser;
private final ClassLoader resourceLoader;
public ButtonsFactory(ScellWrapper scell, Stage primaryStage, ClassLoader resourceLoader) {
this.scell = scell;
this.primaryStage = primaryStage;
this.resourceLoader = resourceLoader;
this.buttonsManager = new ButtonsManager();
this.fileChooser = new FileChooser();
FileChooser.ExtensionFilter extensionFilter =
new FileChooser.ExtensionFilter("SpreadSheet files (*.xlsx)", "*.xlsx");
this.fileChooser.getExtensionFilters().add(extensionFilter);
}
public Button createCreateNewButton() {
Button result = new FlatIconicButton("icons/add.png");
result.setOnAction(actionEvent -> this.scell.createNew());
this.buttonsManager.add(result);
return result;
}
public Button createOpenButton() {
Button result = new FlatIconicButton("icons/upload.png");
result.setOnAction(actionEvent -> {
this.fileChooser.setTitle("Open file");
this.buttonsManager.setDisable(true);
this.scell.loadSpreadsheet(this.fileChooser.showOpenDialog(this.primaryStage)).thenRun(() ->
this.buttonsManager.setDisable(false));
});
this.buttonsManager.add(result);
return result;
}
public Button createUndoButton() {
Button result = new FlatIconicButton("icons/undo.png");
result.setOnAction(actionEvent -> this.scell.undo());
this.buttonsManager.add(result);
return result;
}
public Button createRedoButton() {
Button result = new FlatIconicButton("icons/redo.png");
result.setOnAction(actionEvent -> this.scell.redo());
this.buttonsManager.add(result);
return result;
}
public Button createSaveButton() {
Button result = new FlatIconicButton("icons/save.png");
result.setOnAction(actionEvent -> {
this.buttonsManager.setDisable(true);
this.fileChooser.setTitle("Save file");
this.fileChooser.setInitialFileName("default.xlsx");
this.scell.saveAs(this.fileChooser.showSaveDialog(this.primaryStage)).whenComplete((e, throwable) ->
this.buttonsManager.setDisable(false));
});
this.buttonsManager.add(result);
return result;
}
public Button createAboutButton() {
Button result = new FlatIconicButton("icons/help.png");
result.setOnAction(actionEvent -> this.showAboutDialog());
return result;
}
public Pane createSearchControlsPanel() {
TextField patternInput = new TextField();
patternInput.setStyle("-fx-background-radius: 0;");
patternInput.setMaxHeight(0.01);
FlatIconicButton searchForwardButton = new FlatIconicButton("icons/looking_glass.png");
this.buttonsManager.add(searchForwardButton);
HBox result = new HBox();
result.setAlignment(Pos.CENTER_RIGHT);
result.setSpacing(5);
result.getChildren().addAll(patternInput, searchForwardButton);
this.scell.getScellControlFuture().thenAccept(node -> {
if (node instanceof Label) {
return;
}
this.scell.addSearchActions(patternInput, searchForwardButton, this.primaryStage.getScene().getWindow());
});
return result;
}
public List<Button> createOpenPreloadedFilesButtons() {
List<Button> result = new ArrayList<>();
openButtonsIconsToFilesMap.forEach((iconFileName, xlsxFileName) -> {
Button openFileButton = new FlatIconicButton("icons/" + iconFileName);
openFileButton.setOnAction(e -> this.loadPreset("presets/" + xlsxFileName));
this.buttonsManager.add(openFileButton);
result.add(openFileButton);
});
return result;
}
private void showAboutDialog() {
Alert aboutDialog = new Alert(Alert.AlertType.INFORMATION);
aboutDialog.setTitle("About");
aboutDialog.initOwner(this.primaryStage.getScene().getWindow());
aboutDialog.setHeaderText(this.scell.getApiVersionsInfo());
aboutDialog.setContentText(this.scell.getPlatformVersionsInfo());
aboutDialog.showAndWait();
}
private void loadPreset(String resourcePath) {
this.buttonsManager.setDisable(true);
try (InputStream stream = this.resourceLoader.getResourceAsStream(resourcePath)) {
Utils.withErrorHandling(
this.scell.loadSpreadsheet(stream, resourcePath),
this.primaryStage.getScene().getWindow())
.whenComplete((unused, throwable) -> this.buttonsManager.setDisable(false));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}