do layout at each graph update

This commit is contained in:
millmanorama 2018-01-26 11:49:08 +01:00
parent 096a85b880
commit 0d187049f1

View File

@ -271,6 +271,7 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
final AccountDeviceInstance accountDeviceInstance = accountDeviceInstanceKey.getAccountDeviceInstance(); final AccountDeviceInstance accountDeviceInstance = accountDeviceInstanceKey.getAccountDeviceInstance();
final String name =// accountDeviceInstance.getDeviceId() + ":" + final String name =// accountDeviceInstance.getDeviceId() + ":" +
accountDeviceInstance.getAccount().getTypeSpecificID(); accountDeviceInstance.getAccount().getTypeSpecificID();
final mxCell vertex = nodeMap.computeIfAbsent(name, vertexName -> { final mxCell vertex = nodeMap.computeIfAbsent(name, vertexName -> {
double size = Math.sqrt(accountDeviceInstanceKey.getMessageCount()) + 10; double size = Math.sqrt(accountDeviceInstanceKey.getMessageCount()) + 10;
@ -288,31 +289,34 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
graph.getView().updateLabel(state); graph.getView().updateLabel(state);
graph.getView().updateLabelBounds(state); graph.getView().updateLabelBounds(state);
graph.getView().updateBoundingBox(state); graph.getView().updateBoundingBox(state);
return vertex; return vertex;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void addEdge(Collection<Content> relSources, AccountDeviceInstanceKey account1, AccountDeviceInstanceKey account2) throws TskCoreException { private mxCell addEdge(Collection<Content> relSources, AccountDeviceInstanceKey account1, AccountDeviceInstanceKey account2) throws TskCoreException {
mxCell vertex1 = getOrCreateVertex(account1); mxCell vertex1 = getOrCreateVertex(account1);
mxCell vertex2 = getOrCreateVertex(account2); mxCell vertex2 = getOrCreateVertex(account2);
Object[] edgesBetween = graph.getEdgesBetween(vertex1, vertex2); Object[] edgesBetween = graph.getEdgesBetween(vertex1, vertex2);
mxCell edge;
if (edgesBetween.length == 0) { if (edgesBetween.length == 0) {
final String edgeName = vertex1.getId() + " <-> " + vertex2.getId(); final String edgeName = vertex1.getId() + " <-> " + vertex2.getId();
final HashSet<Content> hashSet = new HashSet<>(relSources); final HashSet<Content> hashSet = new HashSet<>(relSources);
mxCell edge = (mxCell) graph.insertEdge(graph.getDefaultParent(), edgeName, hashSet, vertex1, vertex2, // edgeMap.put(relSource, edge);
edge = (mxCell) graph.insertEdge(graph.getDefaultParent(), edgeName, hashSet, vertex1, vertex2,
"strokeWidth=" + Math.sqrt(hashSet.size())); "strokeWidth=" + Math.sqrt(hashSet.size()));
// edgeMap.put(relSource, edge); } else {
} else if (edgesBetween.length == 1) { edge = (mxCell) edgesBetween[0];
final mxCell edge = (mxCell) edgesBetween[0];
((Collection<Content>) edge.getValue()).addAll(relSources); ((Collection<Content>) edge.getValue()).addAll(relSources);
edge.setStyle("strokeWidth=" + Math.sqrt(((Collection) edge.getValue()).size())); edge.setStyle("strokeWidth=" + Math.sqrt(((Collection) edge.getValue()).size()));
} }
return edge;
} }
@Subscribe @Subscribe
void handleUnPinEvent(CVTEvents.UnpinAccountsEvent pinEvent) { void handleUnPinEvent(CVTEvents.UnpinAccountsEvent pinEvent) {
// graph.getModel().beginUpdate(); graph.getModel().beginUpdate();
try { try {
pinnedAccountDevices.removeAll(pinEvent.getAccountDeviceInstances()); pinnedAccountDevices.removeAll(pinEvent.getAccountDeviceInstances());
@ -322,15 +326,15 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
logger.log(Level.SEVERE, "Error pinning accounts", ex); logger.log(Level.SEVERE, "Error pinning accounts", ex);
} finally { } finally {
// Updates the display // Updates the display
// graph.getModel().endUpdate(); graph.getModel().endUpdate();
} }
// applyOrganicLayout(); applyOrganicLayout();
} }
@Subscribe @Subscribe
void handlePinEvent(CVTEvents.PinAccountsEvent pinEvent) { void handlePinEvent(CVTEvents.PinAccountsEvent pinEvent) {
// graph.getModel().beginUpdate(); graph.getModel().beginUpdate();
try { try {
if (pinEvent.isReplace()) { if (pinEvent.isReplace()) {
pinnedAccountDevices.clear(); pinnedAccountDevices.clear();
@ -342,10 +346,10 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
logger.log(Level.SEVERE, "Error pinning accounts", ex); logger.log(Level.SEVERE, "Error pinning accounts", ex);
} finally { } finally {
// Updates the display // Updates the display
// graph.getModel().endUpdate(); graph.getModel().endUpdate();
} }
// applyOrganicLayout(); applyOrganicLayout();
} }
@Subscribe @Subscribe
@ -363,17 +367,18 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
graph.getModel().endUpdate(); graph.getModel().endUpdate();
} }
// applyOrganicLayout(); applyOrganicLayout();
} }
private void rebuildGraph() throws TskCoreException { private void rebuildGraph() throws TskCoreException {
progressBar.setVisible(true); progressBar.setVisible(true);
progressBar.setIndeterminate(true);
progressBar.setStringPainted(false);
new SwingWorker<Set<RelationshipModel>, RelationshipModel>() { new SwingWorker<Set<RelationshipModel>, RelationshipModel>() {
@Override @Override
protected Set<RelationshipModel> doInBackground() throws Exception { protected Set<RelationshipModel> doInBackground() throws Exception {
progressBar.setIndeterminate(true);
progressBar.setStringPainted(false);
Set<RelationshipModel> relationshipModels = new HashSet<>(); Set<RelationshipModel> relationshipModels = new HashSet<>();
try { try {
@ -398,16 +403,15 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
//for each pair of related accounts add edges if they are related o each other. //for each pair of related accounts add edges if they are related o each other.
// this is O(n^2) in the number of related accounts!!! // this is O(n^2) in the number of related accounts!!!
List<AccountDeviceInstanceKey> relatedAccountsList = new ArrayList<>(relatedAccounts); List<AccountDeviceInstanceKey> relatedAccountsList = new ArrayList<>(relatedAccounts);
SwingUtilities.invokeLater(() -> {
progressBar.setString(""); progressBar.setString("");
progressBar.setStringPainted(true); progressBar.setStringPainted(true);
progressBar.setValue(0); progressBar.setValue(0);
progressBar.setMaximum(relatedAccountsList.size()); progressBar.setMaximum(relatedAccountsList.size());
progressBar.setIndeterminate(false); progressBar.setIndeterminate(false);
});
for (int i = 0; i < relatedAccountsList.size(); i++) { for (int i = 0; i < relatedAccountsList.size(); i++) {
AccountDeviceInstanceKey adiKey1 = relatedAccountsList.get(i); AccountDeviceInstanceKey adiKey1 = relatedAccountsList.get(i);
progressBar.setString(adiKey1.getAccountDeviceInstance().getAccount().getTypeSpecificID());
for (int j = i; j < relatedAccountsList.size(); j++) { for (int j = i; j < relatedAccountsList.size(); j++) {
AccountDeviceInstanceKey adiKey2 = relatedAccountsList.get(j); AccountDeviceInstanceKey adiKey2 = relatedAccountsList.get(j);
@ -420,7 +424,10 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
publish(relationshipModel); publish(relationshipModel);
} }
} }
progressBar.setValue(i); final int p = i;
SwingUtilities.invokeLater(() -> {
progressBar.setValue(p);
});
} }
} catch (TskCoreException tskCoreException) { } catch (TskCoreException tskCoreException) {
logger.log(Level.SEVERE, "Error", tskCoreException); logger.log(Level.SEVERE, "Error", tskCoreException);
@ -434,15 +441,16 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
super.process(chunks); super.process(chunks);
for (RelationshipModel relationShipModel : chunks) { for (RelationshipModel relationShipModel : chunks) {
try { try {
addEdge(relationShipModel.getSources(), relationShipModel.adiKey1, relationShipModel.adiKey2); mxCell addEdge = addEdge(relationShipModel.getSources(),
relationShipModel.getAccount1(),
relationShipModel.getAccount2());
progressBar.setString(addEdge.getId());
} catch (TskCoreException ex) { } catch (TskCoreException ex) {
Exceptions.printStackTrace(ex); Exceptions.printStackTrace(ex);
} }
} }
mxOrganicLayout mxOrganicLayout = new mxOrganicLayout(graph); applyOrganicLayout();
mxOrganicLayout.setMaxIterations(1);
morph(mxOrganicLayout);
} }
@Override @Override
@ -672,10 +680,14 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
}//GEN-LAST:event_jButton7ActionPerformed }//GEN-LAST:event_jButton7ActionPerformed
private void jButton8ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed private void jButton8ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
applyOrganicLayout();
}//GEN-LAST:event_jButton8ActionPerformed
private void applyOrganicLayout() {
mxOrganicLayout mxOrganicLayout = new mxOrganicLayout(graph); mxOrganicLayout mxOrganicLayout = new mxOrganicLayout(graph);
mxOrganicLayout.setMaxIterations(10); mxOrganicLayout.setMaxIterations(10);
morph(mxOrganicLayout); morph(mxOrganicLayout);
}//GEN-LAST:event_jButton8ActionPerformed }
private void fitGraphButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_fitGraphButtonActionPerformed private void fitGraphButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_fitGraphButtonActionPerformed
fitGraph(); fitGraph();
@ -683,9 +695,18 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
private void fitGraph() { private void fitGraph() {
final Object[] childVertices = graph.getChildVertices(graph.getDefaultParent()); final Object[] childVertices = graph.getChildVertices(graph.getDefaultParent());
final mxRectangle boundsForCells = graph.getBoundsForCells(childVertices,true, true,true); mxRectangle boundsForCells = graph.getBoundsForCells(childVertices, true, true, true);
graph.getView().setTranslate(new mxPoint(-boundsForCells.getX(), -boundsForCells.getY())); if (boundsForCells == null){
boundsForCells = new mxRectangle();
}
mxPoint translate = graph.getView().getTranslate();
if(translate == null){
translate = new mxPoint();
}
graph.getView().setTranslate(new mxPoint(translate.getX()-boundsForCells.getX(),translate.getY() -boundsForCells.getY()));
// graph.moveCells(childVertices, -boundsForCells.getX(), -boundsForCells.getY());
// final double widthFactor = (double) graphComponent.getWidth() / (int) view.getGraphBounds().getWidth(); // final double widthFactor = (double) graphComponent.getWidth() / (int) view.getGraphBounds().getWidth();
// final double heightFactor = (double) graphComponent.getHeight() / (int) view.getGraphBounds().getHeight(); // final double heightFactor = (double) graphComponent.getHeight() / (int) view.getGraphBounds().getHeight();
// //
@ -703,6 +724,9 @@ final public class VisualizationPanel extends JPanel implements Lookup.Provider
graph.getModel().endUpdate(); graph.getModel().endUpdate();
fitGraph(); fitGraph();
}); });
morph.addListener(mxEvent.EXECUTE, (Object sender, mxEventObject event) -> {
fitGraph();
});
morph.startAnimation(); morph.startAnimation();
} }