1 /* 2 * Copyright (c) 2002-2014, Mairie de Paris 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice 10 * and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice 13 * and the following disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * License 1.0 33 */ 34 package fr.paris.lutece.portal.service.panel; 35 36 import fr.paris.lutece.portal.service.spring.SpringContextService; 37 38 import java.util.ArrayList; 39 import java.util.Collections; 40 import java.util.Comparator; 41 import java.util.List; 42 43 44 /** 45 * PanelService 46 * @param <T> Abstract type of panels to display. Every class extending the class T will create a panel. 47 */ 48 public final class LutecePanelService<T extends LutecePanel> 49 { 50 private static List<LutecePanelService<?extends LutecePanel>> _listSingletons = new ArrayList<LutecePanelService<?extends LutecePanel>>( ); 51 private List<T> _listPanels; 52 private Class<T> _genericTypeClass; 53 54 /** 55 * Instantiates a new lutece panel service. 56 */ 57 private LutecePanelService( ) 58 { 59 } 60 61 /** 62 * Instantiates a new lutece panel service. 63 * 64 * @param clazz the clazz 65 */ 66 private LutecePanelService( Class<T> clazz ) 67 { 68 _listPanels = SpringContextService.getBeansOfType( clazz ); 69 Collections.sort( _listPanels, new PanelComparator( ) ); 70 _genericTypeClass = clazz; 71 } 72 73 /** 74 * Gets the generic type class. 75 * 76 * @return the generic type class 77 */ 78 private Class<T> getGenericTypeClass( ) 79 { 80 return _genericTypeClass; 81 } 82 83 /** 84 * Get the instance of a PanelService for a given type. 85 * @param <T> Specialized type of the PanelService 86 * @param clazz Class associated to the type A. 87 * @return The instance of the PanelService with the generic type A. This instance is unique. 88 */ 89 public static synchronized <T extends LutecePanel> LutecePanelService<T> instance( Class<T> clazz ) 90 { 91 for ( LutecePanelService<?extends LutecePanel> storedPanelService : _listSingletons ) 92 { 93 if ( storedPanelService.getGenericTypeClass( ).equals( clazz ) ) 94 { 95 return (LutecePanelService<T>) storedPanelService; 96 } 97 } 98 99 LutecePanelService<T> panelService = new LutecePanelService<T>( clazz ); 100 _listSingletons.add( panelService ); 101 102 return panelService; 103 } 104 105 /** 106 * Get the index of a panel from its key 107 * @param strPanelKey Key of the panel to get the index. 108 * @return The index of the panel with the given key, or -1 if the panel could not be found. 109 */ 110 public int getIndex( String strPanelKey ) 111 { 112 int nIndex = 1; 113 114 for ( LutecePanel panel : getPanels( ) ) 115 { 116 if ( panel.getPanelKey( ).equals( strPanelKey ) ) 117 { 118 return nIndex; 119 } 120 121 nIndex++; 122 } 123 124 return -1; 125 } 126 127 /** 128 * Get the list of panels associated to this PanelService. One panel is used for every class extending the class T. 129 * @return The list of panels associated to this PanelService. 130 */ 131 public List<T> getPanels( ) 132 { 133 return _listPanels; 134 } 135 136 /** 137 * PanelComparator 138 */ 139 private class PanelComparator implements Comparator<T> 140 { 141 /** 142 * {@inheritDoc} 143 */ 144 @Override 145 public int compare( T p1, T p2 ) 146 { 147 return p1.getPanelOrder( ) - p2.getPanelOrder( ); 148 } 149 } 150 }