| 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 | 0 | 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 | 0 | return assignedId; |
| 80 | |
} |
| 81 | |
|
| 82 | |
public int getIndex() |
| 83 | |
{ |
| 84 | 0 | return index; |
| 85 | |
} |
| 86 | |
|
| 87 | |
public void setIndex( int index ) |
| 88 | |
{ |
| 89 | 0 | this.index = index; |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
public TwoBlocks getPanel() |
| 93 | |
{ |
| 94 | 0 | return panel; |
| 95 | |
} |
| 96 | |
|
| 97 | |
public void setPanel( TwoBlocks panel ) |
| 98 | |
{ |
| 99 | 0 | this.panel = panel; |
| 100 | 0 | } |
| 101 | |
|
| 102 | |
public Block getTitleIcon() |
| 103 | |
{ |
| 104 | 0 | return titleIcon; |
| 105 | |
} |
| 106 | |
|
| 107 | |
public void setTitleIcon( Block titleIcon ) |
| 108 | |
{ |
| 109 | 0 | this.titleIcon = titleIcon; |
| 110 | 0 | } |
| 111 | |
|
| 112 | |
public List<TwoBlocks> getPanels() |
| 113 | |
{ |
| 114 | 0 | panelCount = 0; |
| 115 | 0 | List<TwoBlocks> list = new ArrayList<TwoBlocks>(); |
| 116 | 0 | for ( int i = 0; i < MAX_PANELS ; i++ ) |
| 117 | |
{ |
| 118 | 0 | String index = Integer.toString( i + 1 ); |
| 119 | 0 | if ( resources.isBound( "title" + index ) && resources.isBound( "content" + index ) ) |
| 120 | |
{ |
| 121 | 0 | list.add( new TwoBlocks( i + 1 ) ); |
| 122 | |
} |
| 123 | |
} |
| 124 | 0 | if ( exclude != null ) |
| 125 | |
{ |
| 126 | 0 | if ( exclude >= 0 ) |
| 127 | |
{ |
| 128 | 0 | if ( list.size() > 0 && exclude < list.size() ) |
| 129 | |
{ |
| 130 | 0 | list.remove( exclude.intValue() ); |
| 131 | |
} |
| 132 | |
} |
| 133 | |
else |
| 134 | |
{ |
| 135 | 0 | int lastIndex = list.size() + ( exclude ); |
| 136 | 0 | if ( lastIndex >= 0 && lastIndex < list.size() ) |
| 137 | |
{ |
| 138 | 0 | list = new ArrayList<TwoBlocks>( list.subList( 0, lastIndex ) ); |
| 139 | |
} |
| 140 | |
} |
| 141 | |
} |
| 142 | 0 | panelCount = list.size(); |
| 143 | 0 | return list; |
| 144 | |
} |
| 145 | |
|
| 146 | |
@SetupRender |
| 147 | |
protected final void setupRender() |
| 148 | |
{ |
| 149 | 0 | assignedId = renderSupport.allocateClientId( resources ); |
| 150 | 0 | Cookie[] cookies = requestGlobals.getHTTPServletRequest().getCookies(); |
| 151 | 0 | if ( cookies != null ) |
| 152 | |
{ |
| 153 | 0 | for ( int i = 0; i < cookies.length; i++ ) |
| 154 | |
{ |
| 155 | 0 | if ( cookies[ i ].getName().equalsIgnoreCase( "active_index_" + assignedId ) ) |
| 156 | |
{ |
| 157 | 0 | String val = cookies[ i ].getValue(); |
| 158 | 0 | if ( val != null && val.trim().length() > 0 ) |
| 159 | |
{ |
| 160 | |
try |
| 161 | |
{ |
| 162 | 0 | activeIndex = Integer.parseInt( val ); |
| 163 | |
} |
| 164 | 0 | catch ( Exception e ) |
| 165 | |
{ |
| 166 | 0 | logger.warn( "Failed to parse cookie with active index: " + val ); |
| 167 | 0 | } |
| 168 | |
} |
| 169 | |
break; |
| 170 | |
} |
| 171 | |
} |
| 172 | |
} |
| 173 | 0 | if ( activeIndex == 0 ) |
| 174 | |
{ |
| 175 | 0 | activeIndex = 1; |
| 176 | |
} |
| 177 | 0 | } |
| 178 | |
|
| 179 | |
public int getPanelCount() |
| 180 | |
{ |
| 181 | 0 | return panelCount; |
| 182 | |
} |
| 183 | |
|
| 184 | 0 | public class TwoBlocks implements Serializable |
| 185 | |
{ |
| 186 | |
private Block title, content; |
| 187 | |
private boolean active; |
| 188 | |
|
| 189 | |
public TwoBlocks( int index ) |
| 190 | 0 | { |
| 191 | 0 | active = ( activeIndex == index ); |
| 192 | 0 | title = resources.getBlockParameter( "title" + index ); |
| 193 | 0 | content = resources.getBlockParameter( "content" + index ); |
| 194 | 0 | } |
| 195 | |
|
| 196 | |
public Block getTitle() |
| 197 | |
{ |
| 198 | 0 | return title; |
| 199 | |
} |
| 200 | |
|
| 201 | |
public void setTitle( Block title ) |
| 202 | |
{ |
| 203 | 0 | this.title = title; |
| 204 | 0 | } |
| 205 | |
|
| 206 | |
public Block getContent() |
| 207 | |
{ |
| 208 | 0 | return content; |
| 209 | |
} |
| 210 | |
|
| 211 | |
public void setContent( Block content ) |
| 212 | |
{ |
| 213 | 0 | this.content = content; |
| 214 | 0 | } |
| 215 | |
|
| 216 | |
public boolean isActive() |
| 217 | |
{ |
| 218 | 0 | return active; |
| 219 | |
} |
| 220 | |
|
| 221 | |
public void setActive( boolean active ) |
| 222 | |
{ |
| 223 | 0 | this.active = active; |
| 224 | 0 | } |
| 225 | |
} |
| 226 | |
} |