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.dependencies.client;
021    
022    import com.google.gwt.event.dom.client.ClickEvent;
023    import com.google.gwt.event.dom.client.ClickHandler;
024    import com.google.gwt.i18n.client.Dictionary;
025    import com.google.gwt.user.client.ui.*;
026    import org.sonar.gwt.Links;
027    import org.sonar.gwt.ui.Icons;
028    import org.sonar.wsclient.services.Dependency;
029    
030    public class DependenciesTable extends Composite {
031    
032      private HorizontalPanel panel;
033    
034      public DependenciesTable() {
035        panel = new HorizontalPanel();
036        panel.setStylePrimaryName("dependencies");
037        initWidget(panel);
038      }
039    
040    
041      public void display(final Data data) {
042        panel.clear();
043        if (data.canDisplay()) {
044          panel.add(createIncomingColumn(data));
045          panel.add(createOutgoingColumn(data));
046        } else {
047          panel.add(new Label(Dictionary.getDictionary("l10n").get("noData")));
048        }
049      }
050    
051    
052      private Panel createIncomingColumn(Data data) {
053        FlexTable grid = new FlexTable();
054        grid.setStyleName("col");
055        grid.setWidget(0, 1, new HTML(Dictionary.getDictionary("l10n").get("depsTab.afferentCouplings") + ": <b>" + data.getResource().getMeasureIntValue("ca") + "</b>"));
056        grid.getRowFormatter().setStyleName(0, "coltitle");
057    
058        int row = 1;
059        for (Dependency dependency : data.getDependencies()) {
060          if (data.getResourceId() == dependency.getToId()) {
061            addDependencyRow(grid, row, dependency.getFromId(), dependency.getFromName() + " (" + dependency.getWeight() + ")");
062            grid.setWidget(row, 0, Icons.forQualifier(dependency.getFromQualifier()).createImage());
063            row++;
064          }
065        }
066    
067        return grid;
068      }
069    
070      private Panel createOutgoingColumn(Data data) {
071        FlexTable grid = new FlexTable();
072        grid.setStyleName("col");
073        grid.setWidget(0, 1, new HTML(Dictionary.getDictionary("l10n").get("depsTab.efferentCouplings") + ": <b>" + data.getResource().getMeasureIntValue("ce") + "</b>"));
074        grid.getRowFormatter().setStyleName(0, "coltitle");
075    
076        int row = 1;
077        for (Dependency dependency : data.getDependencies()) {
078          if (data.getResourceId() == dependency.getFromId()) {
079            addDependencyRow(grid, row, dependency.getToId(), dependency.getToName() + " (" + dependency.getWeight() + ")");
080            grid.setWidget(row, 0, Icons.forQualifier(dependency.getToQualifier()).createImage());
081            row++;
082          }
083        }
084    
085        return grid;
086      }
087    
088      private void addDependencyRow(final FlexTable grid, final int row, final long resourceId, final String name) {
089        Label link = new Label(name);
090        link.setStyleName("link");
091        link.addClickHandler(new ClickHandler() {
092          public void onClick(ClickEvent clickEvent) {
093            Links.openResourcePopup(String.valueOf(resourceId));
094          }
095        });
096        grid.setWidget(row, 1, link);
097      }
098    }