| 1 |
/* |
| 2 |
* Copyright (C) 2011 Openismus GmbH |
| 3 |
* |
| 4 |
* This file is part of GWT-Glom. |
| 5 |
* |
| 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. |
| 10 |
* |
| 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 |
| 14 |
* for more details. |
| 15 |
* |
| 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/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
package org.glom.web.client; |
| 21 |
|
| 22 |
import org.glom.web.shared.DataItem; |
| 23 |
import org.glom.web.shared.GlomNumericFormat; |
| 24 |
import org.glom.web.shared.TypedDataItem; |
| 25 |
import org.glom.web.shared.layout.LayoutItemField.GlomFieldType; |
| 26 |
|
| 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; |
| 34 |
|
| 35 |
/** |
| 36 |
* |
| 37 |
*/ |
| 38 |
public class Utils { |
| 39 |
|
| 40 |
public static NumberFormat getNumberFormat(GlomNumericFormat glomNumericFormat) { |
| 41 |
|
| 42 |
StringBuilder pattern = new StringBuilder("0."); |
| 43 |
|
| 44 |
// add pattern for thousands separator |
| 45 |
if (glomNumericFormat.getUseThousandsSeparator()) { |
| 46 |
pattern.insert(0, "#,##"); |
| 47 |
} |
| 48 |
|
| 49 |
// add pattern for restricted decimal places |
| 50 |
if (glomNumericFormat.getDecimalPlacesRestricted()) { |
| 51 |
for (int i = 0; i < glomNumericFormat.getDecimalPlaces(); i++) { |
| 52 |
pattern.append('0'); |
| 53 |
} |
| 54 |
} else { |
| 55 |
// The default precision in libglom is 15. |
| 56 |
pattern.append("###############"); |
| 57 |
} |
| 58 |
|
| 59 |
// TODO use exponential numbers when more than 15 decimal places |
| 60 |
|
| 61 |
return NumberFormat.getFormat(pattern.toString()); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 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. |
| 69 |
* |
| 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. |
| 73 |
* |
| 74 |
* @param widget |
| 75 |
* get the height information for this widget |
| 76 |
* @return the height of the widget with styling applied |
| 77 |
*/ |
| 78 |
public static int getWidgetHeight(Widget widget) { |
| 79 |
Document doc = Document.get(); |
| 80 |
com.google.gwt.dom.client.Element div = doc.createDivElement(); |
| 81 |
|
| 82 |
// Hidden elements are invisible, don't respond to events and are not part of the tab order. But they will take |
| 83 |
// up space. |
| 84 |
div.getStyle().setVisibility(Visibility.HIDDEN); |
| 85 |
div.appendChild(widget.getElement().<com.google.gwt.user.client.Element> cast()); |
| 86 |
|
| 87 |
doc.getBody().appendChild(div); |
| 88 |
int height = widget.getOffsetHeight(); |
| 89 |
doc.getBody().removeChild(div); |
| 90 |
div = null; |
| 91 |
|
| 92 |
return height; |
| 93 |
} |
| 94 |
|
| 95 |
public static TypedDataItem getTypedDataItem(GlomFieldType glomFieldType, DataItem dataItem) { |
| 96 |
TypedDataItem primaryKeyItem = new TypedDataItem(); |
| 97 |
switch (glomFieldType) { |
| 98 |
case TYPE_BOOLEAN: |
| 99 |
primaryKeyItem.setBoolean(dataItem.getBoolean()); |
| 100 |
break; |
| 101 |
case TYPE_NUMERIC: |
| 102 |
primaryKeyItem.setNumber(dataItem.getNumber()); |
| 103 |
break; |
| 104 |
case TYPE_TEXT: |
| 105 |
primaryKeyItem.setText(new String(dataItem.getText() == null ? "" : dataItem.getText())); |
| 106 |
break; |
| 107 |
default: |
| 108 |
GWT.log("getTypedDataItem(): Unsupported Glom Field Type: " + glomFieldType); |
| 109 |
break; |
| 110 |
} |
| 111 |
|
| 112 |
return primaryKeyItem; |
| 113 |
} |
| 114 |
|
| 115 |
public static String getCurrentLocaleName() { |
| 116 |
String localeID = LocaleInfo.getCurrentLocale().getLocaleName(); |
| 117 |
if(localeID == "default") |
| 118 |
{ |
| 119 |
localeID = ""; //This is how libglom refers to the default locale. |
| 120 |
} |
| 121 |
|
| 122 |
if(StringUtils.isEmpty(localeID)) |
| 123 |
{ |
| 124 |
// LocaleInfo.getCurrentLocale() returns "default" even if a real locale was specified in the URL, |
| 125 |
// if the locale is not specified as supported in our OnlineGlom.gwt.xml file, |
| 126 |
// but people could use locales in .glom files that we have not thought of, |
| 127 |
// so we should allow their use by getting the query parameter value directly: |
| 128 |
final String paramValue = Window.Location.getParameter(LocaleInfo.getLocaleQueryParam()); |
| 129 |
localeID = paramValue; |
| 130 |
} |
| 131 |
|
| 132 |
return localeID; |
| 133 |
} |
| 134 |
|
| 135 |
} |