blob: 6f361b6de2a01ca8cf892f360250fc2d8f958b26 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Istvan Rath and Daniel Varro
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.treeeditor.providers;
import java.util.Collection;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.viatra2.core.EMultiplicityKind;
import org.eclipse.viatra2.core.IEntity;
import org.eclipse.viatra2.core.IModelElement;
import org.eclipse.viatra2.core.IRelation;
import org.eclipse.viatra2.tags.ITag;
import org.eclipse.viatra2.tags.TagKind;
import org.eclipse.viatra2.treeeditor.Plugin;
import org.eclipse.viatra2.treeeditor.ViatraTreeEditor;
import org.eclipse.viatra2.treeeditor.providers.ViatraContentProvider.TargetRelationDummy;
public class ViatraLabelProvider extends LabelProvider
//implements ITableColorProvider, ITableLabelProvider
implements IColorProvider
{
private ViatraTreeEditor iVTE;
public ViatraLabelProvider(ViatraTreeEditor vte) {
super();
iVTE = vte;
}
private static Image eim = Plugin.getImageDescriptor("icons/entity_transparent.png").createImage();
private static Image eim_warn = Plugin.getImageDescriptor("icons/entity_transparent_warning.png").createImage();
private static Image eim_info = Plugin.getImageDescriptor("icons/entity_transparent_info.png").createImage();
//private static Image mim = Plugin.getImageDescriptor("icons/machine_transparent.png").createImage();
private static Image rim = Plugin.getImageDescriptor("icons/relation_transparent.png").createImage();
private static Image rim_target = Plugin.getImageDescriptor("icons/relation_target_transparent.png").createImage();
private static Image rim_warning = Plugin.getImageDescriptor("icons/relation_transparent_warning.png").createImage();
private static Image rim_info = Plugin.getImageDescriptor("icons/relation_transparent_info.png").createImage();
public String getText(Object element) {
if (element instanceof IModelElement || element instanceof TargetRelationDummy) {
// Get model element object
IModelElement me;
if (element instanceof TargetRelationDummy)
{
me = ((TargetRelationDummy)element).rel;
}
else me = ((IModelElement)element);
// Get appropriate format string
String formatString = "%n";
if (element instanceof TargetRelationDummy)
{
formatString = iVTE.getFramework().getProperties().getRuntimeProperty(ViatraEditorPropertyProvider.VIATRA_EDITOR_PROVIDER_ID, ViatraEditorPropertyProvider.PROP_FORMATSTRING_RELATION_TARGET);
}
else if (me instanceof IEntity)
{
formatString = iVTE.getFramework().getProperties().getRuntimeProperty(ViatraEditorPropertyProvider.VIATRA_EDITOR_PROVIDER_ID, ViatraEditorPropertyProvider.PROP_FORMATSTRING_ENTITY);
}
else if (me instanceof IRelation)
{
formatString = iVTE.getFramework().getProperties().getRuntimeProperty(ViatraEditorPropertyProvider.VIATRA_EDITOR_PROVIDER_ID, ViatraEditorPropertyProvider.PROP_FORMATSTRING_RELATION);
}
// Format string overview
// Escape character: %
// %[12]{ [%$]}value
// %% : The '%' character
// %# : An empty sequence (used when a letter is display right after a %property)
// %[limit]property : The given property of a model element (limit: max. number of displayed characters)
// Properties:
// name (n) simple name of the element
// NAME (N) fully qualified name of the element
// type (t) direct types of the element (names only)
// TYPE (T) direct types of the element (fully qualified names)
// instance (i) direct instances of the element
// INSTANCE (I) direct instances of the element (fully qualified names)
// supertype (s)
// SUPERTYPE (S)
// subtype (st)
// SUBTYPE (ST)
// alltypes (at), ...
// value (v)
// source (src), target (trg), SOURCE (SRC), TARGET (TRG)
// sourcemultiplicity (sm) 1 for ONE_TO_MANY or ONE_TO_ONE; * for MANY_TO_MANY or MANY_TO_ONE
// targetmultiplicity (tm) 1 for ONE_TO_ONE or MANY_TO_ONE; * for ONE_TO_MANY or MANY_TO_MANY
// isaggregation (ag) '@' when isAgregation is true
// Generate displayed string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < formatString.length(); i++)
{
char c = formatString.charAt(i);
if (c == '%' && (i + 1) < formatString.length())
{
// Get next character
char c2 = formatString.charAt(++i);
// Check if there is a display limit
int displayLimit = -1;
if (c2 == '[')
{
// Get display limit
++i;
String sLimit = "";
while (i < formatString.length() && Character.isDigit(formatString.charAt(i)))
sLimit = sLimit + formatString.charAt(i++);
if (sLimit.length() < 9)
displayLimit = Integer.parseInt(sLimit);
// Skip limit trailing ']' character
if (i < formatString.length() && formatString.charAt(i) == ']') i++;
if (i < formatString.length())
c2 = formatString.charAt(i);
}
// Check internal format string (may not contain additional format stuff)
String internalFormat = "";
int propPosition = -1;
if (c2 == '{')
{
++i;
while (i < formatString.length() && formatString.charAt(i) != '}')
{
if (formatString.charAt(i) == '%')
{
++i;
char c3 = (i < formatString.length()) ? formatString.charAt(i++) : '.';
if (c3 == '%' || c3 == '$' || c3 == '}')
internalFormat = internalFormat + c3;
else
{
internalFormat = internalFormat + '%';
i--;
}
}
else if (formatString.charAt(i) == '$')
{
i++;
propPosition = internalFormat.length();
}
else
{
internalFormat = internalFormat + formatString.charAt(i++);
}
}
i++;
if (i < formatString.length())
c2 = formatString.charAt(i);
}
// Check escape code
if (c2 == '#');
else if (c2 == '%') sb.append('%');
else
{
// Read property string
String prop = "";
while (i < formatString.length() && Character.isLetter(formatString.charAt(i)))
prop = prop + formatString.charAt(i++);
--i;
// Use fully qualified names, if property is all uppercase
boolean isFQN = prop.equals(prop.toUpperCase());
prop = prop.toLowerCase();
// Decide which property is required
String propValue = "";
if ("name".equals(prop) || "n".equals(prop))
{
propValue = (isFQN ? me.getFullyQualifiedName() : me.getName());
}
else if ("value".equals(prop) || "v".equals(prop))
{
propValue = ((me instanceof IEntity) ? ((IEntity) me).getValue() : "");
}
else if ("source".equals(prop) || "src".equals(prop))
{
if (me instanceof IRelation)
propValue = (isFQN ? ((IRelation) me).getFrom().getFullyQualifiedName() : ((IRelation) me).getFrom().getName());
}
else if ("target".equals(prop) || "trg".equals(prop))
{
if (me instanceof IRelation)
propValue = (isFQN ? ((IRelation) me).getTo().getFullyQualifiedName() : ((IRelation) me).getTo().getName());
}
else if ("type".equals(prop) || "t".equals(prop))
{
propValue = elementCollectionToString(me.getTypes(), isFQN);
}
else if ("instance".equals(prop) || "i".equals(prop))
{
propValue = elementCollectionToString(me.getInstances(), isFQN);
}
else if ("supertype".equals(prop) || "s".equals(prop))
{
propValue = elementCollectionToString(me.getSupertypes(), isFQN);
}
else if ("subtype".equals(prop) || "st".equals(prop))
{
propValue = elementCollectionToString(me.getSubtypes(), isFQN);
}
else if ("alltype".equals(prop) || "at".equals(prop))
{
propValue = elementCollectionToString(me.getAllTypes(), isFQN);
}
else if ("allinstance".equals(prop) || "ai".equals(prop))
{
propValue = elementCollectionToString(me.getAllInstances(), isFQN);
}
else if ("allsupertype".equals(prop) || "as".equals(prop))
{
propValue = elementCollectionToString(me.getAllSupertypes(), isFQN);
}
else if ("allsubtype".equals(prop) || "ast".equals(prop))
{
propValue = elementCollectionToString(me.getAllSubtypes(), isFQN);
}
else if ("targetvalue".equals(prop) || "tv".equals(prop))
{
if (me instanceof IRelation && ((IRelation) me).getTo() instanceof IEntity)
propValue = ((IEntity) ((IRelation) me).getTo()).getValue();
}
else if ("targettype".equals(prop) || "tt".equals(prop))
{
if (me instanceof IRelation)
propValue = elementCollectionToString(((IRelation) me).getTo().getTypes(), isFQN);
}
else if ("targetsupertype".equals(prop) || "ts".equals(prop))
{
if (me instanceof IRelation)
propValue = elementCollectionToString(((IRelation) me).getTo().getSupertypes(), isFQN);
}
else if ("targetalltype".equals(prop) || "tat".equals(prop))
{
if (me instanceof IRelation)
propValue = elementCollectionToString(((IRelation) me).getTo().getAllTypes(), isFQN);
}
else if ("targetallsupertype".equals(prop) || "tas".equals(prop))
{
if (me instanceof IRelation)
propValue = elementCollectionToString(((IRelation) me).getTo().getAllSupertypes(), isFQN);
}
else if ("sourcemultiplicity".equals(prop) || "sm".equals(prop))
{
if (me instanceof IRelation) {
EMultiplicityKind mult = ((IRelation) me).getMultiplicity();
propValue =
(mult == EMultiplicityKind.MANY_TO_MANY) || (mult == EMultiplicityKind.MANY_TO_ONE)
? "*" : "1";
}
}
else if ("targetmultiplicity".equals(prop) || "tm".equals(prop))
{
if (me instanceof IRelation) {
EMultiplicityKind mult = ((IRelation) me).getMultiplicity();
propValue =
(mult == EMultiplicityKind.MANY_TO_MANY) || (mult == EMultiplicityKind.ONE_TO_MANY)
? "*" : "1";
}
}
else if ("isagregation".equals(prop) || "ag".equals(prop)) {
if (me instanceof IRelation) {
propValue = ((IRelation)me).getIsAggregation() ? "@" : "";
}
}
// Add property value to display string
if (displayLimit > 0 && propValue.length() > displayLimit)
propValue = propValue.substring(0, displayLimit - 3) + "...";
// Format, if required
if (internalFormat.length() > 0 && propPosition >= 0 && propValue.length() > 0)
propValue = ((propPosition > 0) ? internalFormat.substring(0, propPosition) : "") +
propValue +
((propPosition < internalFormat.length()) ? internalFormat.substring(propPosition) : "");
sb.append(propValue);
}
} else
{
sb.append(c);
}
}
return sb.toString();
/*
// Added by dave for relation endpoint display
String rep = "";
if (me.isRelation())
rep = " (-> " + ((IRelation) me).getTo().getName() + ")";
// End of added code
// added by Istvan Rath for entity value display
String val = "";
if (me.isEntity())
{
IEntity e = (IEntity)me;
if (e.getValue().length()>0)
{
val = " { " + ((e.getValue().length()>20)?e.getValue().substring(0,17)+"...":e.getValue()) + " } ";
}
}
// end of added code
*/
// return ((IModelElement)element).getName()+/**/rep+val/**/+(types.length()>0?" : "+types:"")+(supertypes.length()>0?" ["+supertypes+"]":"");
}
else
return "@@unknown element";
}
public Image getImage(Object element) {
//if (element instanceof IModelElement) {
// if (iVTE.getFramework().isRunnable((IModelElement)element))
// return mim;
//}
if (element instanceof IModelElement)
{
Collection<ITag> tags = iVTE.getFramework().getTagManager().getTagsForModelElement((IModelElement)element);
boolean hasInfo = false;
boolean hasWarning = false;
if (tags!=null)
{
for (ITag t : tags)
{
if (t.getKind().equals(TagKind.MODELING_INFO))
hasInfo = true;
if (t.getKind().equals(TagKind.MODELING_PROBLEM))
{
hasWarning = true;
break;
}
}
}
if (element instanceof IEntity) {
if (hasWarning) return eim_warn;
if (hasInfo) return eim_info;
else return eim;
}
if (element instanceof IRelation)
{
if (hasWarning) return rim_warning;
if (hasInfo) return rim_info;
return rim;
}
}
else if (element instanceof TargetRelationDummy)
{
return rim_target;
}
return null;
//return super.getImage(element);
}
/**
* Translates a Viatra2 model element collection to a list of comma separated strings
* (names). If the isFQN flag is true, not only the names, but the fully qualified names
* of the element will be shown.
*
* @param elements collection of model elements
* @param isFQN true, if we want to display fully qualified names
* @return a comma separated string of element names
*/
protected String elementCollectionToString(Collection<IModelElement> elements, boolean isFQN)
{
StringBuilder sb = new StringBuilder();
boolean needComma = false;
for (IModelElement elem : elements)
{
if (needComma)
sb.append(", ");
else
needComma = true;
if (isFQN)
sb.append(elem.getFullyQualifiedName());
else
sb.append(elem.getName());
}
return sb.toString();
}
public Color getBackground(Object element) {
if (element instanceof IModelElement)
{
Collection<ITag> tags = iVTE.getFramework().getTagManager().getTagsForModelElement((IModelElement)element);
if (tags!=null && tags.size()>0)
{
return Display.getDefault().getSystemColor(tags.iterator().next().getBackgroundColor());
}
}
return null;
}
public Color getForeground(Object element) {
if (element instanceof IModelElement)
{
Collection<ITag> tags = iVTE.getFramework().getTagManager().getTagsForModelElement((IModelElement)element);
if (tags!=null && tags.size()>0)
{
return Display.getDefault().getSystemColor(tags.iterator().next().getForegroundColor());
}
}
return null;
}
}