Skip to content

Commit

Permalink
[vaadin#4] refactored client-side node-removal code
Browse files Browse the repository at this point in the history
  • Loading branch information
Marlon Richert committed May 30, 2012
1 parent 9d5b21f commit a29f9fb
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 139 deletions.
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType">
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_BUILDER_ENABLED" value="false"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_DISABLED_BUILDER" value="org.eclipse.wst.jsdt.core.javascriptValidator"/>
<mapAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS"/>
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
</launchConfiguration>
13 changes: 6 additions & 7 deletions .project
Expand Up @@ -6,8 +6,13 @@
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
<dictionary>
<key>LaunchConfigHandle</key>
<value>&lt;project&gt;/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
Expand Down Expand Up @@ -35,11 +40,6 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.stateofflow.eclipse.metrics.MetricsBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
Expand All @@ -49,6 +49,5 @@
<nature>com.vaadin.integration.eclipse.widgetsetNature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>edu.umd.cs.findbugs.plugin.eclipse.findbugsNature</nature>
<nature>com.stateofflow.eclipse.metrics.MetricsNature</nature>
</natures>
</projectDescription>
5 changes: 0 additions & 5 deletions src/com/vaadin/graph/GraphExplorer.java
Expand Up @@ -24,11 +24,6 @@
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.themes.Reindeer;

/**
* Server side component for the VGraphExplorer widget.
*
* @author Marlon Richert @ <a href="http://vaadin.com/">Vaadin</a>
*/
@ClientWidget(VGraphExplorer.class)
public class GraphExplorer extends AbstractComponent {
private static final long serialVersionUID = 1L;
Expand Down
44 changes: 21 additions & 23 deletions src/com/vaadin/graph/client/ArcController.java
Expand Up @@ -30,23 +30,23 @@ class ArcController implements Controller {

private static final int ARROWHEAD_LENGTH = 10;
private static final int ARROWHEAD_WIDTH = ARROWHEAD_LENGTH / 2;
private Line arc;
private Line line;
private HTML label;
private Line arrowheadLeft;
private Line arrowheadRight;
private final ArcProxy relationship;
private final ArcProxy arc;
private double terminusX;
private double terminusY;
private final VGraphExplorer parent;

ArcController(VGraphExplorer parent, ArcProxy relationship) {
ArcController(VGraphExplorer parent, ArcProxy arc) {
this.parent = parent;
this.relationship = relationship;
this.arc = arc;
addArc();
addArrowhead();
addLabel();
relationship.setObserver(this);
update();
arc.setController(this);
onUpdateInModel();
}

private void addArrowhead() {
Expand All @@ -57,15 +57,15 @@ private void addArrowhead() {
}

private void addArc() {
arc = new Line(0, 0, 0, 0);
parent.add(arc);
line = new Line(0, 0, 0, 0);
parent.add(line);
}

private void addLabel() {
label = new HTML(relationship.getLabel());
label = new HTML(arc.getLabel());
label.getElement().setClassName("relationship");
if (!relationship.isGroup()) {
label.setTitle(relationship.getId());
if (!arc.isGroup()) {
label.setTitle(arc.getId());
}
parent.add(label);
}
Expand All @@ -74,28 +74,26 @@ private double distance(double fromX, double fromY, double toX, double toY) {
return Math.abs(toX - fromX) + Math.abs(toY - fromY);
}

public void remove() {
relationship.setObserver(null);
parent.remove(arc);
parent.remove(arc);
public void onRemoveFromModel() {
arc.setController(null);
parent.remove(line);
parent.remove(label);
parent.remove(arrowheadLeft);
parent.remove(arrowheadRight);
parent.getGraph().removeArc(relationship.getId());
}

public void update() {
public void onUpdateInModel() {
updateArc();
updateLabel();
updateArrowhead();
}

private void updateArrowhead() {
GraphProxy graph = parent.getGraph();
NodeProxy from = graph.getSource(relationship);
NodeProxy from = graph.getSource(arc);
double fromX = from.getX();
double fromY = from.getY();
NodeProxy to = graph.getDest(relationship);
NodeProxy to = graph.getDest(arc);
double toX = to.getX();
double toY = to.getY();
double dX = toX - fromX;
Expand Down Expand Up @@ -144,15 +142,15 @@ private void updateArrowhead() {

private void updateArc() {
GraphProxy graph = parent.getGraph();
NodeProxy from = graph.getSource(relationship);
NodeProxy to = graph.getDest(relationship);
updateLine(arc, from.getX(), from.getY(), to.getX(), to.getY());
NodeProxy from = graph.getSource(arc);
NodeProxy to = graph.getDest(arc);
updateLine(line, from.getX(), from.getY(), to.getX(), to.getY());
}

private Style updateLabel() {
Style style = label.getElement().getStyle();
GraphProxy graph = parent.getGraph();
NodeProxy from = graph.getSource(relationship);
NodeProxy from = graph.getSource(arc);

double x = getLabelCenter(from.getX(), terminusX)
- label.getOffsetWidth() / 2.0;
Expand Down
5 changes: 0 additions & 5 deletions src/com/vaadin/graph/client/ArcProxy.java
Expand Up @@ -55,9 +55,4 @@ public void setGroup(boolean group) {
public boolean isGroup() {
return group;
}

public IndexedElement getSource() {
// TODO Auto-generated method stub
return null;
}
}
4 changes: 2 additions & 2 deletions src/com/vaadin/graph/client/Controller.java
Expand Up @@ -22,7 +22,7 @@
*/
interface Controller {

void update();
void onUpdateInModel();

void remove();
void onRemoveFromModel();
}
109 changes: 58 additions & 51 deletions src/com/vaadin/graph/client/GraphProxy.java
Expand Up @@ -17,8 +17,6 @@

import java.util.*;

import com.vaadin.terminal.gwt.client.VConsole;

/**
* Data structure consisting of nodes with relationships between them.
*
Expand All @@ -30,18 +28,27 @@ public class GraphProxy {
private final Map<String, ArcProxy> arcs = new HashMap<String, ArcProxy>();
private final Map<NodeProxy, Set<ArcProxy>> inArcSets = new HashMap<NodeProxy, Set<ArcProxy>>();
private final Map<NodeProxy, Set<ArcProxy>> outArcSets = new HashMap<NodeProxy, Set<ArcProxy>>();
private final Map<ArcProxy, NodeProxy> sourceNodes = new HashMap<ArcProxy, NodeProxy>();
private final Map<ArcProxy, NodeProxy> destNodes = new HashMap<ArcProxy, NodeProxy>();

public boolean addArc(ArcProxy e, NodeProxy source, NodeProxy dest) {
if (arcs.containsKey(e.id)) {
private final Map<ArcProxy, NodeProxy> tails = new HashMap<ArcProxy, NodeProxy>();
private final Map<ArcProxy, NodeProxy> heads = new HashMap<ArcProxy, NodeProxy>();

/**
* Adds a new arc from the given tail to the given head.
*
* @return true, if successful; false, otherwise
*/
public boolean addArc(ArcProxy arc, NodeProxy tail, NodeProxy head) {
if (arcs.containsKey(arc.id)) {
return false;
}
arcs.put(e.id, e);
inArcSets.get(dest).add(e);
outArcSets.get(source).add(e);
sourceNodes.put(e, source);
destNodes.put(e, dest);

arcs.put(arc.id, arc);

heads.put(arc, head);
inArcSets.get(head).add(arc);

tails.put(arc, tail);
outArcSets.get(tail).add(arc);

return true;
}

Expand All @@ -55,6 +62,41 @@ public boolean addNode(NodeProxy v) {
return true;
}

public void removeNode(String id) {
if (nodes.containsKey(id)) {
NodeProxy node = nodes.get(id);
for (ArcProxy arc : getIncidentArcs(node)) {
removeArc(arc);
}
nodes.remove(id);
node.notifyRemove();
}
}

private Set<ArcProxy> getIncidentArcs(NodeProxy node) {
Set<ArcProxy> incidentArcs = new HashSet<ArcProxy>(inArcSets.get(node));
incidentArcs.addAll(outArcSets.get(node));
return incidentArcs;
}

private void removeArc(ArcProxy e) {
String id = e.getId();
boolean success = arcs.containsKey(id);
if (success) {

ArcProxy arc = arcs.get(id);

NodeProxy head = heads.remove(arc);
inArcSets.get(head).remove(arc);

NodeProxy tail = tails.remove(arc);
outArcSets.get(tail).remove(arc);

arcs.remove(id);
arc.notifyRemove();
}
}

public boolean containsArc(String id) {
return arcs.containsKey(id);
}
Expand All @@ -75,7 +117,7 @@ public int degree(NodeProxy v) {
}

public NodeProxy getDest(ArcProxy e) {
return destNodes.get(e);
return heads.get(e);
}

public ArcProxy getArc(String id) {
Expand Down Expand Up @@ -114,7 +156,7 @@ public Collection<ArcProxy> getOutArcs(NodeProxy v) {
}

public NodeProxy getSource(ArcProxy e) {
return sourceNodes.get(e);
return tails.get(e);
}

public NodeProxy getNode(String id) {
Expand All @@ -125,42 +167,7 @@ public Collection<NodeProxy> getNodes() {
return Collections.unmodifiableCollection(nodes.values());
}

public void removeArc(ArcProxy e) {
removeArc(e.getId());
}

public boolean removeArc(String id) {
boolean success = arcs.containsKey(id);
if (success) {
ArcProxy e = arcs.remove(id);

VConsole.log("remove " + getSource(e).id + " " + e.getType() + " "
+ getDest(e).id);

outArcSets.get(sourceNodes.remove(e)).remove(e);
inArcSets.get(destNodes.remove(e)).remove(e);
}
return success;
}

public boolean removeNode(String id) {

VConsole.log("removeNode(" + id + ")");

boolean success = nodes.containsKey(id);
if (success) {
NodeProxy v = nodes.remove(id);
for (ArcProxy e : inArcSets.remove(v)) {
removeArc(e);
}
for (ArcProxy e : outArcSets.remove(v)) {
removeArc(e);
}
}
return success;
}

public boolean removeNode(NodeProxy v) {
return removeNode(v.getId());
public void removeNode(NodeProxy node) {
removeNode(node.getId());
}
}
8 changes: 4 additions & 4 deletions src/com/vaadin/graph/client/IndexedElement.java
Expand Up @@ -68,18 +68,18 @@ public final int hashCode() {

public void notifyRemove() {
if (controller != null) {
controller.remove();
controller.onRemoveFromModel();
}
}

public void notifyUpdate() {
if (controller != null) {
controller.update();
controller.onUpdateInModel();
}
}

void setObserver(Controller observer) {
controller = observer;
void setController(Controller controller) {
this.controller = controller;
}

/** Formats the given string for use as a key in a JSON object. */
Expand Down

0 comments on commit a29f9fb

Please sign in to comment.