001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.annotations.gwt.client.view;
023
024import java.util.List;
025import java.util.Set;
026
027import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfiguration;
028import org.nuxeo.ecm.platform.annotations.gwt.client.model.Annotation;
029import org.nuxeo.ecm.platform.annotations.gwt.client.util.AnnotationUtils;
030import org.nuxeo.ecm.platform.annotations.gwt.client.view.i18n.TranslationMessages;
031
032import com.google.gwt.core.client.GWT;
033import com.google.gwt.user.client.Window;
034import com.google.gwt.user.client.ui.ClickListener;
035import com.google.gwt.user.client.ui.Grid;
036import com.google.gwt.user.client.ui.HTML;
037import com.google.gwt.user.client.ui.HorizontalPanel;
038import com.google.gwt.user.client.ui.Image;
039import com.google.gwt.user.client.ui.Label;
040import com.google.gwt.user.client.ui.PopupPanel;
041import com.google.gwt.user.client.ui.PushButton;
042import com.google.gwt.user.client.ui.VerticalPanel;
043import com.google.gwt.user.client.ui.Widget;
044
045public class AnnotationListPopup extends PopupPanel {
046
047    public AnnotationListPopup(String annotationName, List<Annotation> annotations, WebConfiguration configuration) {
048        super();
049        this.setWidth(Window.getClientWidth() + " px");
050        this.setHeight(Window.getClientHeight() + " px");
051        this.setStyleName("annotationListPopup");
052        VerticalPanel verticalPanel = new VerticalPanel();
053        verticalPanel.setWidth("100%");
054        HorizontalPanel titleBar = new HorizontalPanel();
055        titleBar.setStyleName("annotationListPopupTitleBar");
056        TranslationMessages translationMessages = GWT.create(TranslationMessages.class);
057        Label title = new Label(translationMessages.annotationListPopupTitle(annotationName));
058        titleBar.add(title);
059        titleBar.setCellWidth(title, "100%");
060        PushButton closeButton = new PushButton(" ", new ClickListener() {
061            public void onClick(Widget arg0) {
062                hide();
063            }
064        });
065        closeButton.setStyleName("annotationListPopupClose");
066        titleBar.add(closeButton);
067        verticalPanel.add(titleBar);
068        Grid grid = new Grid(annotations.size(), 3);
069        grid.setWidth("100%");
070        grid.setCellSpacing(0);
071        grid.setStyleName("annotationListPopupGrid");
072        Set<String> displayedFields = configuration.getDisplayedFields();
073        String icon = configuration.getAnnotationDefinition(annotationName).getIcon();
074        for (int i = 0; i < annotations.size(); i++) {
075            grid.setWidget(i, 0, new Image(icon));
076            Annotation annotation = annotations.get(i);
077            String fields = annotation.getFormattedDate();
078            for (String displayedField : displayedFields) {
079                String value = annotation.getFields().get(displayedField);
080                if (value != null) {
081                    fields += "<br/>" + value;
082                }
083            }
084            grid.setWidget(i, 1, new HTML(fields));
085            grid.getColumnFormatter().addStyleName(1, "annotationListPopupFields");
086
087            String body = "";
088            Set<String> definedFields = configuration.getAnnotationDefinition(annotationName).getFields().keySet();
089            for (String definedField : definedFields) {
090                if (!displayedFields.contains(definedField)) {
091                    body += annotation.getFields().get(definedField) + " - ";
092                }
093            }
094            body += AnnotationUtils.replaceCarriageReturns(AnnotationUtils.escapeHtml(annotation.getBody()));
095            grid.setWidget(i, 2, new HTML(body));
096            grid.getColumnFormatter().setWidth(2, "100%");
097
098        }
099        verticalPanel.add(grid);
100        verticalPanel.setCellHeight(grid, "100%");
101        this.add(verticalPanel);
102    }
103}