KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > tiles > portal > PortalCatalog


1 /*
2  * $Id: PortalCatalog.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.webapp.tiles.portal;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25
26 /**
27  * A catalog of available tiles for a portal.
28  * Tiles denote a local URL or a Tile definition name.
29  * To check : should be possible to put ComponentDefinition class also.
30  *
31  */

32 public class PortalCatalog
33 {
34        /** List of available Tiles */
35      protected List JavaDoc tiles = new ArrayList JavaDoc();
36        /** List of Tiles labels */
37      protected List JavaDoc tileLabels = new ArrayList JavaDoc();
38
39        /**
40         * Set list of tiles.
41         * Labels come from tiles names
42         * @param list list of tiles
43         */

44      public void setTiles( List JavaDoc list)
45        {
46        setTiles(list, list);
47        }
48
49        /**
50         * add list to list of available Tiles
51         * Labels come from tiles names
52         * @param list list of tiles
53         */

54      public void addTiles( List JavaDoc list)
55        {
56        addTiles( list, list);
57        }
58
59        /**
60         * Set list of available Tiles.
61         * Previous list is disguarded.
62         * @param list list of tiles
63         * @param labels corresponding labels. List size must be the same as list.
64         * If labels is null, use list of tiles.
65         * @throws ArrayIndexOutOfBoundsException if list and labels aren't the same size.
66         */

67      public void setTiles( List JavaDoc list, List JavaDoc labels)
68          throws ArrayIndexOutOfBoundsException JavaDoc
69        {
70          // If no labels, use list keys
71
if( labels == null )
72          labels = list;
73          // Check sizes
74
if( list.size() != labels.size() )
75          {// error
76
System.out.println( "Error : list and labels size must be the same." );
77          throw new java.lang.ArrayIndexOutOfBoundsException JavaDoc( "List of tiles and list of labels must be of the same size" );
78          }
79        this.tiles = list;
80        tileLabels = labels;
81        }
82
83        /**
84         * add list and labels to list of available Tiles.
85         * If labels is null, use keys list as labels.
86         * @list list of choice keys to add
87         * @param labels corresponding labels. List size must be the same as list.
88         * If labels is null, use list of tiles.
89         * @throws ArrayIndexOutOfBoundsException if list and labels aren't the same size.
90         */

91      public void addTiles( List JavaDoc list, List JavaDoc labels)
92          throws ArrayIndexOutOfBoundsException JavaDoc
93        {
94          // If no labels, use list keys
95
if( labels == null )
96          labels = list;
97          // Check sizes
98
if(tiles== null)
99          {
100          setTiles(list, labels);
101          return;
102          }
103
104        if( list.size() != labels.size() )
105          {// error
106
System.out.println( "Error : list and labels size must be the same." );
107          throw new java.lang.ArrayIndexOutOfBoundsException JavaDoc( "List of tiles and list of labels must be of the same size" );
108          }
109        tiles.addAll(list);
110        tileLabels.addAll(labels);
111        }
112
113        /**
114         * Get list of available Tiles
115         */

116      public List JavaDoc getTiles( )
117        {
118        return tiles;
119        }
120
121        /**
122         * Get list of labels for Tiles
123         */

124      public List JavaDoc getTilesLabels( )
125        {
126        return tileLabels;
127        }
128
129        /**
130         * Get label for specified Tile, identified by its key.
131         * @param key Tile key
132         */

133      public String JavaDoc getTileLabel( Object JavaDoc key )
134        {
135        int index = tiles.indexOf( key );
136        if(index==-1)
137          return null;
138        return (String JavaDoc)tileLabels.get(index);
139        }
140
141        /**
142         * Get list of labels for Tile keys
143         * @param keys List of keys to search for labels.
144         */

145      public List JavaDoc getTileLabels( List JavaDoc Keys )
146        {
147        List JavaDoc listLabels = new ArrayList JavaDoc();
148
149        Iterator JavaDoc i = Keys.iterator();
150        while(i.hasNext())
151          {
152          Object JavaDoc key = i.next();
153          listLabels.add( getTileLabel(key) );
154          } // end loop
155
return listLabels;
156        }
157
158        /**
159         * Get Tiles corresponding to keys.
160         * Keys are the one returned by the setting page. Keys are usually issue
161         * from values returned by getTiles().
162         * If a key isn't recognize, it is disguarded from the returned list.
163         * If a key correspond to a special key, appropriate 'definition' is created.
164         * Returned list contains tiles URL, definition name and definitions suitable
165         * as attribute of <tiles:insert >.
166         *
167         * @keys array of keys to add to list.
168         */

169      public List JavaDoc getTiles( String JavaDoc keys[] )
170        {
171        List JavaDoc list = new ArrayList JavaDoc();
172
173          // add keys to list
174
for(int i=0;i<keys.length;i++)
175          {
176          String JavaDoc key = keys[i];
177          if( key.indexOf( '@' )>0 )
178            { // special key
179
}
180          if( tiles.contains( key ) )
181            { // ok, add it
182
list.add( key );
183            }
184          } // end loop
185
return list;
186        }
187
188        /**
189         * Set labels for tiles Tiles.
190         */

191      protected void setTileLabels( List JavaDoc list)
192        {
193        this.tileLabels = list;
194        }
195        /**
196         * add list to list of tiles Tiles
197         */

198      protected void addTileLabels( List JavaDoc list)
199        {
200        if(tileLabels== null)
201          {
202          setTileLabels(list);
203          return;
204          }
205        tileLabels.addAll(list);
206        }
207
208 }
209
Popular Tags