/** * This method will bind the type of the <code>typedElement</code> if the {@link * #getTypeParameter() type parameter} of this <code>ParameterGenericType</code> is in the given * list of type parameters that shall be bound. */ @Override protected boolean doBindGenericType( List<TypeParameter> parameters, List<? extends Type> types, TypedElement typedElement) { int index; // search the type parameter of this generic type in the list of parameters index = ListUtil.indexOf(parameters, getTypeParameter()); // bind the generic type if type parameter was found if (index != -1) { Type type = types.get(index); if (logger.isInfoEnabled()) { logger.info( "Binding type of '" + typedElement.getQualifiedName() // $NON-NLS-1$ + "' with '" + type.getName() + "'."); //$NON-NLS-1$ //$NON-NLS-2$ } // reset the generic type, then set the type (the order is important) typedElement.setGenericType(null); typedElement.setType(type); // break ans signal success return true; } return false; }
/** * @see org.eclipse.emf.edit.provider.ItemPropertyDescriptor#setPropertyValue(java.lang.Object, * java.lang.Object) */ @Override public void setPropertyValue(Object object, Object value) { TypedElement typedElement = (TypedElement) object; // the value can either be a type parameter ... if (value instanceof TypeParameter) { TypeParameter typeParameter = (TypeParameter) value; // create a new generic type for type parameters and set its // referenced type parameter ParameterGenericType genericType = PivotModelFactory.eINSTANCE.createParameterGenericType(); genericType.setTypeParameter(typeParameter); setGenericType(typedElement, genericType); } // ... or a normal type else if (value instanceof Type) { Type type = (Type) value; // check if the type has any type parameters if (!type.getOwnedTypeParameter().isEmpty()) { // create a new complex generic type and set the given type // as a reference ComplexGenericType genericType = PivotModelFactory.eINSTANCE.createComplexGenericType(); genericType.setUnboundType(type); // append type arguments for each type parameter for (int i = 0, size = type.getOwnedTypeParameter().size(); i < size; i++) { genericType.getTypeArgument().add(PivotModelFactory.eINSTANCE.createTypeArgument()); } setGenericType(typedElement, genericType); } // just a normal type without type parameters else { // remove any previously created generic types setGenericType(typedElement, null); super.setPropertyValue(object, value); } } }
/** * Helper method to return the name of the {@link Type} of a {@link TypedElement}. This default * implementation simply returns the name if the type, but subclasses may override to provide a * custom rendering. * * @param type the <code>Type</code>, guaranteed not to be <code>null</code> * @return a formatted name for the type */ protected CharSequence getTypeName(Type type) { return type.getName(); }
private Type findTypeOfNode(Node node) throws TypeNotFoundInModelException { Type result; result = null; /* Collect all parent nodes. */ List<Node> parents; parents = new ArrayList<Node>(); Node aNode; aNode = node; while (aNode != null) { if (aNode == aNode.getParentNode() || aNode instanceof Document) { break; } else { parents.add(aNode); aNode = aNode.getParentNode(); } } // end while. if (parents.size() != 0) { /* Try to find the type of the root node. */ Type rootType; rootType = this.findTypeOfRootNode(parents.remove(parents.size() - 1)); if (rootType != null) { result = rootType; /* Take the next node, try to find its property and its type. */ while (parents.size() > 0) { List<Property> properties; properties = result.allProperties(); aNode = parents.remove(parents.size() - 1); result = null; for (Property property : properties) { if (property.getName().equalsIgnoreCase(aNode.getNodeName().trim())) { if (property.getType() instanceof CollectionType) { result = ((CollectionType) property.getType()).getElementType(); } else { result = property.getType(); } break; } // no else. } // end for. } } // no else (root type not found). } // no else (no parent node found). if (result == null) { throw new TypeNotFoundInModelException( NLS.bind( XmlModelInstanceTypeMessages.XmlModelInstanceFactory_UnknownTypeOfAdaptee, node)); } // no else. return result; }