Skip to content

Commit a2e932b

Browse files
committed
file changes tracker added
1 parent 666f75a commit a2e932b

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/main/java/edu/upvictoria/javasqlide/controllers/IDEController.java

+29-11
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@ public class IDEController {
3636
private final Reader reader = new Reader();
3737
private File file = null;
3838

39-
@FXML
40-
private TreeView<String> dbFileView;
41-
@FXML
42-
private TabPane tabs;
43-
@FXML
44-
private TextArea errorMessages;
45-
@FXML
46-
private CodeArea codeArea;
47-
@FXML
48-
private TableView<ArrayList<String>> tableView;
39+
@FXML private TreeView<String> dbFileView;
40+
@FXML private TabPane tabs;
41+
@FXML private TextArea errorMessages;
42+
@FXML private CodeArea codeArea;
43+
@FXML private TableView<ArrayList<String>> tableView;
44+
@FXML Label fileTitle;
4945

5046
private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", Analyzer.getKeywords()) + ")\\b";
5147
private static final String DATATYPES_PATTERN = "\\b(" + String.join("|", Analyzer.getDataTypes()) + ")\\b";
@@ -135,9 +131,17 @@ protected void openFile(ActionEvent event) {
135131

136132
if (file != null) {
137133
codeArea.replaceText(getFileContent(file));
134+
checkFileChanges();
138135
}
139136
}
140137

138+
@FXML
139+
protected void closeFile(ActionEvent event) {
140+
codeArea.replaceText("");
141+
file = null;
142+
checkFileChanges();
143+
}
144+
141145
private String getFileContent(File file){
142146
StringBuilder text = new StringBuilder();
143147
Charset charset = StandardCharsets.UTF_8;
@@ -165,6 +169,8 @@ protected void saveFile(ActionEvent event) {
165169
}
166170

167171
saveTextToFile(codeArea.getText(), file);
172+
file = new File(file.getAbsolutePath());
173+
checkFileChanges();
168174
}
169175

170176
@FXML
@@ -180,6 +186,8 @@ protected void saveFileAs() {
180186
selectedFile = new File(selectedFile.getAbsolutePath() + ".sql");
181187
}
182188
saveTextToFile(codeArea.getText(), selectedFile);
189+
file = selectedFile;
190+
checkFileChanges();
183191
}
184192
}
185193

@@ -267,11 +275,21 @@ protected void createTable(ArrayList<ArrayList <String>> data){
267275
}
268276

269277
@FXML
270-
protected void checkFileChanges(KeyEvent event) {
278+
protected void checkFileChanges() {
279+
if(file == null) {
280+
fileTitle.setText("Untitled");
281+
return;
282+
}
283+
271284
String codeAreaText = codeArea.getText();
272285
String fileText = getFileContent(file);
273286

287+
if(codeAreaText.equals(fileText)){
288+
fileTitle.setText(file.getName());
289+
return;
290+
}
274291

292+
fileTitle.setText(file.getName() + " - Unsaved Changes");
275293
}
276294

277295
private StyleSpans<Collection<String>> computeHighlighting(String text) {

src/main/resources/edu/upvictoria/javasqlide/main.fxml

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</cursor>
1717
<Menu mnemonicParsing="false" text="File">
1818
<MenuItem mnemonicParsing="false" onAction="#openFile" text="Open..."/>
19+
<MenuItem mnemonicParsing="false" onAction="#closeFile" text="Close"/>
1920
<SeparatorMenuItem mnemonicParsing="false"/>
2021
<MenuItem mnemonicParsing="false" onAction="#saveFile" text="Save"/>
2122
<MenuItem mnemonicParsing="false" onAction="#saveFileAs" text="Save as..."/>
@@ -61,9 +62,11 @@
6162
<Tooltip text="Run Script"/>
6263
</tooltip>
6364
</Button>
65+
<Region prefHeight="29.0" prefWidth="400.0" />
66+
<Label fx:id="fileTitle" text="Untitled" />
6467
</ToolBar>
6568
<Pane prefHeight="450.0" prefWidth="1062.0">
66-
<CodeArea fx:id="codeArea" onKeyPressed="#checkFileChanges" layoutX="3.0" layoutY="1.0" prefHeight="435.0" prefWidth="1055.0"/>
69+
<CodeArea fx:id="codeArea" onKeyTyped="#checkFileChanges" layoutX="3.0" layoutY="1.0" prefHeight="435.0" prefWidth="1055.0"/>
6770
</Pane>
6871
<TabPane fx:id="tabs" prefHeight="280.0" prefWidth="1062.0" tabClosingPolicy="UNAVAILABLE">
6972
<Tab text="Statement Output">

0 commit comments

Comments
 (0)