Skip to content

Commit a54e55d

Browse files
authored
Merge pull request #233 from ProgrammingLife2017/GUI_OnClick
Gui on click
2 parents cb0d848 + 7b47a47 commit a54e55d

File tree

5 files changed

+258
-110
lines changed

5 files changed

+258
-110
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>nl.tudelft.contextproject</groupId>
88
<artifactId>ProgrammingLife</artifactId>
9-
<version>milestone3</version>
9+
<version>milestone4</version>
1010
<packaging>jar</packaging>
1111

1212
<name>ProgrammingLife</name>

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

+124-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import javafx.geometry.Bounds;
44
import javafx.scene.Group;
5+
import javafx.scene.control.TextField;
6+
import javafx.scene.layout.AnchorPane;
57
import javafx.scene.paint.Color;
8+
import javafx.scene.text.Text;
69
import programminglife.model.Dummy;
710
import programminglife.model.GenomeGraph;
811
import programminglife.model.Segment;
@@ -11,6 +14,7 @@
1114
import programminglife.model.drawing.SubGraph;
1215
import programminglife.utility.Console;
1316

17+
import java.util.Arrays;
1418
import java.util.Collection;
1519
import java.util.LinkedList;
1620

@@ -28,16 +32,19 @@ public class GraphController {
2832
private double locationCenterX;
2933
private LinkedList<DrawableNode> oldMinMaxList = new LinkedList<>();
3034
private SubGraph subGraph;
35+
private AnchorPane anchorGraphInfo;
3136
private LinkedList<DrawableNode> oldGenomeList = new LinkedList<>();
3237

3338
/**
3439
* Initialize controller object.
3540
* @param graph The genome graph to control
3641
* @param grpDrawArea The {@link Group} to draw in
42+
* @param anchorGraphInfo the {@link AnchorPane} were to show the info of a node or edge.
3743
*/
38-
public GraphController(GenomeGraph graph, Group grpDrawArea) {
44+
public GraphController(GenomeGraph graph, Group grpDrawArea, AnchorPane anchorGraphInfo) {
3945
this.graph = graph;
4046
this.grpDrawArea = grpDrawArea;
47+
this.anchorGraphInfo = anchorGraphInfo;
4148
}
4249

4350
/**
@@ -172,11 +179,25 @@ private void highlightDummyNode(DrawableNode node, Color color) {
172179
*/
173180
private void drawEdge(DrawableNode parent, DrawableNode child) {
174181
DrawableEdge edge = new DrawableEdge(parent, child);
182+
// If either parent or child are dummy nodes make on click use the link in that dummy.
183+
if (parent.getNode() instanceof Dummy) {
184+
edge.setOnMouseClicked(event -> {
185+
Console.println(parent.getNode().getLink(null).toString());
186+
});
187+
} else if (child.getNode() instanceof Dummy) {
188+
edge.setOnMouseClicked(event -> {
189+
Console.println(child.getNode().getLink(null).toString());
190+
});
191+
} else {
192+
edge.setOnMouseClicked(event -> Console.println(edge.toString()));
193+
}
175194
edge.setOnMouseClicked(event -> {
176-
Console.println(edge.toString());
177-
Console.println("Genomes: " + graph.getGenomeNames(edge.getGenomes()));
195+
if (event.isShiftDown()) {
196+
showInfoEdge(edge, 250);
197+
} else {
198+
showInfoEdge(edge, 10);
199+
}
178200
});
179-
180201
edge.colorize(graph);
181202
edge.setStartLocation(edge.getStart().getRightBorderCenter());
182203
edge.setEndLocation(edge.getEnd().getLeftBorderCenter());
@@ -197,11 +218,14 @@ public void drawNode(DrawableNode drawableNode) {
197218
});
198219
} else {
199220
Dummy node = (Dummy) drawableNode.getNode();
200-
drawableNode.setOnMouseClicked(event -> {
201-
Console.println(node.getLink(null).toString());
202-
Console.println("Genomes: " + graph.getGenomeNames(node.getGenomes()));
203-
});
204221
}
222+
drawableNode.setOnMouseClicked(event -> {
223+
if (event.isShiftDown()) {
224+
showInfoNode(drawableNode, 250);
225+
} else {
226+
showInfoNode(drawableNode, 10);
227+
}
228+
});
205229
drawableNode.colorize(graph);
206230
this.grpDrawArea.getChildren().add(drawableNode);
207231
}
@@ -255,7 +279,99 @@ public void centerOnNodeId(int nodeId) {
255279

256280
grpDrawArea.setTranslateX(locationCenterX);
257281
grpDrawArea.setTranslateY(locationCenterY);
282+
}
283+
284+
/**
285+
* Method to show the information of an edge.
286+
* @param edge DrawableEdge the edge which has been clicked on.
287+
* @param x int the x location of the TextField.
288+
*/
289+
private void showInfoEdge(DrawableEdge edge, int x) {
290+
anchorGraphInfo.getChildren().removeIf(node1 -> node1.getLayoutX() == x);
291+
292+
Text idText = new Text("Genomes: "); idText.setLayoutX(x); idText.setLayoutY(65);
293+
Text parentsText = new Text("Parent: "); parentsText.setLayoutX(x); parentsText.setLayoutY(115);
294+
Text childrenText = new Text("Child: "); childrenText.setLayoutX(x); childrenText.setLayoutY(165);
295+
296+
TextField id = getTextField("Genomes: ", x, 70, graph.getGenomeNames(edge.getLink().getGenomes()).toString());
297+
TextField parent = getTextField("Parent Node: ", x, 120, edge.getStart().getNode().getIdentifier() + "");
298+
TextField child = getTextField("Child Node: ", x, 170, edge.getEnd().getNode().getIdentifier() + "");
258299

300+
anchorGraphInfo.getChildren().addAll(idText, parentsText, childrenText, id, parent, child);
301+
}
302+
303+
/**
304+
* Method to show the information of a node.
305+
* @param node DrawableNode the node which has been clicked on.
306+
* @param x int the x location of the TextField.
307+
*/
308+
private void showInfoNode(DrawableNode node, int x) {
309+
Text idText = new Text("ID: "); idText.setLayoutX(x); idText.setLayoutY(65);
310+
Text parentText = new Text("Parents: "); parentText.setLayoutX(x); parentText.setLayoutY(115);
311+
Text childText = new Text("Children: "); childText.setLayoutX(x); childText.setLayoutY(165);
312+
Text inEdgeText = new Text("Incoming Edges: "); inEdgeText.setLayoutX(x); inEdgeText.setLayoutY(215);
313+
Text outEdgeText = new Text("Outgoing Edges: "); outEdgeText.setLayoutX(x); outEdgeText.setLayoutY(265);
314+
Text genomeText = new Text("Genomes: "); genomeText.setLayoutX(x); genomeText.setLayoutY(315);
315+
Text seqLengthText = new Text("Sequence Length: "); seqLengthText.setLayoutX(x); seqLengthText.setLayoutY(365);
316+
Text seqText = new Text("Sequence: "); seqText.setLayoutX(x); seqText.setLayoutY(415);
317+
318+
anchorGraphInfo.getChildren().removeIf(node1 -> node1.getLayoutX() == x);
319+
320+
TextField id = getTextField("ID: ", x, 70, node.getNode().getIdentifier() + "");
321+
322+
StringBuilder parentSB = new StringBuilder();
323+
node.getNode().getParents().forEach(o -> parentSB.append(o.getIdentifier()).append(", "));
324+
TextField parents;
325+
if (parentSB.length() > 2) {
326+
parentSB.setLength(parentSB.length() - 2);
327+
parents = getTextField("Parents: ", x, 120, parentSB.toString());
328+
} else {
329+
parentSB.replace(0, parentSB.length(), "This node has no parent(s)");
330+
parents = getTextField("Parents: ", x, 120, parentSB.toString());
331+
}
332+
333+
StringBuilder childSB = new StringBuilder();
334+
node.getNode().getChildren().forEach(o -> childSB.append(o.getIdentifier()).append(", "));
335+
TextField children;
336+
if (childSB.length() > 2) {
337+
childSB.setLength(childSB.length() - 2);
338+
children = getTextField("Children: ", x, 170, childSB.toString());
339+
} else {
340+
childSB.replace(0, childSB.length(), "This node has no child(ren)");
341+
children = getTextField("Children: ", x, 170, childSB.toString());
342+
}
343+
344+
TextField inEdges = getTextField("Incoming Edges: ", x, 220, node.getNode().getParentEdges().size() + "");
345+
TextField outEdges = getTextField("Outgoing Edges: ", x, 270, node.getNode().getChildEdges().size() + "");
346+
TextField genome = getTextField("Genome: ", x, 320, graph.getGenomeNames(node.getNode().getGenomes()).toString());
347+
TextField seqLength = getTextField("Sequence Length: ", x, 370, node.getNode().getSequence().length() + "");
348+
TextField seq = getTextField(x + " Sequence: ", x, 420, node.getNode().getSequence());
349+
350+
anchorGraphInfo.getChildren().addAll(idText, parentText, childText, inEdgeText,
351+
outEdgeText, genomeText, seqLengthText, seqText);
352+
anchorGraphInfo.getChildren().addAll(id, parents, children, inEdges, outEdges, genome, seqLength, seq);
353+
}
354+
355+
/**
356+
* Returns a textField to be used by the edge and node information show panel.
357+
* @param id String the id of the textField.
358+
* @param x int the x coordinate of the textField inside the anchorPane.
359+
* @param y int the y coordinate of the textField inside the anchorPane.
360+
* @param text String the text to be shown by the textField.
361+
* @return TextField the created textField.
362+
*/
363+
private TextField getTextField(String id, int x, int y, String text) {
364+
TextField textField = new TextField();
365+
textField.setId(id);
366+
textField.setText(text);
367+
textField.setLayoutX(x);
368+
textField.setLayoutY(y);
369+
textField.setEditable(false);
370+
textField.setStyle("-fx-text-box-border: transparent;-fx-background-color: none; -fx-background-insets: 0;"
371+
+ " -fx-padding: 1 3 1 3; -fx-focus-color: transparent; -fx-faint-focus-color: transparent;");
372+
textField.setPrefSize(220, 25);
373+
374+
return textField;
259375
}
260376

261377
/**

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

+40-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
import javafx.fxml.FXMLLoader;
77
import javafx.geometry.Bounds;
88
import javafx.scene.Group;
9+
import javafx.scene.Node;
910
import javafx.scene.Scene;
1011
import javafx.scene.control.*;
12+
import javafx.scene.control.Button;
13+
import javafx.scene.control.Menu;
14+
import javafx.scene.control.MenuItem;
15+
import javafx.scene.control.TextField;
1116
import javafx.scene.input.KeyCode;
1217
import javafx.scene.input.KeyCodeCombination;
1318
import javafx.scene.input.MouseEvent;
@@ -27,6 +32,9 @@
2732
import programminglife.utility.ProgressCounter;
2833
import programminglife.utility.NumbersOnlyListener;
2934

35+
import java.awt.*;
36+
import java.awt.datatransfer.Clipboard;
37+
import java.awt.datatransfer.StringSelection;
3038
import java.io.*;
3139
import java.nio.charset.Charset;
3240
import java.nio.file.FileAlreadyExistsException;
@@ -58,6 +66,8 @@ public class GuiController implements Observer {
5866
@FXML private Button btnDraw;
5967
@FXML private Button btnDrawRandom;
6068
@FXML private Button btnBookmark;
69+
@FXML private Button btnClipboard;
70+
@FXML private Button btnClipboard2;
6171
@FXML private Button btnHighlight;
6272
@FXML private ProgressBar progressBar;
6373

@@ -67,6 +77,7 @@ public class GuiController implements Observer {
6777
@FXML private Group grpDrawArea;
6878
@FXML private AnchorPane anchorLeftControlPanel;
6979
@FXML private AnchorPane anchorGraphPanel;
80+
@FXML private AnchorPane anchorGraphInfo;
7081

7182
private double orgSceneX, orgSceneY;
7283
private double orgTranslateX, orgTranslateY;
@@ -89,13 +100,14 @@ public class GuiController implements Observer {
89100
@FXML
90101
@SuppressWarnings("unused")
91102
private void initialize() {
92-
this.graphController = new GraphController(null, this.grpDrawArea);
103+
this.graphController = new GraphController(null, this.grpDrawArea, this.anchorGraphInfo);
93104
initRecent();
94105
initMenubar();
95106
initBookmarkMenu();
96107
initLeftControlpanelScreenModifiers();
97108
initLeftControlpanelDraw();
98109
initMouse();
110+
initShowInfoTab();
99111
initConsole();
100112
initLeftControlpanelHighlight();
101113
}
@@ -457,6 +469,7 @@ private void zoom(double deltaX, double deltaY, double sceneX, double sceneY, do
457469
} else {
458470
scale *= delta;
459471
}
472+
460473
scale = clamp(scale, MIN_SCALE, MAX_SCALE);
461474
grpDrawArea.setScaleX(scale);
462475
grpDrawArea.setScaleY(scale);
@@ -521,6 +534,32 @@ private void initConsole() {
521534
private ProgressBar getProgressBar() {
522535
return this.progressBar;
523536
}
537+
538+
/**
539+
* Initializes the info tab.
540+
*/
541+
private void initShowInfoTab() {
542+
btnClipboard.setOnAction(event -> copyToClipboard(10));
543+
btnClipboard2.setOnAction(event -> copyToClipboard(250));
544+
}
545+
546+
/**
547+
* Copies information to the clipboard.
548+
* @param x int used in the ID, to know which sequence to get.
549+
*/
550+
private void copyToClipboard(int x) {
551+
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
552+
String toClipboard = "";
553+
for (Node node : anchorGraphInfo.getChildren()) {
554+
if (node instanceof TextField && node.getId().equals(x + " Sequence: ")) {
555+
toClipboard = toClipboard.concat(((TextField) node).getText()) + System.getProperty("line.separator");
556+
toClipboard = toClipboard.replaceAll("(.{100})", "$1" + System.getProperty("line.separator"));
557+
}
558+
}
559+
StringSelection selection = new StringSelection(toClipboard);
560+
clipboard.setContents(selection, selection);
561+
}
562+
524563
/**
525564
* Sets the text field for drawing the graph.
526565
* @param center The center node

0 commit comments

Comments
 (0)