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.script.ScriptContributionContainer;
027: import net.ar.webonswing.managers.styles.StyleContributionContainer;
028: import net.ar.webonswing.managers.templates.*;
029: import net.ar.webonswing.render.markup.*;
030: import net.ar.webonswing.render.templates.*;
031: import net.ar.webonswing.render.templates.html.*;
032: import net.ar.webonswing.swing.layouts.*;
033: import net.ar.webonswing.wrapping.*;
034:
035: public class MenuBarUIContributor extends
036: AbstractSwingComponentUIContributor {
037: public void doRenderingContribution(
038: RenderingContributionContainer theContribManager) {
039: JMenuBar aMenuBar = (JMenuBar) getJComponent();
040:
041: //TODO: revisar
042: if (!(aMenuBar.getLayout() instanceof TemplateLayout)) {
043: aMenuBar
044: .setLayout(new PropagateMenuTemplateLayout(
045: WosFramework
046: .getKeyPositionTemplateForName("WinMenu.JMenuBar")));
047: aMenuBar.doLayout();
048: }
049:
050: Template theTemplate = WosSwingHelper.getTemplateForComponent(
051: "JMenuBar", aMenuBar);
052:
053: StringBuffer theBarResult = new StringBuffer();
054: StringBuffer theScript = new StringBuffer();
055: StringBuffer theMenusInitScripts = new StringBuffer();
056:
057: Component[] theComponents = aMenuBar.getComponents();
058: for (int i = 0; i < theComponents.length; i++) {
059: JComponent theJMenu = (JComponent) theComponents[i];
060: VisualComponent theMenuComponent = WosFramework
061: .getInstance().getHierarchyWrapper()
062: .getComponentWrapper(theJMenu);
063:
064: String theJMenuName = theMenuComponent.getName();
065: String theTabName = theJMenuName + ".tab";
066:
067: HtmlTemplate theItemTemplate = getItemTemplate(theTemplate);
068: theItemTemplate.mergeFoundTagKeepingName(new Tag("span")
069: .addAttribute("id", theTabName));
070: theItemTemplate.addIdTagTemplateElement("aContent",
071: theContribManager.getContainerRenderer().render(
072: theMenuComponent), theContribManager
073: .getComponentIdTag(theMenuComponent));
074:
075: theBarResult.append(theItemTemplate.renderToText());
076: theBarResult.append(createSeparator(theTemplate,
077: theJMenuName));
078:
079: theScript
080: .append(".add("
081: + theContribManager
082: .getComponentInitScripts(theMenuComponent)[1]
083: + ")");
084:
085: theMenusInitScripts.append(theContribManager
086: .getComponentInitScripts(theMenuComponent)[0]);
087: }
088:
089: String theBegin = getBegin(theComponent.getName(), theTemplate);
090: String theEnd = getEnd(theComponent.getName(), theTemplate);
091: theTemplate.addElement(new IdTagTemplateElement("theItems",
092: new TextContent(theBegin + theBarResult + theEnd)));
093:
094: String theInitScript = "new JMenuBar('"
095: + theComponent.getName() + "')" + theScript + ";"
096: + theMenusInitScripts.toString();
097:
098: theContribManager.doContribution(theComponent, theTemplate,
099: null, theInitScript);
100: }
101:
102: private String createSeparator(Template theTemplate, String theName) {
103: HtmlTemplate theSeparatorLeftTemplate = HtmlTemplateManager
104: .getClonedSubTemplate(theTemplate,
105: "theItems.aSeparatorLeftUp");
106: HtmlTemplate theSeparatorTemplate = HtmlTemplateManager
107: .getClonedSubTemplate(theTemplate,
108: "theItems.aSeparatorDisabled");
109: HtmlTemplate theSeparatorRightTemplate = HtmlTemplateManager
110: .getClonedSubTemplate(theTemplate,
111: "theItems.aSeparatorRightDown");
112:
113: theSeparatorLeftTemplate.mergeFoundTag(new Tag("div")
114: .addAttribute("style", "display:none").addAttribute(
115: "id", theName + ".separator.left"));
116: theSeparatorRightTemplate.mergeFoundTag(new Tag("div")
117: .addAttribute("style", "display:none").addAttribute(
118: "id", theName + ".separator.right"));
119: theSeparatorTemplate.mergeFoundTag(new Tag("div").addAttribute(
120: "style", "display:block").addAttribute("id",
121: theName + ".separator"));
122:
123: HtmlTemplate theItemTemplate = getItemTemplate(theTemplate);
124: theItemTemplate.addIdTagTemplateElement("aContent",
125: theSeparatorLeftTemplate.renderToText()
126: + theSeparatorTemplate.renderToText()
127: + theSeparatorRightTemplate.renderToText());
128:
129: return theItemTemplate.renderToText();
130: }
131:
132: private HtmlTemplate getItemTemplate(Template theTemplate) {
133: return HtmlTemplateManager.getClonedSubTemplate(theTemplate,
134: "theItems.anItem");
135: }
136:
137: private String getBegin(String theMenuBarName, Template theTemplate) {
138: HtmlTemplate theMenuBarBeginDisabledTemplate = HtmlTemplateManager
139: .getClonedSubTemplate(theTemplate,
140: "theItems.aBeginDisabled");
141: theMenuBarBeginDisabledTemplate
142: .mergeFoundTag(new Tag("div").addAttribute("id",
143: theMenuBarName + ".begin.disabled"));
144: HtmlTemplate theMenuBarBeginEnabledTemplate = HtmlTemplateManager
145: .getClonedSubTemplate(theTemplate,
146: "theItems.aBeginEnabled");
147: theMenuBarBeginEnabledTemplate.mergeFoundTag(new Tag("div")
148: .addAttribute("id", theMenuBarName + ".begin.enabled"));
149: HtmlTemplate theItemTemplate = getItemTemplate(theTemplate);
150: theItemTemplate
151: .addIdTagTemplateElement("aContent",
152: theMenuBarBeginDisabledTemplate.renderToText()
153: + theMenuBarBeginEnabledTemplate
154: .renderToText());
155:
156: return theItemTemplate.renderToText();
157: }
158:
159: private String getEnd(String theMenuBarName, Template theTemplate) {
160: HtmlTemplate theMenuBarBeginDisabledTemplate = HtmlTemplateManager
161: .getClonedSubTemplate(theTemplate,
162: "theItems.anEndDisabled");
163: theMenuBarBeginDisabledTemplate.mergeFoundTag(new Tag("div")
164: .addAttribute("id", theMenuBarName + ".end.disabled"));
165: HtmlTemplate theMenuBarBeginEnabledTemplate = HtmlTemplateManager
166: .getClonedSubTemplate(theTemplate,
167: "theItems.anEndEnabled");
168: theMenuBarBeginEnabledTemplate.mergeFoundTag(new Tag("div")
169: .addAttribute("id", theMenuBarName + ".end.enabled"));
170: HtmlTemplate theItemTemplate = getItemTemplate(theTemplate);
171: theItemTemplate
172: .addIdTagTemplateElement("aContent",
173: theMenuBarBeginDisabledTemplate.renderToText()
174: + theMenuBarBeginEnabledTemplate
175: .renderToText());
176:
177: return theItemTemplate.renderToText();
178: }
179:
180: public void installUI(JComponent aComponent) {
181: //TODO: no esta el default
182: JMenuBar theMenuBar = (JMenuBar) aComponent;
183:
184: if (!(theMenuBar.getLayout() instanceof TemplateLayout))
185: theMenuBar
186: .setLayout(new PropagateMenuTemplateLayout(
187: WosFramework
188: .getKeyPositionTemplateForName("WinMenu.JMenuBar")));
189: }
190:
191: public void doScriptContribution(
192: ScriptContributionContainer aContributionManager) {
193: aContributionManager.addInclude(WosFramework.getInstance()
194: .getCompleteResourcePath()
195: + "/js/JMenu.js");
196: }
197:
198: public void doStyleContribution(
199: StyleContributionContainer aContributionManager) {
200: aContributionManager.addInclude(WosFramework.getInstance()
201: .getCompleteResourcePath()
202: + "/styles/winmenu.css");
203: }
204:
205: }
|