Hardcoded pithos site url
[pithos] / web_client / src / gr / grnet / pithos / web / client / DisplayHelper.java
1 /*
2  *  Copyright (c) 2011 Greek Research and Technology Network
3  */
4 package gr.grnet.pithos.web.client;
5
6 import java.util.List;
7
8 import com.google.gwt.user.client.ui.FlexTable;
9 import com.google.gwt.user.client.ui.ListBox;
10
11
12 /**
13  * A helper class with static methods that manipulate display
14  * widgets in various useful ways, not available in GWT.
15  *
16  */
17 public class DisplayHelper {
18
19         /**
20          * A flag that denotes that no selection should be made while
21          * displaying the rows of a table.
22          */
23         public static final int NO_SELECTION = -1;
24
25         /**
26          * Clear any current selection in the specified ListBox.
27          */
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);
32         }
33
34         /**
35          * Select the item in the listBox whose value matches the provided
36          * value.
37          */
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);
42                         else
43                                 listBox.setItemSelected(i, false);
44         }
45
46         /**
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.
52          *
53          * @param listBox the ListBox
54          * @param values the list of values to be selected
55          */
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));
61                         } else
62                                 listBox.setItemSelected(i, false);
63         }
64
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)
70                         logger.log(message);
71         }-*/;
72
73         /**
74          * Make the specified row look like selected or not, according to the
75          * <code>selected</code> flag.
76          *
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
79          *                              be added or removed
80          * @param styleName the name of the CSS style
81          */
82         public static void styleRow(final FlexTable table, final int row, final boolean selected, String styleName) {
83                 if (row != -1)
84                         if (selected)
85                                 table.getRowFormatter().addStyleName(row + 1, styleName);
86                         else
87                                 table.getRowFormatter().removeStyleName(row + 1, styleName);
88         }
89
90         /**
91          * Select the specified row in the table. This entails modifying its style
92          * as well as the style of the previously selected row.
93          *
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
99          */
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);
105                 return row;
106         }
107         /**
108          * The implementation of this trim method also checks for
109          * no brake space characters (nbsp) = '\00A0'
110          * and removes them
111          *
112          * @param input
113          * @return the new trimmed string without whitespace or no brake space
114          */
115         public static native String trim(String input) /*-{
116     if(input.length == 0)
117         return input;
118         if((input[0]||input[input.length-1]) != '\u0020' && (input[0]||input[input.length-1]) != '\u00A0')
119         return input;
120     var r1 = input.replace(/^(\s*)/, '');
121     var r2 = r1.replace(/\s*$/, '');
122     return r2;
123   }-*/;
124
125 }