use of org.gephi.graph.api.Graph in project gephi-plugins-bootcamp by gephi.
the class InvertRowSelection method execute.
@Override
public void execute() {
//Note that a function to inverse selection directly in the table with DataTablesController
//would be more efficient than calculating it here, but this example demonstrates some table selection features.
DataTablesController dtc = Lookup.getDefault().lookup(DataTablesController.class);
Graph graph = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getGraph();
if (dtc.isNodeTableMode()) {
//Get currently selected nodes and calculate inverse set.
Node[] selected = dtc.getNodeTableSelection();
ArrayList<Node> nodes = new ArrayList<Node>();
nodes.addAll(Arrays.asList(graph.getNodes().toArray()));
for (Node node : selected) {
nodes.remove(node);
}
dtc.setNodeTableSelection(nodes.toArray(new Node[0]));
} else if (dtc.isEdgeTableMode()) {
//Get currently selected edges and calculate inverse set.
Edge[] selected = dtc.getEdgeTableSelection();
ArrayList<Edge> edges = new ArrayList<Edge>();
edges.addAll(Arrays.asList(graph.getEdges().toArray()));
for (Edge edge : selected) {
edges.remove(edge);
}
dtc.setEdgeTableSelection(edges.toArray(new Edge[0]));
}
}
use of org.gephi.graph.api.Graph in project gephi-plugins-bootcamp by gephi.
the class SQLiteDatabaseExporter method execute.
@Override
public boolean execute() {
Connection connection = null;
try {
if (path.getParentFile().exists()) {
//Create connection
SQLiteDriver sQLiteDriver = Lookup.getDefault().lookup(SQLiteDriver.class);
String connectionUrl = SQLUtils.getUrl(sQLiteDriver, path.getAbsolutePath(), 0, "");
connection = sQLiteDriver.getConnection(connectionUrl, "", "");
//Create statement and create nodes and egdes table
Statement statement = connection.createStatement();
// set timeout to 30 sec.
statement.setQueryTimeout(30);
statement.executeUpdate("drop table if exists nodes");
statement.executeUpdate("drop table if exists edges");
statement.executeUpdate("create table nodes (id string, label string)");
statement.executeUpdate("create table edges (source string, target string, weight real)");
//Get the current graph in the defined workspace
GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
GraphModel graphModel = graphController.getGraphModel(workspace);
Graph graph = graphModel.getGraphVisible();
//Count the number of tasks (nodes + edges) and start the progress
int tasks = graph.getNodeCount() + graph.getEdgeCount();
Progress.start(progress, tasks);
//Export nodes. Progress is incremented at each step.
for (Node n : graph.getNodes().toArray()) {
String id = n.getId().toString();
String label = n.getLabel();
statement.executeUpdate("insert into nodes values('" + id + "', '" + label + "')");
if (cancel) {
return false;
}
Progress.progress(progress);
}
//Export edges. Progress is incremented at each step.
for (Edge e : graph.getEdges().toArray()) {
String sourceId = e.getSource().getId().toString();
String targetId = e.getTarget().getId().toString();
String weight = String.valueOf(e.getWeight());
statement.executeUpdate("insert into edges values('" + sourceId + "', '" + targetId + "', '" + weight + "')");
if (cancel) {
return false;
}
Progress.progress(progress);
}
//Finish progress
Progress.finish(progress);
return true;
} else {
throw new FileNotFoundException(path.getAbsolutePath() + " does not exist");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// connection close failed.
System.err.println(e);
}
}
}
use of org.gephi.graph.api.Graph in project gephi-plugins-bootcamp by gephi.
the class SortedGridLayout method goAlgo.
@Override
public void goAlgo() {
Graph graph = graphModel.getGraphVisible();
graph.readLock();
int nodeCount = graph.getNodeCount();
Node[] nodes = graph.getNodes().toArray();
if (column != null) {
Arrays.sort(nodes, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
Number n1 = (Number) o1.getAttribute(column);
Number n2 = (Number) o2.getAttribute(column);
if (n1.doubleValue() < n2.doubleValue()) {
return 1;
} else if (n1.doubleValue() > n2.doubleValue()) {
return -1;
} else {
return 0;
}
}
});
}
int rows = (int) Math.round(Math.sqrt(nodeCount)) + 1;
int cols = (int) Math.round(Math.sqrt(nodeCount)) + 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols && (i * rows + j) < nodes.length; j++) {
Node node = nodes[i * rows + j];
float x = (-areaSize / 2f) + ((float) j / cols) * areaSize;
float y = (areaSize / 2f) - ((float) i / rows) * areaSize;
float px = node.x();
float py = node.y();
node.setX(px + (x - px) * (speed / 10000f));
node.setY(py + (y - py) * (speed / 10000f));
}
}
graph.readUnlock();
}
use of org.gephi.graph.api.Graph in project gephi-plugins-bootcamp by gephi.
the class RemoveSelfLoopsAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
//Get the current graph model
GraphController gc = Lookup.getDefault().lookup(GraphController.class);
GraphModel graphModel = gc.getGraphModel();
if (graphModel != null) {
//Remove self loops
int removed = 0;
Graph graph = graphModel.getGraph();
graph.writeLock();
for (Edge edge : graph.getEdges().toArray()) {
if (edge.isSelfLoop()) {
graph.removeEdge(edge);
removed++;
}
}
graph.writeUnlock();
//Notification message
NotifyDescriptor d = new NotifyDescriptor.Message(removed + " self-loop have been removed", NotifyDescriptor.INFORMATION_MESSAGE);
DialogDisplayer.getDefault().notify(d);
} else {
//Error message
NotifyDescriptor d = new NotifyDescriptor.Message("No active workspace", NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notify(d);
}
}
use of org.gephi.graph.api.Graph in project gephi by gephi.
the class DataTableTopComponent method initEdgesView.
private void initEdgesView() {
Runnable initEdgesRunnable = new Runnable() {
@Override
public void run() {
try {
if (dataTablesModel == null) {
return;
}
String busyMsg = NbBundle.getMessage(DataTableTopComponent.class, "DataTableTopComponent.tableScrollPane.busyMessage");
BusyUtils.BusyLabel busylabel = BusyUtils.createCenteredBusyLabel(tableScrollPane, busyMsg, edgeTable.getTable());
busylabel.setBusy(true);
//Attributes columns
edgeAvailableColumnsModel.syncronizeTableColumns();
final Column[] cols = edgeAvailableColumnsModel.getAvailableColumns();
refreshAvailableColumnsButton(edgeAvailableColumnsModel, Lookup.getDefault().lookup(GraphController.class).getGraphModel().getEdgeTable());
//Edges from graph
Graph graph;
if (visibleOnly) {
graph = graphModel.getGraphVisible();
} else {
graph = graphModel.getGraph();
}
if (graph == null) {
tableScrollPane.setViewportView(null);
return;
}
//Model
edgeTable.refreshModel(graph.getEdges().toArray(), cols, graphModel, dataTablesModel);
busylabel.setBusy(false);
edgeTable.scrollToFirstElementSelected();
} catch (Exception e) {
Exceptions.printStackTrace(e);
JLabel errorLabel = new JLabel(NbBundle.getMessage(DataTableTopComponent.class, "DataTableTopComponent.tableScrollPane.error"), SwingConstants.CENTER);
tableScrollPane.setViewportView(errorLabel);
}
}
};
SwingUtilities.invokeLater(initEdgesRunnable);
}
Aggregations