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:
022: import javax.swing.*;
023:
024: import net.ar.webonswing.*;
025: import net.ar.webonswing.helpers.*;
026: import net.ar.webonswing.managers.templates.*;
027: import net.ar.webonswing.render.*;
028: import net.ar.webonswing.render.markup.*;
029: import net.ar.webonswing.render.templates.*;
030: import net.ar.webonswing.render.templates.html.*;
031: import net.ar.webonswing.wrapping.*;
032:
033: public class PopupMenuUIContributor extends
034: AbstractSwingComponentUIContributor {
035: public void doRenderingContribution(
036: RenderingContributionContainer theContribManager) {
037: JPopupMenu aPopupMenu = (JPopupMenu) getJComponent();
038:
039: Template theTemplate = WosSwingHelper.getTemplateForComponent(
040: "JPopupMenu", aPopupMenu);
041: HtmlTemplate theMainTemplate = HtmlTemplateManager
042: .getSubTemplate(theTemplate, "thePopupMenu");
043:
044: if (!theComponent.getName().equals("")) {
045: Tag theMainTag = new Tag("span").addAttribute("id",
046: theComponent.getName());
047: theMainTemplate.mergeFoundTagKeepingName(theMainTag);
048: }
049:
050: StringBuffer theResult = new StringBuffer();
051: StringBuffer theScript = new StringBuffer("new JPopupMenu('"
052: + theComponent.getName() + "')");
053: StringBuffer theMenuItemsInitScripts = new StringBuffer();
054:
055: Component[] theComponents = aPopupMenu.getComponents();
056: for (int i = 0; i < theComponents.length; i++) {
057: VisualComponent theMenuItemComponent = WosFramework
058: .getInstance().getHierarchyWrapper()
059: .getComponentWrapper(theComponents[i]);
060:
061: String theItemName = theMenuItemComponent.getName()
062: + ".tab";
063:
064: HtmlTemplate theItemTemplate = HtmlTemplateManager
065: .getClonedSubTemplate(theTemplate,
066: "thePopupMenu.theMenuItem.anItem");
067: theItemTemplate.mergeFoundTagKeepingName(new Tag("span")
068: .addAttribute("id", theItemName));
069: theItemTemplate.addIdTagTemplateElement("aContent",
070: new ContentRenderer(theContribManager
071: .getContainerRenderer().render(
072: theMenuItemComponent)).getResult()
073: .toString());
074:
075: theResult.append(theItemTemplate.renderToText());
076: theResult.append(createSeparator(theTemplate,
077: theMenuItemComponent.getName()));
078:
079: String theMenuItemScript = theContribManager
080: .getComponentInitScripts(theMenuItemComponent)[1];
081: theMenuItemsInitScripts.append(theContribManager
082: .getComponentInitScripts(theMenuItemComponent)[0]);
083:
084: if (theMenuItemScript.length() > 0)
085: theScript.append(".add(" + theMenuItemScript + ")");
086: }
087:
088: String theBegin = getBegin(theComponent.getName(), theTemplate);
089: String theEnd = getEnd(theComponent.getName(), theTemplate);
090:
091: theMainTemplate.addIdTagTemplateElement("theMenuItem",
092: new TextContent(theBegin + theResult + theEnd));
093:
094: theContribManager.doContribution(theComponent, theTemplate,
095: null, new String[] { theScript.toString(),
096: theMenuItemsInitScripts.toString() });
097: }
098:
099: private String createSeparator(Template theTemplate, String theName) {
100: HtmlTemplate theSeparatorLeftTemplate = HtmlTemplateManager
101: .getClonedSubTemplate(theTemplate,
102: "thePopupMenu.theMenuItem.aSeparatorLeftUp");
103: HtmlTemplate theSeparatorTemplate = HtmlTemplateManager
104: .getClonedSubTemplate(theTemplate,
105: "thePopupMenu.theMenuItem.aSeparatorDisabled");
106: HtmlTemplate theSeparatorRightTemplate = HtmlTemplateManager
107: .getClonedSubTemplate(theTemplate,
108: "thePopupMenu.theMenuItem.aSeparatorRightDown");
109:
110: theSeparatorLeftTemplate.mergeFoundTag(new Tag("div")
111: .addAttribute("style", "display:none").addAttribute(
112: "id", theName + ".separator.left"));
113: theSeparatorRightTemplate.mergeFoundTag(new Tag("div")
114: .addAttribute("style", "display:none").addAttribute(
115: "id", theName + ".separator.right"));
116: theSeparatorTemplate.mergeFoundTag(new Tag("div").addAttribute(
117: "style", "display:block").addAttribute("id",
118: theName + ".separator"));
119:
120: HtmlTemplate theItemTemplate = getItemTemplate(theTemplate);
121: theItemTemplate.addIdTagTemplateElement("aContent",
122: theSeparatorLeftTemplate.renderToText()
123: + theSeparatorTemplate.renderToText()
124: + theSeparatorRightTemplate.renderToText());
125:
126: return theItemTemplate.renderToText();
127: }
128:
129: private HtmlTemplate getItemTemplate(Template theTemplate) {
130: return HtmlTemplateManager.getClonedSubTemplate(theTemplate,
131: "thePopupMenu.theMenuItem.anItem");
132: }
133:
134: private String getBegin(String theMenuName, Template theTemplate) {
135: HtmlTemplate theMenuBarBeginDisabledTemplate = HtmlTemplateManager
136: .getClonedSubTemplate(theTemplate,
137: "thePopupMenu.theMenuItem.aBeginDisabled");
138: theMenuBarBeginDisabledTemplate.mergeFoundTag(new Tag("div")
139: .addAttribute("id", theMenuName + ".begin.disabled"));
140: HtmlTemplate theMenuBarBeginEnabledTemplate = HtmlTemplateManager
141: .getClonedSubTemplate(theTemplate,
142: "thePopupMenu.theMenuItem.aBeginEnabled");
143: theMenuBarBeginEnabledTemplate.mergeFoundTag(new Tag("div")
144: .addAttribute("id", theMenuName + ".begin.enabled"));
145: HtmlTemplate theItemTemplate = getItemTemplate(theTemplate);
146: theItemTemplate
147: .addIdTagTemplateElement("aContent",
148: theMenuBarBeginDisabledTemplate.renderToText()
149: + theMenuBarBeginEnabledTemplate
150: .renderToText());
151:
152: return theItemTemplate.renderToText();
153: }
154:
155: private String getEnd(String theMenuName, Template theTemplate) {
156: HtmlTemplate theMenuBarBeginDisabledTemplate = HtmlTemplateManager
157: .getClonedSubTemplate(theTemplate,
158: "thePopupMenu.theMenuItem.anEndDisabled");
159: theMenuBarBeginDisabledTemplate.mergeFoundTag(new Tag("div")
160: .addAttribute("id", theMenuName + ".end.disabled"));
161: HtmlTemplate theMenuBarBeginEnabledTemplate = HtmlTemplateManager
162: .getClonedSubTemplate(theTemplate,
163: "thePopupMenu.theMenuItem.anEndEnabled");
164: theMenuBarBeginEnabledTemplate.mergeFoundTag(new Tag("div")
165: .addAttribute("id", theMenuName + ".end.enabled"));
166: HtmlTemplate theItemTemplate = getItemTemplate(theTemplate);
167: theItemTemplate
168: .addIdTagTemplateElement("aContent",
169: theMenuBarBeginDisabledTemplate.renderToText()
170: + theMenuBarBeginEnabledTemplate
171: .renderToText());
172:
173: return theItemTemplate.renderToText();
174: }
175: }
|