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.libraries.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.wsclient.gwt.AbstractListCallback;
027    import org.sonar.wsclient.gwt.Sonar;
028    import org.sonar.wsclient.services.DependencyTree;
029    import org.sonar.wsclient.services.DependencyTreeQuery;
030    import org.sonar.wsclient.services.Resource;
031    
032    import java.util.List;
033    
034    public class ProjectPanel extends FlowPanel {
035    
036      private Label title;
037      private Tree tree;
038      private Filters filters;
039    
040      public ProjectPanel(Resource project, Filters filters) {
041        this.filters = filters;
042        setStyleName("libs");
043        getElement().setId("libs-" + project.getKey());
044        add(new Loading(project.getName()));
045        loadLibraries(project);
046      }
047    
048      private void loadLibraries(final Resource project) {
049        Sonar.getInstance().findAll(DependencyTreeQuery.createForProject(project.getId().toString()), new AbstractListCallback<DependencyTree>() {
050    
051          @Override
052          protected void doOnResponse(List<DependencyTree> dependencyTrees) {
053            createTitle(project);
054            createTree();
055    
056            if (dependencyTrees == null || dependencyTrees.isEmpty()) {
057              clear();
058              add(title);
059              add(createNoLibsMessage());
060              
061            } else {
062              display(dependencyTrees, null);
063              filter();
064    
065              clear();
066              add(title);
067              add(tree);
068            }
069          }
070    
071          private void createTitle(Resource project) {
072            String html = Icons.forQualifier(project.getQualifier()).getHTML();
073            html += " <span class=''> " + project.getName() + "</span> ";
074    
075            if (project.getVersion() != null) {
076              html += project.getVersion() + " ";
077            }
078            title = new HTML(html);
079          }
080    
081          private void display(List<DependencyTree> depTrees, TreeItem parentLibrary) {
082            if (depTrees != null) {
083              for (DependencyTree depTree : depTrees) {
084                Library library = new Library(depTree);
085                if (parentLibrary == null) {
086                  tree.addItem(library);
087                  library.setState(true);
088                } else {
089                  parentLibrary.addItem(library);
090                }
091                display(depTree.getTo(), library);
092                library.setState(true);
093              }
094            }
095          }
096    
097          private void createTree() {
098            tree = new Tree();
099            tree.setAnimationEnabled(false);
100          }
101        });
102      }
103    
104      private Label createNoLibsMessage() {
105        Label msg = new Label(Dictionary.getDictionary("l10n").get("libs.noLibraries"));
106        msg.setStyleName("nolibs");
107        return msg;
108      }
109    
110      public void filter() {
111        boolean visible = (tree.getItemCount() == 0 && !filters.hasKeyword());
112        for (int index = 0; index < tree.getItemCount(); index++) {
113          Library lib = (Library) tree.getItem(index);
114          visible |= !lib.filter(filters.getKeywordFilter().getKeyword(), filters.isTestFiltered());
115        }
116        setVisible(visible);
117      }
118    
119      public void expandCollapse(boolean expand) {
120        for (int index = 0; index < tree.getItemCount(); index++) {
121          openItem(tree.getItem(index), expand);
122        }
123      }
124    
125      private void openItem(TreeItem item, boolean open) {
126        item.setState(open);
127        for (int i = 0; i < item.getChildCount(); i++) {
128          openItem(item.getChild(i), open);
129        }
130      }
131    }