if (ObjectUtilities.equal(visualElement.getElementTypeName(), targetElementType.getMetaData().getName()))
{
continue;
}
final ReportAttributeMap oldAttributes = visualElement.getAttributes();
final ElementMetaData data = visualElement.getMetaData();
final AttributeMetaData[] datas = data.getAttributeDescriptions();
for (int j = 0; j < datas.length; j++)
{
final AttributeMetaData metaData = datas[j];
final AttributeMetaData attributeMetaData =
targetMetaData.getAttributeDescription(metaData.getNameSpace(), metaData.getName());
if (attributeMetaData == null)
{
visualElement.setAttribute(metaData.getNameSpace(), metaData.getName(), null);
visualElement.setAttributeExpression(metaData.getNameSpace(), metaData.getName(), null);
}
}
visualElement.setElementType(targetElementType);
// -------------------- manipulate the value and field attributes so that we preserve as much as possible ---------------------
// get the source and destination types
final ElementType srcType = (ElementType) oldAttributes.getAttribute(CORE_NAMESPACE, ELEMENT_TYPE_ATTRIBUE);
// Check if the source-type is a message field. In that case, we have to map the pattern into a field and
// format definition
if (srcType instanceof MessageType)
{
final String message = (String) oldAttributes.getAttribute(CORE_NAMESPACE, VALUE_ATTRIBUTE);
visualElement.setAttribute(CORE_NAMESPACE, VALUE_ATTRIBUTE, null);
try
{
final MessageFormatSupport fmt = new MessageFormatSupport();
fmt.setFormatString(message);
final String rawFormat = fmt.getCompiledFormat();
final MessageFormat msg = new MessageFormat(rawFormat);
final Format[] subFormats = msg.getFormats();
if (targetElementType instanceof DateFieldType)
{
final int sdf = findFirstDateFormat(subFormats);
if (sdf != -1)
{
final SimpleDateFormat df = (SimpleDateFormat) subFormats[sdf];
final String[] fields = fmt.getFields();
visualElement.setAttribute(CORE_NAMESPACE, FIELD_ATTRIBUTE, fields[sdf]);
visualElement.setAttribute(CORE_NAMESPACE, FORMAT_ATTRIBUTE, df.toPattern());
}
}
else if (targetElementType instanceof NumberFieldType)
{
final int sdf = findFirstNumberFormat(subFormats);
if (sdf != -1)
{
final DecimalFormat df = (DecimalFormat) subFormats[sdf];
final String[] fields = fmt.getFields();
visualElement.setAttribute(CORE_NAMESPACE, FIELD_ATTRIBUTE, fields[sdf]);
visualElement.setAttribute(CORE_NAMESPACE, FORMAT_ATTRIBUTE, df.toPattern());
}
}
else
{
final String[] fields = fmt.getFields();
if (fields.length > 0)
{
visualElement.setAttribute(CORE_NAMESPACE, FIELD_ATTRIBUTE, fields[0]);
}
else
{
visualElement.setAttribute(CORE_NAMESPACE, VALUE_ATTRIBUTE, message);
}
}
}
catch (Exception ex)
{
visualElement.setAttribute(CORE_NAMESPACE, VALUE_ATTRIBUTE, message);
}
}
else if (targetElementType instanceof MessageType)
{
// validate that these are the correct combination to use this enhanced morphing.
// This stuff only applies if we're morphing TO a Message Type field
final String srcField = (String) oldAttributes.getAttribute(CORE_NAMESPACE, FIELD_ATTRIBUTE); // and morphing FROM Number, Date, or Text Type field
final String formatString = (String) oldAttributes.getAttribute(CORE_NAMESPACE, FORMAT_ATTRIBUTE);
final StringBuilder buffer = new StringBuilder();
buffer.append("$(").append(srcField); //$NON-NLS-1$
if (srcType instanceof NumberFieldType)
{
buffer.append(", number"); //$NON-NLS-1$
if (formatString != null && formatString.length() > 0)
{
buffer.append(", ").append(formatString); //$NON-NLS-1$
}
}
else if (srcType instanceof DateFieldType)
{
buffer.append(", date"); //$NON-NLS-1$
if (formatString != null && formatString.length() > 0)
{
buffer.append(", ").append(formatString); //$NON-NLS-1$
}
}
buffer.append(")"); //$NON-NLS-1$
visualElement.setAttribute(CORE_NAMESPACE, VALUE_ATTRIBUTE, buffer.toString());
}
final ReportAttributeMap newAttributes = visualElement.getAttributes();
undos.add(new MorphUndoEntry(visualElement.getObjectID(), oldAttributes, newAttributes));
}
getActiveContext().getUndo().addChange(ActionMessages.getString("MorphAction.UndoName"),
new CompoundUndoEntry(undos.toArray(new UndoEntry[undos.size()])));