1 /**
2 * This file is part of the equanda project.
3 *
4 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7 *
8 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
9 * ANY KIND, either express or implied. See the License for the specific language governing rights and
10 * limitations under the License.
11 *
12 * Alternatively, the contents of this file may be used under the terms of
13 * either the GNU General Public License Version 2 or later (the "GPL"), or
14 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
15 * in which case the provisions of the GPL or the LGPL are applicable instead
16 * of those above. If you wish to allow use of your version of this file only
17 * under the terms of either the GPL or the LGPL, and not to allow others to
18 * use your version of this file under the terms of the MPL, indicate your
19 * decision by deleting the provisions above and replace them with the notice
20 * and other provisions required by the GPL or the LGPL. If you do not delete
21 * the provisions above, a recipient may use your version of this file under
22 * the terms of any one of the MPL, the GPL or the LGPL.
23 */
24
25 package org.equanda.tapestry5.components;
26
27 import org.apache.tapestry5.ComponentResources;
28 import org.apache.tapestry5.Link;
29 import org.apache.tapestry5.RenderSupport;
30 import org.apache.tapestry5.annotations.AfterRender;
31 import org.apache.tapestry5.annotations.Environmental;
32 import org.apache.tapestry5.annotations.Persist;
33 import org.apache.tapestry5.annotations.SupportsInformalParameters;
34 import org.apache.tapestry5.ioc.annotations.Inject;
35 import org.apache.tapestry5.json.JSONObject;
36
37 /**
38 * Tabs component that overrides the management of remembering current tab. The parent uses cookies,
39 * this class uses AJAX technology and a persistent field.
40 *
41 * @author <a href="mailto:geert2@blackway.be">Geert Mergan</a>
42 */
43 @SupportsInformalParameters
44 public class MemTabs
45 extends Tabs
46 {
47 @Persist
48 private int persistedActiveIndex;
49
50 @Environmental
51 private RenderSupport renderSupport;
52
53 @Inject
54 private ComponentResources resources;
55
56 public boolean isUsingCookies()
57 {
58 return false;
59 }
60
61 protected int getActiveIndex()
62 {
63 return persistedActiveIndex;
64 }
65
66 protected void setActiveIndex( int activeIndex )
67 {
68 this.persistedActiveIndex = activeIndex;
69 }
70
71
72 public JSONObject onTitleSelected( int tabIndex )
73 {
74 setActiveIndex( tabIndex );
75 return new JSONObject().append( "content", JSONObject.NULL );
76 }
77
78 @AfterRender
79 void afterRender()
80 {
81 for ( int i = 0; i < getPanelCount() ; i++ )
82 {
83 Link link = resources.createEventLink( "TitleSelected", i + 1 );
84 StringBuilder tabId = new StringBuilder();
85 tabId.append( getId() );
86 tabId.append( "t" );
87 tabId.append( i );
88 renderSupport.addScript(
89 "Event.observe( $('%s'), 'click', function myclick( event ) { new Ajax.Request( '%s'); } );",
90 tabId.toString(), link.toAbsoluteURI() );
91 }
92 }
93 }
94