1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.equanda.tapestry5.base;
26
27 import org.apache.tapestry5.Block;
28 import org.apache.tapestry5.ComponentResources;
29 import org.apache.tapestry5.RenderSupport;
30 import org.apache.tapestry5.annotations.Environmental;
31 import org.apache.tapestry5.annotations.Parameter;
32 import org.apache.tapestry5.annotations.SetupRender;
33 import org.apache.tapestry5.annotations.SupportsInformalParameters;
34 import org.apache.tapestry5.ioc.annotations.Inject;
35 import org.apache.tapestry5.services.RequestGlobals;
36 import org.slf4j.Logger;
37
38 import javax.servlet.http.Cookie;
39 import java.io.Serializable;
40 import java.util.ArrayList;
41 import java.util.List;
42
43
44
45
46
47
48 @SupportsInformalParameters
49 public class TitleContent
50 {
51 private static final int MAX_PANELS = 50;
52
53 @Environmental
54 private RenderSupport renderSupport;
55
56 @Inject
57 private ComponentResources resources;
58
59 @Parameter
60 private Block titleIcon;
61
62 @Parameter
63 private Integer exclude;
64
65 @Inject
66 private RequestGlobals requestGlobals;
67
68 @Inject
69 private Logger logger;
70
71 private TwoBlocks panel;
72 private int index;
73 private int panelCount;
74 private String assignedId;
75 private int activeIndex;
76
77 public String getId()
78 {
79 return assignedId;
80 }
81
82 public int getIndex()
83 {
84 return index;
85 }
86
87 public void setIndex( int index )
88 {
89 this.index = index;
90 }
91
92 public TwoBlocks getPanel()
93 {
94 return panel;
95 }
96
97 public void setPanel( TwoBlocks panel )
98 {
99 this.panel = panel;
100 }
101
102 public Block getTitleIcon()
103 {
104 return titleIcon;
105 }
106
107 public void setTitleIcon( Block titleIcon )
108 {
109 this.titleIcon = titleIcon;
110 }
111
112 public List<TwoBlocks> getPanels()
113 {
114 panelCount = 0;
115 List<TwoBlocks> list = new ArrayList<TwoBlocks>();
116 for ( int i = 0; i < MAX_PANELS ; i++ )
117 {
118 String index = Integer.toString( i + 1 );
119 if ( resources.isBound( "title" + index ) && resources.isBound( "content" + index ) )
120 {
121 list.add( new TwoBlocks( i + 1 ) );
122 }
123 }
124 if ( exclude != null )
125 {
126 if ( exclude >= 0 )
127 {
128 if ( list.size() > 0 && exclude < list.size() )
129 {
130 list.remove( exclude.intValue() );
131 }
132 }
133 else
134 {
135 int lastIndex = list.size() + ( exclude );
136 if ( lastIndex >= 0 && lastIndex < list.size() )
137 {
138 list = new ArrayList<TwoBlocks>( list.subList( 0, lastIndex ) );
139 }
140 }
141 }
142 panelCount = list.size();
143 return list;
144 }
145
146 @SetupRender
147 protected final void setupRender()
148 {
149 assignedId = renderSupport.allocateClientId( resources );
150 Cookie[] cookies = requestGlobals.getHTTPServletRequest().getCookies();
151 if ( cookies != null )
152 {
153 for ( int i = 0; i < cookies.length; i++ )
154 {
155 if ( cookies[ i ].getName().equalsIgnoreCase( "active_index_" + assignedId ) )
156 {
157 String val = cookies[ i ].getValue();
158 if ( val != null && val.trim().length() > 0 )
159 {
160 try
161 {
162 activeIndex = Integer.parseInt( val );
163 }
164 catch ( Exception e )
165 {
166 logger.warn( "Failed to parse cookie with active index: " + val );
167 }
168 }
169 break;
170 }
171 }
172 }
173 if ( activeIndex == 0 )
174 {
175 activeIndex = 1;
176 }
177 }
178
179 public int getPanelCount()
180 {
181 return panelCount;
182 }
183
184 public class TwoBlocks implements Serializable
185 {
186 private Block title, content;
187 private boolean active;
188
189 public TwoBlocks( int index )
190 {
191 active = ( activeIndex == index );
192 title = resources.getBlockParameter( "title" + index );
193 content = resources.getBlockParameter( "content" + index );
194 }
195
196 public Block getTitle()
197 {
198 return title;
199 }
200
201 public void setTitle( Block title )
202 {
203 this.title = title;
204 }
205
206 public Block getContent()
207 {
208 return content;
209 }
210
211 public void setContent( Block content )
212 {
213 this.content = content;
214 }
215
216 public boolean isActive()
217 {
218 return active;
219 }
220
221 public void setActive( boolean active )
222 {
223 this.active = active;
224 }
225 }
226 }