001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces.components.input;
034:
035: import com.flexive.faces.FxJsfUtils;
036: import com.flexive.shared.FxContext;
037: import com.flexive.shared.FxFormatUtils;
038: import com.flexive.shared.FxLanguage;
039: import com.flexive.shared.XPathElement;
040: import com.flexive.shared.value.*;
041: import com.flexive.shared.value.renderer.FxValueRendererFactory;
042: import com.flexive.war.servlet.ThumbnailServlet;
043:
044: import javax.faces.component.html.HtmlGraphicImage;
045: import javax.faces.context.ResponseWriter;
046: import java.io.IOException;
047:
048: /**
049: * Renders a FxValue component in read-only mode.
050: *
051: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
052: * @version $Rev: 94 $
053: */
054: class ReadOnlyModeHelper extends RenderHelper {
055:
056: public ReadOnlyModeHelper(ResponseWriter writer,
057: FxValueInput component, String clientId, FxValue value) {
058: super (writer, component, clientId, value);
059: }
060:
061: /**
062: * {@inheritDoc}
063: */
064: @Override
065: protected void encodeMultiLanguageField() throws IOException {
066: encodeField(clientId, FxContext.get().getTicket().getLanguage());
067: }
068:
069: /**
070: * {@inheritDoc}
071: */
072: @Override
073: @SuppressWarnings({"unchecked"})
074: protected void encodeField(String inputId, FxLanguage language)
075: throws IOException {
076: if (!value.isEmpty()) {
077: // TODO: optional "empty" message
078: final FxLanguage outputLanguage = FxContext.get()
079: .getTicket().getLanguage();
080: if (component.getValueFormatter() != null) {
081: // use custom formatter
082: writer.write(component.getValueFormatter().format(
083: value, value.getBestTranslation(language),
084: outputLanguage));
085: } else if (value instanceof FxReference) {
086: // render preview
087: if (language == null
088: || value.getTranslation(language) != null) {
089: final HtmlGraphicImage image = (HtmlGraphicImage) FxJsfUtils
090: .addChildComponent(component,
091: HtmlGraphicImage.COMPONENT_TYPE);
092: image.setUrl(ThumbnailServlet.getLink(
093: ((FxReference) value)
094: .getBestTranslation(language),
095: BinaryDescriptor.PreviewSizes.PREVIEW2));
096: }
097: } else if (value instanceof FxBinary && !value.isEmpty()) {
098: // render preview image
099: final HtmlGraphicImage image = (HtmlGraphicImage) FxJsfUtils
100: .addChildComponent(component,
101: HtmlGraphicImage.COMPONENT_TYPE);
102: final BinaryDescriptor descriptor = ((FxBinary) value)
103: .getTranslation(language);
104: image.setUrl(ThumbnailServlet.getLink(XPathElement
105: .getPK(value.getXPath()),
106: BinaryDescriptor.PreviewSizes.PREVIEW2, value
107: .getXPath(), descriptor
108: .getCreationTime()));
109: } else if (component.isFilter()
110: && !(value instanceof FxHTML)) {
111: // escape HTML code and generate <br/> tags for newlines
112: writer.write(FxFormatUtils
113: .escapeForJavaScript(FxValueRendererFactory
114: .getInstance(outputLanguage).format(
115: value, language), true, true));
116: } else {
117: // write the plain value
118: FxValueRendererFactory.getInstance(outputLanguage)
119: .render(writer, value, language);
120: }
121: // final Object translation = language != null ? value.getBestTranslation(language) : value.getDefaultTranslation();
122: // if (value instanceof FxHTML) {
123: // writer.write((String) translation);
124: // } else if (translation instanceof FxSelectListItem) {
125: // writer.write(((FxSelectListItem) translation).getLabel().getBestTranslation());
126: // } else if (translation instanceof SelectMany) {
127: // writer.write(StringUtils.join(((SelectMany) translation).getSelectedLabels().iterator(), ", "));
128: // } else {
129: // writer.writeText(translation, null);
130: // }
131: }
132: }
133: }
|