fixed tree update when moving a node to trash with no children but trashed ones,...
[pithos] / src / gr / ebs / gss / client / DisplayHelper.java
1 /*
2  * Copyright 2009, 2010 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client;
20
21 import java.util.List;
22
23 import com.google.gwt.user.client.ui.FlexTable;
24 import com.google.gwt.user.client.ui.ListBox;
25
26
27 /**
28  * A helper class with static methods that manipulate display
29  * widgets in various useful ways, not available in GWT.
30  *
31  * @author past
32  */
33 public class DisplayHelper {
34
35         /**
36          * A flag that denotes that no selection should be made while
37          * displaying the rows of a table.
38          */
39         public static final int NO_SELECTION = -1;
40
41         /**
42          * Clear any current selection in the specified ListBox.
43          */
44         public static void clearSelections(ListBox listBox) {
45                 for (int i=0; i<listBox.getItemCount(); i++)
46                         if (listBox.isItemSelected(i))
47                                 listBox.setItemSelected(i, false);
48         }
49
50         /**
51          * Select the item in the listBox whose value matches the provided
52          * value.
53          */
54         public static void selectMatch(ListBox listBox, String value) {
55                 for (int i=0; i<listBox.getItemCount(); i++)
56                         if (listBox.getValue(i).equals(value))
57                                 listBox.setItemSelected(i, true);
58                         else
59                                 listBox.setItemSelected(i, false);
60         }
61
62         /**
63          * Select the items in the listBox whose value matches the provided
64          * value list. Every value that is matched in the listBox is removed
65          * from the value list, in order to let the caller know what values
66          * were not matched. Therefore the caller must be prepared for the
67          * value list to be modified.
68          *
69          * @param listBox the ListBox
70          * @param values the list of values to be selected
71          */
72         public static void selectMultiMatch(ListBox listBox, List values) {
73                 for (int i=0; i<listBox.getItemCount(); i++)
74                         if (values.contains(listBox.getValue(i))) {
75                                 listBox.setItemSelected(i, true);
76                                 values.remove(listBox.getValue(i));
77                         } else
78                                 listBox.setItemSelected(i, false);
79         }
80
81         public static native void log(String message) /*-{
82                 var logger = $wnd.console;
83                 if (logger && logger.debug)
84                         logger.debug(message);
85                 else if (logger && logger.log)
86                         logger.log(message);
87         }-*/;
88
89         /**
90          * Make the specified row look like selected or not, according to the
91          * <code>selected</code> flag.
92          *
93          * @param row the row number in the list of entries (i.e. ignoring the header line)
94          * @param selected the flag that denotes whether the <code>styleName</code> should
95          *                              be added or removed
96          * @param styleName the name of the CSS style
97          */
98         public static void styleRow(final FlexTable table, final int row, final boolean selected, String styleName) {
99                 if (row != -1)
100                         if (selected)
101                                 table.getRowFormatter().addStyleName(row + 1, styleName);
102                         else
103                                 table.getRowFormatter().removeStyleName(row + 1, styleName);
104         }
105
106         /**
107          * Select the specified row in the table. This entails modifying its style
108          * as well as the style of the previously selected row.
109          *
110          * @param table the FlexTable widget
111          * @param row the newly selected row
112          * @param previousRow the previously selected row
113          * @param styleName the name of the CSS style
114          * @return the newly selected row
115          */
116         public static int selectRow(final FlexTable table, final int row, final int previousRow, String styleName) {
117                 // Reset the style of the previously selected row.
118                 styleRow(table, previousRow, false, styleName);
119                 // Select the row that was clicked.
120                 styleRow(table, row, true, styleName);
121                 return row;
122         }
123         /**
124          * The implementation of this trim method also checks for
125          * no brake space characters (nbsp) = '\00A0'
126          * and removes them
127          *
128          * @param input
129          * @return the new trimmed string without whitespace or no brake space
130          */
131         public static native String trim(String input) /*-{
132     if(input.length == 0)
133         return input;
134         if((input[0]||input[input.length-1]) != '\u0020' && (input[0]||input[input.length-1]) != '\u00A0')
135         return input;
136     var r1 = input.replace(/^(\s*)/, '');
137     var r2 = r1.replace(/\s*$/, '');
138     return r2;
139   }-*/;
140
141 }