introduce an interface for the builder of the box model

Signed-off-by: Florian Thienel <florian@thienel.org>
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/IBoxModelBuilder.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/IBoxModelBuilder.java
new file mode 100644
index 0000000..2074b69
--- /dev/null
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/IBoxModelBuilder.java
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Florian Thienel and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ * 		Florian Thienel - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.vex.core.internal.visualization;
+
+import org.eclipse.vex.core.internal.boxes.IInlineBox;
+import org.eclipse.vex.core.internal.boxes.IStructuralBox;
+import org.eclipse.vex.core.internal.boxes.RootBox;
+import org.eclipse.vex.core.provisional.dom.INode;
+
+/**
+ * @author Florian Thienel
+ */
+public interface IBoxModelBuilder {
+
+	RootBox visualizeRoot(INode node);
+
+	IStructuralBox visualizeStructure(INode node);
+
+	IInlineBox visualizeInline(INode node);
+
+}
\ No newline at end of file
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/NodeVisualization.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/NodeVisualization.java
index 0d404d3..485e02e 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/NodeVisualization.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/NodeVisualization.java
@@ -11,15 +11,15 @@
 package org.eclipse.vex.core.internal.visualization;
 
 import org.eclipse.vex.core.internal.boxes.IBox;
-import org.eclipse.vex.core.internal.boxes.IStructuralBox;
 import org.eclipse.vex.core.internal.boxes.IInlineBox;
 import org.eclipse.vex.core.internal.boxes.IParentBox;
+import org.eclipse.vex.core.internal.boxes.IStructuralBox;
 import org.eclipse.vex.core.provisional.dom.BaseNodeVisitorWithResult;
 import org.eclipse.vex.core.provisional.dom.INode;
 
 public class NodeVisualization<T extends IBox> extends BaseNodeVisitorWithResult<T> implements Comparable<NodeVisualization<?>> {
 	private final int priority;
-	private VisualizationChain chain;
+	private IBoxModelBuilder boxModelBuilder;
 
 	public NodeVisualization() {
 		this(0);
@@ -38,13 +38,13 @@
 		return other.priority - priority;
 	}
 
-	public final void setChain(final VisualizationChain chain) {
-		this.chain = chain;
+	public final void setChain(final IBoxModelBuilder boxModelBuilder) {
+		this.boxModelBuilder = boxModelBuilder;
 	}
 
 	protected final <P extends IParentBox<IStructuralBox>> P visualizeChildrenStructure(final Iterable<INode> children, final P parentBox) {
 		for (final INode child : children) {
-			final IStructuralBox childBox = chain.visualizeStructure(child);
+			final IStructuralBox childBox = boxModelBuilder.visualizeStructure(child);
 			if (childBox != null) {
 				parentBox.appendChild(childBox);
 			}
@@ -54,7 +54,7 @@
 
 	protected final <P extends IParentBox<IInlineBox>> P visualizeChildrenInline(final Iterable<INode> children, final P parentBox) {
 		for (final INode child : children) {
-			final IInlineBox childBox = chain.visualizeInline(child);
+			final IInlineBox childBox = boxModelBuilder.visualizeInline(child);
 			if (childBox != null) {
 				parentBox.appendChild(childBox);
 			}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/VisualizationChain.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/VisualizationChain.java
index e8e8d3b..c12af46 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/VisualizationChain.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/VisualizationChain.java
@@ -13,24 +13,31 @@
 import java.util.TreeSet;
 
 import org.eclipse.vex.core.internal.boxes.IBox;
-import org.eclipse.vex.core.internal.boxes.IStructuralBox;
 import org.eclipse.vex.core.internal.boxes.IInlineBox;
+import org.eclipse.vex.core.internal.boxes.IStructuralBox;
 import org.eclipse.vex.core.internal.boxes.RootBox;
 import org.eclipse.vex.core.provisional.dom.INode;
 
-public final class VisualizationChain {
+public final class VisualizationChain implements IBoxModelBuilder {
 	private final TreeSet<NodeVisualization<RootBox>> rootChain = new TreeSet<NodeVisualization<RootBox>>();
 	private final TreeSet<NodeVisualization<IStructuralBox>> structureChain = new TreeSet<NodeVisualization<IStructuralBox>>();
 	private final TreeSet<NodeVisualization<IInlineBox>> inlineChain = new TreeSet<NodeVisualization<IInlineBox>>();
 
+	@Override
 	public RootBox visualizeRoot(final INode node) {
-		return visualize(node, rootChain);
+		final RootBox rootBox = visualize(node, rootChain);
+		if (rootBox == null) {
+			return new RootBox();
+		}
+		return rootBox;
 	}
 
+	@Override
 	public IStructuralBox visualizeStructure(final INode node) {
 		return visualize(node, structureChain);
 	}
 
+	@Override
 	public IInlineBox visualizeInline(final INode node) {
 		return visualize(node, inlineChain);
 	}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DOMController.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DOMController.java
index 5d26bd9..9f69f92 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DOMController.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DOMController.java
@@ -15,7 +15,7 @@
 import org.eclipse.core.runtime.QualifiedName;
 import org.eclipse.vex.core.internal.cursor.Cursor;
 import org.eclipse.vex.core.internal.cursor.ICursorMove;
-import org.eclipse.vex.core.internal.visualization.VisualizationChain;
+import org.eclipse.vex.core.internal.visualization.IBoxModelBuilder;
 import org.eclipse.vex.core.provisional.dom.AttributeChangeEvent;
 import org.eclipse.vex.core.provisional.dom.ContentChangeEvent;
 import org.eclipse.vex.core.provisional.dom.IDocument;
@@ -89,8 +89,8 @@
 		}
 	}
 
-	public void setVisualizationChain(final VisualizationChain visualizationChain) {
-		visualization.setVisualizationChain(visualizationChain);
+	public void setBoxModelBuilder(final IBoxModelBuilder boxModelBuilder) {
+		visualization.setBoxModelBuilder(boxModelBuilder);
 	}
 
 	public void moveCursor(final ICursorMove move) {
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DOMVisualization.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DOMVisualization.java
index 3a95a40..27fecb2 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DOMVisualization.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/DOMVisualization.java
@@ -27,7 +27,7 @@
 import org.eclipse.vex.core.internal.boxes.VerticalBlock;
 import org.eclipse.vex.core.internal.cursor.ContentTopology;
 import org.eclipse.vex.core.internal.cursor.Cursor;
-import org.eclipse.vex.core.internal.visualization.VisualizationChain;
+import org.eclipse.vex.core.internal.visualization.IBoxModelBuilder;
 import org.eclipse.vex.core.provisional.dom.ContentRange;
 import org.eclipse.vex.core.provisional.dom.IDocument;
 import org.eclipse.vex.core.provisional.dom.INode;
@@ -41,7 +41,7 @@
 	private final Cursor cursor;
 	private final BoxView view;
 
-	private VisualizationChain visualizationChain;
+	private IBoxModelBuilder boxModelBuilder;
 	private IDocument document;
 
 	public DOMVisualization(final Cursor cursor, final BoxView view) {
@@ -54,21 +54,18 @@
 		buildAll();
 	}
 
-	public void setVisualizationChain(final VisualizationChain visualizationChain) {
-		Assert.isNotNull(visualizationChain);
-		this.visualizationChain = visualizationChain;
+	public void setBoxModelBuilder(final IBoxModelBuilder boxModelBuilder) {
+		Assert.isNotNull(boxModelBuilder);
+		this.boxModelBuilder = boxModelBuilder;
 		buildAll();
 	}
 
 	public void buildAll() {
-		if (visualizationChain == null) {
+		if (boxModelBuilder == null) {
 			return;
 		}
 
-		RootBox rootBox = visualizationChain.visualizeRoot(document);
-		if (rootBox == null) {
-			rootBox = new RootBox();
-		}
+		final RootBox rootBox = boxModelBuilder.visualizeRoot(document);
 
 		contentTopology.setRootBox(rootBox);
 		cursor.setRootBox(rootBox);
@@ -91,42 +88,42 @@
 		parent.accept(new BaseBoxVisitor() {
 			@Override
 			public void visit(final RootBox box) {
-				box.replaceChildren(modifiedBoxes, visualizationChain.visualizeStructure(node));
+				box.replaceChildren(modifiedBoxes, boxModelBuilder.visualizeStructure(node));
 			}
 
 			@Override
 			public void visit(final VerticalBlock box) {
-				box.replaceChildren(modifiedBoxes, visualizationChain.visualizeStructure(node));
+				box.replaceChildren(modifiedBoxes, boxModelBuilder.visualizeStructure(node));
 			}
 
 			@Override
 			public void visit(final StructuralFrame box) {
-				box.setComponent(visualizationChain.visualizeStructure(node));
+				box.setComponent(boxModelBuilder.visualizeStructure(node));
 			}
 
 			@Override
 			public void visit(final StructuralNodeReference box) {
-				box.setComponent(visualizationChain.visualizeStructure(node));
+				box.setComponent(boxModelBuilder.visualizeStructure(node));
 			}
 
 			@Override
 			public void visit(final Paragraph box) {
-				box.replaceChildren(modifiedBoxes, visualizationChain.visualizeInline(node));
+				box.replaceChildren(modifiedBoxes, boxModelBuilder.visualizeInline(node));
 			}
 
 			@Override
 			public void visit(final InlineNodeReference box) {
-				box.setComponent(visualizationChain.visualizeInline(node));
+				box.setComponent(boxModelBuilder.visualizeInline(node));
 			}
 
 			@Override
 			public void visit(final InlineContainer box) {
-				box.replaceChildren(modifiedBoxes, visualizationChain.visualizeInline(node));
+				box.replaceChildren(modifiedBoxes, boxModelBuilder.visualizeInline(node));
 			}
 
 			@Override
 			public void visit(final InlineFrame box) {
-				box.setComponent(visualizationChain.visualizeInline(node));
+				box.setComponent(boxModelBuilder.visualizeInline(node));
 			}
 		});
 	}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/swt/BoxWidget.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/swt/BoxWidget.java
index de7e1d0..9711e16 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/swt/BoxWidget.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/swt/BoxWidget.java
@@ -34,7 +34,7 @@
 import org.eclipse.vex.core.internal.core.Rectangle;
 import org.eclipse.vex.core.internal.cursor.Cursor;
 import org.eclipse.vex.core.internal.cursor.ICursorMove;
-import org.eclipse.vex.core.internal.visualization.VisualizationChain;
+import org.eclipse.vex.core.internal.visualization.IBoxModelBuilder;
 import org.eclipse.vex.core.internal.widget.BalancingSelector;
 import org.eclipse.vex.core.internal.widget.BoxView;
 import org.eclipse.vex.core.internal.widget.DOMController;
@@ -78,8 +78,8 @@
 		selector.setDocument(document);
 	}
 
-	public void setVisualizationChain(final VisualizationChain visualizationChain) {
-		controller.setVisualizationChain(visualizationChain);
+	public void setBoxModelBuilder(final IBoxModelBuilder visualizationChain) {
+		controller.setBoxModelBuilder(visualizationChain);
 	}
 
 	public DOMController getDOMController() {
diff --git a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/boxview/BoxDemoView.java b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/boxview/BoxDemoView.java
index e31efb3..2737377 100644
--- a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/boxview/BoxDemoView.java
+++ b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/boxview/BoxDemoView.java
@@ -62,7 +62,7 @@
 		boxWidget = new BoxWidget(parent, SWT.V_SCROLL);
 
 		boxWidget.setContent(UniversalTestDocument.createTestDocumentWithInlineElements(SAMPLE_COUNT));
-		boxWidget.setVisualizationChain(buildVisualizationChain());
+		boxWidget.setBoxModelBuilder(buildVisualizationChain());
 		parent.layout();
 	}