-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogHelper.java
More file actions
156 lines (142 loc) · 5.29 KB
/
Copy pathDialogHelper.java
File metadata and controls
156 lines (142 loc) · 5.29 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
/*
* File: proj6BayyurtWenZhang.DialogHelper.java
* Names: Izge Bayyurt, Muqing Wen, Chloe Zhang
* Class: CS361
* Project 6
* Date: 3/17/2022
*/
package proj6BayyurtWenZhang;
import javafx.scene.control.*;
import javafx.stage.FileChooser;
import java.io.File;
import java.util.HashMap;
import java.util.Optional;
/**
* Contains helper methods for creating dialogs.
*/
public class DialogHelper {
private final TabHelper tabHelper;
private final FileController fileHelper;
private final TabPane tabPane;
private HashMap<Tab, File> filenameFileMap;
private HashMap<Tab, Boolean> textHasChangedMap;
private final FileChooser chooser = new FileChooser();
public DialogHelper(TabPane tabPane, TabHelper tabHelper,
FileController fileHelper, HashMap<Tab, File> filenameFileMap,
HashMap<Tab, Boolean> textHasChangedMap){
this.tabHelper = tabHelper;
this.fileHelper = fileHelper;
this.tabPane = tabPane;
this.filenameFileMap = filenameFileMap;
this.textHasChangedMap = textHasChangedMap;
}
/**
* Constructs and manages the About Dialog.
*/
public void aboutDialog(){
Dialog<String> dialog = new Dialog<>();
dialog.setTitle("About...");
dialog.setContentText("This is project 6 by Izge, Wen, and Chloe");
ButtonType ok = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().add(ok);
dialog.showAndWait();
}
/**
* Constructs and manages the What's New Dialog.
*/
public void whatsnewDialog(){
Dialog<String> dialog = new Dialog<>();
dialog.setTitle("What's New?");
dialog.setContentText("New Feature: Toggle Comments,Help Menu, Find Menu Item.");
ButtonType ok = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().add(ok);
dialog.showAndWait();
}
/**
* Builds and manages the Save As Dialog box. Saves a Tab's contents
* to a file path.
*
* @return true if file is saved, false if process is cancelled
*/
public boolean saveAsDialog()
{
String currentTabTitle = tabHelper.getCurrentTabTitle();
chooser.setTitle("Save as...");
chooser.setInitialFileName(currentTabTitle);
File currentFile = chooser.showSaveDialog(null);
if (currentFile != null) {
this.filenameFileMap.put(tabHelper.getCurrentTab(), currentFile);
boolean saved = fileHelper.saveCurrentFile(currentFile);
if(saved){
this.textHasChangedMap.put(tabHelper.getCurrentTab(), false);
tabHelper.getCurrentTab().setTooltip(new Tooltip(currentFile.toString()));
}
return true;
}
else{
return false;
}
}
/**
* Builds and manages the Open Dialog. Opens a file into a Tab.
*
*/
public void openDialog()
{
chooser.setTitle("Open file...");
File openedFile = chooser.showOpenDialog(null);
if (openedFile != null){
for(Tab tab:tabPane.getTabs()){
if(openedFile.equals(filenameFileMap.getOrDefault(tab, null))){
tabPane.getSelectionModel().select(tab);
return;
}
}
Tab newTab = new Tab(openedFile.getName());
tabPane.getTabs().add(newTab);
tabPane.getSelectionModel().select(newTab);
tabHelper.createCodeAreaForTab(newTab);
fileHelper.writeFileToCodeArea(openedFile);
// remember path when saving
filenameFileMap.put(tabHelper.getCurrentTab(), openedFile);
textHasChangedMap.put(tabHelper.getCurrentTab(), false);
newTab.setTooltip(new Tooltip(openedFile.toString()));
}
}
/**
* Constructs the Close Dialog and returns user selection.
*
* @return which type of Button was clicked within the dialog.
*/
public Optional<ButtonType> closeDialog(){
Alert alert = createConfirmationAlert( String.format(
"Do you want to save " + "your progress on %s " +
"before closing?", tabHelper.getCurrentTab().getText()));
return alert.showAndWait();
}
/**
* Constructs the Save Dialog and returns user selection.
*
* @return which type of Button was clicked within the dialog.
*/
public Optional<ButtonType> saveDialog() {
Alert alert = createConfirmationAlert(
"Do you want to save your changes before compiling?");
return alert.showAndWait();
}
/**
* Constructs a confirmation alert box with custom header string
*
* @param headerText - the header text to be displayed for the alert
* @return Alert - the alert object created
*/
private Alert createConfirmationAlert(String headerText){
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setHeaderText(headerText);
ButtonType yes = new ButtonType("Yes");
ButtonType no = new ButtonType("No");
ButtonType cancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(yes, no, cancel);
return alert;
}
}