blob: a3c9c7519e13f8fb8455332300bee8b29a5597fd [file] [log] [blame]
package org.eclipse.wst.xml.ui.internal.quickoutline;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.wst.sse.ui.IContentSelectionProvider;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
public class XMLContentSelectionProvider implements IContentSelectionProvider {
public ISelection getSelection(TreeViewer viewer, ISelection selection) {
ISelection filteredSelection = selection;
if (selection instanceof IStructuredSelection) {
Object[] filteredNodes = getFilteredNodes(((IStructuredSelection) selection).toArray());
filteredSelection = new StructuredSelection(filteredNodes);
}
return filteredSelection;
}
private Object[] getFilteredNodes(Object[] filteredNodes) {
for (int i = 0; i < filteredNodes.length; i++) {
filteredNodes[i] = getFilteredNode(filteredNodes[i]);
}
return filteredNodes;
}
private Object getFilteredNode(Object object) {
if (object instanceof Node) {
Node node = (Node) object;
short nodeType = node.getNodeType();
// replace attribute node in selection with its parent
if (nodeType == Node.ATTRIBUTE_NODE) {
node = ((Attr) node).getOwnerElement();
}
// anything else not visible, replace with parent node
else if (nodeType == Node.TEXT_NODE) {
node = node.getParentNode();
}
return node;
}
return object;
}
}