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.event.*;
021:
022: import javax.swing.*;
023: import javax.swing.event.*;
024:
025: import net.ar.webonswing.*;
026: import net.ar.webonswing.helpers.*;
027: import net.ar.webonswing.managers.templates.*;
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.walkers.*;
032: import net.ar.webonswing.wrapping.*;
033:
034: public class MenuUIContributor extends
035: AbstractSwingComponentUIContributor {
036: public void doRenderingContribution(
037: RenderingContributionContainer theContribManager) {
038: JMenu theMenu = (JMenu) getJComponent();
039: String theMenuName = theComponent.getName();
040:
041: Template theTemplate = WosSwingHelper.getTemplateForComponent(
042: "JMenu", theMenu);
043:
044: removeChangeListener(theMenu);
045:
046: Tag theTag = new Tag("span").addElementToContainer(
047: new TagContent(theMenu.getText())).addAttribute(
048: new TagAttribute("name", theMenuName));
049: if (theMenu.getListeners(ActionListener.class).length > 0)
050: theTag.addAttribute(new TagAttribute("onclick",
051: "ed.dispatch(new ActionEvent(this.name, ''));"));
052:
053: HtmlTemplate theMenuDisabledTemplate = HtmlTemplateManager
054: .getSubTemplate(theTemplate, "aMenuDisabled");
055: theMenuDisabledTemplate.mergeFoundTag(new Tag("div")
056: .addAttribute("id", theMenuName + ".disabled"));
057: theMenuDisabledTemplate
058: .addIdTagTemplateElement("aText", theTag);
059: HtmlTemplate theMenuEnabledTemplate = HtmlTemplateManager
060: .getSubTemplate(theTemplate, "aMenuEnabled");
061: theMenuEnabledTemplate.mergeFoundTag(new Tag("div")
062: .addAttribute("id", theMenuName + ".enabled")
063: .addAttribute("style", "display:none"));
064: theMenuEnabledTemplate.addIdTagTemplateElement("aText", theTag);
065:
066: JPopupMenu thePopup = theMenu.getPopupMenu();
067:
068: VisualComponent thePopupComponent = WosFramework.getInstance()
069: .getHierarchyWrapper().getComponentWrapper(thePopup);
070:
071: HtmlTemplate thePopupTemplate = (HtmlTemplate) theContribManager
072: .getContainerRenderer().render(thePopupComponent);
073:
074: setIcon(theMenu, theMenuDisabledTemplate,
075: theMenuEnabledTemplate);
076:
077: //TODO ver lo de la posicion Y
078:
079: Tag theDiv = new Tag("div").addAttribute(
080: new TagAttribute("id", theMenuName)).addAttribute(
081: new TagStyleAttribute().addElement("position",
082: "absolute").addElement("display", "none")
083: .addElement("left", theMenu.getX()).addElement(
084: "top", 19).addElement("width",
085: theMenu.getWidth()));
086: thePopupTemplate.setFoundTag(theDiv);
087:
088: String theResult = theMenuDisabledTemplate.renderToText()
089: + theMenuEnabledTemplate.renderToText()
090: + thePopupTemplate.renderToText();
091:
092: theContribManager
093: .doContribution(
094: theComponent,
095: new TextContent(theResult),
096: null,
097: new String[] {
098: theContribManager
099: .getComponentInitScripts(thePopupComponent)[1],
100: "new JMenu('"
101: + theMenuName
102: + "').add("
103: + theContribManager
104: .getComponentInitScripts(thePopupComponent)[0]
105: + ")" });
106: }
107:
108: private void setIcon(JMenu theMenu,
109: HtmlTemplate theMenuDisabledTemplate,
110: HtmlTemplate theMenuEnabledTemplate) {
111: Icon theIcon = theMenu.getIcon();
112: Tag theIconTag = null;
113:
114: if (theIcon != null)
115: theIconTag = new Tag("img").addAttribute("src",
116: WosFramework.getInstance().getContextPath() + "/"
117: + ((ImageIcon) theIcon).getDescription());
118:
119: HtmlTemplate theIconElement = HtmlTemplateManager
120: .getSubTemplate(theMenuDisabledTemplate, "theIcon");
121: if (theIconElement != null)
122: theIconElement.setFoundTag(theIconTag);
123:
124: theIconElement = HtmlTemplateManager.getSubTemplate(
125: theMenuEnabledTemplate, "theIcon");
126: if (theIconElement != null)
127: theIconElement.setFoundTag(theIconTag);
128: }
129:
130: private void removeChangeListener(JMenuItem theMenuItem) {
131: ChangeListener[] theChangeListeners = (ChangeListener[]) theMenuItem
132: .getListeners(ChangeListener.class);
133: if (theChangeListeners.length > 0)
134: theMenuItem.removeChangeListener(theChangeListeners[0]);
135: }
136:
137: public VisitableContainer getContainerVisitable() {
138: return new JMenuContainerVisitable();
139: }
140: }
|