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;
021    
022    import com.google.gwt.i18n.client.Dictionary;
023    import com.google.gwt.i18n.client.NumberFormat;
024    import com.google.gwt.user.client.DOM;
025    import com.google.gwt.user.client.Element;
026    import com.google.gwt.user.client.Window;
027    import org.sonar.api.web.gwt.client.webservices.Resource;
028    
029    import java.util.Set;
030    
031    /**
032     * @since 1.10
033     */
034    public final class Utils {
035      private Utils() {
036      }
037    
038      public static String getConfiguration(String key) {
039        return getConfiguration(key, null);
040      }
041    
042      public static String getConfiguration(String key, String defaultValue) {
043        String result = getDictionaryEntry("config", key);
044        if (result == null) {
045          result = defaultValue;
046        }
047        return result;
048      }
049    
050      public static native void setConfiguration(String key, String val)  /*-{
051        $wnd.config[key] = val;
052      }-*/;
053    
054      public static String getRequestParameter(String key) {
055        return getDictionaryEntry("request_parameters", key);
056      }
057    
058      public static Set<String> getConfigurationKeys() {
059        return getDictionaryKeys("config");
060      }
061    
062      public static Set<String> getRequestParameterNames() {
063        return getDictionaryKeys("request_parameters");
064      }
065    
066      private static String getDictionaryEntry(String dictionaryName, String key) {
067        try {
068          Dictionary dic = Dictionary.getDictionary(dictionaryName);
069          if (dic != null) {
070            return dic.get(key);
071          }
072          return null;
073    
074        } catch (Exception e) {
075          return null;
076        }
077      }
078    
079      private static Set<String> getDictionaryKeys(String dictionaryName) {
080        Dictionary dic = Dictionary.getDictionary(dictionaryName);
081        if (dic != null) {
082          return dic.keySet();
083        }
084        return null;
085      }
086    
087      public static String widgetGWTIdJSEncode(String widgetGWTId) {
088        return widgetGWTId.replace('.', '_');
089      }
090    
091      public static String getServerUrl() {
092        return getConfiguration("sonar_url");
093      }
094    
095      public static String getServerApiUrl() {
096        return getServerUrl() + "/api";
097      }
098    
099      public static String escapeHtml(String maybeHtml) {
100        final Element div = DOM.createDiv();
101        DOM.setInnerText(div, maybeHtml);
102        return DOM.getInnerHTML(div);
103      }
104    
105      public static String formatPercent(String percentage) {
106        return percentage == null || percentage.equals("") ? "" : formatPercent(new Double(percentage));
107      }
108    
109      public static String formatPercent(double percentage) {
110        return NumberFormat.getFormat("0.0").format(percentage) + "%";
111      }
112    
113      public static String formatNumber(String number) {
114        return number == null || number.equals("") ? "" : formatNumber(new Double(number));
115      }
116    
117      public static String formatNumber(double number) {
118        return NumberFormat.getDecimalFormat().format(number);
119      }
120    
121      public static native void showError(String message) /*-{
122        $wnd.error(message);
123      }-*/;
124    
125      public static native void showWarning(String message) /*-{
126        $wnd.warning(message);
127      }-*/;
128    
129      public static native void showInfo(String message) /*-{
130        $wnd.info(message);
131      }-*/;
132    
133      /**
134       * Display the resource in a popup.
135       *
136       * @param resource  the resource to display, not null
137       * @param metricKey the metric to highlight (optional : can be null)
138       */
139      public static void openResourcePopup(final Resource resource, final String metricKey) {
140        String url = Utils.getServerUrl() + "/resource/index/" + resource.getId();
141        if (metricKey != null) {
142          url += "?" + ResourceDictionary.CONF_V_METRIC_KEY + "=" + metricKey;
143        }
144        Window.open(url, "resource", "height=800,width=900,scrollbars=1,resizable=1");
145      }
146    }
147    
148