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