Skip to content

Commit 4b59e51

Browse files
authored
Merge pull request #269 from ProgrammingLife2017/GUI_and_GFF
Gui changes.
2 parents bf656cd + 521aa62 commit 4b59e51

9 files changed

+400
-155
lines changed

src/main/java/programminglife/ProgrammingLife.java

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public void start(Stage stage) throws IOException {
4949
close.setOnAction(event -> primaryStage.fireEvent(
5050
new WindowEvent(primaryStage, WindowEvent.WINDOW_CLOSE_REQUEST))
5151
);
52+
primaryStage.setMinWidth(600);
53+
primaryStage.setMinHeight(400);
5254
primaryStage.sizeToScene();
5355
primaryStage.show();
5456
primaryStage.setMaximized(true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
package programminglife.controller;
2+
3+
import javafx.scene.control.Menu;
4+
import javafx.scene.control.MenuItem;
5+
import programminglife.gui.controller.GuiController;
6+
import programminglife.utility.Alerts;
7+
8+
import java.io.*;
9+
import java.nio.file.FileAlreadyExistsException;
10+
import java.nio.file.Files;
11+
import java.util.Scanner;
12+
13+
/**
14+
* Class that handles the recentFiles menuItem.
15+
*/
16+
public class RecentFileController {
17+
private GuiController guiController;
18+
private File recentFile;
19+
private Menu menuRecent;
20+
private File file;
21+
private String recentItems = "";
22+
private int lineAmount;
23+
private String[] lines;
24+
private String file1;
25+
private String file2;
26+
private String file3;
27+
private String file4;
28+
private String file5;
29+
30+
31+
/**
32+
* Constructor for the recent file handler.
33+
* @param recentFile File containing the recent entries.
34+
* @param menuRecent Menu containing the recent entries.
35+
*/
36+
public RecentFileController(File recentFile, Menu menuRecent) {
37+
try {
38+
findLines(recentFile);
39+
} catch (IOException e) {
40+
Alerts.error("Recent.txt doesn't exist. A new one will be created.");
41+
}
42+
43+
this.recentFile = recentFile;
44+
this.menuRecent = menuRecent;
45+
this.initRecent();
46+
lines = recentItems.split(System.getProperty("line.separator"));
47+
48+
for (int i = 0; i < lineAmount; i++) {
49+
switch (i) {
50+
default:
51+
continue;
52+
case 0:
53+
file1 = lines[i];
54+
break;
55+
case 1:
56+
file2 = lines[i];
57+
break;
58+
case 2:
59+
file3 = lines[i];
60+
break;
61+
case 3:
62+
file4 = lines[i];
63+
break;
64+
case 4:
65+
file5 = lines[i];
66+
break;
67+
}
68+
}
69+
doesFileExist();
70+
}
71+
72+
/**
73+
* Find the amount of lines in a given file.
74+
* @param recentFile File to check the amount of lines of.
75+
* @throws IOException Exception to be thrown when file can't be found.
76+
*/
77+
private void findLines(File recentFile) throws IOException {
78+
LineNumberReader reader = new LineNumberReader(new FileReader(recentFile));
79+
String lineRead = "";
80+
while ((lineRead = reader.readLine()) != null) {
81+
lineAmount++;
82+
}
83+
reader.close();
84+
}
85+
86+
/**
87+
* Read out the file which contains all the recently opened files.
88+
*/
89+
private void initRecent() {
90+
try {
91+
Files.createFile(recentFile.toPath());
92+
} catch (FileAlreadyExistsException e) {
93+
//This will always happen if a user has used the program before.
94+
//Therefore it is unnecessary to handle further.
95+
} catch (IOException e) {
96+
Alerts.error("Recent.txt can't be opened");
97+
return;
98+
}
99+
if (recentFile != null) {
100+
try (Scanner sc = new Scanner(recentFile)) {
101+
menuRecent.getItems().clear();
102+
while (sc.hasNextLine()) {
103+
String next = sc.nextLine();
104+
MenuItem mi = new MenuItem(next);
105+
mi.setOnAction(event -> {
106+
try {
107+
file = new File(mi.getText());
108+
guiController.openFile(file);
109+
} catch (IOException e) {
110+
Alerts.error("Recent.txt can't be opened");
111+
}
112+
});
113+
menuRecent.getItems().add(mi);
114+
recentItems = recentItems.concat(next + System.getProperty("line.separator"));
115+
}
116+
} catch (FileNotFoundException e) {
117+
Alerts.error("Recent.txt can't be found.");
118+
}
119+
}
120+
}
121+
122+
/**
123+
* Updates the recent files file after opening a file.
124+
* @param recentFile File containing the recent entries.
125+
* @param file File to check if it already contained.
126+
*/
127+
public void updateRecent(File recentFile, File file) {
128+
if (checkDuplicate(file)) {
129+
moveFiles(file);
130+
try (BufferedWriter recentWriter = new BufferedWriter(new FileWriter(recentFile))) {
131+
if (file1 != null) {
132+
recentWriter.write(file1 + System.getProperty("line.separator"));
133+
}
134+
if (file2 != null) {
135+
recentWriter.write(file2 + System.getProperty("line.separator"));
136+
}
137+
if (file3 != null) {
138+
recentWriter.write(file3 + System.getProperty("line.separator"));
139+
}
140+
if (file4 != null) {
141+
recentWriter.write(file4 + System.getProperty("line.separator"));
142+
}
143+
if (file5 != null) {
144+
recentWriter.write(file5 + System.getProperty("line.separator"));
145+
}
146+
recentWriter.flush();
147+
recentWriter.close();
148+
initRecent();
149+
} catch (IOException e) {
150+
Alerts.error("Recent.txt cannot be updated");
151+
}
152+
}
153+
}
154+
155+
/**
156+
* Updates the recent files file after opening a file.
157+
* @param recentFile File containing the recent entries.
158+
*/
159+
private void updateRecent(File recentFile) {
160+
try (BufferedWriter recentWriter = new BufferedWriter(new FileWriter(recentFile))) {
161+
if (file1 != null) {
162+
recentWriter.write(file1 + System.getProperty("line.separator"));
163+
}
164+
if (file2 != null) {
165+
recentWriter.write(file2 + System.getProperty("line.separator"));
166+
}
167+
if (file3 != null) {
168+
recentWriter.write(file3 + System.getProperty("line.separator"));
169+
}
170+
if (file4 != null) {
171+
recentWriter.write(file4 + System.getProperty("line.separator"));
172+
}
173+
if (file5 != null) {
174+
recentWriter.write(file5 + System.getProperty("line.separator"));
175+
}
176+
recentWriter.flush();
177+
recentWriter.close();
178+
initRecent();
179+
} catch (IOException e) {
180+
Alerts.error("Recent.txt cannot be updated");
181+
}
182+
}
183+
184+
/**
185+
* Checks if there is a duplicate.
186+
* @param file File to be added to the list.
187+
* @return boolean, true if it is not a duplicate.
188+
*/
189+
private boolean checkDuplicate(File file) {
190+
for (String s : lines) {
191+
if (!s.contains(file.getAbsolutePath())) {
192+
return true;
193+
}
194+
}
195+
return false;
196+
}
197+
198+
/**
199+
* Removes a file that cannot be opened anymore in any way.
200+
*/
201+
private void doesFileExist() {
202+
if (file1 != null) {
203+
File f1 = new File(file1);
204+
if (!f1.exists()) {
205+
file1 = null;
206+
}
207+
}
208+
if (file2 != null) {
209+
File f2 = new File(file2);
210+
if (!f2.exists()) {
211+
file2 = null;
212+
}
213+
}
214+
if (file3 != null) {
215+
File f3 = new File(file3);
216+
if (!f3.exists()) {
217+
file3 = null;
218+
}
219+
}
220+
if (file4 != null) {
221+
File f4 = new File(file4);
222+
if (!f4.exists()) {
223+
file4 = null;
224+
}
225+
}
226+
if (file5 != null) {
227+
File f5 = new File(file5);
228+
if (!f5.exists()) {
229+
file5 = null;
230+
}
231+
}
232+
updateRecent(this.recentFile);
233+
}
234+
235+
/**
236+
* Moves all the recentFiles down by 1 position.
237+
* @param file File to be added to the list.
238+
*/
239+
private void moveFiles(File file) {
240+
file5 = file4;
241+
file4 = file3;
242+
file3 = file2;
243+
file2 = file1;
244+
file1 = file.getAbsolutePath();
245+
}
246+
247+
/**
248+
* Sets the guicontroller for controlling the menu.
249+
* @param guiController The gui controller
250+
*/
251+
public void setGuiController(GuiController guiController) {
252+
this.guiController = guiController;
253+
}
254+
}

src/main/java/programminglife/gui/controller/GraphController.java

+45-26
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,13 @@ private void showInfoEdge(DrawableEdge edge, int x) {
276276
*/
277277
private void showInfoNode(DrawableSegment node, int x) {
278278
Text idText = new Text("ID: "); idText.setLayoutX(x); idText.setLayoutY(65);
279-
Text parentText = new Text("Parents: "); parentText.setLayoutX(x); parentText.setLayoutY(115);
280-
Text childText = new Text("Children: "); childText.setLayoutX(x); childText.setLayoutY(165);
281-
Text inEdgeText = new Text("Incoming Edges: "); inEdgeText.setLayoutX(x); inEdgeText.setLayoutY(215);
282-
Text outEdgeText = new Text("Outgoing Edges: "); outEdgeText.setLayoutX(x); outEdgeText.setLayoutY(265);
283-
Text genomeText = new Text("Genomes: "); genomeText.setLayoutX(x); genomeText.setLayoutY(315);
284-
Text seqLengthText = new Text("Sequence Length: "); seqLengthText.setLayoutX(x); seqLengthText.setLayoutY(365);
285-
Text seqText = new Text("Sequence: "); seqText.setLayoutX(x); seqText.setLayoutY(415);
279+
Text parentText = new Text("Parents: "); parentText.setLayoutX(x); parentText.setLayoutY(105);
280+
Text childText = new Text("Children: "); childText.setLayoutX(x); childText.setLayoutY(145);
281+
Text inEdgeText = new Text("Incoming Edges: "); inEdgeText.setLayoutX(x); inEdgeText.setLayoutY(185);
282+
Text outEdgeText = new Text("Outgoing Edges: "); outEdgeText.setLayoutX(x); outEdgeText.setLayoutY(225);
283+
Text seqLengthText = new Text("Sequence Length: "); seqLengthText.setLayoutX(x); seqLengthText.setLayoutY(265);
284+
Text genomeText = new Text("Genomes: "); genomeText.setLayoutX(x); genomeText.setLayoutY(305);
285+
Text seqText = new Text("Sequence: "); seqText.setLayoutX(x); seqText.setLayoutY(370);
286286

287287
anchorGraphInfo.getChildren().removeIf(node1 -> node1.getLayoutX() == x);
288288

@@ -293,42 +293,60 @@ private void showInfoNode(DrawableSegment node, int x) {
293293
TextField parents;
294294
if (parentSB.length() > 2) {
295295
parentSB.setLength(parentSB.length() - 2);
296-
parents = getTextField("Parents: ", x, 120, parentSB.toString());
296+
parents = getTextField("Parents: ", x, 110, parentSB.toString());
297297
} else {
298298
parentSB.replace(0, parentSB.length(), "This node has no parent(s)");
299-
parents = getTextField("Parents: ", x, 120, parentSB.toString());
299+
parents = getTextField("Parents: ", x, 110, parentSB.toString());
300300
}
301301

302302
StringBuilder childSB = new StringBuilder();
303303
node.getChildren().forEach(id -> childSB.append(id).append(", "));
304304
TextField children;
305305
if (childSB.length() > 2) {
306306
childSB.setLength(childSB.length() - 2);
307-
children = getTextField("Children: ", x, 170, childSB.toString());
307+
children = getTextField("Children: ", x, 150, childSB.toString());
308308
} else {
309309
childSB.replace(0, childSB.length(), "This node has no child(ren)");
310-
children = getTextField("Children: ", x, 170, childSB.toString());
310+
children = getTextField("Children: ", x, 150, childSB.toString());
311311
}
312312

313-
TextField inEdges = getTextField("Incoming Edges: ", x, 220, Integer.toString(node.getParents().size()));
314-
TextField outEdges = getTextField("Outgoing Edges: ", x, 270, Integer.toString(node.getChildren().size()));
315-
TextField genome = getTextField("Genome: ", x, 320,
316-
graph.getGenomeNames(node.getGenomes()).toString());
317-
TextField seqLength = getTextField("Sequence Length: ", x, 370, Integer.toString(node.getSequence().length()));
318-
319-
TextArea seq = new TextArea("Sequence: ");
320-
seq.setEditable(false);
321-
seq.setLayoutX(x); seq.setLayoutY(420);
322-
seq.setText(node.getSequence().replaceAll("(.{25})", "$1" + System.getProperty("line.separator")));
323-
seq.setPrefWidth(225); seq.setPrefHeight(25 * Math.ceil(node.getSequence().length() / 25));
324-
seq.setStyle("-fx-text-box-border: transparent;-fx-background-color: none; -fx-background-insets: 0;"
325-
+ " -fx-padding: 1 3 1 3; -fx-focus-color: transparent; -fx-faint-focus-color: transparent;");
326-
313+
String genomesString = graph.getGenomeNames(node.getGenomes()).toString();
314+
String sequenceString = node.getSequence().replaceAll("(.{24})", "$1" + System.getProperty("line.separator"));
315+
TextField inEdges = getTextField("Incoming Edges: ", x, 190, Integer.toString(node.getParents().size()));
316+
TextField outEdges = getTextField("Outgoing Edges: ", x, 230, Integer.toString(node.getChildren().size()));
317+
TextField seqLength = getTextField("Sequence Length: ", x, 270, Integer.toString(node.getSequence().length()));
318+
TextArea genome = getTextArea("Genome: ", x, 310, genomesString.substring(1, genomesString.length() - 1), 40);
319+
genome.setWrapText(true);
320+
TextArea seq = getTextArea("Sequence: ", x, 375, sequenceString, 250);
327321
anchorGraphInfo.getChildren().addAll(idText, parentText, childText, inEdgeText,
328322
outEdgeText, genomeText, seqLengthText, seqText);
329323
anchorGraphInfo.getChildren().addAll(idTextField, parents, children, inEdges, outEdges, genome, seqLength, seq);
330324
}
331325

326+
/**
327+
* Returns a textField to be used by the edge and node information show panel.
328+
* @param id String the id of the textField.
329+
* @param x int the x coordinate of the textField inside the anchorPane.
330+
* @param y int the y coordinate of the textField inside the anchorPane.
331+
* @param text String the text to be shown by the textField.
332+
* @param height int of the height of the area.
333+
* @return TextField the created textField.
334+
*/
335+
private TextArea getTextArea(String id, int x, int y, String text, int height) {
336+
TextArea textArea = new TextArea();
337+
textArea.setId(id);
338+
textArea.setText(text);
339+
textArea.setLayoutX(x);
340+
textArea.setLayoutY(y);
341+
textArea.setEditable(false);
342+
textArea.setStyle("-fx-text-box-border: transparent;-fx-background-color: none; -fx-background-insets: 0;"
343+
+ " -fx-padding: 1 3 1 3; -fx-focus-color: transparent; "
344+
+ "-fx-faint-focus-color: transparent; -fx-font-family: monospace;");
345+
textArea.setPrefSize(225, height);
346+
347+
return textArea;
348+
}
349+
332350
/**
333351
* Returns a textField to be used by the edge and node information show panel.
334352
* @param id String the id of the textField.
@@ -345,7 +363,8 @@ private TextField getTextField(String id, int x, int y, String text) {
345363
textField.setLayoutY(y);
346364
textField.setEditable(false);
347365
textField.setStyle("-fx-text-box-border: transparent;-fx-background-color: none; -fx-background-insets: 0;"
348-
+ " -fx-padding: 1 3 1 3; -fx-focus-color: transparent; -fx-faint-focus-color: transparent;");
366+
+ " -fx-padding: 1 3 1 3; -fx-focus-color: transparent; "
367+
+ "-fx-faint-focus-color: transparent; -fx-font-family: monospace;");
349368
textField.setPrefSize(220, 20);
350369

351370
return textField;

0 commit comments

Comments
 (0)