| /******************************************************************************* |
| * Copyright (c) 2010 Zoltan Ujhelyi and 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.visualisation.common.labelproviders; |
| |
| import org.eclipse.draw2d.IFigure; |
| import org.eclipse.draw2d.text.FlowPage; |
| import org.eclipse.draw2d.text.TextFlow; |
| import org.eclipse.jface.resource.FontRegistry; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.graphics.FontData; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.viatra2.core.IEntity; |
| import org.eclipse.viatra2.core.IModelElement; |
| import org.eclipse.viatra2.visualisation.modelspace.datasource.ModelSpaceLabelProvider; |
| |
| /** |
| * "Smart" label provider that displays useful labels. |
| * @author istvan rath |
| * |
| */ |
| public class SmartLabelProvider extends ModelSpaceLabelProvider { |
| |
| FontRegistry fontRegistry; |
| |
| public SmartLabelProvider() { |
| fontRegistry = new FontRegistry(Display.getDefault()); |
| fontRegistry.put("default", new FontData[]{new FontData("Sans", 9, SWT.BOLD)} ); |
| fontRegistry.put("code", new FontData[]{new FontData("Sans", 9, SWT.NORMAL)}); |
| } |
| |
| protected static String getTypesAsString(IModelElement element) { |
| StringBuffer r = new StringBuffer(); |
| for (IModelElement type : element.getTypes()) { |
| if (r.length()>0) r.append(","); |
| r.append(type.getName()); |
| } |
| return r.toString(); |
| } |
| |
| protected static String getShortText2(String s) { |
| return s; |
| } |
| |
| @Override |
| public String getText(Object element) { |
| if (element instanceof IModelElement) { |
| IModelElement me = (IModelElement)element; |
| String nameStr = ""; |
| if (!me.getName().startsWith("uN")) { |
| nameStr+=me.getName(); |
| } |
| if (element instanceof IEntity) { |
| IEntity ent = (IEntity)element; |
| // if there is a value, display that too |
| if (ent.getValue()!=null && ent.getValue().length()>0) { |
| //return ent.getValue(); |
| nameStr += " {"+ent.getValue()+"}"; |
| } |
| } |
| // else if (element instanceof IRelation) { |
| // relation-specific tricks |
| // } |
| String typeStr = getShortText2(getTypesAsString(me)); |
| if (typeStr.length()>0) { |
| return nameStr+": "+typeStr; |
| } |
| return nameStr; |
| } |
| return super.getText(element); |
| } |
| |
| |
| |
| @Override |
| public boolean fisheyeNode(Object entity) { |
| return false; // "fisheye" nodes don't work too well with tooltips |
| } |
| |
| |
| @Override |
| public IFigure getTooltip(Object entity) { |
| if (entity instanceof IModelElement) { |
| IModelElement me = (IModelElement)entity; |
| |
| FlowPage fp = new FlowPage(); |
| |
| TextFlow nameTf = new TextFlow(); |
| nameTf.setFont(fontRegistry.get("default")); |
| TextFlow infoTf = new TextFlow(); |
| infoTf.setFont(fontRegistry.get("code")); |
| |
| nameTf.setText(me.getName()); |
| String info="\n"; |
| info+="FQN: "+me.getFullyQualifiedName()+"\n"; |
| info+="Namespace: "+me.getNamespace().getName()+"\n"; |
| info+="Types: "+getTypesAsString(me)+"\n"; |
| if (me instanceof IEntity) { |
| info+="Value: "+((IEntity)me).getValue()+"\n"; |
| } |
| info+="Comments: "+me.getViewInfo()+"\n"; |
| infoTf.setText(info); |
| fp.add(nameTf); |
| fp.add(infoTf); |
| return fp; |
| } |
| return null; |
| } |
| |
| } |