001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 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 */
020 package org.sonar.plugins.design.ui.lcom4.client;
021
022 import com.google.gwt.i18n.client.Dictionary;
023 import com.google.gwt.user.client.ui.*;
024 import org.sonar.gwt.ui.Icons;
025 import org.sonar.gwt.ui.Loading;
026 import org.sonar.gwt.ui.Page;
027 import org.sonar.wsclient.gwt.AbstractCallback;
028 import org.sonar.wsclient.gwt.Sonar;
029 import org.sonar.wsclient.services.Resource;
030 import org.sonar.wsclient.services.ResourceQuery;
031
032 public class Lcom4Tab extends Page {
033 public static final String GWT_ID = "org.sonar.plugins.design.ui.lcom4.Lcom4Tab";
034
035 private VerticalPanel panel;
036
037 @Override
038 protected Widget doOnResourceLoad(Resource resource) {
039 panel = new VerticalPanel();
040 panel.setWidth("100%");
041 loadData(resource);
042 return panel;
043 }
044
045 private void loadData(Resource resource) {
046 panel.add(new Loading());
047 ResourceQuery query = ResourceQuery.createForMetrics(resource.getId().toString(), "lcom4", "lcom4_blocks");
048 Sonar.getInstance().find(query, new AbstractCallback<Resource>() {
049 @Override
050 protected void doOnResponse(Resource resource) {
051 panel.clear();
052 panel.add(new Header(resource));
053 if (resource != null && resource.getMeasure("lcom4_blocks") != null) {
054 String json = resource.getMeasure("lcom4_blocks").getData();
055 Data.Blocks blocks = Data.parse(json);
056
057 Grid grid = new Grid(blocks.size(), 2);
058 grid.setStyleName("lcom4blocks");
059 grid.getColumnFormatter().setStyleName(0, "index");
060
061 for (int indexBlock = 0; indexBlock < blocks.size(); indexBlock++) {
062 Data.Block block = blocks.get(indexBlock);
063 grid.setHTML(indexBlock, 0, "<div class='index'>" + (indexBlock + 1) + "</div>");
064
065 VerticalPanel blockPanel = new VerticalPanel();
066 blockPanel.setStyleName("lcom4block");
067
068 for (int indexEntity = 0; indexEntity < block.size(); indexEntity++) {
069 Data.Entity entity = block.get(indexEntity);
070 HTML row = new HTML(Icons.forQualifier(entity.getQualifier()).getHTML() + " " + entity.getName());
071 row.setStyleName("lcom4row");
072 blockPanel.add(row);
073 }
074 grid.setWidget(indexBlock, 1, blockPanel);
075
076 }
077 panel.add(grid);
078 }
079 }
080 });
081 }
082
083 private static class Header extends Composite {
084 private FlowPanel header;
085
086 public Header(Resource resource) {
087 header = new FlowPanel();
088 header.setStyleName("gwt-ViewerHeader");
089
090 HorizontalPanel panel = new HorizontalPanel();
091 HTML html = new HTML(Dictionary.getDictionary("l10n").get("lcom4.metric") + ": ");
092 html.setStyleName("metric");
093 panel.add(html);
094
095 if (resource != null) {
096 html = new HTML(resource.getMeasureFormattedValue("lcom4", "-"));
097 html.setStyleName("value");
098 panel.add(html);
099 }
100
101 header.add(panel);
102 initWidget(header);
103 }
104 }
105 }