-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeedsPeerReview.java
256 lines (234 loc) · 9.7 KB
/
NeedsPeerReview.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
/*
* 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.
*/
import java.awt.Dimension;
import java.awt.FileDialog;
import java.io.FileInputStream;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.DoubleProperty;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.stage.FileChooser;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.util.HashMap;
/**
*
* @author 03-aaliyah
*/
public class NeedsPeerReview extends Application {
TabPane tbPane;
MenuBar menuArea;
MenuBar tstBar;
ArrayList<String> readLines = new ArrayList<String>();
ArrayList<File> filePointers = new ArrayList<File>();
HashMap<Tab, File> tabFiles = new HashMap<Tab, File>();
FileChooser fileLoader = new FileChooser();
FileChooser fileSaver = new FileChooser();
public Tab getCompleteTab(){
Tab newTab = new Tab();
TextArea txtArea = new TextArea();
ScrollPane scrPane = new ScrollPane();
txtArea.setPrefColumnCount(135);
txtArea.setPrefRowCount(57);
scrPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrPane.setFitToHeight(true);
scrPane.setFitToWidth(true);
scrPane.setContent(txtArea);
newTab.setContent(scrPane);
newTab.setClosable(true);
return newTab;
}
public MenuBar returnMenuBar(){
menuArea = new MenuBar();
Menu fileMenu = new Menu("File");
MenuItem nwTxtAr = new MenuItem("New");
MenuItem svTxtAr = new MenuItem("Save");
MenuItem ldTxtAr = new MenuItem("Load");
MenuItem svAsAr = new MenuItem("Save as");
fileMenu.getItems().add(nwTxtAr);
fileMenu.getItems().add(svTxtAr);
fileMenu.getItems().add(ldTxtAr);
fileMenu.getItems().add(svAsAr);
Menu btnMenu = new Menu("Buttons");
MenuItem someButton = new MenuItem("FixSomething");
Menu sbTxtAr = new Menu("SubTextArea");
MenuItem clrAr = new MenuItem("ClearSub");
sbTxtAr.getItems().add(clrAr);
menuArea.getMenus().addAll(fileMenu, btnMenu, sbTxtAr);
return menuArea;
}
public AnchorPane returnAnchorPane(){
AnchorPane anchorPane = new AnchorPane();
return anchorPane;
}
public void saveSource(){
for(int c = 0; c < tbPane.getTabs().size(); c++){
if(tbPane.getTabs().get(c).isSelected()){ //Whichever tab that is focused will be subjected to an operation
//Meaning, when represented in FA diagram, "focused" could be like innitial state.
//If there is a File object associated with the selected tab, then, save to that path.
Tab selectedTab = tbPane.getTabs().get(c);
if(!(tabFiles.get(selectedTab) == null)){
ScrollPane scrPn = (ScrollPane) tbPane.getTabs().get(c).getContent();
TextArea txtTst = (TextArea) scrPn.getContent();
File someFile = tabFiles.get(selectedTab);
try {
FileWriter fileWrtr = new FileWriter(someFile);
fileWrtr.write(txtTst.getText());
fileWrtr.close();
return;
} catch (IOException ex) {
Logger.getLogger(NeedsPeerReview.class.getName())
.log(Level.SEVERE, null, ex);
}
}
saveSourceAs();
return;
}
}
}
public void saveSourceAs(){
Tab selectedTab = null; // So, the compiler doesn't scream at me for not innitializing.
String focusedName = ""; // Same reason as above.
fileSaver.setTitle("Save as");
fileSaver.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("standard text files", "*.txt"),
new FileChooser.ExtensionFilter("java files", "*.java"));
File saveAsFile = fileSaver.showSaveDialog(new Stage());
if(saveAsFile == null){
System.out.println("Cancel selected.");
return;
}
for(int c = 0; c < tbPane.getTabs().size(); c++){
if(tbPane.getTabs().get(c).isSelected()){
selectedTab = tbPane.getTabs().get(c);
focusedName = saveAsFile.getName();
ScrollPane scrPn = (ScrollPane)tbPane.getTabs().get(c).getContent();
TextArea txtTst = (TextArea)scrPn.getContent();
try{
FileWriter fileWriter = new FileWriter(saveAsFile);
fileWriter.write(txtTst.getText());
fileWriter.close();
tbPane.getTabs().get(c).setText(focusedName);
break;
}catch(Exception ex){
Logger.getLogger(NeedsPeerReview.class.getName())
.log(Level.SEVERE, null, ex);
}
}
}
fileSaver.setInitialDirectory(saveAsFile.getParentFile()); // This line enables the saveDialog to open up on the last save location.
tabFiles.put(selectedTab, saveAsFile); // So there is an association between Tab and the saved file.
}
public void loadSource(){
fileLoader.setTitle("FileChooser");
File loadedFile = fileLoader.showOpenDialog(new Stage());
//If not .txt, deal with it accordingly.
if(!(loadedFile.getName().contains(".txt"))){
System.out.println("Not txt, returning from loadSource()");
return;
}
Tab newTab = getCompleteTab();
ScrollPane newScrl = (ScrollPane) newTab.getContent();
TextArea newTxtAr = (TextArea) newScrl.getContent();
try {
Charset charset = Charset.forName("ISO-8859-1");
readLines.addAll(Files.readAllLines(loadedFile.toPath(), charset));
for(int c = 0; c < readLines.size(); c++){
newTxtAr.appendText(readLines.get(c));
newTxtAr.appendText("\n");
}
} catch (Exception ex) {
Logger.getLogger(NeedsPeerReview.class.getName()).log(Level.SEVERE, null, ex);
}
newTab.setText(loadedFile.getName());
newTab.setClosable(true);
newTab.setContent(newScrl);
tbPane.getTabs().add(newTab);
fileLoader.setInitialDirectory(loadedFile.getParentFile());
}
public void innitAll(){
tstBar = returnMenuBar();
tbPane = new TabPane();
tbPane.setPrefSize(900, 800);
tstBar.getMenus().get(0).getItems().get(0).setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent e){
Tab newTab = getCompleteTab();
newTab.setText("NewTab");
tbPane.getTabs().add(newTab);
tabFiles.put(newTab, null);
}
});
/*
11/04/17
Those three lines were composed of anounymous classes, but now,
they are lambdas.
*/
tstBar.getMenus().get(0).getItems().get(1).setOnAction((ActionEvent) -> saveSource());
tstBar.getMenus().get(0).getItems().get(2).setOnAction((ActionEvent) -> loadSource());
tstBar.getMenus().get(0).getItems().get(3).setOnAction((ActionEvent) -> saveSourceAs());
}
public void start(Stage primaryStage) {
innitAll();
tbPane.prefWidthProperty().bind(primaryStage.widthProperty().subtract(150));
tbPane.prefHeightProperty().bind(primaryStage.heightProperty().subtract(100));
menuArea.prefWidthProperty().bind(primaryStage.widthProperty());
Tab newTab = getCompleteTab();
newTab.setText("NewTab");
tbPane.getTabs().add(newTab);
tabFiles.put(newTab, null);
AnchorPane tstAnchor = returnAnchorPane();
tstAnchor.setTopAnchor(tbPane, 25.0);
tstAnchor.setLeftAnchor(tbPane, 10.0);
tstAnchor.getChildren().add(tbPane);
Group group = new Group(tstAnchor, menuArea);
Scene scene = new Scene(group);
primaryStage.setHeight(900);
primaryStage.setWidth(1200);
primaryStage.setResizable(true);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}