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.perspectives; 016 017import java.util.ArrayList; 018import java.util.List; 019 020import org.nuxeo.theme.Manager; 021import org.nuxeo.theme.Registrable; 022import org.nuxeo.theme.elements.Element; 023import org.nuxeo.theme.relations.DefaultPredicate; 024import org.nuxeo.theme.relations.DyadicRelation; 025import org.nuxeo.theme.relations.Predicate; 026import org.nuxeo.theme.relations.Relation; 027import org.nuxeo.theme.relations.RelationStorage; 028import org.nuxeo.theme.types.Type; 029import org.nuxeo.theme.types.TypeFamily; 030 031public class PerspectiveManager implements Registrable { 032 033 private static final String PREDICATE_NAME = "_ is visible in perspective _"; 034 035 private static final Predicate predicate = new DefaultPredicate(PREDICATE_NAME); 036 037 private final RelationStorage relationStorage = Manager.getRelationStorage(); 038 039 public static PerspectiveType getPerspectiveByName(String name) { 040 return (PerspectiveType) Manager.getTypeRegistry().lookup(TypeFamily.PERSPECTIVE, name); 041 } 042 043 public static boolean hasPerspective(String perspectiveName) { 044 List<String> perspectiveNames = Manager.getTypeRegistry().getTypeNames(TypeFamily.PERSPECTIVE); 045 if (perspectiveNames == null) { 046 return false; 047 } 048 return perspectiveNames.contains(perspectiveName); 049 } 050 051 public boolean isVisibleInPerspective(Element element, PerspectiveType perspective) { 052 // the fragment is not in relation with any specific perspective: it is 053 // always visible. 054 if (relationStorage.search(predicate, element, null).isEmpty()) { 055 return true; 056 } 057 return !relationStorage.search(predicate, element, perspective).isEmpty(); 058 } 059 060 public static void setVisibleInPerspective(Element element, PerspectiveType perspective) { 061 Manager.getRelationStorage().add(new DyadicRelation(predicate, element, perspective)); 062 } 063 064 public void setVisibleInAllPerspectives(Element element) { 065 // remove existing relations 066 removeRelationsFrom(element); 067 // create new relations 068 for (PerspectiveType perspective : listPerspectives()) { 069 Manager.getRelationStorage().add(new DyadicRelation(predicate, element, perspective)); 070 } 071 } 072 073 public void setVisibleInPerspectives(Element element, List<String> perspectiveNames) { 074 // remove existing relations 075 removeRelationsFrom(element); 076 // create new relations 077 for (String perspectiveName : perspectiveNames) { 078 PerspectiveType perspective = getPerspectiveByName(perspectiveName); 079 Manager.getRelationStorage().add(new DyadicRelation(predicate, element, perspective)); 080 } 081 } 082 083 public void setAlwaysVisible(Element element) { 084 removeRelationsFrom(element); 085 } 086 087 public boolean isAlwaysVisible(Element element) { 088 return getPerspectivesFor(element).isEmpty(); 089 } 090 091 public List<PerspectiveType> getPerspectivesFor(Element element) { 092 List<PerspectiveType> perspectives = new ArrayList<PerspectiveType>(); 093 for (Relation relation : relationStorage.search(predicate, element, null)) { 094 PerspectiveType perspective = (PerspectiveType) relation.getRelate(2); 095 perspectives.add(perspective); 096 } 097 return perspectives; 098 } 099 100 public static List<PerspectiveType> listPerspectives() { 101 List<PerspectiveType> perspectives = new ArrayList<PerspectiveType>(); 102 for (Type perspective : Manager.getTypeRegistry().getTypes(TypeFamily.PERSPECTIVE)) { 103 perspectives.add((PerspectiveType) perspective); 104 } 105 return perspectives; 106 } 107 108 private void removeRelationsFrom(Element element) { 109 for (Relation relation : relationStorage.search(predicate, element, null)) { 110 relationStorage.remove(relation); 111 } 112 } 113 114 public void clear() { 115 } 116 117}