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