1
1
package edu .upvictoria .javasqlide .controllers ;
2
2
3
3
import edu .upvictoria .poo .Analyzer ;
4
+ import edu .upvictoria .poo .Reader ;
4
5
import edu .upvictoria .poo .Column ;
5
6
import edu .upvictoria .poo .Database ;
6
7
import edu .upvictoria .poo .Table ;
8
+ import javafx .beans .property .SimpleStringProperty ;
9
+ import javafx .collections .FXCollections ;
10
+ import javafx .collections .ObservableList ;
7
11
import javafx .event .ActionEvent ;
8
12
import javafx .fxml .FXML ;
9
13
import javafx .scene .control .*;
13
17
import javafx .scene .input .KeyEvent ;
14
18
import javafx .stage .DirectoryChooser ;
15
19
import javafx .stage .FileChooser ;
16
- import javafx .stage .Stage ;
17
20
import org .fxmisc .richtext .CodeArea ;
18
21
import org .fxmisc .richtext .LineNumberFactory ;
19
22
import org .fxmisc .richtext .model .StyleSpans ;
24
27
import java .nio .charset .StandardCharsets ;
25
28
import java .time .LocalDateTime ;
26
29
import java .time .format .DateTimeFormatter ;
27
- import java .util .ArrayList ;
28
- import java .util .Collection ;
29
- import java .util .Collections ;
30
- import java .util .Objects ;
30
+ import java .util .*;
31
31
import java .util .regex .Matcher ;
32
32
import java .util .regex .Pattern ;
33
33
34
34
public class IDEController {
35
35
private final Analyzer analyzer = new Analyzer ();
36
+ private final Reader reader = new Reader ();
36
37
private File file = null ;
37
38
38
39
@ FXML
@@ -43,6 +44,8 @@ public class IDEController {
43
44
private TextArea errorMessages ;
44
45
@ FXML
45
46
private CodeArea codeArea ;
47
+ @ FXML
48
+ private TableView <ArrayList <String >> tableView ;
46
49
47
50
private static final String KEYWORD_PATTERN = "\\ b(" + String .join ("|" , Analyzer .getKeywords ()) + ")\\ b" ;
48
51
private static final String DATATYPES_PATTERN = "\\ b(" + String .join ("|" , Analyzer .getDataTypes ()) + ")\\ b" ;
@@ -189,18 +192,86 @@ private void saveTextToFile(String content, File file) {
189
192
}
190
193
191
194
@ FXML
192
- protected void executeSelectedStatement (ActionEvent event ) {
195
+ protected void executeSelectedStatement () {
193
196
String selection = codeArea .getSelectedText ();
197
+ selection = selection .trim ();
198
+ ArrayList <ArrayList <String >> printableTable = null ;
194
199
195
200
try {
196
- analyzer .analyzeSyntax (selection );
201
+ String line = reader .consoleReader (selection );
202
+ printableTable = analyzer .analyzeSyntax (line );
197
203
} catch (Exception e ) {
198
204
handleExceptions (e .getMessage ());
199
205
}
200
206
201
- if (file != null ) {
207
+ if (file != null && analyzer . getDatabase (). getDbFile () != null ) {
202
208
loadTree ();
203
209
}
210
+
211
+ if (printableTable != null ) {
212
+ createTable (printableTable );
213
+ }
214
+ }
215
+
216
+ @ FXML
217
+ protected void executeScript (){
218
+ String script = codeArea .getText ();
219
+
220
+ Pattern pattern = Pattern .compile ("(?s).*?;" );
221
+ Matcher matcher = pattern .matcher (script );
222
+
223
+ ArrayList <String > statements = new ArrayList <>();
224
+ while (matcher .find ()) {
225
+ statements .add (matcher .group ().trim ());
226
+ }
227
+
228
+ System .out .println (statements );
229
+
230
+ for (String statement : statements ) {
231
+ try {
232
+ String line = reader .consoleReader (statement );
233
+ ArrayList <ArrayList <String >> printableTable = analyzer .analyzeSyntax (line );
234
+
235
+ if (file != null && analyzer .getDatabase ().getDbFile () != null ) {
236
+ loadTree ();
237
+ }
238
+
239
+ if (printableTable != null ) {
240
+ createTable (printableTable );
241
+ }
242
+ } catch (Exception e ) {
243
+ System .out .println (statement );
244
+ handleExceptions (e .getMessage ());
245
+ }
246
+ }
247
+ }
248
+
249
+ protected void createTable (ArrayList <ArrayList <String >> data ){
250
+ SingleSelectionModel <Tab > selectionModel = tabs .getSelectionModel ();
251
+ selectionModel .select (0 );
252
+
253
+ tableView .getColumns ().clear ();
254
+ tableView .getItems ().clear ();
255
+
256
+ ArrayList <String > columnNames = data .get (0 );
257
+ for (int i = 0 ; i < columnNames .size (); i ++) {
258
+ final int colIndex = i ;
259
+ TableColumn <ArrayList <String >, String > column = new TableColumn <>(columnNames .get (i ));
260
+ column .setCellValueFactory (cellData -> new SimpleStringProperty (cellData .getValue ().get (colIndex )));
261
+ tableView .getColumns ().add (column );
262
+ }
263
+
264
+ ObservableList <ArrayList <String >> rows = FXCollections .observableArrayList (data .subList (1 , data .size ()));
265
+ tableView .setItems (rows );
266
+ tableView .setVisible (true );
267
+ }
268
+
269
+ @ FXML
270
+ protected void checkFileChanges (KeyEvent event ) {
271
+ String codeAreaText = codeArea .getText ();
272
+ String fileText = getFileContent (file );
273
+
274
+
204
275
}
205
276
206
277
private StyleSpans <Collection <String >> computeHighlighting (String text ) {
0 commit comments