Java源码示例:org.eclipse.xtext.ui.editor.outline.IOutlineNode
示例1
protected void openOutlineView() throws PartInitException, InterruptedException {
outlineView = editor.getEditorSite().getPage().showView("org.eclipse.ui.views.ContentOutline");
executeAsyncDisplayJobs();
Object adapter = editor.getAdapter(IContentOutlinePage.class);
assertTrue(adapter instanceof OutlinePage);
outlinePage = new SyncableOutlinePage((OutlinePage) adapter);
outlinePage.resetSyncer();
try {
outlinePage.waitForUpdate(EXPECTED_TIMEOUT);
} catch (TimeoutException e) {
System.out.println("Expected timeout exceeded: " + EXPECTED_TIMEOUT);// timeout is OK here
}
treeViewer = outlinePage.getTreeViewer();
assertSelected(treeViewer);
assertExpanded(treeViewer);
assertTrue(treeViewer.getInput() instanceof IOutlineNode);
IOutlineNode rootNode = (IOutlineNode) treeViewer.getInput();
List<IOutlineNode> children = rootNode.getChildren();
assertEquals(1, children.size());
modelNode = children.get(0);
}
示例2
protected void selectInTreeView(ISelection selection) {
if (selection instanceof ITextSelection && !treeViewer.getTree().isDisposed()) {
ITextSelection textSelection = (ITextSelection) selection;
ITextRegion selectedTextRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength());
Object input = treeViewer.getInput();
if (input instanceof IOutlineNode) {
try {
IOutlineNode nodeToBeSelected = findBestNode((IOutlineNode) input, selectedTextRegion);
if (nodeToBeSelected != null)
treeViewer.setSelection(new StructuredSelection(nodeToBeSelected));
} catch(Exception exc) {
// ignore, editor can have a different state than the tree
}
}
}
}
示例3
@Override
protected void _createNode(IOutlineNode parentNode, EObject modelElement) {
if (modelElement instanceof HtmlContent) {
HtmlContent htmlContent = (HtmlContent) modelElement;
/**
* Skip the empty (containing nothing or only white-spaces)
* htmlContent elements, but process their tag children.
*/
if (htmlContent.getText() != null
&& !htmlContent.getText().trim().isEmpty()) {
super._createNode(parentNode, htmlContent);
} else {
if (htmlContent.getTag() != null) {
super._createNode(parentNode, htmlContent.getTag());
}
}
} else {
super._createNode(parentNode, modelElement);
}
}
示例4
protected boolean isEquivalentIndex(IOutlineNode node1, IOutlineNode node2) {
IOutlineNode parent1 = node1.getParent();
IOutlineNode parent2 = node2.getParent();
if (parent1 == null && parent2 == null)
return true;
if (parent1 != null && parent2 != null) {
List<IOutlineNode> siblings1 = parent1.getChildren();
List<IOutlineNode> siblings2 = parent2.getChildren();
int index1 = siblings1.indexOf(node1);
int index2 = siblings2.indexOf(node2);
// same siblings => same index
// sibling inserted after => same index
// sibling inserted before => same # of following siblings
if (index1 == index2 || siblings1.size() - index1 == siblings2.size() - index2)
return true;
}
return false;
}
示例5
@Override
public List<IOutlineNode> getChildren() {
if (isLeaf)
return Collections.emptyList();
if (children == null) {
readOnly(new IUnitOfWork.Void<EObject>() {
@Override
public void process(EObject eObject) throws Exception {
getTreeProvider().createChildren(AbstractOutlineNode.this, eObject);
}
});
if (children == null) {
// tree provider did not create any child
isLeaf = true;
return Collections.emptyList();
}
}
return Collections.unmodifiableList(children);
}
示例6
@Override
protected boolean apply(IOutlineNode node) {
if (node instanceof EObjectNode) {
return !isBehaviorUnit(((EObjectNode) node).getEClass());
}
if (node instanceof EStructuralFeatureNode) {
return !isBehaviorUnit(((EStructuralFeatureNode) node).getEStructuralFeature().eClass());
}
return true;
}
示例7
protected AssertBuilder newAssertBuilder(XtendFile xtendFile) throws Exception, CoreException {
XtextDocument document = documentProvider.get();
document.setInput((XtextResource) xtendFile.eResource());
IOutlineNode root = treeProvider.createRoot(document);
AssertBuilder assertBuilder = new AssertBuilder(root);
return assertBuilder;
}
示例8
@Override
public IXtendOutlineContext buildDispatcherNode(JvmDeclaredType baseType, JvmFeature dispatcher,
List<JvmOperation> dispatchCases, IXtendOutlineContext context) {
EclipseXtendOutlineContext eclipseXtendOutlineContext = (EclipseXtendOutlineContext) context;
IOutlineNode parentNode = eclipseXtendOutlineContext.getParentNode();
int inheritanceDepth = eclipseXtendOutlineContext.getInheritanceDepth();
XtendFeatureNode dispatcherNode = createNodeForFeature(parentNode, baseType, dispatcher, inheritanceDepth);
dispatcherNode.setDispatch(true);
if (isInheritsDispatchCases(baseType, dispatchCases)) {
dispatcherNode.setImageDescriptor(images.forDispatcherFunction(dispatcher.getVisibility(),
adornments.get(dispatcher) | JavaElementImageDescriptor.OVERRIDES));
}
return eclipseXtendOutlineContext.withParentNode(dispatcherNode);
}
示例9
@Override
protected Object doGetText(Object element) {
if (element instanceof IOutlineNode) {
return ((IOutlineNode) element).getText();
}
return super.doGetText(element);
}
示例10
@Override
public IXtendOutlineContext buildXtendNode(EObject modelElement, IXtendOutlineContext context) {
IXtendOutlineContext resultedContext = super.buildXtendNode(modelElement, context);
if (!context.isShowInherited()) {
EclipseXtendOutlineContext eclipseXtendOutlineContext = (EclipseXtendOutlineContext) context;
IOutlineNode parentNode = eclipseXtendOutlineContext.getParentNode();
if (parentNode instanceof DocumentRootNode) {
if (modelElement instanceof JvmDeclaredType) {
JvmDeclaredType jvmDeclaredType = (JvmDeclaredType) modelElement;
String packageName = jvmDeclaredType.getPackageName();
if (packageName != null) {
EObject rootElement = modelElement.eResource().getContents().get(0);
if (rootElement instanceof XtendFile) {
XtendFile xtendFile = (XtendFile) rootElement;
String primaryPackage = xtendFile.getPackage();
if (!packageName.equals(primaryPackage)) {
EObjectNode typeNode = (EObjectNode) ((EclipseXtendOutlineContext) resultedContext).getParentNode();
if (typeNode.getText() instanceof StyledString) {
typeNode.setText(((StyledString) typeNode.getText()).append(new StyledString(" - "
+ packageName, StyledString.QUALIFIER_STYLER)));
}
}
}
}
}
}
}
return resultedContext;
}
示例11
/** Assert the number of children for the node is equal
* to the given value.
*
* @param num expected number of children.
* @return this
*/
public OutlineAsserts numChildren(int num) {
IOutlineNode[] filteredAndSortedChildren = getSorter().filterAndSort(
this.node.getChildren());
assertEquals("Wrong number of children\n" //$NON-NLS-1$
+ Joiner.on("\n").join(filteredAndSortedChildren), num, //$NON-NLS-1$
filteredAndSortedChildren.length);
return this;
}
示例12
@Override
public IXtendOutlineContext buildResolvedFeatureNode(JvmDeclaredType inferredType,
IResolvedFeature resolvedFeature, IXtendOutlineContext context) {
EclipseXtendOutlineContext eclipseXtendOutlineContext = (EclipseXtendOutlineContext) context;
IOutlineNode parentNode = eclipseXtendOutlineContext.getParentNode();
int inheritanceDepth = eclipseXtendOutlineContext.getInheritanceDepth();
XtendFeatureNode node = createNodeForResolvedFeature(parentNode, inferredType, resolvedFeature, inheritanceDepth);
return eclipseXtendOutlineContext.withParentNode(node);
}
示例13
/**
* Returns N4JSEObjectNode instead of simple EObjectNode to allow for attaching additional information such as
* inheritance state of members.
*/
@Override
public N4JSEObjectNode createEObjectNode(IOutlineNode parentNode, EObject modelElement,
ImageDescriptor imageDescriptor,
Object text,
boolean isLeaf) {
N4JSEObjectNode eObjectNode = new N4JSEObjectNode(modelElement, parentNode, imageDescriptor, text, isLeaf);
ICompositeNode parserNode = NodeModelUtils.getNode(modelElement);
if (parserNode != null)
eObjectNode.setTextRegion(parserNode.getTextRegion());
if (isLocalElement(parentNode, modelElement))
eObjectNode.setShortTextRegion(getLocationInFileProvider().getSignificantTextRegion(modelElement));
return eObjectNode;
}
示例14
@Override
protected void createNode(IOutlineNode parent, EObject modelElement) {
// make sure that non-container name-value-pairs are marked as leaf nodes
if (modelElement instanceof NameValuePair && !((NameValuePair) modelElement).getValue().isContainer()) {
createEObjectNode(parent, modelElement, this.imageDispatcher.invoke(modelElement),
this.textDispatcher.invoke(modelElement),
// mark as leaf node
true);
return;
}
// otherwise delegate to default behavior
super.createNode(parent, modelElement);
}
示例15
protected void addChildren(List<IOutlineNode> nodes, List<IOutlineNode> allChildren, int depth) {
if (depth > 1) {
for (IOutlineNode node : nodes) {
List<IOutlineNode> children = node.getChildren();
allChildren.addAll(children);
addChildren(children, allChildren, depth - 1);
}
}
}
示例16
protected IOutlineNode expectedNodeAt(int offset) {
if (offset < modelAsText.indexOf("two"))
return oneNode;
if (offset < modelAsText.indexOf(" three"))
return twoNode;
if (offset < modelAsText.indexOf("three"))
return oneNode;
if (offset < modelAsText.indexOf(" } four"))
return threeNode;
if (offset < modelAsText.indexOf(" four"))
return oneNode;
if (offset < modelAsText.indexOf("four"))
return modelNode;
return fourNode;
}
示例17
protected void refreshViewer(final IOutlineNode rootNode, final Collection<IOutlineNode> nodesToBeExpanded,
final Collection<IOutlineNode> selectedNodes) {
DisplayRunHelper.runAsyncInDisplayThread(new Runnable() {
@Override
public void run() {
try {
TreeViewer treeViewer = getTreeViewer();
if (!treeViewer.getTree().isDisposed()) {
getTreeViewer().getTree().setRedraw(false);
if (treeViewer.getLabelProvider() != labelProvider) {
if (treeViewer.getInput() != null && treeViewer.getContentProvider() != null)
treeViewer.setInput(null);
treeViewer.setLabelProvider(labelProvider);
}
if (treeViewer.getContentProvider() != contentProvider) {
if (treeViewer.getInput() != null && treeViewer.getContentProvider() != null)
treeViewer.setInput(null);
treeViewer.setContentProvider(contentProvider);
}
treeViewer.setInput(rootNode);
treeViewer.expandToLevel(1);
treeViewer.setExpandedElements(Iterables.toArray(nodesToBeExpanded, Object.class));
treeViewer.setSelection(new StructuredSelection(Iterables.toArray(selectedNodes,
IOutlineNode.class)));
treeUpdated();
}
} catch (Throwable t) {
LOG.error("Error refreshing outline", t);
} finally {
if (!getTreeViewer().getTree().isDisposed()) {
getTreeViewer().getTree().setRedraw(true);
}
}
}
});
}
示例18
/**
* Skip the empty (containing nothing or only white-spaces) htmlContent
* elements, but process their tag children.
*/
protected void _createNode(IOutlineNode parent, HtmlContent htmlContent) {
if (htmlContent.getText() != null
&& !htmlContent.getText().trim().isEmpty()) {
super._createNode(parent, htmlContent);
} else {
if (htmlContent.getTag() != null) {
super._createNode(parent, htmlContent.getTag());
}
}
}
示例19
protected void assertSelected(TreeViewer treeViewer, IOutlineNode... expectedSelection) {
ISelection selection = treeViewer.getSelection();
assertTrue(selection instanceof IStructuredSelection);
assertEquals(expectedSelection.length, ((IStructuredSelection) selection).size());
OUTER: for (Iterator<?> i = ((IStructuredSelection) selection).iterator(); i.hasNext();) {
Object selectedObject = i.next();
assertTrue(selectedObject instanceof IOutlineNode);
for (IOutlineNode expectedSelected : expectedSelection) {
if (nodeComparer.equals((IOutlineNode) selectedObject, expectedSelected))
continue OUTER;
}
fail("Unexpected selection " + selectedObject.toString());
}
}
示例20
private IOutlineNode getOutlineNode(ExecutionEvent event) {
ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
if (!(currentSelection instanceof IStructuredSelection)) {
return null;
}
IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
return (IOutlineNode) structuredSelection.getFirstElement();
}
示例21
@Test public void testCreateChildren() throws Exception {
final String modelAsText = "element1 { element11 {}} element2 {}";
IXtextDocument document = createXtextDocument(modelAsText);
final IOutlineNode rootNode = treeProvider.createRoot(document);
document.readOnly(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(XtextResource state) throws Exception {
treeProvider.createChildren(rootNode, state.getContents().get(0));
assertEquals(1, rootNode.getChildren().size());
IOutlineNode modelNode = rootNode.getChildren().get(0);
assertEquals(state.getURI().trimFileExtension().lastSegment(), modelNode.getText());
assertEquals(new TextRegion(0, modelAsText.length()), modelNode.getSignificantTextRegion());
assertEquals(new TextRegion(0, modelAsText.length()), modelNode.getFullTextRegion());
assertEquals(rootNode, modelNode.getParent());
assertTrue(modelNode.hasChildren());
assertEquals(2, modelNode.getChildren().size());
IOutlineNode element1 = modelNode.getChildren().get(0);
assertEquals("element1", element1.getText().toString());
assertEquals(new TextRegion(0, 8), element1.getSignificantTextRegion());
assertEquals(new TextRegion(0, 24), element1.getFullTextRegion());
assertEquals(modelNode, element1.getParent());
assertTrue(element1.hasChildren());
IOutlineNode element2 = modelNode.getChildren().get(1);
assertEquals("element2", element2.getText().toString());
assertEquals(new TextRegion(25, 8), element2.getSignificantTextRegion());
assertEquals(new TextRegion(25, 11), element2.getFullTextRegion());
assertEquals(modelNode, element2.getParent());
assertFalse(element2.hasChildren());
}
});
}
示例22
@Test public void testNoNames() throws Exception {
final DefaultOutlineTreeProvider noNamesTreeProvider = new DefaultOutlineTreeProvider(new DefaultEObjectLabelProvider(),
new DefaultLocationInFileProvider()) {
@Override
protected Object _text(Object modelElement) {
return null;
}
};
final String modelAsText = "element1 { element11 {}} element2 {}";
IXtextDocument document = createXtextDocument(modelAsText);
final IOutlineNode rootNode = noNamesTreeProvider.createRoot(document);
document.readOnly(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(XtextResource state) throws Exception {
noNamesTreeProvider.createChildren(rootNode, state.getContents().get(0));
assertEquals(1, rootNode.getChildren().size());
IOutlineNode modelNode = rootNode.getChildren().get(0);
assertEquals(state.getURI().trimFileExtension().lastSegment(), modelNode.getText());
assertTrue(modelNode.hasChildren());
assertEquals(1, modelNode.getChildren().size());
IOutlineNode element1 = modelNode.getChildren().get(0);
assertEquals("<unnamed>", element1.getText().toString());
assertEquals(new TextRegion(0, 8), element1.getSignificantTextRegion());
assertEquals(new TextRegion(0, 24), element1.getFullTextRegion());
assertEquals(modelNode, element1.getParent());
// node does not know that its children will be skipped
assertTrue(element1.hasChildren());
assertTrue(element1.getChildren().isEmpty());
}
});
}
示例23
/**
* Skip the 'AttrList' wrapper element in the outline structure.
*
* @param parent
* The outline parent node.
* @param stmt
* The attribute statement.
*/
protected void _createChildren(IOutlineNode parent, AttrStmt stmt) {
if (stmt.getAttrLists().size() > 0) {
EList<Attribute> attributes = stmt.getAttrLists().get(0)
.getAttributes(); // skip the 'AttrList'
for (Attribute attribute : attributes) {
createNode(parent, attribute);
}
}
}
示例24
@Override
protected EObjectNode createEObjectNode(IOutlineNode parentNode,
EObject modelElement, Image image, Object text, boolean isLeaf) {
if (EcoreUtil2.getContainerOfType(modelElement,
HtmlLabel.class) != null) {
// in case of a html-like label addition offset should be calculated
EObjectNode eObjectNode = new EObjectNode(modelElement, parentNode,
image, text, isLeaf);
ICompositeNode parserNode = NodeModelUtils.getNode(modelElement);
if (parserNode != null) {
ITextRegion parserNodeTextRegion = parserNode.getTextRegion();
ITextRegion newTextRegion = new TextRegion(
parserNodeTextRegion.getOffset()
+ attributeValueStartOffset,
parserNodeTextRegion.getLength());
eObjectNode.setTextRegion(newTextRegion);
}
if (isLocalElement(parentNode, modelElement)) {
ITextRegion significantTextRegion = locationInFileProvider
.getSignificantTextRegion(modelElement);
ITextRegion shortTextRegion = new TextRegion(
significantTextRegion.getOffset()
+ attributeValueStartOffset,
significantTextRegion.getLength());
eObjectNode.setShortTextRegion(shortTextRegion);
}
return eObjectNode;
} else {
return super.createEObjectNode(parentNode, modelElement, image,
text, isLeaf);
}
}
示例25
protected TreeViewer getOutlineTreeViewer() throws PartInitException {
document = editor.getDocument();
outlineView = editor.getEditorSite().getPage()
.showView("org.eclipse.ui.views.ContentOutline");
executeAsyncDisplayJobs();
Object adapter = editor.getAdapter(IContentOutlinePage.class);
assertTrue(adapter instanceof OutlinePage);
outlinePage = (OutlinePage) adapter;
TreeViewer treeViewer = outlinePage.getTreeViewer();
awaitForTreeViewer(treeViewer);
assertTrue(treeViewer.getInput() instanceof IOutlineNode);
return treeViewer;
}
示例26
protected XtendEObjectNode createXtendNode(IOutlineNode parentNode, EObject modelElement, int inheritanceDepth) {
Object text = computeDecoratedText(modelElement, inheritanceDepth);
ImageDescriptor image = getImageDescriptor(modelElement);
XtendEObjectNode objectNode = getOutlineNodeFactory().createXtendEObjectNode(parentNode, modelElement, image,
text, true, inheritanceDepth);
return objectNode;
}
示例27
@Test public void testNonInheritMode() throws Exception {
IOutlineNode node = assertNoException("grammar Foo with org.eclipse.xtext.common.Terminals " +
"generate foo 'Foo' " +
"Foo: 'foo'; " +
"Bar: 'bar';");
assertEquals(1, node.getChildren().size());
IOutlineNode grammar = node.getChildren().get(0);
assertNode(grammar, "grammar Foo", 3);
assertNode(grammar.getChildren().get(0), "generate foo", 0);
assertNode(grammar.getChildren().get(1), "Foo", 0);
assertNode(grammar.getChildren().get(2), "Bar", 0);
}
示例28
@Test public void testInheritModeWithOverride() throws Exception {
setShowInherited(true);
String model = "grammar Foo with org.eclipse.xtext.common.Terminals " +
"generate foo 'Foo' " +
"Foo: 'foo'; " +
"terminal ID: 'bar';";
IOutlineNode node = assertNoException(model);
assertEquals(1, node.getChildren().size());
IOutlineNode grammar = node.getChildren().get(0);
assertNode(grammar, "grammar Foo", 10);
assertNode(grammar.getChildren().get(0), "generate foo", 0);
IOutlineNode foo = grammar.getChildren().get(1);
assertNode(foo, "Foo", 0);
assertEquals(model.lastIndexOf("Foo"), foo.getFullTextRegion().getOffset());
assertEquals(11, foo.getFullTextRegion().getLength());
assertEquals(model.lastIndexOf("Foo"), foo.getSignificantTextRegion().getOffset());
assertEquals(3, foo.getSignificantTextRegion().getLength());
assertNode(grammar.getChildren().get(2), "ID", 0);
IOutlineNode id = grammar.getChildren().get(3);
assertNode(id, "ID (org.eclipse.xtext.common.Terminals)", 0);
assertNull(id.getSignificantTextRegion());
assertEquals(ITextRegion.EMPTY_REGION, id.getFullTextRegion());
assertNode(grammar.getChildren().get(4), "INT (org.eclipse.xtext.common.Terminals)", 0);
assertNode(grammar.getChildren().get(5), "STRING (org.eclipse.xtext.common.Terminals)", 0);
assertNode(grammar.getChildren().get(6), "ML_COMMENT (org.eclipse.xtext.common.Terminals)", 0);
assertNode(grammar.getChildren().get(7), "SL_COMMENT (org.eclipse.xtext.common.Terminals)", 0);
assertNode(grammar.getChildren().get(8), "WS (org.eclipse.xtext.common.Terminals)", 0);
assertNode(grammar.getChildren().get(9), "ANY_OTHER (org.eclipse.xtext.common.Terminals)", 0);
}
示例29
/**
* Default for createChildrenDispatcher with outline node as a parent node.
*
* @param parentNode
* the {@link IOutlineNode}
* @param modelElement
* the {@link EObject} model element
*/
// CHECKSTYLE:CHECK-OFF Name (dispatcher enforces names starting with underscore)
public void _createChildren(final IOutlineNode parentNode, final EObject modelElement) {
// CHECKSTYLE:CHECK-ON
if (modelElement != null && parentNode.hasChildren()) {
if (parentNode instanceof DocumentRootNode) {
internalCreateChildren((DocumentRootNode) parentNode, modelElement);
} else if (parentNode instanceof EStructuralFeatureNode) {
internalCreateChildren((EStructuralFeatureNode) parentNode, modelElement);
} else {
internalCreateChildren(parentNode, modelElement);
}
}
}
示例30
protected void findNodesInRange(final IOutlineNode input, final ITextRegion selectedTextRegion, final List<IOutlineNode> nodes) {
final ITextRegion textRegion = input.getFullTextRegion();
if (((textRegion == null) || textRegion.contains(selectedTextRegion))) {
nodes.add(input);
}
List<IOutlineNode> _children = input.getChildren();
for (final IOutlineNode child : _children) {
this.findNodesInRange(child, selectedTextRegion, nodes);
}
}