001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.web.gwt.client.widgets;
021    
022    import com.google.gwt.core.client.JavaScriptObject;
023    import com.google.gwt.user.client.ui.Composite;
024    import com.google.gwt.user.client.ui.FlowPanel;
025    import com.google.gwt.user.client.ui.HTML;
026    import com.google.gwt.user.client.ui.Panel;
027    import org.sonar.api.web.gwt.client.webservices.*;
028    
029    import java.util.List;
030    
031    public abstract class AbstractViewerHeader extends Composite {
032      private List<WSMetrics.Metric> metrics;
033      private FlowPanel header;
034    
035      public AbstractViewerHeader(Resource resource, List<WSMetrics.Metric> metrics) {
036        this.metrics = metrics;
037        header = new FlowPanel();
038        header.setStyleName("gwt-ViewerHeader");
039        initWidget(header);
040        loadMeasures(resource);
041      }
042    
043      public List<WSMetrics.Metric> getMetrics() {
044        return metrics;
045      }
046    
047      private void loadMeasures(Resource resource) {
048        ResourcesQuery.get(resource.getKey())
049            .setMetrics(metrics)
050            .setVerbose(true)
051            .execute(new BaseQueryCallback<Resources>() {
052              public void onResponse(Resources resources, JavaScriptObject jsonRawResponse) {
053                display(header, resources.firstResource());
054              }
055            });
056      }
057    
058      protected abstract void display(FlowPanel header, Resource resource);
059    
060      protected static class MeasureLabel {
061        private String metricName;
062        private String value;
063    
064        public MeasureLabel(Measure measure) {
065          if (measure != null) {
066            this.metricName = measure.getMetricName();
067            this.value = measure.getFormattedValue();
068          }
069        }
070    
071        public MeasureLabel(Measure measure, String metricName, String defaultValue) {
072          this.metricName = metricName;
073          if (measure != null) {
074            this.value = measure.getFormattedValue();
075          } else {
076            this.value = defaultValue;
077          }
078        }
079    
080        public String getMetricName() {
081          return metricName;
082        }
083    
084        public String getValue() {
085          return value;
086        }
087    
088        public boolean hasValue() {
089          return value != null;
090        }
091      }
092    
093      protected void addCell(Panel panel, Measure... measures) {
094        if (measures != null) {
095          String names = "";
096          String values = "";
097          boolean first = true;
098          for (Measure measure : measures) {
099            if (measure != null && measure.getFormattedValue() != null) {
100              if (!first) {
101                names += "<br/>";
102                values += "<br/>";
103              }
104              names += "<b>" + measure.getMetricName() + "</b>: ";
105              values += measure.getFormattedValue();
106              first = false;
107            }
108          }
109    
110          if (!first) {
111            HTML html = new HTML(names);
112            html.setStyleName("metric");
113            panel.add(html);
114    
115            html = new HTML(values);
116            html.setStyleName("value");
117            panel.add(html);
118          }
119        }
120      }
121    
122      protected void addCell(Panel panel, String metric, String value) {
123        HTML html = new HTML(metric);
124        html.setStyleName("metric");
125        panel.add(html);
126    
127        html = new HTML(value);
128        html.setStyleName("value");
129        panel.add(html);
130      }
131    
132      protected void addBigCell(Panel panel, String html) {
133        HTML htmlDiv = new HTML(html);
134        htmlDiv.setStyleName("big");
135        panel.add(htmlDiv);
136      }
137    }