001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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 */
020package org.sonar.gwt.ui;
021
022import com.google.gwt.user.client.ui.Composite;
023import com.google.gwt.user.client.ui.FlowPanel;
024import com.google.gwt.user.client.ui.HTML;
025import com.google.gwt.user.client.ui.Panel;
026import org.sonar.wsclient.gwt.AbstractCallback;
027import org.sonar.wsclient.gwt.Sonar;
028import org.sonar.wsclient.services.Measure;
029import org.sonar.wsclient.services.Resource;
030import org.sonar.wsclient.services.ResourceQuery;
031
032public abstract class ViewerHeader extends Composite {
033  private String[] metrics;
034  private FlowPanel header;
035
036  public ViewerHeader(Resource resource, String[] metrics) {
037    this.metrics = metrics;
038    header = new FlowPanel();
039    header.setStyleName("gwt-ViewerHeader");
040    initWidget(header);
041    loadMeasures(resource);
042  }
043
044  public String[] getMetrics() {
045    return metrics;
046  }
047
048  private void loadMeasures(Resource resource) {
049    ResourceQuery query = ResourceQuery.createForMetrics(resource.getKey(), metrics).setVerbose(true);
050    Sonar.getInstance().find(query, new AbstractCallback<Resource>() {
051
052      @Override
053      protected void doOnResponse(Resource resource) {
054        display(header, resource);
055      }
056    });
057  }
058
059  protected abstract void display(FlowPanel header, Resource resource);
060
061  protected static class MeasureLabel {
062    private String metricName;
063    private String value;
064
065    public MeasureLabel(Measure measure) {
066      if (measure != null) {
067        this.metricName = measure.getMetricName();
068        this.value = measure.getFormattedValue();
069      }
070    }
071
072    public MeasureLabel(Measure measure, String metricName, String defaultValue) {
073      this.metricName = metricName;
074      if (measure != null) {
075        this.value = measure.getFormattedValue();
076      } else {
077        this.value = defaultValue;
078      }
079    }
080
081    public String getMetricName() {
082      return metricName;
083    }
084
085    public String getValue() {
086      return value;
087    }
088
089    public boolean hasValue() {
090      return value != null;
091    }
092  }
093
094  protected void addCell(Panel panel, Measure... measures) {
095    if (measures != null) {
096      String names = "";
097      String values = "";
098      boolean first = true;
099      for (Measure measure : measures) {
100        if (measure != null && measure.getFormattedValue() != null) {
101          if (!first) {
102            names += "<br/>";
103            values += "<br/>";
104          }
105          names += "<b>" + measure.getMetricName() + "</b>: ";
106          values += measure.getFormattedValue();
107          first = false;
108        }
109      }
110
111      if (!first) {
112        HTML html = new HTML(names);
113        html.setStyleName("metric");
114        panel.add(html);
115
116        html = new HTML(values);
117        html.setStyleName("value");
118        panel.add(html);
119      }
120    }
121  }
122
123  protected void addCell(Panel panel, String metric, String value) {
124    HTML html = new HTML(metric);
125    html.setStyleName("metric");
126    panel.add(html);
127
128    html = new HTML(value);
129    html.setStyleName("value");
130    panel.add(html);
131  }
132
133  protected void addBigCell(Panel panel, String html) {
134    HTML htmlDiv = new HTML(html);
135    htmlDiv.setStyleName("big");
136    panel.add(htmlDiv);
137  }
138}