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.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), "dependency", Links.DEFAULT_POPUP_HTML_FEATURES);
062
063 } else {
064 INSTANCE.show(dependencyId);
065 }
066 }
067
068 public void show(String dependencyId) {
069 panel.clear();
070 currentDependencyId = dependencyId;
071 if (dependencyId !=null) {
072 panel.add(loading);
073 loadDependency(dependencyId);
074 }
075 }
076
077 public DependencyInfo setPopupMode(boolean b) {
078 this.popupMode = b;
079 return this;
080 }
081
082 public void popup() {
083 popupMode = true;
084 panel.clear();
085 showOrPopup(currentDependencyId);
086 }
087
088 private void setLoaded() {
089 loading.removeFromParent();
090 }
091
092 private void loadDependency(String dependencyId) {
093 DependencyQuery query = DependencyQuery.createForId(dependencyId);
094 Sonar.getInstance().find(query, new AbstractCallback<Dependency>() {
095 @Override
096 protected void doOnResponse(Dependency dependency) {
097 if (dependency == null) {
098 setLoaded();
099 panel.add(new Label(Dictionary.getDictionary("l10n").get("noData")));
100 } else {
101 loadSubDependencies(dependency);
102 }
103 }
104
105 @Override
106 protected void doOnError(int errorCode, String errorMessage) {
107 super.doOnError(errorCode, errorMessage);
108 }
109 });
110 }
111
112 private void loadSubDependencies(final Dependency dependency) {
113 DependencyQuery query = DependencyQuery.createForSubDependencies(dependency.getId());
114 Sonar.getInstance().findAll(query, new AbstractListCallback<Dependency>() {
115
116 @Override
117 protected void doOnResponse(final List<Dependency> subDependencies) {
118 Grid table = new Grid(subDependencies.size() + 1, 5);
119 table.setStyleName("depInfo");
120 createHeader(dependency, table);
121
122 for (int row = 0; row < subDependencies.size(); row++) {
123 Dependency dep = subDependencies.get(row);
124 table.setWidget(row + 1, 0, new HTML(Icons.forQualifier(dep.getFromQualifier()).getHTML()));
125 if (Resource.QUALIFIER_FILE.equals(dep.getFromQualifier()) || Resource.QUALIFIER_CLASS.equals(dep.getFromQualifier())) {
126 table.setWidget(row + 1, 1, createLink(dep.getFromId(), dep.getFromName()));
127 } else {
128 table.setText(row + 1, 1, dep.getFromName());
129 }
130 table.setText(row + 1, 2, " " + dep.getUsage() + " ");
131 table.setWidget(row + 1, 3, new HTML(Icons.forQualifier(dep.getToQualifier()).getHTML()));
132 if (Resource.QUALIFIER_FILE.equals(dep.getToQualifier()) || Resource.QUALIFIER_CLASS.equals(dep.getToQualifier())) {
133 table.setWidget(row + 1, 4, createLink(dep.getToId(), dep.getToName()));
134 } else {
135 table.setText(row + 1, 4, dep.getToName());
136 }
137 }
138
139
140 panel.clear();
141 if (!popupMode) {
142 panel.add(createNewWindowLink());
143 }
144 panel.add(table);
145 }
146 });
147 }
148
149 private Label createLink(final long resourceId, final String resourceName) {
150 Label link = new Label(resourceName);
151 link.setStyleName("link");
152 link.addClickHandler(new ClickHandler() {
153
154 public void onClick(ClickEvent event) {
155 Links.openResourcePopup(String.valueOf(resourceId));
156 }
157 });
158 return link;
159 }
160
161 private void createHeader(final Dependency dependency, final Grid grid) {
162 grid.getRowFormatter().setStyleName(0, "depInfoHeader");
163
164 grid.setWidget(0, 0, Icons.forQualifier(dependency.getFromQualifier()).createImage());
165 grid.setText(0, 1, dependency.getFromName());
166
167 grid.setWidget(0, 3, Icons.forQualifier(dependency.getToQualifier()).createImage());
168 grid.setText(0, 4, dependency.getToName());
169 }
170
171 private Widget createNewWindowLink() {
172 Label popup = new Label(Dictionary.getDictionary("l10n").get("newWindow"));
173 popup.setStyleName("newwindow");
174 popup.addClickHandler(new ClickHandler() {
175 public void onClick(ClickEvent event) {
176 popup();
177 }
178 });
179 return popup;
180 }
181 }