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.remote.*;
029: import net.ar.webonswing.render.markup.*;
030: import net.ar.webonswing.render.templates.html.*;
031: import net.ar.webonswing.swing.layouts.*;
032: import net.ar.webonswing.wrapping.*;
033:
034: public class LabelUIContributor extends
035: AbstractSwingComponentUIContributor {
036: public void doRenderingContribution(
037: RenderingContributionContainer theContribManager) {
038: JLabel aLabel = (JLabel) getJComponent();
039:
040: RemoteHelper.removeToolTipListeners(aLabel);
041:
042: String theComponentName = theComponent.getName();
043: HtmlTemplate theTemplate = (HtmlTemplate) WosSwingHelper
044: .getTemplateForComponent("JLabel", aLabel);
045: String theAlign = "left";
046:
047: Tag theAnchorTag = null;
048: Tag theImageTag = null;
049: Tag theIdTag = null;
050: if (aLabel.getText() != null) {
051: theAnchorTag = new Tag("span");
052: theAnchorTag.addAttribute(new TagAttribute("id",
053: theComponentName));
054: theAnchorTag.addElementToContainer(new TagContent(aLabel
055: .getText()));
056: Color theBackGroundColor = aLabel.getBackground();
057:
058: if (theBackGroundColor != null
059: && !theBackGroundColor.equals(UIManager
060: .getColor("Label.background")))
061: theAnchorTag
062: .addAttribute(new TagStyleAttribute()
063: .addElement(
064: "BACKGROUND-COLOR",
065: "#"
066: + WosSwingHelper
067: .getHTMLColorString(theBackGroundColor)));
068:
069: theIdTag = theAnchorTag;
070: }
071:
072: if (aLabel.getIcon() != null) {
073: theImageTag = new Tag("img");
074: theAlign = "center";
075:
076: theImageTag.setNeedsClosure(false);
077: theImageTag.addAttribute(new TagAttribute("name",
078: theComponentName));
079: theImageTag.addAttribute(new TagAttribute("id",
080: theComponentName));
081: theImageTag.addAttribute(new TagAttribute("src",
082: WosFramework.getInstance().getContextPath()
083: + "/"
084: + ((ImageIcon) aLabel.getIcon())
085: .getDescription()));
086:
087: if (aLabel.getToolTipText() != null)
088: theImageTag.addAttribute(new TagAttribute("alt", aLabel
089: .getToolTipText()));
090:
091: theIdTag = theImageTag;
092: }
093:
094: RemoteHelper.addMouseListeners(aLabel, theComponentName,
095: theIdTag);
096:
097: if (aLabel.getLayout() instanceof TemplateLayout) {
098: theTemplate.addElement(new IdTagTemplateElement("theLabel",
099: theAnchorTag));
100:
101: HtmlTemplate theImageTemplate = theTemplate
102: .getSubTemplate("theImage");
103: if (theImageTemplate != null && theImageTag != null)
104: theImageTemplate.getFoundTag().addAllTagAttributes(
105: theImageTag);
106: } else {
107: HtmlTemplate theAnchorTemplate = theTemplate
108: .getSubTemplate("theTable.theTd.theLabel");
109: HtmlTemplate theImageTemplate = theTemplate
110: .getSubTemplate("theTable.theTd.theImage");
111: HtmlTemplate theTableTemplate = theTemplate
112: .getSubTemplate("theTable");
113: HtmlTemplate theTdTemplate = theTemplate
114: .getSubTemplate("theTable.theTd");
115:
116: if (theAnchorTag != null)
117: theAnchorTemplate.mergeFoundTag(theAnchorTag);
118: else
119: theAnchorTemplate.deleteFoundTag();
120:
121: if (theImageTag != null)
122: theImageTemplate.getFoundTag()
123: .addAllTagAttributesAndName(theImageTag);
124: else
125: theImageTemplate.deleteFoundTag();
126:
127: if (aLabel.getWidth() != 0 && aLabel.getHeight() != 0) {
128: Tag theTableTag = theTableTemplate.getFoundTag();
129: theTableTag.addAttribute("width", aLabel.getWidth());
130: theTableTag.addAttribute("height", aLabel.getHeight());
131: theTdTemplate.getFoundTag().addNewTagAttributes(
132: new Tag("td").addAttribute("align", theAlign));
133: } else {
134: theTdTemplate.deleteFoundTag();
135: theTemplate = theTdTemplate;
136: }
137: }
138:
139: theContribManager.doContribution(theComponent, theTemplate,
140: theIdTag, RemoteHelper
141: .getComponentInitScript(theComponent));
142: }
143:
144: public void dispatchEvents(List anEvents) {
145: for (Iterator i = anEvents.iterator(); i.hasNext();) {
146: RemoteEvent theEvent = (RemoteEvent) i.next();
147: JLabel theLabel = (JLabel) ((ComponentWrapper) theEvent
148: .getSource()).getWrappedComponent();
149:
150: if (theEvent.getType().equals("mouseClicked"))
151: RemoteHelper.fireClickEvent(theLabel);
152: }
153: }
154: }
|