2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client;
8 import com.google.gwt.user.client.ui.FlexTable;
9 import com.google.gwt.user.client.ui.ListBox;
13 * A helper class with static methods that manipulate display
14 * widgets in various useful ways, not available in GWT.
17 public class DisplayHelper {
20 * A flag that denotes that no selection should be made while
21 * displaying the rows of a table.
23 public static final int NO_SELECTION = -1;
26 * Clear any current selection in the specified ListBox.
28 public static void clearSelections(ListBox listBox) {
29 for (int i=0; i<listBox.getItemCount(); i++)
30 if (listBox.isItemSelected(i))
31 listBox.setItemSelected(i, false);
35 * Select the item in the listBox whose value matches the provided
38 public static void selectMatch(ListBox listBox, String value) {
39 for (int i=0; i<listBox.getItemCount(); i++)
40 if (listBox.getValue(i).equals(value))
41 listBox.setItemSelected(i, true);
43 listBox.setItemSelected(i, false);
47 * Select the items in the listBox whose value matches the provided
48 * value list. Every value that is matched in the listBox is removed
49 * from the value list, in order to let the caller know what values
50 * were not matched. Therefore the caller must be prepared for the
51 * value list to be modified.
53 * @param listBox the ListBox
54 * @param values the list of values to be selected
56 public static void selectMultiMatch(ListBox listBox, List values) {
57 for (int i=0; i<listBox.getItemCount(); i++)
58 if (values.contains(listBox.getValue(i))) {
59 listBox.setItemSelected(i, true);
60 values.remove(listBox.getValue(i));
62 listBox.setItemSelected(i, false);
65 public static native void log(String message) /*-{
66 var logger = $wnd.console;
67 if (logger && logger.debug)
68 logger.debug(message);
69 else if (logger && logger.log)
74 * Make the specified row look like selected or not, according to the
75 * <code>selected</code> flag.
77 * @param row the row number in the list of entries (i.e. ignoring the header line)
78 * @param selected the flag that denotes whether the <code>styleName</code> should
80 * @param styleName the name of the CSS style
82 public static void styleRow(final FlexTable table, final int row, final boolean selected, String styleName) {
85 table.getRowFormatter().addStyleName(row + 1, styleName);
87 table.getRowFormatter().removeStyleName(row + 1, styleName);
91 * Select the specified row in the table. This entails modifying its style
92 * as well as the style of the previously selected row.
94 * @param table the FlexTable widget
95 * @param row the newly selected row
96 * @param previousRow the previously selected row
97 * @param styleName the name of the CSS style
98 * @return the newly selected row
100 public static int selectRow(final FlexTable table, final int row, final int previousRow, String styleName) {
101 // Reset the style of the previously selected row.
102 styleRow(table, previousRow, false, styleName);
103 // Select the row that was clicked.
104 styleRow(table, row, true, styleName);
108 * The implementation of this trim method also checks for
109 * no brake space characters (nbsp) = '\00A0'
113 * @return the new trimmed string without whitespace or no brake space
115 public static native String trim(String input) /*-{
116 if(input.length == 0)
118 if((input[0]||input[input.length-1]) != '\u0020' && (input[0]||input[input.length-1]) != '\u00A0')
120 var r1 = input.replace(/^(\s*)/, '');
121 var r2 = r1.replace(/\s*$/, '');