001 /*
002 * Apache License
003 * Version 2.0, January 2004
004 * http://www.apache.org/licenses/
005 *
006 * Copyright 2008-2010 by chenillekit.org
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 */
014
015 package org.chenillekit.tapestry.core.components;
016
017 import org.apache.tapestry5.BindingConstants;
018 import org.apache.tapestry5.Block;
019 import org.apache.tapestry5.ClientElement;
020 import org.apache.tapestry5.ComponentResources;
021 import org.apache.tapestry5.MarkupWriter;
022 import org.apache.tapestry5.annotations.Component;
023 import org.apache.tapestry5.annotations.Environmental;
024 import org.apache.tapestry5.annotations.Import;
025 import org.apache.tapestry5.annotations.Parameter;
026 import org.apache.tapestry5.annotations.Property;
027 import org.apache.tapestry5.corelib.components.EventLink;
028 import org.apache.tapestry5.corelib.components.Loop;
029 import org.apache.tapestry5.corelib.components.Zone;
030 import org.apache.tapestry5.ioc.annotations.Inject;
031 import org.apache.tapestry5.services.ClientBehaviorSupport;
032 import org.apache.tapestry5.services.Request;
033 import org.apache.tapestry5.services.javascript.JavaScriptSupport;
034
035 import java.util.List;
036
037 /**
038 * Simple tab controlled panel component.
039 * <p/>
040 * This component looks in the container message resource for an entry like <em>label-panelid</em> for inserting as panel title.
041 * If key not found the panel id inserted instead.
042 *
043 * @version $Id: TabSet.java 674 2010-07-29 12:47:25Z homburgs $
044 */
045 @Import(library = {"../Chenillekit.js", "TabSet.js"}, stylesheet = {"TabSet.css"})
046 public class TabSet implements ClientElement
047 {
048 private static String EVENT_NAME = "clicked";
049
050 @Inject
051 private ComponentResources resources;
052
053 /**
054 * The id used to generate a page-unique client-side identifier for the component. If a component renders multiple
055 * times, a suffix will be appended to the to id to ensure uniqueness.
056 */
057 @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL)
058 private String clientId;
059
060 /**
061 * Name of a function on the client-side Tapestry.ElementEffect object that is invoked after the Zone's content has
062 * been updated. If not specified, then the basic "highlight" method is used, which performs a classic "yellow fade"
063 * to indicate to the user that and update has taken place.
064 */
065 @Parameter(defaultPrefix = BindingConstants.LITERAL)
066 private String update;
067
068 /**
069 * list of div id's (for each panel).
070 */
071 @Parameter(required = true, defaultPrefix = "list")
072 private List<String> panelIds;
073
074 /**
075 * set the panel with given id as activated.
076 */
077 @Parameter
078 private String activePanelId;
079
080 @Environmental
081 private JavaScriptSupport javascriptSupport;
082
083 @Environmental
084 private ClientBehaviorSupport clientBehaviorSupport;
085
086 @Inject
087 private Request request;
088
089 @Component(parameters = {"source=inherit:panelIds", "value=panelId"})
090 private Loop tabLoop;
091
092 @Component(parameters = {"update=inherit:update"})
093 private Zone contentZone;
094
095 @Component(parameters = {"event=clicked", "zone=contentZone", "context=panelId"})
096 private EventLink eventLink;
097
098 @Property
099 private String panelId;
100
101 private String assignedClientId;
102
103 void setupRender()
104 {
105 assignedClientId = javascriptSupport.allocateClientId(clientId);
106
107 if (activePanelId == null)
108 activePanelId = panelIds.get(0);
109 }
110
111 /**
112 * Tapestry render phase method. End a tag here.
113 *
114 * @param writer the markup writer
115 */
116 void afterRender(MarkupWriter writer)
117 {
118 javascriptSupport.addScript("new Ck.TabSet('%s', '%s');", getClientId() + "_panel", activePanelId);
119 }
120
121
122 public String getPanelTitle()
123 {
124 String panelTitle = panelId;
125
126 if (resources.getContainerResources().getMessages().contains("label-" + panelId))
127 panelTitle = resources.getContainerResources().getMessages().get("label-" + panelId);
128
129 return panelTitle;
130 }
131
132 /**
133 * activate the named block if page refreshed.
134 *
135 * @return corresponding block or an exception if named block not found.
136 */
137 public Block getInitialActiveBlock()
138 {
139 return resources.getContainer().getComponentResources().getBlock(activePanelId);
140 }
141
142 /**
143 * method get the clicked panel id and returns the corresponding block.
144 *
145 * @param panelId the panel id
146 *
147 * @return corresponding block or an exception if named block not found.
148 */
149 Object onClicked(String panelId)
150 {
151 activePanelId = panelId;
152 return resources.getContainer().getComponentResources().getBlock(panelId);
153 }
154
155 /**
156 * Returns a unique id for the element. This value will be unique for any given rendering of a
157 * page. This value is intended for use as the id attribute of the client-side element, and will
158 * be used with any DHTML/Ajax related JavaScript.
159 */
160 public String getClientId()
161 {
162 return assignedClientId;
163 }
164 }