001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.widget.netvibes;
023:
024: import java.net.URL;
025: import java.util.Iterator;
026: import java.util.Locale;
027: import java.util.Map;
028:
029: import org.jboss.portal.common.i18n.LocalizedString;
030: import org.jboss.portal.common.text.FastURLEncoder;
031: import org.jboss.portal.common.util.UUIDGenerator;
032: import org.jboss.portal.widget.Widget;
033:
034: /**
035: * @author <a href="mailto:emuckenh@redhat.com">Emanuel Muckenhuber</a>
036: * @version $Revision$
037: */
038: public class NetvibesWidget implements Widget {
039:
040: /** . */
041: private static final UUIDGenerator generator = new UUIDGenerator();
042:
043: /** The Widget url */
044: private final URL url;
045:
046: /** The Widget id */
047: private final String id;
048:
049: /** The default widget height */
050: private String defaultHeight = "250";
051:
052: /** The netvibes widget info */
053: private final NetvibesWidgetInfo widgetInfo;
054:
055: public NetvibesWidget(URL url, NetvibesWidgetInfo widgetInfo) {
056: this .url = url;
057: this .id = url.toString();
058: this .widgetInfo = widgetInfo;
059: }
060:
061: public void setDefaultHeight(int defaultHeight) {
062: this .defaultHeight = String.valueOf(defaultHeight);
063: }
064:
065: public LocalizedString getDescription() {
066: return this .widgetInfo.getMetaData().getDescription();
067: }
068:
069: public String getId() {
070: return this .id;
071: }
072:
073: public LocalizedString getTitle() {
074: return this .widgetInfo.getTitle();
075: }
076:
077: public NetvibesWidgetInfo getWidgetInfo() {
078: return widgetInfo;
079: }
080:
081: public String render(Map parameters) {
082: return render(parameters, null);
083: }
084:
085: public String render(Map parameters, Locale locale) {
086: NetvibesWidgetInfo widgetInfo = getWidgetInfo();
087: if (widgetInfo != null) {
088: String id = generator.generateKey();
089:
090: String tempHeight = null;
091:
092: StringBuffer frameUrl = new StringBuffer();
093: frameUrl
094: .append("http://www.netvibes.com/api/uwa/frame/uwa_generic.php?id="
095: + id);
096: frameUrl.append("&moduleUrl="
097: + FastURLEncoder.getUTF8Instance().encode(
098: url.toString()));
099:
100: if (parameters.size() > 0) {
101: NetvibesPreferencesInfo preferences = widgetInfo
102: .getPreferencesInfo();
103: for (Iterator i = preferences.getPreferences()
104: .iterator(); i.hasNext();) {
105: NetvibesPreferenceInfo info = preferences
106: .getPreference((String) i.next());
107: String name = info.getName();
108: String[] values = (String[]) parameters.get(name);
109: String value = values != null ? values[0] : info
110: .getDefaultValue();
111: if (value != null) {
112: int index = name.toLowerCase()
113: .indexOf("height");
114: // for now getting height information like this, because there is no other implementation
115: if (index != -1) {
116: tempHeight = value;
117: }
118: if (!value.equals(info.getDefaultValue())) {
119: frameUrl.append("&"
120: + FastURLEncoder.getUTF8Instance()
121: .encode(name)
122: + "="
123: + FastURLEncoder.getUTF8Instance()
124: .encode(value));
125: }
126: }
127: }
128: }
129:
130: String actualHeight = tempHeight != null ? String
131: .valueOf(Integer.valueOf(tempHeight).intValue() + 50)
132: : defaultHeight;
133:
134: StringBuffer output = new StringBuffer();
135:
136: output.append("<iframe id=\"frame_" + id
137: + "\" name=\"frame_" + id
138: + "\" frameborder=\"0\" width=\"100%\" height=\""
139: + actualHeight + "\" ");
140: output.append("src=\"");
141: output.append(frameUrl);
142: output.append("\"></iframe>");
143:
144: return output.toString();
145: } else {
146: return "Error";
147: }
148: }
149:
150: }
|