Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem of synchronization between a GraphStream's View and JTextField #19

Closed
cachambon opened this issue Jan 31, 2024 · 1 comment
Closed

Comments

@cachambon
Copy link

cachambon commented Jan 31, 2024

Hello,
I created a JFrame composed of one View and two JTextField:
1
The View displays five nodes: four are just landmarks and are not expected to be moved by the user ("fixed_*"), and one is there to be moved by the user ("unfixed"). The two JTextField show the coordinates of the "unfixed" node. Both the View and the JTextField have to be synchronized with each other. Indeed, when the user moves the "unfixed" node in the View, then the two JTextField have to be updated accordingly :
2
Reciprocally, when the user modifies the coordinate in one of the JTextField, then the View has to be updated accordingly too:
3
Here are four test cases:

  • Test case 1: the user runs the application and moves the "unfixed" node in the View.
  • Test case 2: the user runs the application and modifies the coordinate in one of the JTextField.
  • Test case 3: the user runs the application, modifies the coordinate in one of the JTextField, and then moves the "unfixed" node in the View.
  • Test case 4: the user runs the application, moves the "unfixed" node in the View, and then modifies the coordinate in one of the JTextField.

Test cases 1, 2 and 3 work fine, but test case 4 doesn't work. Actually, in test case 4, once the user moved the "unfixed" node in the View, then the modification of the coordinate in one of the JTextField doesn't update the View.

I tried to analyze what differs between the execution of test cases 3 and 4. To do so, I printed the name of the current thread at different places of the code. I saw that modifications through the JTextField are run on thread "AWT-EventQueue-0" (this is the Event Dispatch Thread of Swing, isn't it?), and that modifications through the View are run on thread "Thread-0". In my implementation, "Thread-0" is the thread on which I run the GraphStream's pumping loop, to wait for events occurred in the GraphStream's Viewer thread and to copy back them inside "Thread-0". From what I understood of GraphStream's documentation:

  • the GraphStream's Viewer always runs in the Event Dispatch Thread (EDT) of Swing,
  • the GraphStream's Graph associated to a GraphStream's Viewer can be accessed either from the EDT or from another thread, depending on the GraphStream's ThreadingModel used,
  • it is more powerful to access the GraphStream's Graph from another thread than the EDT, because it allows running algorithms on the graph in parallel with the GraphStream's Viewer.

Did I understand well the documentation?
In my implementation, I chose to access the GraphStream's Graph from another thread than the Swing one. Thus, I deduce from my previous runs of test cases 3 and 4 that:

  • the update of the GraphStream's View from the EDT doesn't prevent a future update of the GraphStream's View from "Thread-0" (test case 3),
  • but the update of the GraphStream's View from "Thread-0" blocks any future update of the GraphStream's View from the EDT (test case 4).

I have the impression that I'm doing something wrong with all these threads. Could you help me please?

I tried to make a minimum working example (MWE) to reproduce my problem. Here is the content of the Java source file NodeSyncTest.java:

package mwe;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.apache.commons.lang3.math.NumberUtils;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.MultiGraph;
import org.graphstream.ui.graphicGraph.GraphicGraph;
import org.graphstream.ui.graphicGraph.GraphicNode;
import org.graphstream.ui.swing_viewer.SwingViewer;
import org.graphstream.ui.view.View;
import org.graphstream.ui.view.Viewer;
import org.graphstream.ui.view.ViewerListener;
import org.graphstream.ui.view.ViewerPipe;

class NodeSyncTest {
	
	public static void main(String[] args) {
		System.out.println("NodeSyncTest.main : " + Thread.currentThread().getName());
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			@Override
            public void run() {
				System.out.println("SwingUtilities.invokeLater.Runnable.run : " + Thread.currentThread().getName());
				new NodeSyncTest();
            }
        });
	}
	
	NodeSyncTest() {
				
		Map<String, MyNode> myNodes = Map.of(
			    "fixed_top_left", new MyNode(-2, 2),
			    "fixed_top_right", new MyNode(2, 2),
			    "fixed_bottom_left", new MyNode(-2, -2),
			    "fixed_bottom_right", new MyNode(2, -2),
			    "unfixed", new MyNode(0, 0)
			);

		GraphStreamControl graphStreamControl = new GraphStreamControl(myNodes);
		
		JTextFieldsControl jTextFieldsControl = new JTextFieldsControl(myNodes);
		
		graphStreamControl.jTextFieldsControl = jTextFieldsControl;
		jTextFieldsControl.graphStreamControl = graphStreamControl;
		
		graphStreamControl.fillGraphStreamGraph();
		
		JFrame mainDialog = new JFrame();
		
		mainDialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainDialog.setSize(300, 300);

		mainDialog.getContentPane().add((Component) graphStreamControl.view, BorderLayout.CENTER);
		mainDialog.getContentPane().add(jTextFieldsControl.panel, BorderLayout.SOUTH);
		
		mainDialog.setLocationRelativeTo(null);
		
		mainDialog.setVisible(true);
		
		graphStreamControl.startPumpLoop();
	}
	
	 class GraphStreamControl {
		 
		 Map<String, MyNode> myNodes;
		 
		 MyNode myUnfixedNode;
		 
		 Graph graphStreamGraph;
		 
		 Viewer viewer;
		 
		 View view;
		 
		 ViewerPipe viewerPipe;
		 
		 JTextFieldsControl jTextFieldsControl;
		
		 GraphStreamControl(Map<String, MyNode> myNodes) {
			 
			this.myNodes = myNodes;
			
			myUnfixedNode = myNodes.get("unfixed");
						
			System.setProperty("org.graphstream.ui", "swing");
			
			graphStreamGraph = new MultiGraph("");
			
			viewer = new SwingViewer(graphStreamGraph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
			viewer.disableAutoLayout();
			view = viewer.addDefaultView(false);
			viewerPipe = viewer.newViewerPipe();
			viewerPipe.addSink(graphStreamGraph);
			
			viewerPipe.addViewerListener(new ViewerListener() {

				@Override
				public void viewClosed(String viewName) {}

				@Override
				public void buttonPushed(String id) {}

				@Override
				public void buttonReleased(String id) {
					System.out.println("ViewerListener.buttonReleased : " + Thread.currentThread().getName());
					if ("unfixed".equals(id)) {
						GraphicGraph graphicGraph = viewer.getGraphicGraph();
						GraphicNode unfixedGraphStreamNode = (GraphicNode) graphicGraph.getNode("unfixed");
						myUnfixedNode.x = unfixedGraphStreamNode.getX();
						myUnfixedNode.y = unfixedGraphStreamNode.getY();
						jTextFieldsControl.update();
					}
				}

				@Override
				public void mouseOver(String id) {}

				@Override
				public void mouseLeft(String id) {}

			});

		}
		 
		public void fillGraphStreamGraph() {
			for (var entry : myNodes.entrySet()) {
			    String nodeId = entry.getKey();
			    MyNode myNode = entry.getValue();
			    Node graphStreamNode = graphStreamGraph.addNode(nodeId);
			    graphStreamNode.setAttribute("xy", myNode.x, myNode.y);
			    graphStreamNode.setAttribute("ui.label", nodeId);
			    graphStreamNode.setAttribute("ui.style", "text-alignment: under;");
			}
			
		}

		void startPumpLoop() {
			new Thread(() -> {
				System.out.println("GraphStreamControl.startPumpLoop : " + Thread.currentThread().getName());
				while (true) {
					try {
						viewerPipe.blockingPump();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}).start();
		}

		void update() {
			GraphicGraph graphicGraph = viewer.getGraphicGraph();
			GraphicNode unfixedGraphStreamNode = (GraphicNode) graphicGraph.getNode("unfixed");
			unfixedGraphStreamNode.setAttribute("xy", myUnfixedNode.x, myUnfixedNode.y);	
		}
		
	}
	 
	class JTextFieldsControl {
		
		Map<String, MyNode> myNodes;
		
		MyNode myUnfixedNode;
		
		JPanel panel;
		
		JTextField xTextField;
		
		JTextField yTextField;
		
		GraphStreamControl graphStreamControl;
		
		JTextFieldsControl(Map<String, MyNode> myNodes) {
			
			this.myNodes = myNodes;
			
			myUnfixedNode = myNodes.get("unfixed");
									
			panel = new JPanel(new GridLayout(1, 4));
			
			JLabel xLabel = new JLabel("X:", SwingConstants.RIGHT);
			xLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
			panel.add(xLabel);
			xTextField = new JTextField(3);
			xTextField.setHorizontalAlignment(SwingConstants.RIGHT);
			xTextField.setText(Double.toString(myUnfixedNode.x));
			xTextField.getCaret().setDot(0);
			xTextField.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					System.out.println("JTextFieldsControl - actionPerformed on xTextField : " + Thread.currentThread().getName());
					String xNodeString = xTextField.getText();
					double xNodeDouble = NumberUtils.toDouble(xNodeString);
					myUnfixedNode.x = xNodeDouble;
					graphStreamControl.update();
					
				}
			});
			panel.add(xTextField);
			
			JLabel yLabel = new JLabel("Y:", SwingConstants.RIGHT);
			yLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
			panel.add(yLabel);
			yTextField = new JTextField(3);
			yTextField.setHorizontalAlignment(SwingConstants.RIGHT);
			yTextField.setText(Double.toString(myUnfixedNode.y));
			yTextField.getCaret().setDot(0);
			yTextField.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					System.out.println("JTextFieldsControl - actionPerformed on yTextField : " + Thread.currentThread().getName());
					String yNodeString = yTextField.getText();
					double yNodeDouble = NumberUtils.toDouble(yNodeString);
					myUnfixedNode.y = yNodeDouble;
					graphStreamControl.update();
				}
			});
			panel.add(yTextField);
		}
		
		void update() {
			String xNodeString = Double.toString(myUnfixedNode.x);
			xTextField.setText(xNodeString);
			xTextField.getCaret().setDot(0);
			String yNodeString = Double.toString(myUnfixedNode.y);
			yTextField.setText(yNodeString);
			yTextField.getCaret().setDot(0);
		}
	}
	
	class MyNode {
		double x;
		double y;
		MyNode(double x, double y) {
			this.x = x;
			this.y = y;
		}
	}
	
}

and here is the one of the the Maven POM file pom.xml to build an executable JAR including all dependencies:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>mwe</groupId>
	<artifactId>mwe</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<name>MWE</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>21</maven.compiler.source>
		<maven.compiler.target>21</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.14.0</version>
		</dependency>
		<dependency>
			<groupId>org.graphstream</groupId>
			<artifactId>gs-core</artifactId>
			<version>2.0</version>
		</dependency>
		<dependency>
			<groupId>org.graphstream</groupId>
			<artifactId>gs-ui-swing</artifactId>
			<version>2.0</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<!-- Build an executable JAR -->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.1.0</version>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<!-- here we specify that we want to use the main
							method within the App class -->
							<mainClass>mwe.NodeSyncTest</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>mwe.NodeSyncTest</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

To use these two files, just create a folder mwe/, put NodeSyncTest.java into mwe/src/main/java/mwe/ and pom.xml into mwe/, and, in mwe/, run
mvn compile assembly:single
and
java -jar target/mwe-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Here is the full project folder:
MWE.zip

EDIT: I just posted my question on Stack Overflow (https://stackoverflow.com/q/77913500/3890496) to reach more readers. I will update this issue if I get answer from there.

@cachambon
Copy link
Author

I found from where my issue comes from. See my answer on https://stackoverflow.com/a/77927726/3890496. I will create another issue, more specific than this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant