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.gwt;
021    
022    import com.google.gwt.user.client.Window;
023    
024    public final class Links {
025    
026      public static final String DEFAULT_POPUP_HTML_FEATURES = "height=800,width=900,scrollbars=1,resizable=1";
027    
028      private Links() {
029        // only static methods
030      }
031    
032      public static String baseUrl() {
033        return Configuration.getParameter("sonar_url");
034      }
035    
036      public static String apiUrl() {
037        return baseUrl() + "/api";
038      }
039    
040    
041      public static String urlForResource(String resourceIdOrKey) {
042        return urlForMeasure(resourceIdOrKey, null);
043      }
044    
045      public static String urlForMeasure(String resourceIdOrKey, String metricKey) {
046        String url = baseUrl() + "/resource/index/" + resourceIdOrKey;
047        if (metricKey != null) {
048          url += "?metric=" + metricKey;
049        }
050        return url;
051      }
052      
053      /**
054       *
055       * @param resourceIdOrKey
056       * @param pageId
057       * @param query additional query parameters. Can be null. Example "layout=false&param1=val1"
058       */
059      public static String urlForResourcePage(String resourceIdOrKey, String pageId, String query) {
060        String url = baseUrl() + "/plugins/resource/";
061        if (resourceIdOrKey != null) {
062          url += resourceIdOrKey;
063        }
064        url += "?page=";
065        url += pageId;
066        if (query != null) {
067          url += "&";
068          url += query;
069        }
070        return url;
071      }
072    
073      public static String urlForRule(String ruleIdOrKey, boolean showLayout) {
074        return baseUrl() + "/rules/show/" + ruleIdOrKey + "?layout=" + showLayout;
075      }
076    
077      public static void openResourcePopup(final String resourceIdOrKey) {
078        openMeasurePopup(resourceIdOrKey, null);
079      }
080    
081      /**
082       * Open the resource in a popup with HTML features like: height=800,width=900,scrollbars=1,resizable=1
083       *
084       * @param resourceIdOrKey the id or key of the resource to display, not null
085       * @param metricKey       the metric to highlight (optional : can be null)
086       */
087      public static void openMeasurePopup(final String resourceIdOrKey, final String metricKey) {
088        openMeasurePopup(resourceIdOrKey, metricKey, DEFAULT_POPUP_HTML_FEATURES);
089      }
090    
091    
092      public static void openMeasurePopup(final String resourceKey, final String metricKey, final String htmlFeatures) {
093        String url = urlForMeasure(resourceKey, metricKey);
094        Window.open(url, "resource", htmlFeatures);
095      }
096    
097    
098      public static void openResourcePage(final String pageId, final String resourceIdOrKey, final String query) {
099        String url = urlForResourcePage(pageId, resourceIdOrKey, query);
100        Window.Location.assign(url);
101      }
102    }