-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPHPServer.java
489 lines (393 loc) · 14.3 KB
/
PHPServer.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.input.*;
import javafx.beans.binding.*;
import javafx.stage.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import java.io.*;
import java.io.File;
import javafx.scene.web.*;
import javafx.scene.image.*;
import javafx.beans.value.*;
import java.util.*;
import javafx.event.*;
import javafx.geometry.Side;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.collections.*;
public class PHPServer extends Application{
//File to store php code temporarely
File temp;
//file to store html code temporarely
File temphtml;
//initial zoom levels
double zoom=1;
//Current active tab
Tabbber curtab;
//Root scene instance
Scene sc;
//Tabpane to contain all tabs
TabPane tab_pane;
//TODO
String query;
double x;
double y;
Thread check=null;
Stage suggest=null;
Scene scene;
public static void main(String[] args){
launch(args);
}
public void start(Stage mstg){
//set title
mstg.setTitle("PHP Editer");
//set icon
mstg.getIcons().add(new Image("phpe2.png"));
//Create root layout pane
BorderPane root=new BorderPane();
//initialize tabpane
tab_pane=new TabPane();
//Set alignment
tab_pane.setSide(Side.LEFT);
//initialize root Scene
sc=new Scene(root,800,700);
//add Style sheet
sc.getStylesheets().add("Editer.css");
//Menubar for actions can be taken on tab
MenuBar mb=new MenuBar();
//mstg.getIcons().add(new Image("note.png"));
//File menu for primary action
Menu file=new Menu("File");
//Menu item for file menu
MenuItem opn=new MenuItem("Open");
MenuItem save=new MenuItem("Save");
MenuItem save_as=new MenuItem("Save as");
MenuItem exit=new MenuItem("Exit");
//Sortcuut keys for file menu
save.setAccelerator(KeyCombination.keyCombination("CTRL+S"));
save_as.setAccelerator(KeyCombination.keyCombination("CTRL+SHIFT+S"));
opn.setAccelerator(KeyCombination.keyCombination("CTRL+O"));
exit.setAccelerator(KeyCombination.keyCombination("CTRL+E"));
//View menu for view related customization
Menu edit=new Menu("View");
//Menu item for view Menu
MenuItem zoom_in=new MenuItem("zoom in");
MenuItem zoom_out=new MenuItem("zoom out");
//Shortcut keys for view menu item
zoom_in.setAccelerator(KeyCombination.keyCombination("CTRL+P"));
zoom_out.setAccelerator(KeyCombination.keyCombination("CTRL+M"));
//Add view menu items to View Menu
edit.getItems().addAll(zoom_in,zoom_out);
//Add file menu items
file. getItems().addAll(opn,save,save_as,exit);
//Create help menu
Menu hp=new Menu("Help");
//add all menus to menubar
mb.getMenus().addAll(file,edit,hp);
//Add menubar to the of root layout
root.setTop(mb);
//Add tabpane to the center of root layout
root.setCenter(tab_pane);
//Create initial tab object of custom tab class (Tabbbler)
Tabbber one=new Tabbber("main");
//Tab to create new tabs 😅
Tabbber add=new Tabbber("+");
add.TYPE=Tabbber.TAB_OPEN_NEW;
add.getStyleClass().add("add");
tab_pane.getTabs().addAll(add,one);
tab_pane.getSelectionModel().select(one);
curtab=one;
//Change listener-> listen for changes in text inside text area and make preview of text available in web view
ChangeListener changeListener= new ChangeListener<String>(){
@Override
public void changed(ObservableValue<? extends String> val,String o,String n){
//New Thread to reduce work load on main thread
new Thread(()->{
//if in the current tab a file is opened then open temporary file for php code with the name of file in same location
if(curtab.opend!=null)
temp=new File(curtab.opend.getAbsolutePath()+".temp.php");
//if currently no file is opened in current tab then open file without name as .temp.php
else
temp=new File(".temp.php");
//if opened file does not exits create new file
if(!temp.exists()){
try{
temp.createNewFile();
}catch(Exception ex){
}
}
//if file created successfully
if(temp!=null){
//Delete temporary file after window closed
temp.deleteOnExit();
//Create output stream to write php code written in text Area to temporary file
try(FileOutputStream tout=new FileOutputStream(temp)){
// Write code file
for(char a: curtab.txt.getText().toCharArray()){
tout.write(a);
}
//get Runtime and call php program
Runtime r=Runtime.getRuntime();
Process p=r.exec("php "+temp.getAbsolutePath());
//Get output from php program
InputStream stream=p.getInputStream();
String html="";
int c;
do{
c=stream.read();
if(c!=-1){
html+=(char)c;
}
}while(c!=-1);
//if there is opened file for html open file with that file name
if(curtab.opendhtml!=null)
temphtml=new File(curtab.opendhtml.getAbsolutePath()+".temp.html");
//otherwise open temp file without name as temp
else
temphtml=new File(".temp.html");
//Open file does not exists create new
if(!temphtml.exists()){
try{
temphtml.createNewFile();
}catch(Exception ex){
}
}
//Temp html file created successfully
if(temphtml!=null){
//Delete when window is closed
temphtml.deleteOnExit();
//write output of php program to html file
try(PrintWriter htmlout=new PrintWriter(new FileOutputStream(temphtml))){
String[] str=html.split("\n");
for(int i=0;i<str.length;i++){
htmlout.println(str[i]);
}
}
// load temp html file to webview in current tab
Platform.runLater(()->{
curtab.engine.load(temphtml.toURI().toString());
});
}
}catch(Exception ex){
}
}
}).start();
}
};
//Obeserve tab selection
tab_pane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>(){
public void changed(ObservableValue<? extends Tab> val,Tab old, Tab newval){
Tabbber tmpTab=(Tabbber)newval;
//Tab clicked to open new tab
if(tmpTab.TYPE==Tabbber.TAB_OPEN_NEW){
//add title to new tab
curtab=new Tabbber("untitled");
//Add mouse listener useless till now
curtab.txtCon.addEventHandler(MouseEvent.MOUSE_DRAGGED,(event)->{
x=event.getScreenX();
y=event.getScreenY();
});
//Add change listener to observe changes in Text Area
curtab.txt.textProperty().addListener(changeListener);
//Add tab to Tab pane
tab_pane.getTabs().add(curtab);
//Select current tab
tab_pane.getSelectionModel().select(curtab);
}
//If One of previous tabs selected
else{
//make selected tab current tab;
curtab=(Tabbber)newval;
if(curtab.opend!=null)
System.out.println(curtab.opend.getName());
}
}
});
//Add Scene to the stage
mstg.setScene(sc);
//Show Gui to user
mstg.show();
//Add zoom listener
zoom_out.setOnAction((ae)-> {
zoom=zoom-0.1;
curtab.txt.setStyle("-fx-font-size:"+zoom+"em");
});
zoom_in.setOnAction((ae)-> {
zoom=zoom+0.1;
curtab.txt.setStyle("-fx-font-size:"+zoom+"em");
});
//Exit window when user choose exit menu item
exit.setOnAction((ae)->System.exit(0));
//save opened file
save.setOnAction((ae)->{
if(curtab.opend!=null){
try(FileOutputStream fout=new FileOutputStream(curtab.opend)){
for(char a: curtab.txt.getText().toCharArray()){
fout.write(a);
}
}catch(Exception ex){
}
}else{
FileChooser flc=new FileChooser();
flc.setTitle("save file");
File fl=flc.showSaveDialog(mstg);
if(fl!=null){
try{
FileOutputStream fout=new FileOutputStream(fl);
for(char a : curtab.txt.getText().toCharArray()){
fout.write(a);
}
}catch(Exception ex){
System.out.println("bekar hay tu");
}
}
}
});
//Mouse drag event useless till now -> tring to get mouse location to show context menu
curtab.txtCon.addEventHandler(MouseEvent.MOUSE_DRAGGED,(event)->{
x=event.getScreenX();
y=event.getScreenY();
});
//Change listener to observe text area
curtab.txt.textProperty().addListener(changeListener);
//Save as option to save file in defferent file formats
save_as.setOnAction((ae)->{
FileChooser flc=new FileChooser();
flc.setTitle("save file");
File fl=flc.showSaveDialog(mstg);
if(fl!=null){
try{
FileOutputStream fout=new FileOutputStream(fl);
for(char a : curtab.txt.getText().toCharArray()){
fout.write(a);
}
}catch(Exception ex){
System.out.println("bekar hay tu");
}
}
});
//open file menu to open files
opn.setOnAction((ae)->{
FileChooser flc=new FileChooser();
flc.setTitle("open file");
curtab.opend=flc.showOpenDialog(mstg);
String str="";
if(curtab.opend!=null)
{
try{
FileInputStream fin=new FileInputStream(curtab.opend);
int size=fin.available();
for(int i=0;i<size;i++){
str+=(char)fin.read();
}
curtab.setText(curtab.opend.getName());
curtab.txt.setText(str);
}catch(Exception ek){
System.out.print("lag gaye");
}
System.out.println(curtab.opend.getName());
//Save temp file with new file and show in web view
if(curtab.opendhtml!=null)
temphtml=new File(curtab.opendhtml.getAbsolutePath()+".temp.html");
else
temphtml=new File(".temp.html");
if(!temphtml.exists()){
try{
temphtml.createNewFile();
}catch(Exception ex){
}
}
if(temphtml!=null){
temphtml.deleteOnExit();
try(FileOutputStream htmlout=new FileOutputStream(temphtml)){
for(char a: str.toCharArray()){
htmlout.write(a);
}
}catch(Exception ex){
System.out.println("bas bahut hua");
}
curtab.engine.load(temphtml.toURI().toString());
}
System.out.println("WEb");
}
});
}
public void stop(){
}
//Custom tab class
private class Tabbber extends Tab{
static final int TAB_REGULAR=2;
int TYPE=TAB_REGULAR;
static final int TAB_OPEN_NEW=1;
HBox topcon;
VBox webcon;
VBox txtCon;
WebView web;
WebEngine engine;
TextArea txt;
File opend=null;
File opendhtml=null;
double x;
double y;
public Tabbber(String str){
this.setText(str);
topcon=new HBox();
txtCon=new VBox();
webcon=new VBox();
web=new WebView();
engine=web.getEngine();
engine.setOnAlert((event)->{
Alert al=new Alert(Alert.AlertType.WARNING,event.getData());
al.showAndWait();
});
engine.setConfirmHandler((event)->{
Alert al=new Alert(Alert.AlertType.CONFIRMATION,event,ButtonType.YES,ButtonType.NO,ButtonType.CANCEL);
Optional<ButtonType> res=al.showAndWait();
if(res.get()==ButtonType.YES)
{
return true;
}else{
return false;
}
});
engine.setPromptHandler((event)->{
TextInputDialog intxt=new TextInputDialog(event.getDefaultValue());
intxt.setContentText(event.getMessage());
Optional<String> res=intxt.showAndWait();
return res.get();
});
txt=new TextArea();
web.setPrefWidth(sc.getWidth()/2);
web.prefWidthProperty().bind(sc.widthProperty());
web.prefHeightProperty().bind(sc.heightProperty());
txt.setPrefWidth(sc.getWidth()/2);
txt.prefWidthProperty().bind(sc.widthProperty());
txt.prefHeightProperty().bind(sc.heightProperty());
Button span=new Button("|");
span.setCursor(Cursor.H_RESIZE);
webcon.getChildren().addAll(web);
txtCon.getChildren().add(txt);
webcon.setVgrow(web,Priority.ALWAYS);
topcon.getChildren().addAll(webcon,span,txt);
if(span!=null) {
span.addEventHandler(MouseEvent.MOUSE_PRESSED,new EventHandler<MouseEvent>(){
public void handle(MouseEvent ae){
x=ae.getScreenX();
y=ae.getScreenY();
}
});
span.addEventHandler(MouseEvent.MOUSE_DRAGGED,new EventHandler<MouseEvent>(){
public void handle(MouseEvent ae){
web.prefWidthProperty().bind(sc.widthProperty().subtract(x-ae.getScreenX()));
txt.prefWidthProperty().bind(sc.widthProperty().add(x-ae.getScreenX()));
}
});
}
topcon.setHgrow(txtCon,Priority.ALWAYS);
span.prefHeightProperty().bind(sc.heightProperty());
topcon.setHgrow(webcon,Priority.ALWAYS);
this.setContent(topcon);
}
}
}