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); }
private int getCategory(IOutlineNode node) { if (node instanceof EObjectNode) { EClass eclass = ((EObjectNode) node).getEClass(); int id = eclass.getClassifierID(); int key = 10000 + id; Integer sortKey = METATYPESORTORDER.get(id); if (sortKey != null) { key = sortKey * 1000; } if (node instanceof N4JSEObjectNode) { N4JSEObjectNode n4node = (N4JSEObjectNode) node; if (!n4node.isStatic) { key += 100; } if (n4node.isConstructor) { key -= 50; } } return key; } return -1; }
/** * Recursively searches for a node with the given name and type. * * @param node * a root node of a subtree where the desired node is searched for * @param nodeName * the name of the node to search, must not be {@code null} * @param nodeType * the name of the type of the node to search, may be {@code null} if only the name of the node is to be tested * @return * a node with the given name and type (if specified). If such a node is not found, returns null. */ private IOutlineNode findNode(final IOutlineNode node, final String nodeName, final String nodeType) { IOutlineNode fieldNode = null; String[] textParts = node.getText().toString().split(":"); if (nodeName.equals(textParts[0].trim()) && (nodeType == null || (textParts.length > 1 && nodeType.equals(textParts[1].trim())))) { fieldNode = node; } else { List<IOutlineNode> children = node.getChildren(); for (IOutlineNode child : children) { fieldNode = findNode(child, nodeName, nodeType); if (fieldNode != null) { break; } } } return fieldNode; }
/** * Inspects the outline nodes and checks that given condition applies to at least one of the represented model objects. * * @param <T> * the generic type * @param clazz * the clazz * @param predicate * the predicate */ protected <T extends EObject> void assertEObjectInOutlineNodesExists(final Class<T> clazz, final Predicate<T> predicate) { final List<T> result = Lists.newArrayList(); for (final IOutlineNode n : getOutlineMap().get(clazz)) { result.add(n.readOnly(new IUnitOfWork<T, EObject>() { @Override @SuppressWarnings("unchecked") public T exec(final EObject state) throws Exception { // NOPMD return (T) state; } })); } try { Assert.assertNotNull(NLS.bind("At least one outline node represents an object of type \"{0}\"", clazz.getName()), Iterables.find(result, predicate)); } catch (NoSuchElementException e) { Assert.fail(NLS.bind("Could not find an object of type \"{0}\" in outline", clazz.getName())); } }
/** * Add the given outline node to the outline map. * * @param node * IOutlineNode to add. */ private void addToOutlineMap(final IOutlineNode node) { EStructuralFeature eFeature = null; if (node instanceof EObjectNode) { eFeature = ((EObjectNode) node).getEObject(getTestSource().getXtextResource()).eContainingFeature(); // TODO : remove the following part once all tests have been refactored Class<?> nodeClazz = ((EObjectNode) node).getEClass().getInstanceClass(); if (!getOutlineMap().containsKey(nodeClazz)) { getOutlineMap().put(nodeClazz, new ArrayList<IOutlineNode>()); } getOutlineMap().get(nodeClazz).add(node); } else if (node instanceof EStructuralFeatureNode) { eFeature = ((EStructuralFeatureNode) node).getEStructuralFeature(); } if (eFeature == null) { return; } if (!getOutlineMap().containsKey(eFeature)) { getOutlineMap().put(eFeature, new ArrayList<IOutlineNode>()); } getOutlineMap().get(eFeature).add(node); }
protected IOutlineNode findBestNode(IOutlineNode input, ITextRegion selectedTextRegion) { ITextRegion textRegion = input.getFullTextRegion(); if (textRegion == null || textRegion.contains(selectedTextRegion)) { IOutlineNode currentBestNode = input; for (IOutlineNode child : input.getChildren()) { IOutlineNode candidate = findBestNode(child, selectedTextRegion); if (candidate != null && (currentBestNode.getFullTextRegion() == null || currentBestNode.getFullTextRegion() .getLength() >= candidate.getFullTextRegion().getLength())) { currentBestNode = candidate; } } return currentBestNode; } return null; }
protected Set<IOutlineNode> getExpandedNodes(final TreeViewer treeViewer) { final Set<IOutlineNode> expandedNodes = Sets.newHashSet(); DisplayRunHelper.runSyncInDisplayThread(new Runnable() { public void run() { if (!treeViewer.getTree().isDisposed()) { Object[] expandedElements = treeViewer.getExpandedElements(); for (Object expandedElement : expandedElements) { if (!(expandedElement instanceof IOutlineNode)) LOG.error("Content outline contains illegal node " + expandedElement); else expandedNodes.add((IOutlineNode) expandedElement); } } } }); return expandedNodes; }
protected Set<IOutlineNode> getSelectedNodes(final TreeViewer treeViewer) { DisplayRunHelper.runSyncInDisplayThread(new Runnable() { public void run() { selectedNodes = Sets.newHashSet(); ISelection selection = treeViewer.getSelection(); if (selection instanceof IStructuredSelection) { for (Iterator<?> selectionIter = ((IStructuredSelection) selection).iterator(); selectionIter .hasNext();) { Object selectedElement = selectionIter.next(); if (!(selectedElement instanceof IOutlineNode)) LOG.error("Content outline contains illegal node " + selectedElement); else selectedNodes.add((IOutlineNode) selectedElement); } } } }); return selectedNodes; }
protected EStructuralFeatureNode createEStructuralFeatureNode(IOutlineNode parentNode, EObject owner, EStructuralFeature feature, Image image, Object text, boolean isLeaf) { boolean isFeatureSet = owner.eIsSet(feature); EStructuralFeatureNode eStructuralFeatureNode = new EStructuralFeatureNode(owner, feature, parentNode, image, text, isLeaf || !isFeatureSet); if (isFeatureSet) { ITextRegion region = locationInFileProvider.getFullTextRegion(owner, feature, 0); if (feature.isMany()) { int numValues = ((Collection<?>) owner.eGet(feature)).size(); ITextRegion fullTextRegion = locationInFileProvider.getFullTextRegion(owner, feature, numValues - 1); if (fullTextRegion != null) region = region.merge(fullTextRegion); } eStructuralFeatureNode.setTextRegion(region); } return eStructuralFeatureNode; }
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); }
public void open(IOutlineNode node, ISourceViewer textViewer) { if (node != null) { ITextRegion textRegion = node.getSignificantTextRegion(); if (textRegion != null && textRegion != ITextRegion.EMPTY_REGION) { int offset = textRegion.getOffset(); int length = textRegion.getLength(); textViewer.setRangeIndication(offset, length, true); textViewer.revealRange(offset, length); textViewer.setSelectedRange(offset, length); } else { node.readOnly(new IUnitOfWork.Void<EObject>() { @Override public void process(EObject state) throws Exception { openEObject(state); } }); } } }
public EStructuralFeatureNode createEStructuralFeatureNode(IOutlineNode parentNode, EObject owner, EStructuralFeature feature, ImageDescriptor imageDescriptor, Object text, boolean isLeaf) { boolean isFeatureSet = owner.eIsSet(feature); EStructuralFeatureNode eStructuralFeatureNode = new EStructuralFeatureNode(owner, feature, parentNode, imageDescriptor, text, isLeaf || !isFeatureSet); if (isFeatureSet) { ITextRegion region = locationInFileProvider.getFullTextRegion(owner, feature, 0); if (feature.isMany()) { int numValues = ((Collection<?>) owner.eGet(feature)).size(); ITextRegion fullTextRegion = locationInFileProvider.getFullTextRegion(owner, feature, numValues - 1); if(fullTextRegion != null) region = region.merge(fullTextRegion); } eStructuralFeatureNode.setTextRegion(region); } return eStructuralFeatureNode; }
protected void refreshViewer(final IOutlineNode rootNode, final Collection<IOutlineNode> nodesToBeExpanded, final Collection<IOutlineNode> selectedNodes) { DisplayRunHelper.runAsyncInDisplayThread(new Runnable() { public void run() { try { TreeViewer treeViewer = getTreeViewer(); if (!treeViewer.getTree().isDisposed()) { treeViewer.setInput(rootNode); treeViewer.expandToLevel(1); treeViewer.setExpandedElements(Iterables.toArray(nodesToBeExpanded, IOutlineNode.class)); treeViewer.setSelection(new StructuredSelection(Iterables.toArray(selectedNodes, IOutlineNode.class))); treeUpdated(); } } catch (Throwable t) { LOG.error("Error refreshing outline", t); } } }); }
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; }
@Override public IOutlineNode[] filterAndSort(Iterable<IOutlineNode> nodes) { final Iterable<IFilter> enabledFilters = getEnabledFilters(); Iterable<IOutlineNode> filteredNodes; if (Iterables.isEmpty(enabledFilters)) { filteredNodes = nodes; } else { List<IOutlineNode> childrenFromFeatureNodes = extractChildrenFromStructuralFeatureNodes(nodes); if (!childrenFromFeatureNodes.isEmpty()) { filteredNodes = filterNodes(enabledFilters, childrenFromFeatureNodes); } else { filteredNodes = filterNodes(enabledFilters, nodes); } } IOutlineNode[] nodesAsArray = Iterables.toArray(filteredNodes, IOutlineNode.class); if (comparator != null && isSortingEnabled()) { Arrays.sort(nodesAsArray, comparator); } return nodesAsArray; }
/** * Create specific expression-nodes. This means creating DSL-, let-, and implication-expression nodes. * * TODO Implication expression nodes are currently not supported! * * @param exprList The list of expression-declarations of this project. * @param parentNode The parent node of the expression-nodes (the project-contents-node). */ private void createExpressionNodes(List<ExpressionStatement> exprList, IOutlineNode parentNode) { for (ExpressionStatement exprStmt : exprList) { if (exprStmt != null) { // LET-EXPRESSIONS if (exprStmt.getExpr().getLet() != null && exprStmt.getExpr().getLet().getName() != null) { StyledString lExprString = new StyledString(); String lExprName = exprStmt.getExpr().getLet().getName(); lExprString.append(lExprName); lExprString.append(" : Let", StyledString.QUALIFIER_STYLER); createEStructuralFeatureNode(parentNode, exprStmt.getExpr().getLet(), IvmlPackage.Literals.LET_EXPRESSION__NAME, imageHelper.getImage(Images.NAME_CONSTRAINT), lExprString, true); } // TODO: IMPLICATIONEXPRESSIONS } } // End for-loop }
/** * Create attribute-nodes. * * @param attrList The list of attribute-declarations of this project. * @param parentNode The parent node of the attribute-nodes (the project-contents-node). */ private void createAttributeNodes(List<AnnotateTo> attrList, IOutlineNode parentNode) { for (AnnotateTo attrStmt : attrList) { if (!isEmpty(attrStmt)) { StyledString resultString = new StyledString(); String typeText = ModelUtility.stringValue(attrStmt.getAnnotationType()); for (String attachText : attrStmt.getNames()) { String attributeName = attrStmt.getAnnotationDecl().getName(); resultString.append(attributeName); resultString.append(" : " + typeText + " -> " + attachText, StyledString.QUALIFIER_STYLER); createEStructuralFeatureNode(parentNode, attrStmt, IvmlPackage.Literals.ANNOTATE_TO__ANNOTATION_DECL, imageHelper.getImage(Images.NAME_ATTRIBUTE), resultString, true); } } } }
/** * Create attribute-assignment nodes. * * @param assignList The list of attribute-assignment-declarations of this project. * @param parentNode The parent node of the attribute-assignment-nodes (the project-contents-node). */ private void createAssignNodes(List<AttrAssignment> assignList, IOutlineNode parentNode) { StyledString resultString = new StyledString(); for (AttrAssignment attrAssign : assignList) { if (attrAssign != null && !isEmpty(attrAssign.getParts())) { for (AttrAssignmentPart assignPart : attrAssign.getParts()) { if (assignPart != null) { resultString.append("assign"); resultString.append(" : " + assignPart.getName(), StyledString.QUALIFIER_STYLER); createEStructuralFeatureNode(parentNode, attrAssign, IvmlPackage.Literals.ATTR_ASSIGNMENT__PARTS, imageHelper.getImage(Images.NAME_ATTACHMENT), resultString, true); } } } } }
/** * Create freeze-nodes. * * @param freezeList The list of freeze-declarations of this project. * @param parentNode The parent node of the freeze-nodes (the project-contents-node). */ private void createFreezeNodes(List<Freeze> freezeList, IOutlineNode parentNode) { for (Freeze freeze : freezeList) { if (freeze != null && !isEmpty(freeze.getNames())) { String dvName = "" + freeze.getNames().get(0).getName().getQName(); dvName = dvName.substring(1, dvName.length() - 1); String resultName = dvName; if (freeze.getNames().size() > 1) { resultName += ", ..."; } StyledString resultString = new StyledString(); resultString.append("freeze"); resultString.append(" : " + resultName, StyledString.QUALIFIER_STYLER); createEStructuralFeatureNode(parentNode, freeze, IvmlPackage.Literals.FREEZE__NAMES, imageHelper.getImage(Images.NAME_FREEZE), resultString, true); } } }
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()); } }
protected void assertExpanded(TreeViewer aTreeViewer, IOutlineNode... expectedExpansion) { Object[] expandedElements = aTreeViewer.getExpandedElements(); assertEquals(expectedExpansion.length, expandedElements.length); OUTER: for (Object expandedObject : expandedElements) { assertTrue(expandedObject instanceof IOutlineNode); for (IOutlineNode expectedExpanded : expectedExpansion) { if (nodeComparer.equals((IOutlineNode) expandedObject, expectedExpanded)) continue OUTER; } fail("Unexpected expansion" + expandedObject.toString()); } }
protected void assertSelected(TreeViewer aTreeViewer, IOutlineNode... expectedSelection) { ISelection selection = aTreeViewer.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()); } }
@Override protected boolean apply(IOutlineNode node) { if (node instanceof N4JSEObjectNode) { N4JSEObjectNode n4jseObjectNode = (N4JSEObjectNode) node; if (!n4jseObjectNode.isMember) { return !n4jseObjectNode.isLocal; } } return true; }
@Override protected boolean apply(IOutlineNode node) { if (node instanceof N4JSEObjectNode) { N4JSEObjectNode n4jseObjectNode = (N4JSEObjectNode) node; if (n4jseObjectNode.isMember) { return !n4jseObjectNode.isStatic; } } return true; }
@Override protected boolean apply(IOutlineNode node) { if (node instanceof N4JSEObjectNode) { N4JSEObjectNode n4jseObjectNode = (N4JSEObjectNode) node; if (n4jseObjectNode.isMember) { return n4jseObjectNode.isPublic; } } return true; }
@Override protected boolean apply(IOutlineNode node) { if (node instanceof N4JSEObjectNode) { N4JSEObjectNode n4jseObjectNode = (N4JSEObjectNode) node; return !n4jseObjectNode.isInherited; } return true; }
@Override public int compare(IOutlineNode o1, IOutlineNode o2) { int category1 = getCategory(o1); int category2 = getCategory(o2); if (category1 != category2) return category1 - category2; return o1.getText().toString().compareTo(o2.getText().toString()); }
/** * 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; }
@Override public void _createChildren(final IOutlineNode parentNode, final EObject modelElement) { if ((modelElement instanceof Policies)) { this.createNode(parentNode, ((Policies)modelElement).getSeverity()); } else { super._createChildren(parentNode, modelElement); } }
protected LaunchConfig getLaunchConfigFromOutline(ExecutionEvent event) { IOutlineNode outlineNode = getOutlineNode(event); if (outlineNode == null) { return null; } return outlineNode.readOnly(new IUnitOfWork<LaunchConfig, EObject>() { @Override public LaunchConfig exec(EObject selectedElement) throws Exception { return findParentLaunchConfig(selectedElement); } }); }
private IOutlineNode getOutlineNode(ExecutionEvent event) { ISelection currentSelection = HandlerUtil.getCurrentSelection(event); if (!(currentSelection instanceof IStructuredSelection)) { return null; } IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection; return (IOutlineNode) structuredSelection.getFirstElement(); }
@Override public int getCategory(final IOutlineNode node) { if (node instanceof EStructuralFeatureNode) { EStructuralFeature eStructuralFeature = ((EStructuralFeatureNode) node).getEStructuralFeature(); if (eStructuralFeature == ValidPackage.Literals.VALID_MODEL__IMPORTS) { return IMPORTS_ORDER; } } return 0; }
@Override public int getCategory(final IOutlineNode node) { if (node instanceof EStructuralFeatureNode) { EStructuralFeature eStructuralFeature = ((EStructuralFeatureNode) node).getEStructuralFeature(); if (eStructuralFeature == ExportPackage.Literals.EXPORT_MODEL__IMPORTS) { return IMPORTS_ORDER; } } return 0; }
@Override public int getCategory(final IOutlineNode node) { if (node instanceof EStructuralFeatureNode) { EStructuralFeature eStructuralFeature = ((EStructuralFeatureNode) node).getEStructuralFeature(); if (eStructuralFeature == ScopePackage.Literals.SCOPE_MODEL__IMPORTS) { return IMPORTS_ORDER; } } return 0; }
/** * Returns a directory of outline nodes indexed by the class of the represented semantic model node. * * @return directory of outline nodes indexed by the class of the represented semantic model node */ @SuppressWarnings("unchecked") private Map<Object, ArrayList<IOutlineNode>> getOutlineMap() { Object obj = getTestInformation().getTestObject(IOutlineNode.class); if (obj instanceof Map<?, ?>) { return (Map<Object, ArrayList<IOutlineNode>>) obj; } else { return null; } }
/** * Set up the test by reading the input file and generating the outline tree. */ @Override protected final void beforeAllTests() { super.beforeAllTests(); IOutlineTreeProvider provider = getXtextTestUtil().get(IOutlineTreeProvider.class); getTestInformation().putTestObject(IOutlineNode.class, new HashMap<Object, ArrayList<IOutlineNode>>()); buildOutlineMap(provider.createRoot(getDocument())); }
/** * Traverse the outline tree node recursively and build up the outlineMap. * * @param node * the outline tree node to traverse. */ private void buildOutlineMap(final IOutlineNode node) { addToOutlineMap(node); // add node's children List<IOutlineNode> children = node.getChildren(); for (IOutlineNode child : children) { buildOutlineMap(child); } }
/** * 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); } } }
public int compare(IOutlineNode o1, IOutlineNode o2) { int category1 = getCategory(o1); int category2 = getCategory(o2); if(category1 != category2) return category1-category2; return o1.getText().toString().compareTo(o2.getText().toString()); }
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) { IOutlineNode nodeToBeSelected = findBestNode((IOutlineNode) input, selectedTextRegion); if (nodeToBeSelected != null) treeViewer.setSelection(new StructuredSelection(nodeToBeSelected)); } } }