Author: sdzmitrovich
Date: 2008-08-11 09:37:42 -0400 (Mon, 11 Aug 2008)
New Revision: 9636
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/mozilla/MozillaEditor.java
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/NodesManagingUtil.java
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/SelectionUtil.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-2526
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/mozilla/MozillaEditor.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/mozilla/MozillaEditor.java 2008-08-11
10:06:22 UTC (rev 9635)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/mozilla/MozillaEditor.java 2008-08-11
13:37:42 UTC (rev 9636)
@@ -727,6 +727,10 @@
//here we getting inline table editor and disable it's too
nsIHTMLInlineTableEditor inlineTableEditor = (nsIHTMLInlineTableEditor)
editor.queryInterface(nsIHTMLInlineTableEditor.NS_IHTMLINLINETABLEEDITOR_IID);
inlineTableEditor.setInlineTableEditingEnabled(false);
+
+ // Added by Sergey Dzmitrovich Fix for JBIDE-2526
+ // It need for correct work of selection in source
+ iEditingSession.tearDownEditorOnWindow(getXulRunnerEditor().getWebBrowser().getContentDOMWindow());
}
return editor;
}
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-08-11
10:06:22 UTC (rev 9635)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/SelectionManager.java 2008-08-11
13:37:42 UTC (rev 9636)
@@ -16,9 +16,7 @@
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
-import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
-import org.eclipse.wst.xml.core.internal.document.NodeImpl;
import org.jboss.tools.vpe.editor.context.VpePageContext;
import org.jboss.tools.vpe.editor.mapping.NodeData;
import org.jboss.tools.vpe.editor.mapping.VpeDomMapping;
@@ -118,7 +116,7 @@
}
} else {
- //here we processed text node
+ // here we processed text node
targetVisualNode = nodeMapping.getVisualNode();
targetSourceNode = nodeMapping.getSourceNode();
isNodeEditable = true;
@@ -130,7 +128,8 @@
if (isNodeEditable) {
- Point sourceSelectionRange =
SelectionUtil.getSourceSelectionRange(selection,targetSourceNode);
+ Point sourceSelectionRange = SelectionUtil.getSourceSelectionRange(
+ selection, targetSourceNode);
focusOffset = sourceSelectionRange.x;
length = sourceSelectionRange.y;
@@ -241,152 +240,194 @@
targetVisualNode);
}
+
/**
- * Syncronization visual selection and source selection,
- * actually moves source selection to visual selection
+ * 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
+ // TODO Max Areshkau Adjust for restoring cursor position
IStructuredModel model = null;
-
+
try {
- //checks for null, for case when we close editor and background update job is running
- if(getSourceEditor().getTextViewer()==null) {
-
- return;
- }
- //gets source model for read, model should be released see JBIDE-2219
- model = StructuredModelManager.getModelManager()
- .getExistingModelForRead(getSourceEditor().getTextViewer().getDocument());
-
- Point range = SelectionUtil.getSourceSelectionRange(getSourceEditor());
-
- ISelection selection = getSourceEditor().getTextViewer().getSelection();
+ // checks for null, for case when we close editor and background
+ // update job is running
+ if (getSourceEditor().getTextViewer() == null) {
- if (range == null)
- return;
-
- int focusOffcetInSourceDocument = range.x;
+ return;
+ }
+ // gets source model for read, model should be released see
+ // JBIDE-2219
+ model = StructuredModelManager.getModelManager()
+ .getExistingModelForRead(
+ getSourceEditor().getTextViewer().getDocument());
- int anchorOffcetInSourceDocument = focusOffcetInSourceDocument + range.y;
+ Point range = SelectionUtil
+ .getSourceSelectionRange(getSourceEditor());
- // get element mapping
- VpeNodeMapping nodeMapping = SelectionUtil
- .getNodeMappingBySourceSelection(model,
- getDomMapping(), focusOffcetInSourceDocument, anchorOffcetInSourceDocument);
+ ISelection selection = getSourceEditor().getTextViewer()
+ .getSelection();
- if (nodeMapping == null)
- return;
+ if (range == null)
+ return;
- // visual node which will be selected
- nsIDOMNode targetVisualNode;
-
-// int visualNodeOffcet =
TextUtil.visualPosition(((Node)targetSourceNode).getNodeValue(),offcetReferenceToSourceNode);
-
- // if mapping is elementMapping
- if (nodeMapping instanceof VpeElementMapping) {
+ int focusOffcetInSourceDocument = range.x;
- VpeElementMapping elementMapping = (VpeElementMapping) nodeMapping;
+ int anchorOffcetInSourceDocument = focusOffcetInSourceDocument
+ + range.y;
- VpeTemplate template = elementMapping.getTemplate();
+ // get element mapping
+ VpeNodeMapping nodeMapping = SelectionUtil
+ .getNodeMappingBySourceSelection(model, getDomMapping(),
+ focusOffcetInSourceDocument,
+ anchorOffcetInSourceDocument);
- targetVisualNode = template.getVisualNodeByBySourcePosition(
- elementMapping, focusOffcetInSourceDocument, anchorOffcetInSourceDocument,
getDomMapping());
+ if (nodeMapping == null)
+ return;
+
+ // visual node which will be selected
+ nsIDOMNode targetVisualNode;
+
+ // int visualNodeOffcet =
+ // TextUtil.visualPosition(((Node)targetSourceNode
+ // ).getNodeValue(),offcetReferenceToSourceNode);
+
+ // if mapping is elementMapping
- NodeData nodeData = template.getNodeData(targetVisualNode,
elementMapping.getElementData(), getDomMapping());
- //we can restore cursor position only if we have nodeData and range.y==0
- if(nodeData!=null && range.y==0) {
- //restore cursor position in source document
- restoreVisualCursorPosition(template, nodeData, focusOffcetInSourceDocument);
+ SelectionUtil.clearSelection(selectionController);
+
+ if (nodeMapping instanceof VpeElementMapping) {
+
+ VpeElementMapping elementMapping = (VpeElementMapping) nodeMapping;
+
+ VpeTemplate template = elementMapping.getTemplate();
+
+ targetVisualNode = template.getVisualNodeByBySourcePosition(
+ elementMapping, focusOffcetInSourceDocument,
+ anchorOffcetInSourceDocument, getDomMapping());
+
+ NodeData nodeData = template.getNodeData(targetVisualNode,
+ elementMapping.getElementData(), getDomMapping());
+ // we can restore cursor position only if we have nodeData and
+ // range.y==0
+ if (nodeData != null) {
+ // restore cursor position in source document
+ restoreVisualCursorPosition(template, nodeData,
+ focusOffcetInSourceDocument,
+ anchorOffcetInSourceDocument);
+ }
+ } else {
+
+ targetVisualNode = nodeMapping.getVisualNode();
+ // restore cursor position for source node
+// restoreVisualCursorPositionForTextNode(targetVisualNode,
+// focusOffcetInSourceDocument, model);
}
- } else {
-
- targetVisualNode = nodeMapping.getVisualNode();
- //restore cursor position for source node
- restoreVisualCursorPositionForTextNode(targetVisualNode, focusOffcetInSourceDocument,
model);
- }
- //here we restore only highlight
- getPageContext().getVisualBuilder().setSelectionRectangle(
- targetVisualNode);
+ // here we restore only highlight
+ getPageContext().getVisualBuilder().setSelectionRectangle(
+ targetVisualNode);
} finally {
- if(model!=null) {
+ if (model != null) {
model.releaseFromRead();
}
}
}
+
/**
* Restores visual cursor position in visual part of editor
*
- * @param template - current template in scope of which we are editing data
- * !IMPORTANT for current implementation in should be a text node
- * @param nodeData -contains mapping before sourceNode(it's can be an attribute) and
visual node, attribute
+ * @param template
+ * - current template in scope of which we are editing data
+ * !IMPORTANT for current implementation in should be a text node
+ * @param nodeData
+ * -contains mapping before sourceNode(it's can be an attribute)
+ * and visual node, attribute
*/
- private void restoreVisualCursorPosition(VpeTemplate template, NodeData nodeData,int
focusOffcetInSourceDocument) {
-
+ private void restoreVisualCursorPosition(VpeTemplate template,
+ NodeData nodeData, int focusOffcetInSourceDocument,
+ int anchorOffsetrInSourceDocument) {
+
nsIDOMNode visualNode = nodeData.getVisualNode();
-
- if(visualNode!=null
- &&visualNode.getNodeType()==nsIDOMNode.TEXT_NODE
- &&nodeData.getSourceNode()!=null){
-
- if(nodeData.getSourceNode().getNodeType()==Node.ATTRIBUTE_NODE) {
- NodeImpl targetSourceNode = (NodeImpl)nodeData.getSourceNode();
- String sourceNodeValue = nodeData.getSourceNode().getNodeValue();
- ITextRegion valueRegion = targetSourceNode.getValueRegion();
- if(valueRegion==null) {
- return;
- }
- ITextRegion nameRegion = targetSourceNode.getNameRegion();
- int offcetReferenceToSourceNode =
focusOffcetInSourceDocument-valueRegion.getStart()-targetSourceNode.getStartOffset()+nameRegion.getStart()-1;
-
- if(offcetReferenceToSourceNode<visualNode.getNodeValue().length()){
-
- selectionController.getSelection(nsISelectionController.SELECTION_NORMAL).collapse(visualNode,
offcetReferenceToSourceNode);
- }
- }else if (nodeData.getSourceNode().getNodeType()==Node.TEXT_NODE){
-
- IndexedRegion targetSourceNode = (IndexedRegion) nodeData.getSourceNode();
-
- int offcetReferenceToSourceNode =
focusOffcetInSourceDocument-targetSourceNode.getStartOffset();
- int visualNodeOffcet =
TextUtil.visualPosition(((Node)targetSourceNode).getNodeValue(),offcetReferenceToSourceNode);
-
- if(visualNodeOffcet<visualNode.getNodeValue().length()) {
-
-
selectionController.getSelection(nsISelectionController.SELECTION_NORMAL).collapse(visualNode,
visualNodeOffcet);
- }
- }
+ if (visualNode != null
+ && visualNode.getNodeType() == nsIDOMNode.TEXT_NODE
+ && nodeData.getSourceNode() != null) {
+
+ Node targetSourceNode = nodeData.getSourceNode();
+
+ int focusOffcetReferenceToSourceNode = focusOffcetInSourceDocument
+ - NodesManagingUtil.getStartOffsetNode(targetSourceNode);
+
+ int anchorOffcetReferenceToSourceNode = anchorOffsetrInSourceDocument
+ - NodesManagingUtil.getStartOffsetNode(targetSourceNode);
+
+ int length = targetSourceNode.getNodeValue().length();
+ if (focusOffcetReferenceToSourceNode > length
+ || anchorOffcetReferenceToSourceNode > length)
+ return;
+
+ int visualNodeFocusOffcet = TextUtil.visualPosition(
+ ((Node) targetSourceNode).getNodeValue(),
+ focusOffcetReferenceToSourceNode);
+
+ int visualNodeAnchorOffcet = TextUtil.visualPosition(
+ ((Node) targetSourceNode).getNodeValue(),
+ anchorOffcetReferenceToSourceNode);
+
+ selectionController.getSelection(
+ nsISelectionController.SELECTION_NORMAL).collapse(
+ visualNode, visualNodeFocusOffcet);
+
+ if (visualNodeAnchorOffcet != visualNodeFocusOffcet) {
+ selectionController.getSelection(
+ nsISelectionController.SELECTION_NORMAL).extend(
+ visualNode, visualNodeAnchorOffcet);
}
-
+
+ }
+
}
+
/**
+ *
+ * @deprecated
* Restore cursor position in visual document for by source position
+ *
* @param visualNode
* @param focusOffcetInSourceDocument
*/
- private void restoreVisualCursorPositionForTextNode(nsIDOMNode visualNode, int
focusOffcetInSourceDocument,IStructuredModel model) {
-
- if(visualNode==null) return;
-
+ private void restoreVisualCursorPositionForTextNode(nsIDOMNode visualNode,
+ int focusOffcetInSourceDocument, IStructuredModel model) {
+
+ if (visualNode == null)
+ return;
+
nsIDOMNode targetVisualNode = visualNode.getFirstChild();
-
- if(targetVisualNode==null||targetVisualNode.getNodeType()!=nsIDOMNode.TEXT_NODE) {
+
+ if (targetVisualNode == null
+ || targetVisualNode.getNodeType() != nsIDOMNode.TEXT_NODE) {
return;
}
-
- IndexedRegion targetSourceNode = (IndexedRegion)
SelectionUtil.getSourceNodeByPosition(model, focusOffcetInSourceDocument);
+
+ IndexedRegion targetSourceNode = (IndexedRegion) SelectionUtil
+ .getSourceNodeByPosition(model, focusOffcetInSourceDocument);
// should be a text node
- if(((Node)targetSourceNode).getNodeType()!=Node.TEXT_NODE) {
+ if (((Node) targetSourceNode).getNodeType() != Node.TEXT_NODE) {
return;
}
- int offcetReferenceToSourceNode =
focusOffcetInSourceDocument-targetSourceNode.getStartOffset();
+ int offcetReferenceToSourceNode = focusOffcetInSourceDocument
+ - targetSourceNode.getStartOffset();
- int visualNodeOffcet =
TextUtil.visualPosition(((Node)targetSourceNode).getNodeValue(),offcetReferenceToSourceNode);
+ int visualNodeOffcet = TextUtil.visualPosition(
+ ((Node) targetSourceNode).getNodeValue(),
+ offcetReferenceToSourceNode);
- selectionController.getSelection(nsISelectionController.SELECTION_NORMAL).collapse(targetVisualNode,
visualNodeOffcet);
+ selectionController.getSelection(
+ nsISelectionController.SELECTION_NORMAL).collapse(
+ targetVisualNode, visualNodeOffcet);
}
-
+
protected VpePageContext getPageContext() {
return pageContext;
}
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/NodesManagingUtil.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/NodesManagingUtil.java 2008-08-11
10:06:22 UTC (rev 9635)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/NodesManagingUtil.java 2008-08-11
13:37:42 UTC (rev 9636)
@@ -99,7 +99,23 @@
}
/**
+ * if position belong to node return true
*
+ * @param node
+ * @param position
+ * @return
+ */
+ public static boolean isNodeContainsPosition(Node node, int position) {
+
+ if ((NodesManagingUtil.getStartOffsetNode(node) <= position)
+ && (NodesManagingUtil.getEndOffsetNode(node) > position))
+ return true;
+
+ return false;
+ }
+
+ /**
+ *
* @param pageContext
* @param sourceElement
* @return
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-08-11
10:06:22 UTC (rev 9635)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/SelectionUtil.java 2008-08-11
13:37:42 UTC (rev 9636)
@@ -11,9 +11,6 @@
package org.jboss.tools.vpe.editor.util;
-import java.util.ArrayList;
-import java.util.List;
-
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
@@ -21,33 +18,25 @@
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.xml.core.internal.provisional.document.IDOMNode;
import org.jboss.tools.vpe.editor.context.VpePageContext;
import org.jboss.tools.vpe.editor.mapping.VpeDomMapping;
import org.jboss.tools.vpe.editor.mapping.VpeNodeMapping;
+import org.jboss.tools.vpe.editor.selection.VpeSelectionController;
import org.mozilla.interfaces.nsIDOMNode;
import org.mozilla.interfaces.nsIDOMRange;
import org.mozilla.interfaces.nsISelection;
+import org.mozilla.interfaces.nsISelectionController;
import org.w3c.dom.Node;
/**
* Util class for selection manupalating
+ *
* @author S.Dzmitrovich
*
*/
public class SelectionUtil {
- private static final String EMPTY_STRING = ""; //$NON-NLS-1$
- private static List<String> NO_INPUT_TYPES;
- static {
- NO_INPUT_TYPES = new ArrayList<String>();
-
- NO_INPUT_TYPES.add(HTML.VALUE_TEXT_TYPE);
- NO_INPUT_TYPES.add(HTML.VALUE_PASSWORD_TYPE);
- NO_INPUT_TYPES.add(EMPTY_STRING);
- }
-
/**
* get selected visual node from nsISelection
*
@@ -125,7 +114,8 @@
Node focusNode = getSourceNodeByPosition(model, focus);
// if focus node also contain anchor point (selected only 1 element)
- if ((focusNode != null) && isNodeContainsPosition(focusNode, anchor)) {
+ if ((focusNode != null)
+ && NodesManagingUtil.isNodeContainsPosition(focusNode, anchor)) {
return NodesManagingUtil.getNodeMapping(domMapping, focusNode);
@@ -134,22 +124,6 @@
}
- /**
- * if position belong to node return true
- *
- * @param node
- * @param position
- * @return
- */
- private static boolean isNodeContainsPosition(Node node, int position) {
-
- if ((((IDOMNode) node).getStartOffset() <= position)
- && (((IDOMNode) node).getEndOffset() >= position))
- return true;
-
- return false;
- }
-
public static Node getSourceNodeByPosition(IStructuredModel model,
int position) {
@@ -158,11 +132,13 @@
return node;
}
+
/**
- * Returns selection range for visual part of editor
- * Focus offset and anchor offset can be not equals to source focus offset and anchor
offset
+ * 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
+ * @param selection
+ * -selection in visual part of editor
*
* @return selection range for visual part of editor
*/
@@ -179,17 +155,36 @@
}
return range;
}
+
/**
- * Return source editor part selection range, range returns relatively to start of text
in source,
- * not for start of document
+ * 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) {
+ public static Point getSourceSelectionRange(nsISelection selection,
+ Node sourceNode) {
+
+ if (selection.getAnchorNode() != null) {
+
+ System.out.print("\n anchor name:"
+ + selection.getAnchorNode().getNodeName() + "\t value:"
+ + selection.getAnchorNode().getNodeValue());
+ System.out.print("\n anchor offset " + selection.getAnchorOffset());
+
+ }
+ if (selection.getFocusNode() != null) {
+
+ System.out.print("\n focus name:"
+ + selection.getFocusNode().getNodeName() + "\t value:"
+ + selection.getFocusNode().getNodeValue());
+ System.out.print("\n focus offset " + selection.getFocusOffset());
+
+ }
nsIDOMNode focusedNode = getSelectedNode(selection);
- //gets visual selection range
+ // gets visual selection range
Point sourceRange = new Point(0, 0);
//converts to source selection
if (sourceNode != null) {
@@ -199,54 +194,57 @@
}
return sourceRange;
}
-
+
public static VpeNodeMapping getNodeMappingBySourceSelection(
StructuredTextEditor sourceEditor, VpeDomMapping domMapping) {
Point range = sourceEditor.getTextViewer().getSelectedRange();
IDocument document = sourceEditor.getTextViewer().getDocument();
-
- IStructuredModel model =null;
-
+
+ IStructuredModel model = null;
+
try {
- model = StructuredModelManager.getModelManager()
- .getExistingModelForRead(document);
+ model = StructuredModelManager.getModelManager()
+ .getExistingModelForRead(document);
- int anchor = range.x;
- int focus = range.x + range.y;
+ int anchor = range.x;
+ int focus = range.x + range.y;
- /*
- * implementation of IDOMModel's method getIndexedRegion(...) has one
- * feature : if cursor is situated at the border of elements then this
- * method return next element. For example ... <h:inputText
- * ../><h:outputText/>... - if cursor will be situated at the right
- * border of "h:inputText" element then getIndexedRegion() return
- * "h:outputText" element. So for focus position we choose smaller value
- */
+ /*
+ * implementation of IDOMModel's method getIndexedRegion(...) has
+ * one feature : if cursor is situated at the border of elements
+ * then this method return next element. For example ...
+ * <h:inputText ../><h:outputText/>... - if cursor will be situated
+ * at the right border of "h:inputText" element then
+ * getIndexedRegion() return "h:outputText" element. So for focus
+ * position we choose smaller value
+ */
- if (anchor < focus) {
- focus = anchor;
- anchor = focus;
- }
+ if (anchor < focus) {
+ focus = anchor;
+ anchor = focus;
+ }
- // get source node by offset
- Node focusNode = getSourceNodeByPosition(model, focus);
+ // get source node by offset
+ Node focusNode = getSourceNodeByPosition(model, focus);
- // if focus node also contain anchor point (selected only 1 element)
- if ((focusNode != null) && isNodeContainsPosition(focusNode, anchor)) {
+ // if focus node also contain anchor point (selected only 1 element)
+ if ((focusNode != null)
+ && NodesManagingUtil.isNodeContainsPosition(focusNode,
+ anchor)) {
- return NodesManagingUtil.getNodeMapping(domMapping, focusNode);
+ return NodesManagingUtil.getNodeMapping(domMapping, focusNode);
- }
+ }
} finally {
-
- if(model!=null) {
-
+
+ if (model != null) {
+
model.releaseFromRead();
}
}
-
+
return null;
}
@@ -257,12 +255,15 @@
.getLastSelectedNode();
}
+
/**
* Returns sourceSelectionRange
+ *
* @param sourceEditor
* @return sourceSelectionRange
*/
- static public Point getSourceSelectionRange(StructuredTextEditor sourceEditor) {
+ static public Point getSourceSelectionRange(
+ StructuredTextEditor sourceEditor) {
ITextViewer textViewer = sourceEditor.getTextViewer();
if (textViewer != null)
@@ -270,5 +271,14 @@
return null;
}
+
+ /**
+ *
+ * @param selectionController
+ */
+ static public void clearSelection(VpeSelectionController selectionController) {
+ selectionController.getSelection(
+ nsISelectionController.SELECTION_NORMAL).removeAllRanges();
+ }
}