[jbosstools-commits] JBoss Tools SVN: r9175 - in trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor: util and 1 other directory.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Jul 18 09:58:05 EDT 2008


Author: mareshkau
Date: 2008-07-18 09:58:05 -0400 (Fri, 18 Jul 2008)
New Revision: 9175

Modified:
   trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/SelectionManager.java
   trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/SelectionUtil.java
Log:
JBIDE-2505

Modified: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/SelectionManager.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/SelectionManager.java	2008-07-18 13:24:41 UTC (rev 9174)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/SelectionManager.java	2008-07-18 13:58:05 UTC (rev 9175)
@@ -1,6 +1,8 @@
 package org.jboss.tools.vpe.editor.template;
 
+import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.swt.graphics.Point;
+import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
 import org.eclipse.wst.sse.ui.StructuredTextEditor;
 import org.jboss.tools.vpe.editor.context.VpePageContext;
 import org.jboss.tools.vpe.editor.mapping.NodeData;
@@ -10,7 +12,9 @@
 import org.jboss.tools.vpe.editor.selection.VpeSelectionController;
 import org.jboss.tools.vpe.editor.util.NodesManagingUtil;
 import org.jboss.tools.vpe.editor.util.SelectionUtil;
+import org.jboss.tools.vpe.editor.util.TextUtil;
 import org.jboss.tools.vpe.editor.util.VisualDomUtil;
+import org.jboss.tools.vpe.editor.util.VpeDebugUtil;
 import org.mozilla.interfaces.nsIDOMMouseEvent;
 import org.mozilla.interfaces.nsIDOMNSUIEvent;
 import org.mozilla.interfaces.nsIDOMNode;
@@ -47,7 +51,7 @@
 		this.selectionController = selectionController;
 	}
 
-	final public void setSelection(nsISelection selection) {
+	public final void setSelection(nsISelection selection) {
 
 		nsIDOMNode selectedVisualNode = SelectionUtil
 				.getSelectedNode(selection);
@@ -99,7 +103,7 @@
 			}
 
 		} else {
-
+			//here we processed text node
 			targetVisualNode = nodeMapping.getVisualNode();
 			targetSourceNode = nodeMapping.getSourceNode();
 			isNodeEditable = true;
@@ -111,10 +115,10 @@
 
 		if (isNodeEditable) {
 
-			Point range = SelectionUtil.getSelectionRange(selection);
+			Point sourceSelectionRange = SelectionUtil.getSourceSelectionRange(selection,targetSourceNode);
 
-			focusOffset = range.x;
-			length = range.y;
+			focusOffset = sourceSelectionRange.x;
+			length = sourceSelectionRange.y;
 
 		} else {
 
@@ -222,30 +226,40 @@
 				targetVisualNode);
 
 	}
-
+	/**
+	 * Syncronization visual selection and source selection,
+	 * actually moves source selection to visual selection
+	 */
 	final public void refreshVisualSelection() {
+		//TODO Max Areshkau Adjust for restoring cursor position
+		Point range = SelectionUtil.getSourceSelectionRange(getSourceEditor());
+		
+		ISelection selection = getSourceEditor().getTextViewer().getSelection();
 
-		Point range = SelectionUtil.getSourceSelection(getSourceEditor());
-
 		if (range == null)
 			return;
 		
-		int focus = range.x;
+		int focusOffcetInSourceDocument = range.x;
 
-		int anchor = focus + range.y;
+		int anchorOffcetInSourceDocument = focusOffcetInSourceDocument + range.y;
 
 		// get element mapping
 		VpeNodeMapping nodeMapping = SelectionUtil
 				.getNodeMappingBySourceSelection(NodesManagingUtil
 						.getStructuredModel(getSourceEditor()),
-						getDomMapping(), focus, anchor);
+						getDomMapping(), focusOffcetInSourceDocument, anchorOffcetInSourceDocument);
 
 		if (nodeMapping == null)
 			return;
 
 		// visual node which will be selected
 		nsIDOMNode targetVisualNode;
-
+		
+		IndexedRegion targetSourceNode = (IndexedRegion) SelectionUtil.getSourceNodeByPosition(NodesManagingUtil
+				.getStructuredModel(getSourceEditor()), focusOffcetInSourceDocument);
+		
+		int offcetReferenceToSourceNode = focusOffcetInSourceDocument-targetSourceNode.getStartOffset();
+		
 		// if mapping is elementMapping
 		if (nodeMapping.getType() == VpeNodeMapping.ELEMENT_MAPPING) {
 
@@ -254,19 +268,28 @@
 			VpeTemplate template = elementMapping.getTemplate();
 
 			targetVisualNode = template.getVisualNodeByBySourcePosition(
-					elementMapping, focus, anchor, getDomMapping());
+					elementMapping, focusOffcetInSourceDocument, anchorOffcetInSourceDocument, getDomMapping());
 
 		} else {
 
 			targetVisualNode = nodeMapping.getVisualNode();
 
 		}
-
+		//here we restore only highlight
 		getPageContext().getVisualBuilder().setSelectionRectangle(
 				targetVisualNode);
-
+		//TODO Max Areshkau now it's workd only for simple text, and should be adjusted
+		targetVisualNode = targetVisualNode.getFirstChild();
+		int visualNodeOffcet = TextUtil.visualPosition(((Node)targetSourceNode).getNodeValue(),offcetReferenceToSourceNode);
+		//added by Max Areshkau restore selection
+//		Point visualSelectionRange = SelectionUtil.getVisualSelectionRange(selection);
+		VpeDebugUtil.debugInfo(targetVisualNode.getNodeValue());
+//		TextUtil.visualPosition(sourceText, sourcePosition)
+		selectionController.getSelection((short)1).collapse(targetVisualNode, visualNodeOffcet);
+//		selectionController.getSelection((short)1).extend(targetVisualNode, focus);
 	}
-
+	
+	
 	protected VpePageContext getPageContext() {
 		return pageContext;
 	}

Modified: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/SelectionUtil.java
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/SelectionUtil.java	2008-07-18 13:24:41 UTC (rev 9174)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/SelectionUtil.java	2008-07-18 13:58:05 UTC (rev 9175)
@@ -4,11 +4,11 @@
 import java.util.List;
 
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextViewer;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.wst.sse.core.StructuredModelManager;
 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
 import org.eclipse.wst.sse.ui.StructuredTextEditor;
-import org.eclipse.wst.sse.ui.internal.StructuredTextViewer;
 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
 import org.jboss.tools.vpe.editor.context.VpePageContext;
 import org.jboss.tools.vpe.editor.mapping.VpeDomMapping;
@@ -19,7 +19,7 @@
 import org.w3c.dom.Node;
 
 /**
- * 
+ * Util class for selection manupalating
  * @author S.Dzmitrovich
  * 
  */
@@ -147,8 +147,15 @@
 
 		return node;
 	}
-
-	public static Point getSelectionRange(nsISelection selection) {
+	/**
+	 * Returns selection range for visual part of editor
+	 * Focus offset and anchor offset can be not equals to source focus offset and anchor offset
+	 * 
+	 * @param selection -selection in visual part of editor
+	 * 
+	 * @return selection range for visual part of editor
+	 */
+	public static Point getVisualSelectionRange(nsISelection selection) {
 		nsIDOMNode focusedNode = getSelectedNode(selection);
 
 		Point range = new Point(0, 0);
@@ -161,7 +168,24 @@
 		}
 		return range;
 	}
-
+	/**
+	 * Return source editor part selection range, range returns relatively to start of text in source,
+	 * not for start of document  
+	 * 
+	 * @param selection
+	 * @return source editor selection range
+	 */
+	public static Point getSourceSelectionRange(nsISelection selection, Node sourceNode) {
+		
+		nsIDOMNode focusedNode = getSelectedNode(selection);
+		//gets visual selection range
+		Point sourceRange = new Point(0, 0);
+		//converts to source selection
+		sourceRange.x = TextUtil.sourcePosition(sourceNode.getNodeValue(), focusedNode.getNodeValue(), selection.getFocusOffset());
+		sourceRange.y = TextUtil.sourcePosition(sourceNode.getNodeValue(), focusedNode.getNodeValue(), selection.getAnchorOffset())-sourceRange.x;
+		return sourceRange;
+	}
+	
 	public static VpeNodeMapping getNodeMappingBySourceSelection(
 			StructuredTextEditor sourceEditor, VpeDomMapping domMapping) {
 
@@ -208,11 +232,15 @@
 				.getLastSelectedNode();
 
 	}
+	/**
+	 * Returns sourceSelectionRange
+	 * @param sourceEditor
+	 * @return sourceSelectionRange
+	 */
+	static public Point getSourceSelectionRange(StructuredTextEditor sourceEditor) {
 
-	static public Point getSourceSelection(StructuredTextEditor sourceEditor) {
+		ITextViewer textViewer = sourceEditor.getTextViewer();
 
-		StructuredTextViewer textViewer = sourceEditor.getTextViewer();
-
 		if (textViewer != null)
 			return textViewer.getSelectedRange();
 




More information about the jbosstools-commits mailing list