-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseDialogClass.java
More file actions
286 lines (212 loc) · 10.6 KB
/
DatabaseDialogClass.java
File metadata and controls
286 lines (212 loc) · 10.6 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ocr3;
import java.io.File;
import java.io.FilenameFilter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
/**
*
* @author gsb
*/
public class DatabaseDialogClass {
Dialog databaseDialog;
//ChoiceBox<String> databaseCB;
// ChoiceBox<String> rowListChoice;
ListView<String> dbListView;
ListView<String> tableListView;
ListView<String> rowListView;
String selectedChKey = "";
String selectedTable = "";
String selectedDatabase = "burmese.db";//default database
ButtonType deleteButtonType;
// to store recent table's characterKeys
ArrayList<String> characterKeyList = new ArrayList<>();
// TextField textField;
// TextField textField2;
public DatabaseDialogClass(){
DialogPane dgPane = new DialogPane();//create a custom dialogpane
//dgPane.setHeader(null); // no need of header, that's ugly!
GridPane gridPane = new GridPane();
//----------------Handling Database list--------------------------------
Label dbLabel = new Label("Select database:");
//Get the database files
String[] dbList = getDBFileList();
//Add the database files in the dbListView
dbListView = new ListView<>();
if(dbList.length>0)dbListView.getItems().addAll(dbList);
dbListView.setPrefSize(150, 100);
dbListView.getSelectionModel().selectedItemProperty()
.addListener(this::dbSelectionChanged);
Button dbDeleteButton = new Button("Delete Selected Database");
dbDeleteButton.setOnAction(ac -> {
if(!selectedDatabase.isEmpty()){
//Get the path of the selected database file
String curDir = System.getProperty("user.dir")+"/"+selectedDatabase;
System.out.println("filePath: "+curDir);
File ff = new File(curDir);
if(!ff.delete()){
Alert alert = new Alert(AlertType.ERROR);
alert.setHeaderText("Cannot delete File. Something went wrong!");
alert.setContentText(null);
alert.showAndWait();
}
else{
//After successfully deleting file, update the database list in the UI
dbListView.getItems().clear();
tableListView.getItems().clear();
rowListView.getItems().clear();
String[] dbList2 = getDBFileList();
if(dbList2.length>0)dbListView.getItems().addAll(dbList2);
}
}
});
//----------------Handling Table list--------------------------------
Label cgLabel = new Label("Select a table:");
// tablelistChoice = new ChoiceBox<>();
// tablelistChoice.setMinWidth(200);
// //tablelistChoice.getSelectionModel().
tableListView = new ListView<>();
tableListView.setPrefSize(200, 100);
tableListView.getSelectionModel().selectedItemProperty()
.addListener(this::tableSelectionChanged);
Button tableDeleteButton = new Button("Delete Selected Table");
tableDeleteButton.setOnAction(ac -> {
if(!selectedTable.isEmpty()){
try {
SQLiteManager.deleteTableFromDB(selectedTable, selectedDatabase);
ArrayList<String> tableList1 = SQLiteManager.getAllTableNamesFromDB(selectedDatabase);
//clear the previous list of tables in the tablelistView
tableListView.getItems().clear();
rowListView.getItems().clear();
//load the tablelist of current database to the tableListView
if(!tableList1.isEmpty())tableListView.getItems().addAll(tableList1);
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(DatabaseDialogClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
//----------------Handling row list--------------------------------
Label rowLabel = new Label("Select row:");
rowListView = new ListView<>();
rowListView.setPrefSize(500, 200);
rowListView.getSelectionModel().selectedItemProperty()
.addListener(this::rowSelectionChanged);
Button rowDeleteButton = new Button("Delete Selected Row");
rowDeleteButton.setOnAction(ac -> {
if(!selectedChKey.isEmpty()){
try {
SQLiteManager.deleteRowFromTable(selectedChKey,
selectedTable, selectedDatabase);
ArrayList<String> rows2 = this.getRows(selectedTable, selectedDatabase);
rowListView.getItems().clear();
if(!rows2.isEmpty())rowListView.getItems().addAll(rows2);
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(DatabaseDialogClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
gridPane.addRow(0, dbLabel, cgLabel, rowLabel);
gridPane.addRow(1, dbListView, tableListView, rowListView);
gridPane.addRow(2, dbDeleteButton, tableDeleteButton, rowDeleteButton);
//gridPane.addRow(1, rowLabel, rowListChoice);
gridPane.setHgap(20);
gridPane.setVgap(10);
//Add gridpane to the dialogPane
dgPane.setContent(gridPane);
dgPane.setHeaderText("Database Operations");
//Add cancel and Add button to the dialogPane
//ButtonType addButton = new ButtonType("Add");
// deleteButtonType = new ButtonType("Delete");
dgPane.getButtonTypes().addAll(ButtonType.CLOSE);
// textField.requestFocus();
databaseDialog = new Dialog(); //create a dialog
databaseDialog.setDialogPane(dgPane);// set the dialogpane to the dialog
//settingsDialog.showAndWait();
}
//-----------------------constructor ended----------------------------------
//---------------------methods starting-------------------------------------
public String[] getDBFileList(){
String curDir = System.getProperty("user.dir")+"/src/ocr3";
System.out.println("Current directory Path: "+curDir);
File ff = new File(curDir);
FilenameFilter dbFileFilter = new DBFileFilter("db");
// String[] fileList = ff.list();
String[] dbList = ff.list(dbFileFilter);
return dbList;
}
public void dbSelectionChanged(ObservableValue<? extends String> observable,
String oldDB, String newDB){
System.out.println("Database selected: "+ newDB);
//ArrayList<String> tableList2;
if(characterKeyList != null)characterKeyList.clear();
try {
if(!newDB.isEmpty()){
selectedDatabase = newDB;
ArrayList<String> tableList1 = SQLiteManager.getAllTableNamesFromDB(newDB);
//clear the previous list of tables in the tablelistView
tableListView.getItems().clear();
//load the tablelist of current database to the tableListView
if(!tableList1.isEmpty())tableListView.getItems().addAll(tableList1);
}
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(DatabaseDialogClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void tableSelectionChanged(ObservableValue<? extends String> observable,
String oldTable, String newTable){
System.out.println("Table selected: "+ newTable);
if(newTable != null){
selectedTable = newTable;
ArrayList<String> rows2 = this.getRows(selectedTable, selectedDatabase);
rowListView.getItems().clear();
if(!rows2.isEmpty())rowListView.getItems().addAll(rows2);
}
}
public void rowSelectionChanged(ObservableValue<? extends String> observable,
String oldRow, String newRow){
int index = rowListView.getSelectionModel().getSelectedIndex();
System.out.println("row Index: "+index);
//when group changes, rowIndex becomes -1, so ignore it.
if(index>=0){
selectedChKey = characterKeyList.get(index);
System.out.println("Selected character Key: "+ selectedChKey);
}
}
public ArrayList<String> getRows(String table, String database){
ArrayList<String> rows2 = new ArrayList<>();
try {
if(!table.isEmpty() || table == null || database == null || !database.isEmpty()){
ArrayList<String[]> rows = SQLiteManager.getAllRowsFromTable(table, database);
characterKeyList.clear();
System.out.println("table and database exist.");
if(!rows.isEmpty()){
System.out.println("rows not empty.");
rows.forEach(e -> {
rows2.add(e[0]+" = "+e[1]);
characterKeyList.add(e[0]);
});
System.out.println("characterKeyList length: "+characterKeyList.size());
}
}
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(DatabaseDialogClass.class.getName()).log(Level.SEVERE, null, ex);
}
return rows2;
}
}