use BoxWidget instead of VexWidget in VexEditor

Signed-off-by: Florian Thienel <florian@thienel.org>
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/cursor/Cursor.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/cursor/Cursor.java
index a209f51..2f82ce5 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/cursor/Cursor.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/cursor/Cursor.java
@@ -101,6 +101,13 @@
 		return caret.getVisibleArea();
 	}
 
+	public Rectangle getCaretArea() {
+		if (caret == null) {
+			return Rectangle.NULL;
+		}
+		return caret.getHotArea();
+	}
+
 	@Override
 	public void addPositionListener(final ICursorPositionListener listener) {
 		cursorPositionListeners.add(listener);
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 fec8f8f..126cb4a 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
@@ -67,6 +67,9 @@
 		if (boxModelBuilder == null) {
 			return;
 		}
+		if (document == null) {
+			return;
+		}
 
 		final RootBox rootBox = boxModelBuilder.visualizeRoot(document);
 
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 17c550f..af9fb1d 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
@@ -286,6 +286,10 @@
 		controller.refreshAll();
 	}
 
+	public Rectangle getCaretArea() {
+		return cursor.getCaretArea();
+	}
+
 	/*
 	 * ISelectionProvider
 	 */
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 6876cda..cbfaf72 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
@@ -51,6 +51,7 @@
 import org.eclipse.vex.core.internal.css.StyleSheetReader;
 import org.eclipse.vex.core.internal.io.UniversalTestDocument;
 import org.eclipse.vex.core.internal.visualization.CssBasedBoxModelBuilder;
+import org.eclipse.vex.core.internal.widget.CssTableModel;
 import org.eclipse.vex.core.internal.widget.swt.BoxWidget;
 import org.eclipse.vex.core.internal.widget.swt.IVexSelection;
 import org.eclipse.vex.core.provisional.dom.ContentRange;
@@ -160,6 +161,7 @@
 		final StyleSheet styleSheet = readStyleSheet();
 		boxWidget.setBoxModelBuilder(new CssBasedBoxModelBuilder(styleSheet));
 		boxWidget.setWhitespacePolicy(new CssWhitespacePolicy(styleSheet));
+		boxWidget.setTableModel(new CssTableModel(styleSheet));
 		boxWidgetParent.layout();
 		boxWidget.addSelectionChangedListener(selectionChangedListener);
 
diff --git a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/DocumentContextSourceProvider.java b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/DocumentContextSourceProvider.java
index 8ba251a..dfc0abe 100644
--- a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/DocumentContextSourceProvider.java
+++ b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/DocumentContextSourceProvider.java
@@ -16,6 +16,7 @@
 import org.eclipse.ui.AbstractSourceProvider;
 import org.eclipse.ui.ISources;
 import org.eclipse.vex.core.internal.core.Rectangle;
+import org.eclipse.vex.core.internal.widget.IDocumentEditor;
 import org.eclipse.vex.core.internal.widget.swt.VexWidget;
 import org.eclipse.vex.core.provisional.dom.BaseNodeVisitorWithResult;
 import org.eclipse.vex.core.provisional.dom.IComment;
@@ -110,17 +111,17 @@
 	/**
 	 * Synchronizes the variable values which will be exposed by this service with the specified {@link VexWidget}.
 	 *
-	 * @param widget
+	 * @param editor
 	 *            the Vex widget containing the actual states
 	 * @param caretArea
 	 *            TODO
 	 */
-	public void fireUpdate(final VexWidget widget, final Rectangle caretArea) {
+	public void fireUpdate(final IDocumentEditor editor, final Rectangle caretArea) {
 		final Map<String, Object> changes = new HashMap<String, Object>();
-		final RowColumnInfo rowColumnInfo = VexHandlerUtil.getRowColumnInfo(widget);
+		final RowColumnInfo rowColumnInfo = VexHandlerUtil.getRowColumnInfo(editor);
 
 		// column
-		final int columnIndex = VexHandlerUtil.getCurrentColumnIndex(widget);
+		final int columnIndex = VexHandlerUtil.getCurrentColumnIndex(editor);
 		final int columnCount = rowColumnInfo == null ? -1 : rowColumnInfo.maxColumnCount;
 		isColumn = update(changes, isColumn, columnIndex != -1, IS_COLUMN);
 		isFirstColumn = update(changes, isFirstColumn, columnIndex == 0, IS_FIRST_COLUMN);
@@ -134,7 +135,7 @@
 		isLastRow = update(changes, isLastRow, rowIndex == rowCount - 1, IS_LAST_ROW);
 
 		// nodes
-		final INode selectedNode = widget.getCurrentNode();
+		final INode selectedNode = editor.getCurrentNode();
 		if (!selectedNode.equals(currentNode)) {
 			// No need to evaluate if the node has not changed
 			currentNode = selectedNode;
diff --git a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexEditor.java b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexEditor.java
index 22a4204..f6c2e7d 100644
--- a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexEditor.java
+++ b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/editor/VexEditor.java
@@ -100,9 +100,10 @@
 import org.eclipse.vex.core.internal.io.DocumentReader;
 import org.eclipse.vex.core.internal.io.DocumentWriter;
 import org.eclipse.vex.core.internal.validator.WTPVEXValidator;
+import org.eclipse.vex.core.internal.visualization.CssBasedBoxModelBuilder;
 import org.eclipse.vex.core.internal.widget.CssTableModel;
 import org.eclipse.vex.core.internal.widget.IDocumentEditor;
-import org.eclipse.vex.core.internal.widget.swt.VexWidget;
+import org.eclipse.vex.core.internal.widget.swt.BoxWidget;
 import org.eclipse.vex.core.provisional.dom.AttributeChangeEvent;
 import org.eclipse.vex.core.provisional.dom.BaseNodeVisitorWithResult;
 import org.eclipse.vex.core.provisional.dom.ContentChangeEvent;
@@ -172,7 +173,7 @@
 	private IDocument document;
 	private Style style;
 
-	private VexWidget vexWidget;
+	private BoxWidget editorWidget;
 
 	private boolean dirty;
 
@@ -467,8 +468,8 @@
 				jFaceDoc.removePosition(positionOfCurrentNode);
 			}
 
-			positionOfCurrentNode = createDocumentWriter().write(document, doc, vexWidget.getCurrentNode());
-			positionOfCurrentNode.setOffsetInNode(vexWidget.getCaretPosition().getOffset() - vexWidget.getCurrentNode().getStartPosition().getOffset());
+			positionOfCurrentNode = createDocumentWriter().write(document, doc, editorWidget.getCurrentNode());
+			positionOfCurrentNode.setOffsetInNode(editorWidget.getCaretPosition().getOffset() - editorWidget.getCurrentNode().getStartPosition().getOffset());
 
 			try {
 				jFaceDoc.addPosition(positionOfCurrentNode);
@@ -485,7 +486,7 @@
 
 	private DocumentWriter createDocumentWriter() {
 		final DocumentWriter result = new DocumentWriter();
-		result.setWhitespacePolicy(vexWidget.getWhitespacePolicy());
+		result.setWhitespacePolicy(editorWidget.getWhitespacePolicy());
 		result.setIndent(preferences.getIndentationPattern());
 		result.setWrapColumn(preferences.getLineWidth());
 		return result;
@@ -529,7 +530,7 @@
 	 * Returns the VexWidget that implements this editor.
 	 */
 	public IDocumentEditor getVexWidget() {
-		return vexWidget;
+		return editorWidget;
 	}
 
 	public void gotoMarker(final IMarker marker) {
@@ -649,8 +650,8 @@
 
 	@Override
 	public void setFocus() {
-		if (vexWidget != null) {
-			vexWidget.setFocus();
+		if (editorWidget != null) {
+			editorWidget.setFocus();
 			setStatus(getLocationPath());
 		}
 	}
@@ -704,7 +705,7 @@
 		VexDocumentContentModel documentContentModel;
 
 		try {
-			if (vexWidget != null) {
+			if (editorWidget != null) {
 				vexEditorListeners.fireEvent("documentUnloaded", new VexEditorEvent(this)); //$NON-NLS-1$
 			}
 			if (document != null) {
@@ -801,16 +802,16 @@
 
 			document.addDocumentListener(documentListener);
 
-			vexWidget.setDebugging(debugging);
-			vexWidget.setWhitespacePolicy(reader.getWhitespacePolicy());
-			vexWidget.setTableModel(new CssTableModel(style.getStyleSheet()));
-			vexWidget.setDocument(document, style.getStyleSheet());
-			vexWidget.setReadOnly(isEditorInputReadOnly());
+			editorWidget.setBoxModelBuilder(new CssBasedBoxModelBuilder(style.getStyleSheet()));
+			editorWidget.setWhitespacePolicy(reader.getWhitespacePolicy());
+			editorWidget.setTableModel(new CssTableModel(style.getStyleSheet()));
+			editorWidget.setDocument(document);
+			editorWidget.setReadOnly(isEditorInputReadOnly());
 
 			final INode nodeAtCaret = reader.getNodeAtCaret();
 			if (nodeAtCaret != null) {
 				final int offsetInNode = Math.min(nodeAtCaret.getStartOffset() + positionOfCurrentNode.getOffsetInNode(), nodeAtCaret.getEndOffset());
-				vexWidget.moveTo(new ContentPosition(document, offsetInNode));
+				editorWidget.moveTo(new ContentPosition(document, offsetInNode));
 			}
 
 			loaded = true;
@@ -872,8 +873,10 @@
 	 */
 	public void setStyle(final Style style) {
 		this.style = style;
-		if (vexWidget != null) {
-			vexWidget.setStyleSheet(style.getStyleSheet());
+		if (editorWidget != null) {
+			editorWidget.setBoxModelBuilder(new CssBasedBoxModelBuilder(style.getStyleSheet()));
+			editorWidget.setWhitespacePolicy(new CssWhitespacePolicy(style.getStyleSheet()));
+			editorWidget.setTableModel(new CssTableModel(style.getStyleSheet()));
 			preferences.setPreferredStyleId(doctype, style.getUniqueId());
 		}
 		vexEditorListeners.fireEvent("styleChanged", new VexEditorEvent(this)); //$NON-NLS-1$
@@ -889,9 +892,9 @@
 	 */
 	private void showLabel(final String message, final Exception ex) {
 		if (loadingLabel == null) {
-			if (vexWidget != null) {
-				vexWidget.dispose();
-				vexWidget = null;
+			if (editorWidget != null) {
+				editorWidget.dispose();
+				editorWidget = null;
 			}
 			final GridLayout layout = new GridLayout();
 			layout.numColumns = 1;
@@ -931,7 +934,7 @@
 
 	private void showVexWidget() {
 
-		if (vexWidget != null) {
+		if (editorWidget != null) {
 			return;
 		}
 
@@ -969,17 +972,17 @@
 		gd.horizontalAlignment = GridData.FILL;
 		gd.verticalAlignment = GridData.FILL;
 
-		vexWidget = new VexWidget(parentControl, SWT.V_SCROLL);
+		editorWidget = new BoxWidget(parentControl, SWT.V_SCROLL);
 		gd = new GridData();
 		gd.grabExcessHorizontalSpace = true;
 		gd.grabExcessVerticalSpace = true;
 		gd.horizontalAlignment = GridData.FILL;
 		gd.verticalAlignment = GridData.FILL;
-		vexWidget.setLayoutData(gd);
+		editorWidget.setLayoutData(gd);
 
 		final MenuManager menuManager = new MenuManager();
-		getSite().registerContextMenu("org.eclipse.vex.ui.popup", menuManager, vexWidget);
-		vexWidget.setMenu(menuManager.createContextMenu(vexWidget));
+		getSite().registerContextMenu("org.eclipse.vex.ui.popup", menuManager, editorWidget);
+		editorWidget.setMenu(menuManager.createContextMenu(editorWidget));
 
 		setClean();
 
@@ -987,7 +990,7 @@
 		final IContextService cs = (IContextService) getSite().getService(IContextService.class);
 		cs.activateContext("org.eclipse.vex.ui.VexEditorContext");
 
-		vexWidget.addSelectionChangedListener(selectionProvider);
+		editorWidget.addSelectionChangedListener(selectionProvider);
 
 		parentControl.layout(true);
 
@@ -1010,7 +1013,9 @@
 						// Oops, style went bye-bye
 						// Let's just hold on to it in case it comes back later
 					} else {
-						vexWidget.setStyleSheet(newStyle.getStyleSheet());
+						editorWidget.setBoxModelBuilder(new CssBasedBoxModelBuilder(style.getStyleSheet()));
+						editorWidget.setWhitespacePolicy(new CssWhitespacePolicy(style.getStyleSheet()));
+						editorWidget.setTableModel(new CssTableModel(style.getStyleSheet()));
 						style = newStyle;
 					}
 				}
@@ -1046,12 +1051,16 @@
 			// update context service
 			final ISourceProviderService service = (ISourceProviderService) window.getService(ISourceProviderService.class);
 			final DocumentContextSourceProvider contextProvider = (DocumentContextSourceProvider) service.getSourceProvider(DocumentContextSourceProvider.IS_COLUMN);
-			final Point caretLocation = vexWidget.toDisplay(vexWidget.getLocationForContentAssist());
-			final Rectangle caretArea = new Rectangle(caretLocation.x, caretLocation.y, 1, 1); // TODO get the real caret area form the BoxWidget
-			contextProvider.fireUpdate(vexWidget, caretArea);
+			contextProvider.fireUpdate(editorWidget, getCaretArea());
 		}
 	};
 
+	private Rectangle getCaretArea() {
+		final Rectangle relativeArea = editorWidget.getCaretArea();
+		final Point caretLocation = editorWidget.toDisplay(new Point(relativeArea.getX(), relativeArea.getY()));
+		return new Rectangle(caretLocation.x, caretLocation.y, relativeArea.getWidth(), relativeArea.getHeight());
+	}
+
 	private final IDocumentListener documentListener = new IDocumentListener() {
 
 		@Override
@@ -1115,7 +1124,7 @@
 
 	private String getLocationPath() {
 		final List<String> path = new ArrayList<String>();
-		INode node = vexWidget.getCurrentNode();
+		INode node = editorWidget.getCurrentNode();
 		while (node != null) {
 			path.add(node.accept(nodePathVisitor));
 			node = node.getParent();
@@ -1146,15 +1155,15 @@
 				@Override
 				public IPropertySource getPropertySource(final Object object) {
 					if (object instanceof IElement) {
-						final IStructuredSelection selection = (IStructuredSelection) vexWidget.getSelection();
+						final IStructuredSelection selection = (IStructuredSelection) editorWidget.getSelection();
 						final boolean multipleElementsSelected = selection != null && selection.size() > 1;
-						final IValidator validator = vexWidget.getDocument().getValidator();
+						final IValidator validator = editorWidget.getDocument().getValidator();
 						return new ElementPropertySource((IElement) object, validator, multipleElementsSelected);
 					}
 					if (object instanceof IIncludeNode) {
-						final IStructuredSelection selection = (IStructuredSelection) vexWidget.getSelection();
+						final IStructuredSelection selection = (IStructuredSelection) editorWidget.getSelection();
 						final boolean multipleElementsSelected = selection != null && selection.size() > 1;
-						final IValidator validator = vexWidget.getDocument().getValidator();
+						final IValidator validator = editorWidget.getDocument().getValidator();
 						return new ElementPropertySource(((IIncludeNode) object).getReference(), validator, multipleElementsSelected);
 					}
 					if (object instanceof IDocument) {