blob: 0750b902e3024554d54dc35c4da1c2d0bcb30b63 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
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;
}
}