2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
6 * GWT-Glom is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
11 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with GWT-Glom. If not, see <http://www.gnu.org/licenses/>.
20 package org.glom.web.client;
22 import org.glom.web.shared.DataItem;
23 import org.glom.web.shared.TypedDataItem;
24 import org.glom.web.shared.libglom.Field.GlomFieldType;
25 import org.glom.web.shared.libglom.NumericFormat;
27 import com.google.gwt.core.client.GWT;
28 import com.google.gwt.dom.client.Document;
29 import com.google.gwt.dom.client.Style.Visibility;
30 import com.google.gwt.i18n.client.LocaleInfo;
31 import com.google.gwt.i18n.client.NumberFormat;
32 import com.google.gwt.user.client.Window;
33 import com.google.gwt.user.client.ui.Widget;
40 public static NumberFormat getNumberFormat(final NumericFormat numericFormat) {
42 final StringBuilder pattern = new StringBuilder("0.");
44 // add pattern for thousands separator
45 if (numericFormat.getUseThousandsSeparator()) {
46 pattern.insert(0, "#,##");
49 // add pattern for restricted decimal places
50 if (numericFormat.getDecimalPlacesRestricted()) {
51 for (int i = 0; i < numericFormat.getDecimalPlaces(); i++) {
55 // The default precision in libglom is 15.
56 pattern.append("###############");
59 // TODO use exponential numbers when more than 15 decimal places
61 return NumberFormat.getFormat(pattern.toString());
66 * Get the vertical height with decorations (i.e. CSS) by temporarily adding the widget to the body element of the
67 * document in a transparent container. This is required because the size information is only available when the
68 * widget is attached to the DOM.
70 * This method must be called before the widget is added to its container because it will be removed from any
71 * container it is already inside. TODO: Fix this problem by saving a reference to its parent and re-addding it
72 * after the height information has been calculated.
75 * get the height information for this widget
76 * @return the height of the widget with styling applied
78 public static int getWidgetHeight(final Widget widget) {
79 final Document doc = Document.get();
80 com.google.gwt.dom.client.Element div = doc.createDivElement();
82 // Hidden elements are invisible, don't respond to events, and are not part of the tab order. But they will take
84 div.getStyle().setVisibility(Visibility.HIDDEN);
85 div.appendChild(widget.getElement().<com.google.gwt.dom.client.Element> cast());
87 doc.getBody().appendChild(div);
88 final int height = widget.getOffsetHeight();
89 doc.getBody().removeChild(div);
95 public static TypedDataItem getTypedDataItem(final GlomFieldType glomFieldType, final DataItem dataItem) {
96 final TypedDataItem primaryKeyItem = new TypedDataItem();
97 switch (glomFieldType) {
99 primaryKeyItem.setBoolean(dataItem.getBoolean());
102 primaryKeyItem.setNumber(dataItem.getNumber());
105 primaryKeyItem.setText(new String(dataItem.getText() == null ? "" : dataItem.getText()));
108 GWT.log("getTypedDataItem(): Unsupported Glom Field Type: " + glomFieldType);
112 return primaryKeyItem;
115 public static String getCurrentLocaleID() {
116 String localeID = LocaleInfo.getCurrentLocale().getLocaleName();
117 if (localeID.equals("default")) {
118 localeID = ""; // This is how libglom refers to the default locale.
121 if (StringUtils.isEmpty(localeID)) {
122 // LocaleInfo.getCurrentLocale() returns "default" even if a real locale was specified in the URL,
123 // if the locale is not specified as supported in our OnlineGlom.gwt.xml file,
124 // but people could use locales in .glom files that we have not thought of,
125 // so we should allow their use by getting the query parameter value directly:
126 final String paramValue = Window.Location.getParameter(LocaleInfo.getLocaleQueryParam());
127 localeID = paramValue;
129 // Prevent a null string from being used,
130 // in case the caller does not expect it.
131 if (localeID == null) {