001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Jean-Marc Orliaguet, Chalmers 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.theme.perspectives; 023 024import java.util.ArrayList; 025import java.util.List; 026 027import org.nuxeo.theme.Manager; 028import org.nuxeo.theme.Registrable; 029import org.nuxeo.theme.elements.Element; 030import org.nuxeo.theme.relations.DefaultPredicate; 031import org.nuxeo.theme.relations.DyadicRelation; 032import org.nuxeo.theme.relations.Predicate; 033import org.nuxeo.theme.relations.Relation; 034import org.nuxeo.theme.relations.RelationStorage; 035import org.nuxeo.theme.types.Type; 036import org.nuxeo.theme.types.TypeFamily; 037 038public class PerspectiveManager implements Registrable { 039 040 private static final String PREDICATE_NAME = "_ is visible in perspective _"; 041 042 private static final Predicate predicate = new DefaultPredicate(PREDICATE_NAME); 043 044 private final RelationStorage relationStorage = Manager.getRelationStorage(); 045 046 public static PerspectiveType getPerspectiveByName(String name) { 047 return (PerspectiveType) Manager.getTypeRegistry().lookup(TypeFamily.PERSPECTIVE, name); 048 } 049 050 public static boolean hasPerspective(String perspectiveName) { 051 List<String> perspectiveNames = Manager.getTypeRegistry().getTypeNames(TypeFamily.PERSPECTIVE); 052 if (perspectiveNames == null) { 053 return false; 054 } 055 return perspectiveNames.contains(perspectiveName); 056 } 057 058 public boolean isVisibleInPerspective(Element element, PerspectiveType perspective) { 059 // the fragment is not in relation with any specific perspective: it is 060 // always visible. 061 if (relationStorage.search(predicate, element, null).isEmpty()) { 062 return true; 063 } 064 return !relationStorage.search(predicate, element, perspective).isEmpty(); 065 } 066 067 public static void setVisibleInPerspective(Element element, PerspectiveType perspective) { 068 Manager.getRelationStorage().add(new DyadicRelation(predicate, element, perspective)); 069 } 070 071 public void setVisibleInAllPerspectives(Element element) { 072 // remove existing relations 073 removeRelationsFrom(element); 074 // create new relations 075 for (PerspectiveType perspective : listPerspectives()) { 076 Manager.getRelationStorage().add(new DyadicRelation(predicate, element, perspective)); 077 } 078 } 079 080 public void setVisibleInPerspectives(Element element, List<String> perspectiveNames) { 081 // remove existing relations 082 removeRelationsFrom(element); 083 // create new relations 084 for (String perspectiveName : perspectiveNames) { 085 PerspectiveType perspective = getPerspectiveByName(perspectiveName); 086 Manager.getRelationStorage().add(new DyadicRelation(predicate, element, perspective)); 087 } 088 } 089 090 public void setAlwaysVisible(Element element) { 091 removeRelationsFrom(element); 092 } 093 094 public boolean isAlwaysVisible(Element element) { 095 return getPerspectivesFor(element).isEmpty(); 096 } 097 098 public List<PerspectiveType> getPerspectivesFor(Element element) { 099 List<PerspectiveType> perspectives = new ArrayList<PerspectiveType>(); 100 for (Relation relation : relationStorage.search(predicate, element, null)) { 101 PerspectiveType perspective = (PerspectiveType) relation.getRelate(2); 102 perspectives.add(perspective); 103 } 104 return perspectives; 105 } 106 107 public static List<PerspectiveType> listPerspectives() { 108 List<PerspectiveType> perspectives = new ArrayList<PerspectiveType>(); 109 for (Type perspective : Manager.getTypeRegistry().getTypes(TypeFamily.PERSPECTIVE)) { 110 perspectives.add((PerspectiveType) perspective); 111 } 112 return perspectives; 113 } 114 115 private void removeRelationsFrom(Element element) { 116 for (Relation relation : relationStorage.search(predicate, element, null)) { 117 relationStorage.remove(relation); 118 } 119 } 120 121 public void clear() { 122 } 123 124}