From 446f76bff82adf62786c9b57bc6449fdec308101 Mon Sep 17 00:00:00 2001
From: Philipp Spohr <spohr.philipp@web.de>
Date: Thu, 14 Sep 2017 19:28:00 +0200
Subject: [PATCH] More work on solution tab

---
 .../graphModel/YoshikoCluster.java            | 12 ++---
 .../swing/components/ClusterView.java         | 48 +++++++++++++++----
 .../swing/components/ClusterViewList.java     | 36 ++++++++++++++
 .../swing/components/ResultPanel.java         |  4 +-
 .../swing/components/SolutionTab.java         | 32 ++++++++++---
 .../tasks/CreateMetaGraphTask.java            |  6 +--
 6 files changed, 110 insertions(+), 28 deletions(-)
 create mode 100644 src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ClusterViewList.java

diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/graphModel/YoshikoCluster.java b/src/main/java/de/hhu/ba/yoshikoWrapper/graphModel/YoshikoCluster.java
index d9eb0bb..0292e61 100644
--- a/src/main/java/de/hhu/ba/yoshikoWrapper/graphModel/YoshikoCluster.java
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/graphModel/YoshikoCluster.java
@@ -97,7 +97,7 @@ public class YoshikoCluster {
 	/**
 	 * Attempt to select the nodes belonging to this cluster in the original Graph given that they still exist
 	 */
-	public void select() {
+	public void highlightInOriginalGraph() {
 		try {
 			List<CyRow> allRows = originalGraph.getDefaultNodeTable().getAllRows();
 			for (CyRow r: allRows) {
@@ -114,16 +114,17 @@ public class YoshikoCluster {
 	}
 
 
-	public void applyImage(JLabel label, int width, int height) {
+	public void applyImage(JLabel label, int width, int height) throws Exception {
 
 		if (net == null) {
-			return;
+			throw new Exception("Can't have an image without having a network associated");
 		}
 
 		if (img != null) {
-			getImage(width,height,label);
 			label.setIcon(new ImageIcon(img));
-			return;
+		}
+		else {
+			getImage(width,height,label);
 		}
 	}
 
@@ -160,7 +161,6 @@ public class YoshikoCluster {
 						public void run() {
 							img = renderingEngine.createImage(width,height);
 							label.setIcon(new ImageIcon(img));
-
 							renderingEngine.dispose();
 							view.dispose();
 						}
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ClusterView.java b/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ClusterView.java
index 79a1fbf..c0fa84e 100644
--- a/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ClusterView.java
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ClusterView.java
@@ -22,14 +22,17 @@
 package de.hhu.ba.yoshikoWrapper.swing.components;
 
 import java.awt.Color;
+import java.awt.Dimension;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 
 import javax.swing.BorderFactory;
 import javax.swing.Box;
 import javax.swing.BoxLayout;
+import javax.swing.GroupLayout;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
+import javax.swing.GroupLayout.Alignment;
 import javax.swing.border.Border;
 
 import org.cytoscape.model.CyNode;
@@ -43,12 +46,14 @@ import de.hhu.ba.yoshikoWrapper.swing.SwingUtil;
 @SuppressWarnings("serial")
 public class ClusterView extends JPanel {
 
+	private final static int CLUSTER_ICON_SIZE = 128;
+
 	private final JLabel title;
 	private final JLabel clusterSize;
 	private final JLabel icon;
 	private final BasicCollapsiblePanel nodeList;
 
-	private final Border regularBorder = BorderFactory.createLineBorder(Color.GRAY,1);
+	private final Border regularBorder = BorderFactory.createLineBorder(Color.GRAY,3);
 	private final Border highlightBorder = BorderFactory.createLineBorder(GraphicsLoader.yoshikoGreen,3);
 	private final Border selectedBorder = BorderFactory.createLineBorder(Color.BLUE,3);
 
@@ -59,18 +64,18 @@ public class ClusterView extends JPanel {
 	 */
 	private YoshikoCluster cluster;
 
-	public ClusterView(YoshikoCluster c) {
+	public ClusterView(YoshikoCluster c) throws Exception {
 
 		this.cluster = c;
 
 		//Swing init
-		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
 
 		title = new JLabel(LocalizationManager.get("cluster")+" "+(c.getID()+1));
 		clusterSize = new JLabel(LocalizationManager.get("clusterSize")+" "+c.getSize());
 		icon = new JLabel();
+		icon.setPreferredSize(new Dimension(CLUSTER_ICON_SIZE,CLUSTER_ICON_SIZE));
 		icon.setBorder(BorderFactory.createLineBorder(Color.BLACK));
-		cluster.applyImage(icon,128, 128);
+		cluster.applyImage(icon,CLUSTER_ICON_SIZE, CLUSTER_ICON_SIZE);
 
 		SwingUtil.addAll(this,title,clusterSize,icon);
 		this.add(Box.createVerticalStrut(4));
@@ -80,18 +85,41 @@ public class ClusterView extends JPanel {
 		//Loop over nodes in the cluster and add them to the view
 		for (CyNode n : c.getSubNetwork().getNodeList()) {
 			nodeList.add(
-					new JLabel(
-							c.getNodeName(n)
-					)
-				);
+				new JLabel(
+					c.getNodeName(n)
+				)
+			);
 		}
 
 		this.add(nodeList);
 
 		this.addMouseListener(mouseListener);
-
 		this.setBorder(regularBorder);
 
+		GroupLayout layout = new GroupLayout(this);
+		layout.setHorizontalGroup(layout.createParallelGroup()
+				.addGroup(layout.createSequentialGroup()
+						.addGroup(layout.createParallelGroup()
+								.addComponent(title)
+								.addComponent(clusterSize)
+						)
+						.addComponent(icon)
+					)
+				.addComponent(nodeList)
+		);
+		layout.setVerticalGroup(layout.createSequentialGroup()
+				.addGroup(layout.createParallelGroup(Alignment.CENTER)
+						.addGroup(layout.createSequentialGroup()
+								.addComponent(title)
+								.addComponent(clusterSize)
+						)
+						.addComponent(icon)
+					)
+				.addComponent(nodeList)
+		);
+
+		this.setLayout(layout);
+
 	}
 
 	private MouseListener mouseListener = new MouseListener() {
@@ -101,7 +129,7 @@ public class ClusterView extends JPanel {
 
 		@Override
 		public void mousePressed(MouseEvent e) {
-			cluster.select();
+			cluster.highlightInOriginalGraph();
 		}
 
 		@Override
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ClusterViewList.java b/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ClusterViewList.java
new file mode 100644
index 0000000..610c525
--- /dev/null
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ClusterViewList.java
@@ -0,0 +1,36 @@
+package de.hhu.ba.yoshikoWrapper.swing.components;
+
+import java.awt.Dimension;
+import java.awt.Rectangle;
+
+import javax.swing.JPanel;
+import javax.swing.Scrollable;
+
+public class ClusterViewList extends JPanel implements Scrollable {
+
+	@Override
+	public Dimension getPreferredScrollableViewportSize() {
+		return this.getPreferredSize();
+	}
+
+	@Override
+	public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
+		return 16;
+	}
+
+	@Override
+	public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
+		return 16;
+	}
+
+	@Override
+	public boolean getScrollableTracksViewportWidth() {
+		return true;
+	}
+
+	@Override
+	public boolean getScrollableTracksViewportHeight() {
+		return false;
+	}
+
+}
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ResultPanel.java b/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ResultPanel.java
index d541c1a..f8830e9 100644
--- a/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ResultPanel.java
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/ResultPanel.java
@@ -59,7 +59,7 @@ public class ResultPanel extends JPanel implements CytoPanelComponent{
 
 	private final YoshikoResult result;
 
-	public ResultPanel(YoshikoResult result) {
+	public ResultPanel(YoshikoResult result) throws Exception {
 
 		this.result = result;
 
@@ -159,7 +159,7 @@ public class ResultPanel extends JPanel implements CytoPanelComponent{
 		super.setVisible(false);
 	}
 
-	private void addSolutionTab(YoshikoSolution s) {
+	private void addSolutionTab(YoshikoSolution s) throws Exception {
 		SolutionTab tab = new SolutionTab(s);
 		solutionTabs.add(
 				LocalizationManager.get("solution")+" "+(s.getId()+1),
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/SolutionTab.java b/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/SolutionTab.java
index 2f593cd..1577d93 100644
--- a/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/SolutionTab.java
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/swing/components/SolutionTab.java
@@ -21,16 +21,22 @@
  ******************************************************************************/
 package de.hhu.ba.yoshikoWrapper.swing.components;
 
-import java.awt.Dimension;
+import static javax.swing.GroupLayout.DEFAULT_SIZE;
+import static javax.swing.GroupLayout.PREFERRED_SIZE;
+
+import java.awt.ScrollPane;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.Comparator;
 
 import javax.swing.BoxLayout;
+import javax.swing.GroupLayout;
+import javax.swing.GroupLayout.Alignment;
 import javax.swing.JButton;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
+import javax.swing.ScrollPaneConstants;
 
 import org.cytoscape.work.TaskIterator;
 
@@ -45,19 +51,18 @@ import de.hhu.ba.yoshikoWrapper.tasks.CreateMetaGraphTask;
 public class SolutionTab extends JPanel {
 
 	private final JScrollPane scrollPane;
-	private final JPanel scrollPaneContent;
+	private final ClusterViewList scrollPaneContent;
 	private final JLabel clusterCount;
 	private final JButton createMetaGraph;
 
 	private final YoshikoSolution solution;
 
-	public SolutionTab(YoshikoSolution s) {
+	public SolutionTab(YoshikoSolution s) throws Exception {
 
 		this.solution = s;
 
-		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
 		//init swing components
-		scrollPaneContent = new JPanel();
+		scrollPaneContent = new ClusterViewList();
 		scrollPaneContent.setLayout(new BoxLayout(scrollPaneContent,BoxLayout.Y_AXIS));
 
 		//Sort cluster by size, descending
@@ -83,7 +88,7 @@ public class SolutionTab extends JPanel {
 
 		scrollPane = new JScrollPane(scrollPaneContent);
 
-		scrollPane.setPreferredSize(new Dimension(150,400));
+		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
 
 		clusterCount = new JLabel(LocalizationManager.get("clusterFound")+" "+s.cluster.size());
 
@@ -101,6 +106,21 @@ public class SolutionTab extends JPanel {
 
 		});
 		SwingUtil.addAll(this,clusterCount,scrollPane,createMetaGraph);
+
+		//Layout
+		GroupLayout layout = new GroupLayout(this);
+		layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING,true)
+				.addComponent(clusterCount,DEFAULT_SIZE,DEFAULT_SIZE,DEFAULT_SIZE)
+				.addComponent(scrollPane,PREFERRED_SIZE,PREFERRED_SIZE,PREFERRED_SIZE)
+				.addComponent(createMetaGraph,DEFAULT_SIZE,DEFAULT_SIZE,DEFAULT_SIZE)
+		);
+		layout.setVerticalGroup(layout.createSequentialGroup()
+				.addComponent(clusterCount,DEFAULT_SIZE,DEFAULT_SIZE,PREFERRED_SIZE)
+				.addComponent(scrollPane,DEFAULT_SIZE,DEFAULT_SIZE,PREFERRED_SIZE)
+				.addComponent(createMetaGraph,DEFAULT_SIZE,DEFAULT_SIZE,PREFERRED_SIZE)
+		);
+
+		this.setLayout(layout);
 	}
 
 }
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/tasks/CreateMetaGraphTask.java b/src/main/java/de/hhu/ba/yoshikoWrapper/tasks/CreateMetaGraphTask.java
index 6f75831..0eb0e0e 100644
--- a/src/main/java/de/hhu/ba/yoshikoWrapper/tasks/CreateMetaGraphTask.java
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/tasks/CreateMetaGraphTask.java
@@ -47,8 +47,6 @@ public class CreateMetaGraphTask extends AbstractTask {
 		//Add nodes
 		for (YoshikoCluster c: solution.getCluster()) {
 
-
-
 			CyNode clusterNode = metaGraph.addNode();
 			CySubNetwork subnet = c.getSubNetwork();
 			CyCore.networkManager.addNetwork(subnet);
@@ -127,12 +125,12 @@ public class CreateMetaGraphTask extends AbstractTask {
 		});
 
 		CyCore.dialogTaskManager.execute(
-				it
+			it
 		);
 
 		CyCore.networkManager.addNetwork(metaGraph);
 		CyCore.networkViewManager.addNetworkView(
-				view
+			view
 		);
 
 		view.updateView();
-- 
GitLab