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.plugins.design.ui.libraries.client;
021
022import com.google.gwt.event.dom.client.ClickEvent;
023import com.google.gwt.event.dom.client.ClickHandler;
024import com.google.gwt.event.dom.client.KeyUpEvent;
025import com.google.gwt.event.dom.client.KeyUpHandler;
026import com.google.gwt.user.client.ui.Panel;
027import com.google.gwt.user.client.ui.VerticalPanel;
028import com.google.gwt.user.client.ui.Widget;
029import org.sonar.gwt.ui.Page;
030import org.sonar.wsclient.gwt.AbstractListCallback;
031import org.sonar.wsclient.gwt.Sonar;
032import org.sonar.wsclient.services.Resource;
033import org.sonar.wsclient.services.ResourceQuery;
034
035import java.util.ArrayList;
036import java.util.List;
037
038public class LibrariesPage extends Page implements KeyUpHandler, ClickHandler {
039  public static final String GWT_ID = "org.sonar.plugins.design.ui.libraries.LibrariesPage";
040
041  private Filters filters;
042  private List<ProjectPanel> projectPanels = new ArrayList<ProjectPanel>();
043  private Panel container;
044
045  @Override
046  protected Widget doOnResourceLoad(Resource resource) {
047    container = new VerticalPanel();
048    container.setWidth("100%");
049
050    filters = new Filters(resource);
051    filters.getKeywordFilter().addKeyUpHandler(this);
052    filters.getTestCheckbox().addClickHandler(this);
053    filters.getExpandCollapseLink().addClickHandler(this);
054    
055    container.add(filters);
056    load(resource);
057    return container;
058  }
059
060
061  private void load(Resource resource) {
062    ResourceQuery resourceQuery = new ResourceQuery(resource.getId().toString());
063    resourceQuery.setDepth(-1).setScopes(Resource.SCOPE_SET);
064    Sonar.getInstance().findAll(resourceQuery, new AbstractListCallback<Resource>() {
065      @Override
066      protected void doOnResponse(List<Resource> subProjects) {
067        for (Resource subProject : subProjects) {
068          ProjectPanel projectPanel = new ProjectPanel(subProject, filters);
069          projectPanels.add(projectPanel);
070          container.add(projectPanel);
071        }
072      }
073    });
074  }
075
076  public void onKeyUp(KeyUpEvent event) {
077    for (ProjectPanel projectPanel : projectPanels) {
078      projectPanel.filter();
079    }
080  }
081
082  public void onClick(ClickEvent event) {
083    if (event.getSource() == filters.getTestCheckbox()) {
084      for (ProjectPanel projectPanel : projectPanels) {
085        projectPanel.filter();
086      }
087    } else if (event.getSource() == filters.getExpandCollapseLink()) {
088      if (filters.isExpanded()) {
089        filters.collapse();
090        expandCollapseLibs(false);
091      } else {
092        filters.expand();
093        expandCollapseLibs(true);
094      }
095    }
096  }
097
098  private void expandCollapseLibs(boolean expand) {
099    for (ProjectPanel projectPanel : projectPanels) {
100      projectPanel.expandCollapse(expand);
101    }
102  }
103}