001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Jean-Marc Orliaguet, Chalmers 011 * 012 * $Id$ 013 */ 014 015package org.nuxeo.theme.elements; 016 017import java.util.ArrayList; 018import java.util.Collection; 019import java.util.Iterator; 020 021import org.nuxeo.theme.Manager; 022import org.nuxeo.theme.formats.Format; 023import org.nuxeo.theme.formats.FormatType; 024import org.nuxeo.theme.relations.DyadicRelation; 025import org.nuxeo.theme.relations.Relate; 026import org.nuxeo.theme.relations.Relation; 027import org.nuxeo.theme.relations.RelationStorage; 028import org.nuxeo.theme.types.TypeFamily; 029import org.nuxeo.theme.types.TypeRegistry; 030 031public final class ElementFormatter { 032 033 private ElementFormatter() { 034 // This class is not supposed to be instantiated. 035 } 036 037 public static void setFormat(final Object object, final Format format) { 038 // Remove the old format of the same type 039 final Format oldFormat = getFormatByType(object, format.getFormatType()); 040 if (oldFormat != null) { 041 removeFormat(object, oldFormat); 042 } 043 Manager.getRelationStorage().add(new DyadicRelation(format.getPredicate(), (Element) object, format)); 044 } 045 046 public static Format getFormatByType(final Object object, final FormatType type) { 047 final Collection<Relation> relations = Manager.getRelationStorage().search(type.getPredicate(), 048 (Element) object, null); 049 final Iterator<Relation> i = relations.iterator(); 050 if (i.hasNext()) { 051 return (Format) ((DyadicRelation) i.next()).getRelate(2); 052 } 053 // TODO throw exception; 054 return null; 055 } 056 057 public static Collection<Format> getFormatsFor(final Element element) { 058 final Collection<Format> formats = new ArrayList<Format>(); 059 // TODO use a type manager for registering and getting format types 060 final String[] formatTypeNames = { "widget", "style", "layout" }; 061 for (String typeName : formatTypeNames) { 062 Format format = getFormatFor(element, typeName); 063 if (format != null) { 064 formats.add(format); 065 } 066 } 067 return formats; 068 } 069 070 public static Format getFormatFor(final Element element, final String typeName) { 071 final RelationStorage relationStorage = Manager.getRelationStorage(); 072 final TypeRegistry typeRegistry = Manager.getTypeRegistry(); 073 final FormatType type = (FormatType) typeRegistry.lookup(TypeFamily.FORMAT, typeName); 074 // FIXME: this loop doesn't loop! 075 for (Relation relation : relationStorage.search(type.getPredicate(), element, null)) { 076 return (Format) relation.getRelate(2); 077 } 078 return null; 079 } 080 081 public static Collection<Element> getElementsFor(final Format format) { 082 final Collection<Element> elements = new ArrayList<Element>(); 083 final RelationStorage relationStorage = Manager.getRelationStorage(); 084 final String[] formatTypeNames = { "widget", "style", "layout" }; 085 final TypeRegistry typeRegistry = Manager.getTypeRegistry(); 086 for (String typeName : formatTypeNames) { 087 final FormatType type = (FormatType) typeRegistry.lookup(TypeFamily.FORMAT, typeName); 088 for (Relation relation : relationStorage.search(type.getPredicate(), null, format)) { 089 elements.add((Element) relation.getRelate(1)); 090 } 091 } 092 return elements; 093 } 094 095 public static void removeFormat(final Object object, final Format format) { 096 final RelationStorage relationStorage = Manager.getRelationStorage(); 097 for (Relation relation : relationStorage.search(format.getPredicate(), (Relate) object, format)) { 098 relationStorage.remove(relation); 099 } 100 } 101 102}