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 */
020 package org.sonar.plugins.design.ui.page.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.Window;
026 import com.google.gwt.user.client.ui.*;
027 import org.sonar.gwt.Configuration;
028 import org.sonar.gwt.Links;
029 import org.sonar.gwt.ui.Icons;
030 import org.sonar.gwt.ui.Loading;
031 import org.sonar.wsclient.gwt.AbstractCallback;
032 import org.sonar.wsclient.gwt.AbstractListCallback;
033 import org.sonar.wsclient.gwt.Sonar;
034 import org.sonar.wsclient.services.Dependency;
035 import org.sonar.wsclient.services.DependencyQuery;
036 import org.sonar.wsclient.services.Resource;
037
038 import java.util.List;
039
040 public final class DependencyInfo extends Composite {
041
042 private static DependencyInfo INSTANCE = new DependencyInfo();
043
044 private VerticalPanel panel;
045 private Loading loading = new Loading();
046 private String currentDependencyId = null;
047 private boolean popupMode = false;
048
049 private DependencyInfo() {
050 panel = new VerticalPanel();
051 initWidget(panel);
052 }
053
054 public static DependencyInfo getInstance() {
055 return INSTANCE;
056 }
057
058
059 public void showOrPopup(String dependencyId) {
060 if (popupMode) {
061 Window.open(Links.urlForResourcePage(Configuration.getResourceId(), DesignPage.GWT_ID, "layout=false&depId=" + dependencyId),
062 "dependency", Links.DEFAULT_POPUP_HTML_FEATURES);
063
064 } else {
065 INSTANCE.show(dependencyId);
066 }
067 }
068
069 public void show(String dependencyId) {
070 panel.clear();
071 currentDependencyId = dependencyId;
072 if (dependencyId != null) {
073 panel.add(loading);
074 loadDependency(dependencyId);
075 }
076 }
077
078 public DependencyInfo setPopupMode(boolean b) {
079 this.popupMode = b;
080 return this;
081 }
082
083 public void popup() {
084 popupMode = true;
085 panel.clear();
086 showOrPopup(currentDependencyId);
087 }
088
089 private void setLoaded() {
090 loading.removeFromParent();
091 }
092
093 private void loadDependency(String dependencyId) {
094 DependencyQuery query = DependencyQuery.createForId(dependencyId);
095 Sonar.getInstance().find(query, new AbstractCallback<Dependency>() {
096 @Override
097 protected void doOnResponse(Dependency dependency) {
098 if (dependency == null) {
099 setLoaded();
100 panel.add(new Label(Dictionary.getDictionary("l10n").get("noData")));
101 } else {
102 loadSubDependencies(dependency);
103 }
104 }
105
106 @Override
107 protected void doOnError(int errorCode, String errorMessage) {
108 super.doOnError(errorCode, errorMessage);
109 }
110 });
111 }
112
113 private void loadSubDependencies(final Dependency dependency) {
114 DependencyQuery query = DependencyQuery.createForSubDependencies(dependency.getId());
115 Sonar.getInstance().findAll(query, new AbstractListCallback<Dependency>() {
116
117 @Override
118 protected void doOnResponse(final List<Dependency> subDependencies) {
119 Grid table = new Grid(subDependencies.size() + 1, 5);
120 table.setStyleName("depInfo");
121 createHeader(dependency, table);
122
123 for (int row = 0; row < subDependencies.size(); row++) {
124 Dependency dep = subDependencies.get(row);
125 table.setWidget(row + 1, 0, new HTML(Icons.forQualifier(dep.getFromQualifier()).getHTML()));
126 if (Resource.QUALIFIER_FILE.equals(dep.getFromQualifier()) || Resource.QUALIFIER_CLASS.equals(dep.getFromQualifier())) {
127 table.setWidget(row + 1, 1, createLink(dep.getFromId(), dep.getFromName()));
128 } else {
129 table.setText(row + 1, 1, dep.getFromName());
130 }
131 table.setText(row + 1, 2, " " + dep.getUsage() + " ");
132 table.setWidget(row + 1, 3, new HTML(Icons.forQualifier(dep.getToQualifier()).getHTML()));
133 if (Resource.QUALIFIER_FILE.equals(dep.getToQualifier()) || Resource.QUALIFIER_CLASS.equals(dep.getToQualifier())) {
134 table.setWidget(row + 1, 4, createLink(dep.getToId(), dep.getToName()));
135 } else {
136 table.setText(row + 1, 4, dep.getToName());
137 }
138 }
139
140
141 panel.clear();
142 if (!popupMode) {
143 panel.add(createNewWindowLink());
144 }
145 panel.add(table);
146 }
147 });
148 }
149
150 private Label createLink(final long resourceId, final String resourceName) {
151 Label link = new Label(resourceName);
152 link.setStyleName("link");
153 link.addClickHandler(new ClickHandler() {
154
155 public void onClick(ClickEvent event) {
156 Links.openResourcePopup(String.valueOf(resourceId));
157 }
158 });
159 return link;
160 }
161
162 private void createHeader(final Dependency dependency, final Grid grid) {
163 grid.getRowFormatter().setStyleName(0, "depInfoHeader");
164
165 grid.setWidget(0, 0, Icons.forQualifier(dependency.getFromQualifier()).createImage());
166 grid.setText(0, 1, dependency.getFromName());
167
168 grid.setWidget(0, 3, Icons.forQualifier(dependency.getToQualifier()).createImage());
169 grid.setText(0, 4, dependency.getToName());
170 }
171
172 private Widget createNewWindowLink() {
173 Label popup = new Label(Dictionary.getDictionary("l10n").get("newWindow"));
174 popup.setStyleName("newwindow");
175 popup.addClickHandler(new ClickHandler() {
176 public void onClick(ClickEvent event) {
177 popup();
178 }
179 });
180 return popup;
181 }
182 }