001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
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.api.web.gwt.client.widgets;
021
022 import com.google.gwt.user.client.Window;
023 import com.google.gwt.user.client.ui.ClickListener;
024 import com.google.gwt.user.client.ui.Composite;
025 import com.google.gwt.user.client.ui.Hyperlink;
026 import com.google.gwt.user.client.ui.Widget;
027 import org.sonar.api.web.gwt.client.ResourceDictionary;
028
029 public class ResourceViewerPermaLink extends Composite {
030
031 private Hyperlink permaLink;
032 private ClickListener listener = null;
033
034 private final static String[] PARAMS_TO_REPLACE = {
035 ResourceDictionary.CONF_V_METRIC_KEY, ResourceDictionary.CONF_V_PLUGIN_KEY, ResourceDictionary.CONF_V_RESOURCE_KEY};
036
037 public ResourceViewerPermaLink() {
038 this("[Permalink]");
039 }
040
041 public ResourceViewerPermaLink(String linkName) {
042 permaLink = new Hyperlink(linkName, "");
043 permaLink.setStyleName("permaLink");
044 initWidget(permaLink);
045 }
046
047 public void update() {
048 final String permaLinkURL = generateHref();
049
050 if (listener != null) {
051 permaLink.removeClickListener(listener);
052 }
053 listener = new ClickListener() {
054 public void onClick(Widget sender) {
055 Window.Location.replace(permaLinkURL);
056 }
057 };
058 // the listener need to be added to the link since for some unknown reason
059 // updating the link href does not make it work anymore
060 permaLink.addClickListener(listener);
061 permaLink.getElement().getElementsByTagName("a").getItem(0).setAttribute("href", permaLinkURL);
062 }
063
064 private String generateHref() {
065 String permaLinkUrlBase = ResourceDictionary.getPermaLinkURLBase();
066 if (permaLinkUrlBase.contains("?")) {
067 int indexQuestionMark = permaLinkUrlBase.indexOf("?");
068 String queryString = permaLinkUrlBase.substring(indexQuestionMark + 1);
069 String baseUrlReconstructed = permaLinkUrlBase.substring(0, indexQuestionMark) + "?";
070
071 String[] params = queryString.split("&");
072 for (String param : params) {
073 if (!isReplaceableParam(param)) {
074 baseUrlReconstructed += param + "&";
075 }
076 }
077 permaLinkUrlBase = baseUrlReconstructed + "&";
078 } else {
079 permaLinkUrlBase += "?";
080 }
081
082 permaLinkUrlBase = addParam(permaLinkUrlBase, ResourceDictionary.CONF_V_PLUGIN_KEY, ResourceDictionary.getViewerPluginKey());
083 permaLinkUrlBase = addParam(permaLinkUrlBase, ResourceDictionary.CONF_V_RESOURCE_KEY, ResourceDictionary.getViewerResourceKey());
084
085 return permaLinkUrlBase.substring(0, permaLinkUrlBase.length() - 1);
086 }
087
088 private String addParam(String url, String name, String value) {
089 return url.concat(name).concat("=").concat(value).concat("&");
090 }
091
092 private boolean isReplaceableParam(String param) {
093 for (String replaceable : PARAMS_TO_REPLACE) {
094 if (param.startsWith(replaceable + "=")) return true;
095 }
096 return false;
097 }
098
099 }