Implement NodePressAndDraggingEventListener

Please use a mailing-list, see http://gephi.org/developers/
Post Reply
yacine
Posts: 7
Joined: 09 Feb 2017 12:04

Implement NodePressAndDraggingEventListener

Post by yacine » 09 Feb 2017 13:00

I'm new with Gephi, I want to use Gephi in a java program to generate graphics, Firstly I import the GEXF file, and display it into a JFrame.

What I need to do, is to allow user to drag the nodes, I cant find how to do that,

Any help will be appreciated.

this is my code:
import org.gephi.graph.api.DirectedGraph;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphEvent;
import org.gephi.graph.api.GraphListener;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.GraphView;
import org.gephi.graph.api.Node;
import org.gephi.graph.api.UndirectedGraph;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.EdgeDefault;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.preview.api.PreviewController;
import org.gephi.preview.api.PreviewModel;
import org.gephi.preview.api.PreviewProperty;
import org.gephi.preview.api.ProcessingTarget;
import org.gephi.preview.api.RenderTarget;
import org.gephi.preview.types.DependantOriginalColor;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;

import processing.core.PApplet;

public class GephiTest {

public static void main (String args[]){

//Init a project - and therefore a workspace
ProjectController projectController = Lookup.getDefault().lookup(ProjectController.class);
projectController.newProject();
projectController.newWorkspace(projectController.getCurrentProject());

ImportController importController = Lookup.getDefault().lookup(ImportController.class);

Container container;
try {

File file = new File(GephiTest.class.getClassLoader().getResource("test.gexf").toURI());
container = importController.importFile(file);
}
catch (Exception ex) {
Exceptions.printStackTrace(ex);
return;
}

importController.process(container, new DefaultProcessor(), projectController.getCurrentWorkspace());
//Preview configuration
PreviewController previewController = Lookup.getDefault().lookup(PreviewController.class);


PreviewModel previewModel = previewController.getModel();

//previewModel.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.FALSE);
previewModel.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE);
previewModel.getProperties().putValue(PreviewProperty.VISIBILITY_RATIO, 0.5);
previewModel.getProperties().putValue(PreviewProperty.NODE_LABEL_COLOR, new DependantOriginalColor(Color.WHITE));
previewModel.getProperties().putValue(PreviewProperty.EDGE_CURVED, Boolean.FALSE);
previewModel.getProperties().putValue(PreviewProperty.EDGE_OPACITY, 50);
previewModel.getProperties().putValue(PreviewProperty.EDGE_RADIUS, 10f);
previewModel.getProperties().putValue(PreviewProperty.BACKGROUND_COLOR, Color.BLACK);
previewController.refreshPreview();

//New Processing target, get the PApplet
ProcessingTarget target = (ProcessingTarget) previewController.getRenderTarget(RenderTarget.PROCESSING_TARGET);
PApplet applet = target.getApplet();
applet.init();
//Refresh the preview and reset the zoom
previewController.render(target);
target.refresh();
target.resetZoom();


//test get nodes:
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
DirectedGraph directedGraph = graphModel.getDirectedGraph();

Node node = directedGraph.getNode("23");
System.out.println(node.getNodeData().getLabel());


//

//Add the applet to a JFrame and display
JFrame frame = new JFrame("Test Preview");
frame.setLayout(new BorderLayout());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add((Component) applet, BorderLayout.CENTER);

frame.pack();
frame.setVisible(true);

}
}

User avatar
eduramiba
Gephi Code Manager
Posts: 976
Joined: 22 Mar 2010 15:30
Location: Madrid, Spain

Re: Implement NodePressAndDraggingEventListener

Post by eduramiba » 09 Feb 2017 13:26


yacine
Posts: 7
Joined: 09 Feb 2017 12:04

Re: Implement NodePressAndDraggingEventListener

Post by yacine » 09 Feb 2017 17:50

Thanks for the example, I check it and I understand that I need to implement the MouseResponsiveRenderer Interface, and use the ServiceProvider annotation, to enable mouse event.

I did but I get nothing when I click on the graph nodes;

Here is my code:

package giphy;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.io.File;
import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;

import org.gephi.graph.api.DirectedGraph;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphEvent;
import org.gephi.graph.api.GraphListener;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.GraphView;
import org.gephi.graph.api.Node;
import org.gephi.graph.api.UndirectedGraph;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.EdgeDefault;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.layout.plugin.AutoLayout;
import org.gephi.layout.plugin.force.StepDisplacement;
import org.gephi.layout.plugin.force.yifanHu.YifanHuLayout;
import org.gephi.layout.plugin.forceAtlas.ForceAtlasLayout;
import org.gephi.preview.api.PreviewController;
import org.gephi.preview.api.PreviewModel;
import org.gephi.preview.api.PreviewMouseEvent;
import org.gephi.preview.api.PreviewProperties;
import org.gephi.preview.api.PreviewProperty;
import org.gephi.preview.api.ProcessingTarget;
import org.gephi.preview.api.RenderTarget;
import org.gephi.preview.spi.MouseResponsiveRenderer;
import org.gephi.preview.spi.PreviewMouseListener;
import org.gephi.preview.types.DependantOriginalColor;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.lookup.ServiceProvider;

import processing.core.PApplet;

@ServiceProvider(service = MouseResponsiveRenderer.class)

public class GephiTest implements MouseResponsiveRenderer, PreviewMouseListener {

public static void main (String args[]){

//Init a project - and therefore a workspace
ProjectController projectController = Lookup.getDefault().lookup(ProjectController.class);
projectController.newProject();
projectController.newWorkspace(projectController.getCurrentProject());

ImportController importController = Lookup.getDefault().lookup(ImportController.class);

Container container;

//Import FILE
try {

File file = new File(GephiTest.class.getClassLoader().getResource("test.gexf").toURI());
container = importController.importFile(file);
}
catch (Exception ex) {
Exceptions.printStackTrace(ex);
return;
}

importController.process(container, new DefaultProcessor(), projectController.getCurrentWorkspace());
//Preview configuration
PreviewController previewController = Lookup.getDefault().lookup(PreviewController.class);


PreviewModel previewModel = previewController.getModel();

previewModel.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.FALSE);
previewModel.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.FALSE);
//previewModel.getProperties().putValue(PreviewProperty.VISIBILITY_RATIO, 0.5);
previewModel.getProperties().putValue(PreviewProperty.NODE_LABEL_COLOR, new DependantOriginalColor(Color.WHITE));
previewModel.getProperties().putValue(PreviewProperty.EDGE_CURVED, Boolean.FALSE);
previewModel.getProperties().putValue(PreviewProperty.EDGE_OPACITY, 50);
previewModel.getProperties().putValue(PreviewProperty.EDGE_RADIUS, 10f);
previewModel.getProperties().putValue(PreviewProperty.BACKGROUND_COLOR, Color.BLACK);
previewController.refreshPreview();

//New Processing target, get the PApplet
ProcessingTarget target = (ProcessingTarget) previewController.getRenderTarget(RenderTarget.PROCESSING_TARGET);
PApplet applet = target.getApplet();
applet.init();

//Refresh the preview and reset the zoom
previewController.render(target);
target.refresh();
target.resetZoom();

//Add the applet to a JFrame and display
JFrame frame = new JFrame("Test Preview");
frame.setLayout(new BorderLayout());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add((Component) applet, BorderLayout.CENTER);

frame.pack();
frame.setVisible(true);

}

@Override
public boolean needsPreviewMouseListener(
PreviewMouseListener previewMouseListener) {
// TODO Auto-generated method stub
System.err.println("needsPreviewMouseListener");
return true;
}

@Override
public void mouseClicked(PreviewMouseEvent event,
PreviewProperties properties, Workspace workspace) {
// TODO Auto-generated method stub
System.err.println("mouseClicked");

}

@Override
public void mouseDragged(PreviewMouseEvent event,
PreviewProperties properties, Workspace workspace) {
// TODO Auto-generated method stub
System.err.println("mouseDragged");

}

@Override
public void mousePressed(PreviewMouseEvent event,
PreviewProperties properties, Workspace workspace) {
// TODO Auto-generated method stub
System.err.println("mousePressed");

}

@Override
public void mouseReleased(PreviewMouseEvent event,
PreviewProperties properties, Workspace workspace) {
// TODO Auto-generated method stub
System.err.println("mouseReleased");

}


}

User avatar
eduramiba
Gephi Code Manager
Posts: 976
Joined: 22 Mar 2010 15:30
Location: Madrid, Spain

Re: Implement NodePressAndDraggingEventListener

Post by eduramiba » 09 Feb 2017 18:07

Hi,
You should use separate classes for each thing.

My advice is: copy my example files as-is. Verify it's working. Then, make small changes in the files until it does what you need.

yacine
Posts: 7
Joined: 09 Feb 2017 12:04

Re: Implement NodePressAndDraggingEventListener

Post by yacine » 09 Feb 2017 19:23

Hi, sorry for my multiple questions,

So ok I get started with your example,

Firstly, I'm using eclipse, is that a problem, because I see that NetBeans is recomended? and in my case I can't use Netbeans.

Secondly, I'm trying to use the MouseListenerTemplate but eclipse show me an compilation error on the @Override, and when I delete the implemented methods, and implement them again it doesn't show the @Override attribute before method declaration, is that the problem why I get nothing when clicked on the graph nodes?

Thanks again for your response.
Attachments
errors.PNG

User avatar
eduramiba
Gephi Code Manager
Posts: 976
Joined: 22 Mar 2010 15:30
Location: Madrid, Spain

Re: Implement NodePressAndDraggingEventListener

Post by eduramiba » 09 Feb 2017 19:45

Hi,
It should work in any IDE, or even without an IDE.

That seems to indicate that you are not correctly including the gephi toolkit jar in your compile path or something similar.

What does your pom look like? It should be similar to this https://github.com/gephi/gephi-toolkit- ... er/pom.xml Are you able to build it with maven command line? Eclipse + maven has always been a painful experience to me.

yacine
Posts: 7
Joined: 09 Feb 2017 12:04

Re: Implement NodePressAndDraggingEventListener

Post by yacine » 10 Feb 2017 15:13

Hi,

Yes I have the same pom.xml file, and as you said Eclipse+Maven is the cause of the problem, so I tried to execute with Maven command line in Windows, and it works good, Thanks.

Now I need to figure how to fix this problem in Eclipse, because I need to use Gephi in an existing java application in Eclipse.

Post Reply