Skip to content

Commit

Permalink
[lang][ui] Update the outline for supporting OOP.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed May 17, 2015
1 parent 93c65fb commit c97408e
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 99 deletions.
@@ -0,0 +1,69 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2015 Sebastian RODRIGUEZ, Nicolas GAUD, Stéphane GALLAND.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.lang.ui.outline;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.swt.graphics.Image;
import org.eclipse.xtext.ui.editor.outline.IOutlineNode;
import org.eclipse.xtext.ui.editor.outline.impl.EObjectNode;

/**
* Customize the outline page.
* The outline page is expanding the nodes at the startup.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
class SARLEObjectNode extends EObjectNode {

private boolean isStatic;

/**
* @param eObject - the object represented by the node.
* @param parent - the parent node.
* @param imageDescriptor - the descriptor of the image related to the node..
* @param text - the text for the node.
* @param isLeaf - indicates if the node is a leaf.
*/
public SARLEObjectNode(EObject eObject, IOutlineNode parent, Image imageDescriptor, Object text,
boolean isLeaf) {
super(eObject, parent, imageDescriptor, text, isLeaf);
}

/** Change the static flag for the node.
*
* @param isStatic - the value of the static flag.
*/
public void setStatic(boolean isStatic) {
this.isStatic = isStatic;
}

/** Replies the static flag for the node.
*
* @return the value of the static flag.
*/
public boolean isStatic() {
return this.isStatic;
}

}
Expand Up @@ -20,7 +20,6 @@
*/
package io.sarl.lang.ui.outline;

import static io.sarl.lang.sarl.SarlPackage.Literals.SARL_ACTION;
import static io.sarl.lang.sarl.SarlPackage.Literals.SARL_BEHAVIOR_UNIT;
import static io.sarl.lang.sarl.SarlPackage.Literals.SARL_CAPACITY_USES;
import static io.sarl.lang.sarl.SarlPackage.Literals.SARL_REQUIRED_CAPACITY;
Expand All @@ -32,6 +31,8 @@

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtend.core.xtend.XtendPackage;
import org.eclipse.xtext.common.types.TypesPackage;
import org.eclipse.xtext.ui.editor.outline.IOutlineNode;
import org.eclipse.xtext.ui.editor.outline.actions.SortOutlineContribution.DefaultComparator;
import org.eclipse.xtext.ui.editor.outline.impl.EObjectNode;
Expand All @@ -50,18 +51,17 @@ public class SARLOutlineNodeComparator extends DefaultComparator {
private static final int SCRIPT_PRIORITY = 0;
private static final int IMPORT_PRIORITY = 10;
private static final int TOPELEMENT_PRIORITY = 20;

private static final int PRIORITY_STEP = 10;

private final EClass[] types = new EClass[] {
SARL_CAPACITY_USES,
SARL_REQUIRED_CAPACITY,
XTEND_FIELD,
XTEND_CONSTRUCTOR,
SARL_BEHAVIOR_UNIT,
SARL_ACTION,
XTEND_FUNCTION,
};

private static final int CAPACITY_USE_PRIORITY = 30;
private static final int CAPACITY_REQUIREMENT_PRIORITY = 40;
private static final int STATIC_INNER_TYPE_PRIORITY = 50;
private static final int INNER_TYPE_PRIORITY = 60;
private static final int STATIC_FIELD_PRIORITY = 70;
private static final int STATIC_METHOD_PRIORITY = 80;
private static final int FIELD_PRIORITY = 90;
private static final int CONSTRUCTOR_PRIORITY = 100;
private static final int BEHAVIOR_UNIT_PRIORITY = 110;
private static final int METHOD_PRIORITY = 120;

/**
*/
Expand All @@ -79,19 +79,52 @@ public int getCategory(IOutlineNode node) {
return TOPELEMENT_PRIORITY;
}
if (node instanceof EObjectNode) {
EClass eClass = ((EObjectNode) node).getEClass();
EObjectNode objectNode = (EObjectNode) node;
EClass eClass = objectNode.getEClass();
if (XIMPORT_SECTION.isSuperTypeOf(eClass)) {
return IMPORT_PRIORITY;
}
int priority = 0;
for (EClass type : this.types) {
priority += PRIORITY_STEP;
if (type.isSuperTypeOf(eClass)) {
return priority;
if (XtendPackage.Literals.XTEND_TYPE_DECLARATION.isSuperTypeOf(eClass)
|| TypesPackage.Literals.JVM_DECLARED_TYPE.isSuperTypeOf(eClass)
|| TypesPackage.Literals.JVM_ENUMERATION_LITERAL.isSuperTypeOf(eClass)) {
if (isStatic(objectNode)) {
return STATIC_INNER_TYPE_PRIORITY;
}
return INNER_TYPE_PRIORITY;
}
if (SARL_CAPACITY_USES.isSuperTypeOf(eClass)) {
return CAPACITY_USE_PRIORITY;
}
if (SARL_REQUIRED_CAPACITY.isSuperTypeOf(eClass)) {
return CAPACITY_REQUIREMENT_PRIORITY;
}
if (XTEND_FIELD.isSuperTypeOf(eClass)) {
if (isStatic(objectNode)) {
return STATIC_FIELD_PRIORITY;
}
return FIELD_PRIORITY;
}
if (XTEND_FUNCTION.isSuperTypeOf(eClass)) {
if (isStatic(objectNode)) {
return STATIC_METHOD_PRIORITY;
}
return METHOD_PRIORITY;
}
if (XTEND_CONSTRUCTOR.isSuperTypeOf(eClass)) {
return CONSTRUCTOR_PRIORITY;
}
if (SARL_BEHAVIOR_UNIT.isSuperTypeOf(eClass)) {
return BEHAVIOR_UNIT_PRIORITY;
}
}
return Integer.MAX_VALUE;
}

private static boolean isStatic(EObjectNode eobjectNode) {
if (eobjectNode instanceof SARLEObjectNode) {
return ((SARLEObjectNode) eobjectNode).isStatic();
}
return false;
}

}
Expand Up @@ -20,29 +20,33 @@
*/
package io.sarl.lang.ui.outline;

import io.sarl.lang.sarl.SarlAction;
import io.sarl.lang.sarl.SarlAgent;
import io.sarl.lang.sarl.SarlBehavior;
import io.sarl.lang.jvmmodel.SarlJvmModelAssociations;
import io.sarl.lang.sarl.SarlBehaviorUnit;
import io.sarl.lang.sarl.SarlCapacity;
import io.sarl.lang.sarl.SarlCapacityUses;
import io.sarl.lang.sarl.SarlEvent;
import io.sarl.lang.sarl.SarlRequiredCapacity;
import io.sarl.lang.sarl.SarlSkill;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.swt.graphics.Image;
import org.eclipse.xtend.core.xtend.XtendConstructor;
import org.eclipse.xtend.core.xtend.XtendField;
import org.eclipse.xtend.core.xtend.XtendFile;
import org.eclipse.xtend.core.xtend.XtendFunction;
import org.eclipse.xtend.core.xtend.XtendMember;
import org.eclipse.xtend.core.xtend.XtendPackage;
import org.eclipse.xtend.core.xtend.XtendTypeDeclaration;
import org.eclipse.xtext.common.types.JvmDeclaredType;
import org.eclipse.xtext.common.types.JvmFeature;
import org.eclipse.xtext.common.types.JvmParameterizedTypeReference;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.ui.editor.outline.IOutlineNode;
import org.eclipse.xtext.ui.editor.outline.impl.DocumentRootNode;
import org.eclipse.xtext.ui.editor.outline.impl.EObjectNode;
import org.eclipse.xtext.ui.editor.outline.impl.EStructuralFeatureNode;
import org.eclipse.xtext.xbase.annotations.ui.outline.XbaseWithAnnotationsOutlineTreeProvider;

import com.google.common.base.Strings;
import com.google.inject.Inject;

/**
* Customization of the default outline structure.
Expand All @@ -55,6 +59,9 @@
*/
public class SARLOutlineTreeProvider extends XbaseWithAnnotationsOutlineTreeProvider {

@Inject
private SarlJvmModelAssociations associations;

/** Create a node for the SARL script.
*
* @param parentNode - the parent node.
Expand Down Expand Up @@ -97,13 +104,11 @@ protected void _createNode(DocumentRootNode parentNode, XtendTypeDeclaration mod
EObjectNode capacityRequirementNode = null;

for (EObject feature : modelElement.getMembers()) {
if (feature instanceof XtendField) {
createNode(elementNode, feature);
} else if (feature instanceof SarlAction) {
createNode(elementNode, feature);
} else if (feature instanceof SarlBehaviorUnit) {
createNode(elementNode, feature);
} else if (feature instanceof XtendConstructor) {
if (feature instanceof XtendField
|| feature instanceof XtendFunction
|| feature instanceof SarlBehaviorUnit
|| feature instanceof XtendConstructor
|| feature instanceof XtendTypeDeclaration) {
createNode(elementNode, feature);
} else if (feature instanceof SarlCapacityUses) {
capacityUseNode = createCapacityUseNode(elementNode, (SarlCapacityUses) feature, capacityUseNode);
Expand Down Expand Up @@ -155,94 +160,67 @@ private EObjectNode createRequiredCapacityNode(EStructuralFeatureNode elementNod
return capacityRequirementNode;
}

/** Replies if the agent element is a leaf in the outline.
/** Replies if the type declaration element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(SarlAgent modelElement) {
protected boolean _isLeaf(XtendTypeDeclaration modelElement) {
return modelElement.getMembers().isEmpty();
}

/** Replies if the capacity element is a leaf in the outline.
/** Replies if the member element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(SarlCapacity modelElement) {
return modelElement.getMembers().isEmpty();
}

/** Replies if the skill element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(SarlSkill modelElement) {
return modelElement.getMembers().isEmpty();
protected boolean _isLeaf(XtendMember modelElement) {
return true;
}

/** Replies if the event element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(SarlEvent modelElement) {
return modelElement.getMembers().isEmpty();
@Override
protected EObjectNode createEObjectNode(
IOutlineNode parentNode,
EObject modelElement, Image image, Object text,
boolean isLeaf) {
SARLEObjectNode eObjectNode = new SARLEObjectNode(modelElement, parentNode, image, text, isLeaf);
configureNode(parentNode, modelElement, eObjectNode);
return eObjectNode;
}

/** Replies if the behavior element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(SarlBehavior modelElement) {
return modelElement.getMembers().isEmpty();
}
private void configureNode(IOutlineNode parentNode, EObject modelElement, SARLEObjectNode eObjectNode) {
EObject primarySourceElement = this.associations.getPrimarySourceElement(modelElement);
ICompositeNode parserNode = NodeModelUtils.getNode(
(primarySourceElement == null) ? modelElement : primarySourceElement);

/** Replies if the action element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(SarlAction modelElement) {
return true;
}
if (parserNode != null) {
eObjectNode.setTextRegion(parserNode.getTextRegion());
}

/** Replies if the constructor element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(XtendConstructor modelElement) {
return true;
}
if (isLocalElement(parentNode, modelElement)) {
eObjectNode.setShortTextRegion(this.locationInFileProvider.getSignificantTextRegion(modelElement));
}

/** Replies if the behabior unit element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(SarlBehaviorUnit modelElement) {
return true;
eObjectNode.setStatic(isStatic(modelElement));
}

/** Replies if the attribute element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(XtendField modelElement) {
return true;
private static boolean isStatic(EObject element) {
if (element instanceof JvmFeature) {
return ((JvmFeature) element).isStatic();
}
if (element instanceof JvmDeclaredType) {
return ((JvmDeclaredType) element).isStatic();
}
if (element instanceof XtendMember) {
try {
return ((XtendMember) element).isStatic();
} catch (Exception _) {
// Some XtendMember does not support
}
}
return false;
}

}
Expand Up @@ -66,5 +66,5 @@ protected JvmVisibility getDefaultVisibility() {
}
return super.getDefaultVisibility();
}

}

0 comments on commit c97408e

Please sign in to comment.