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.user.client.ui.TreeItem;
023    import org.sonar.gwt.ui.Icons;
024    import org.sonar.wsclient.services.DependencyTree;
025    
026    public class Library extends TreeItem {
027    
028      private String keywords;
029      private String usage;
030    
031      public Library(DependencyTree dep) {
032        setHTML(toHTML(dep));
033        keywords = toKeywords(dep);
034        usage = dep.getUsage();
035      }
036    
037      private static String toKeywords(DependencyTree dep) {
038        String text = dep.getResourceName() + " ";
039    
040        if (dep.getResourceKey() != null) {
041          text += dep.getResourceKey() + " ";
042        }
043        if (dep.getResourceVersion() != null) {
044          text += dep.getResourceVersion() + " ";
045        }
046        text += dep.getUsage();
047        return text.toUpperCase();
048      }
049    
050    
051      /**
052       * @param keyword upper-case keyword
053       */
054      public boolean containsKeyword(String keyword) {
055        if (keywords.indexOf(keyword) >= 0) {
056          return true;
057        }
058        for (int index = 0; index < getChildCount(); index++) {
059          if (((Library) getChild(index)).containsKeyword(keyword)) {
060            return true;
061          }
062        }
063        return false;
064      }
065    
066      private static String toHTML(DependencyTree tree) {
067        String html = Icons.forQualifier(tree.getResourceQualifier()).getHTML();
068        html += " <span> " + tree.getResourceName() + "</span> ";
069    
070        if (tree.getResourceVersion() != null) {
071          html += tree.getResourceVersion() + " ";
072        }
073        html += "(" + tree.getUsage() + ")";
074        return html;
075      }
076    
077    
078      public boolean filter(String keyword, boolean testFiltered) {
079        if (testFiltered && "test".equals(usage)) {
080          setVisible(false);
081          return true;
082        }
083    
084        boolean filtered = false;
085        if (!"".equals(keyword) && !containsKeyword(keyword)) {
086          filtered = true;
087        }
088    
089        boolean hasVisibleChild = false;
090        for (int index = 0; index < getChildCount(); index++) {
091            hasVisibleChild |= !((Library) getChild(index)).filter(keyword, testFiltered);
092        }
093    
094        boolean visible = !filtered || hasVisibleChild;
095        setVisible(visible);
096        return !visible;
097      }
098    }