/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * 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: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.modeling.adapters; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.adaption.SimpleAdapter; import org.simantics.db.exception.AdaptionException; import org.simantics.db.exception.ServiceException; import org.simantics.db.exception.ValidationException; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; public class ElementStringAdapter extends SimpleAdapter { @Override public String adapt(ReadGraph g, Resource r) throws ValidationException, ServiceException { try { ModelingResources mr = ModelingResources.getInstance(g); Resource name = r; Resource component = g.getPossibleObject(r, mr.ElementToComponent); if (component != null) name = component; StringBuilder sb = new StringBuilder(); Layer0 b = Layer0.getInstance(g); String nameName = g.getPossibleRelatedValue(name, b.HasName); if (nameName != null) sb.append(nameName); boolean first = true; for (Resource rType : g.getPrincipalTypes(r)) { String s = g.adapt(rType, String.class); if (first) { sb.append(" : "); first = false; } else { sb.append(", "); } sb.append(s); } return sb.toString(); } catch (AdaptionException e) { throw new ValidationException(e); } } }