Skip to content

Commit

Permalink
Fix #2706 and improve selection on graph
Browse files Browse the repository at this point in the history
  • Loading branch information
mbastian committed Nov 26, 2022
1 parent b0e652e commit 4f0eddc
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ Development and Distribution License("CDDL") (collectively, the
import org.gephi.datalab.spi.ManipulatorUI;
import org.gephi.graph.api.Edge;
import org.gephi.visualization.VizController;
import org.gephi.visualization.api.VisualizationController;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;

/**
Expand All @@ -67,8 +69,11 @@ public void setup(Edge[] nodes, Edge clickedEdge) {

@Override
public void execute() {
VizController.getInstance().getSelectionManager().selectEdges(edges);
VizController.getInstance().getSelectionManager().centerOnNode(clickedEdge.getSource());
VisualizationController vc = Lookup.getDefault().lookup(VisualizationController.class);
if (vc != null) {
vc.selectEdges(edges);
vc.centerOnEdge(clickedEdge);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ Development and Distribution License("CDDL") (collectively, the
import org.gephi.datalab.spi.ManipulatorUI;
import org.gephi.graph.api.Node;
import org.gephi.visualization.VizController;
import org.gephi.visualization.api.VisualizationController;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;

/**
Expand All @@ -67,8 +69,11 @@ public void setup(Node[] nodes, Node clickedNode) {

@Override
public void execute() {
VizController.getInstance().getSelectionManager().selectNodes(nodes);
VizController.getInstance().getSelectionManager().centerOnNode(clickedNode);
VisualizationController vc = Lookup.getDefault().lookup(VisualizationController.class);
if (vc != null) {
vc.selectNodes(nodes);
vc.centerOnNode(clickedNode);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public interface VisualizationController {

public void resetSelection();

public void resetNodesSelection();
public void centerOnNode(Node node);

public void resetEdgesSelection();
public void centerOnEdge(Edge edge);

public void selectNodes(Node[] nodes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,21 @@ public void destroy() {
@Override
public void resetSelection() {
if (selectionManager != null) {
selectionManager.resetSelection();
selectionManager.resetSelection(currentModel);
}
}

@Override
public void resetNodesSelection() {
public void centerOnNode(Node node) {
if (selectionManager != null) {
selectionManager.selectNodes(null);
selectionManager.centerOnNode(node);
}
}

@Override
public void resetEdgesSelection() {
public void centerOnEdge(Edge edge) {
if (selectionManager != null) {
selectionManager.selectEdges(null);
selectionManager.centerOnEdge(edge);
}
}

Expand All @@ -237,16 +237,14 @@ public void selectEdge(Edge edge) {
@Override
public void selectNodes(Node[] nodes) {
if (selectionManager != null) {
currentModel.setAutoSelectNeighbor(false);
selectionManager.selectNodes(nodes);
selectionManager.selectNodes(nodes, currentModel);
}
}

@Override
public void selectEdges(Edge[] edges) {
if (selectionManager != null) {
currentModel.setAutoSelectNeighbor(false);
selectionManager.selectEdges(edges);
selectionManager.selectEdges(edges, currentModel);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ Development and Distribution License("CDDL") (collectively, the

package org.gephi.visualization.api.selection;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.gephi.graph.api.Edge;
import org.gephi.graph.api.Node;
import org.gephi.visualization.VizArchitecture;
import org.gephi.visualization.VizController;
import org.gephi.visualization.VizModel;
import org.gephi.visualization.apiimpl.VizConfig;
import org.gephi.visualization.model.Model;
import org.gephi.visualization.opengl.AbstractEngine;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.util.ArrayList;
import java.util.List;

/**
* @author Mathieu Bastian
*/
Expand All @@ -69,6 +69,10 @@ public class SelectionManager implements VizArchitecture {
private boolean selectionUpdateWhileDragging;
//States
private boolean blocked = false;
private boolean wasAutoSelectNeighbors = false;
private boolean wasRectangleSelection = false;
private boolean wasDragSelection = false;
private boolean wasDirectSelection = false;

public SelectionManager() {
listeners = new ArrayList<>();
Expand Down Expand Up @@ -104,48 +108,54 @@ public void setDraggingEnable(boolean dragging) {
}

public void setRectangleSelection() {
engine.setRectangleSelection(true);
vizConfig.setDraggingEnable(false);
vizConfig.setCustomSelection(false);
vizConfig.setSelectionEnable(true);
engine.setRectangleSelection(true);
this.blocked = false;
fireChangeEvent();
}

public void setDirectMouseSelection() {
engine.setRectangleSelection(false);
vizConfig.setSelectionEnable(true);
vizConfig.setDraggingEnable(false);
vizConfig.setCustomSelection(false);
engine.setRectangleSelection(false);
this.blocked = false;
fireChangeEvent();
}

public void setDraggingMouseSelection() {
engine.setRectangleSelection(false);
vizConfig.setDraggingEnable(true);
vizConfig.setMouseSelectionUpdateWhileDragging(false);
vizConfig.setSelectionEnable(true);
vizConfig.setCustomSelection(false);
engine.setRectangleSelection(false);
this.blocked = false;
fireChangeEvent();
}

public void setCustomSelection() {
engine.setRectangleSelection(false);
vizConfig.setSelectionEnable(false);
vizConfig.setDraggingEnable(false);
vizConfig.setCustomSelection(true);
engine.setRectangleSelection(false);
//this.blocked = true;
fireChangeEvent();
}

public void resetSelection() {
public void resetSelection(VizModel vizModel) {
if (isCustomSelection()) {
vizConfig.setCustomSelection(false);
setDirectMouseSelection();
engine.resetSelection();
vizModel.setAutoSelectNeighbor(wasAutoSelectNeighbors);
if (wasRectangleSelection) {
setRectangleSelection();
} else if (wasDragSelection) {
setDraggingMouseSelection();
} else if (wasDirectSelection) {
setDirectMouseSelection();
}
}
engine.resetSelection();
}

public List<Node> getSelectedNodes() {
Expand All @@ -156,25 +166,27 @@ public List<Edge> getSelectedEdges() {
return engine.getSelectedUnderlyingEdges();
}

public void selectNode(Node node) {
selectNodes(new Node[] {node});
}

public void selectEdge(Edge edge) {
selectEdges(new Edge[] {edge});
}

public void selectNodes(Node[] nodes) {
public void selectNodes(Node[] nodes, VizModel vizModel) {
if (!isCustomSelection()) {
wasAutoSelectNeighbors = vizModel.isAutoSelectNeighbor();
wasDragSelection = isDraggingEnabled();
wasDirectSelection = isDirectMouseSelection();
wasRectangleSelection = isRectangleSelection();
vizModel.setAutoSelectNeighbor(false);
setCustomSelection();
}

Model[] models = engine.getNodeModelsForNodes(nodes);
engine.selectObject(models);
}

public void selectEdges(Edge[] edges) {
public void selectEdges(Edge[] edges, VizModel vizModel) {
if (!isCustomSelection()) {
wasAutoSelectNeighbors = vizModel.isAutoSelectNeighbor();
wasDragSelection = isDraggingEnabled();
wasDirectSelection = isDirectMouseSelection();
wasRectangleSelection = isRectangleSelection();
vizModel.setAutoSelectNeighbor(false);
setCustomSelection();
}

Expand All @@ -184,7 +196,20 @@ public void selectEdges(Edge[] edges) {

public void centerOnNode(Node node) {
if (node != null) {
VizController.getInstance().getGraphIO().centerOnCoordinate(node.x(), node.y(), node.z() + node.size() * 40);
VizController.getInstance().getGraphIO()
.centerOnCoordinate(node.x(), node.y(), node.z() + node.size() * 40);
engine.getScheduler().requireUpdateVisible();
}
}

public void centerOnEdge(Edge edge) {
if (edge != null) {
Node source = edge.getSource();
Node target = edge.getTarget();
float len = (float) Math.hypot(source.x() - target.x(), source.y() - target.y());
VizController.getInstance().getGraphIO()
.centerOnCoordinate((source.x() + target.x()) / 2f, (source.y() + target.y()) / 2f,
(source.z() + target.z()) / 2f + len * 5);
engine.getScheduler().requireUpdateVisible();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ public void stateChanged(ChangeEvent e) {
if (!buttonGroup.isSelected(mouseButton.getModel())) {
buttonGroup.setSelected(mouseButton.getModel(), true);
}
} else if (selectionManager.isRectangleSelection()) {
if (!buttonGroup.isSelected(rectangleButton.getModel())) {
buttonGroup.setSelected(rectangleButton.getModel(), true);
}
} else if (selectionManager.isDraggingEnabled()) {
if (!buttonGroup.isSelected(dragButton.getModel())) {
buttonGroup.setSelected(dragButton.getModel(), true);
}
}
}
});
Expand Down

0 comments on commit 4f0eddc

Please sign in to comment.