001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.ui;
019:
020: import java.awt.*;
021: import java.util.*;
022: import java.util.List;
023:
024: import javax.swing.*;
025:
026: import net.ar.webonswing.*;
027: import net.ar.webonswing.helpers.*;
028: import net.ar.webonswing.managers.script.*;
029: import net.ar.webonswing.managers.templates.*;
030: import net.ar.webonswing.remote.*;
031: import net.ar.webonswing.render.*;
032: import net.ar.webonswing.render.markup.*;
033: import net.ar.webonswing.render.templates.*;
034: import net.ar.webonswing.render.templates.html.*;
035: import net.ar.webonswing.wrapping.*;
036: import net.ar.webonswing.wrapping.swing.*;
037:
038: import org.apache.commons.logging.*;
039:
040: public class TabbedPaneUIContributor extends
041: AbstractSwingComponentUIContributor {
042: StringBuffer theScript = new StringBuffer();
043:
044: public void doRenderingContribution(
045: RenderingContributionContainer theContribManager) {
046: try {
047: JTabbedPane theTabbedPane = (JTabbedPane) getJComponent();
048:
049: Template theTemplate = WosSwingHelper
050: .getTemplateForComponent("JTabbedPane",
051: theTabbedPane);
052: HtmlTemplate theMainTemplate = HtmlTemplateManager
053: .getSubTemplate(theTemplate, "theMainTag");
054:
055: Tag theMainTag = new Tag("div");
056:
057: if (theTabbedPane.getWidth() + theTabbedPane.getHeight() > 0)
058: theMainTag
059: .addAttribute(new TagStyleAttribute()
060: .addElement("width",
061: theTabbedPane.getWidth())
062: .addElement("height",
063: theTabbedPane.getHeight()));
064:
065: JComponent theSelectedPane = (JComponent) theTabbedPane
066: .getComponentAt(theTabbedPane.getSelectedIndex());
067:
068: theMainTag
069: .getTheMarkupContainer()
070: .addElement(
071: new Tag("input")
072: .setNeedsClosure(false)
073: .addAttribute(
074: new TagAttribute("type",
075: "hidden"))
076: .addAttribute(
077: new TagAttribute("name",
078: theComponent
079: .getName()))
080: .addAttribute(
081: new TagAttribute(
082: "value",
083: WosFramework
084: .getInstance()
085: .getHierarchyWrapper()
086: .getComponentWrapper(
087: theSelectedPane)
088: .getName())));
089: theMainTemplate.mergeFoundTag(theMainTag);
090:
091: createTabPane(theContribManager, theTabbedPane,
092: theTemplate, theMainTemplate);
093:
094: theContribManager.doContribution(theComponent,
095: theMainTemplate, theMainTag, getInitScript());
096: } catch (Exception e) {
097: LogFactory.getLog(TabbedPaneUIContributor.class).error("",
098: e);
099: }
100: }
101:
102: private StringBuffer createTabPane(
103: RenderingContributionContainer theContribManager,
104: JTabbedPane theTabbedPane, Template theTemplate,
105: HtmlTemplate theMainTemplate)
106: throws CloneNotSupportedException {
107: HtmlTemplate theTabPaneTemplate = HtmlTemplateManager
108: .getSubTemplate(theTemplate, "theMainTag.theTabPane");
109:
110: StringBuffer theResult = new StringBuffer();
111: theScript = new StringBuffer();
112: HtmlTemplate theTabTemplate = HtmlTemplateManager
113: .getSubTemplate(theTemplate, "theMainTag.theBar.theTab");
114: StringBuffer theTabsResult = new StringBuffer();
115: Component[] theComponents = theTabbedPane.getComponents();
116:
117: for (int i = 0; i < theComponents.length; i++) {
118: Container theSubComponent = (Container) theComponents[i];
119: VisualComponent theTabComponent = WosFramework
120: .getInstance().getHierarchyWrapper()
121: .getComponentWrapper(theSubComponent);
122:
123: if (theSubComponent.isVisible()
124: || theSubComponent instanceof JPanel) {
125: Tag theTabPaneTag = new Tag("div");
126: theTabPaneTag.addAttribute(new TagAttribute("id",
127: theTabComponent.getName()));
128: TagStyleAttribute theStyle = new TagStyleAttribute();
129: theStyle
130: .addElement("width", theSubComponent.getWidth())
131: .addElement("height", "100%").addElement(
132: "left", 2).addElement("top", "20")
133: .addElement("position", "absolute");
134: if (!theSubComponent.isVisible())
135: theStyle.addElement("display", "none");
136: theTabPaneTag.addAttribute(theStyle);
137:
138: HtmlTemplate theClonedTabPaneTemplate = (HtmlTemplate) theTabPaneTemplate
139: .clone();
140: theClonedTabPaneTemplate.mergeFoundTag(theTabPaneTag);
141: theClonedTabPaneTemplate
142: .addElement(new IdTagTemplateElement(
143: "theContent",
144: new TagContent(
145: new ContentRenderer(
146: theContribManager
147: .getContainerRenderer()
148: .render(
149: theTabComponent))
150: .getResult())));
151:
152: theResult.append(new TextTemplateRenderer(
153: theClonedTabPaneTemplate).getResult());
154: theScript.append(theContribManager
155: .getComponentInitScripts(theTabComponent)[0]);
156:
157: Tag theTabTag = new Tag("div");
158: theTabTag.addAttribute(new TagAttribute("onclick",
159: "getElement('" + theComponent.getName()
160: + "').value+='-"
161: + theTabComponent.getName()
162: + "'; ed.dispatch(new ChangeEvent('"
163: + theComponent.getName() + "'));"));
164: theTabTag
165: .addAttribute(new TagAttribute(
166: "class",
167: theTabbedPane.getSelectedIndex() == i ? "tabDown"
168: : "tabUp"));
169: theTabTag.addAttribute(new TagAttribute("id",
170: theComponent.getName() + "_"
171: + theTabComponent.getName()));
172:
173: HtmlTemplate theClonedTabTemplate = (HtmlTemplate) theTabTemplate
174: .clone();
175: theClonedTabTemplate.mergeFoundTag(theTabTag);
176: theClonedTabTemplate
177: .addElement(new IdTagTemplateElement(
178: "theContent", new TagContent(
179: theTabbedPane.getTitleAt(i))));
180:
181: theTabsResult.append(new TextTemplateRenderer(
182: theClonedTabTemplate).getResult());
183:
184: }
185: }
186:
187: theMainTemplate.addElement(new IdTagTemplateElement("theBar",
188: new TextContent(theTabsResult)));
189: theMainTemplate.addElement(new IdTagTemplateElement(
190: "theTabPane", new TextContent(theResult), null));
191:
192: return theResult;
193: }
194:
195: public void dispatchEvents(List anEvents) {
196: for (Iterator i = anEvents.iterator(); i.hasNext();) {
197: RemoteEvent theEvent = (RemoteEvent) i.next();
198: JTabbedPane theTabbedPane = (JTabbedPane) ((ComponentWrapper) theEvent
199: .getSource()).getWrappedComponent();
200:
201: if (theEvent.getType().equals("stateChanged")
202: || theEvent.getType().equals("update")) {
203: String theTab = (String) theEvent.getParameters()[0];
204:
205: SwingComponentWrapper theTabComponent = (SwingComponentWrapper) WosFramework
206: .getInstance().getHierarchyWrapper()
207: .getComponentWrapper(theTabbedPane)
208: .getTopParent().findComponent(theTab);
209:
210: theTabbedPane.setSelectedComponent(theTabComponent
211: .getJComponent());
212: }
213: }
214: }
215:
216: public String getInitScript() {
217: String theComponentName = theComponent.getName();
218: // theScriptTag.getTheMarkupContainer().addAll(RemoteHelper.getListenersAdds(theComponent));
219: return theScript.toString() + "getComponent('"
220: + theComponentName
221: + "').addListener(new TabbedPaneChangeListener());";
222: }
223:
224: public void doScriptContribution(
225: ScriptContributionContainer aContributionManager) {
226: aContributionManager.addInclude(WosFramework.getInstance()
227: .getCompleteResourcePath()
228: + "/js/JTabbedPane.js");
229: }
230: }
|