Remove the redundant gss top-level directory.
[pithos] / src / gr / ebs / gss / client / DisplayHelper.java
1 /*
2  * Copyright 2009 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 import com.google.gwt.user.client.ui.SuggestBox;
26
27
28 /**
29  * A helper class with static methods that manipulate display
30  * widgets in various useful ways, not available in GWT.
31  *
32  * @author past
33  */
34 public class DisplayHelper {
35
36         /**
37          * A flag that denotes that no selection should be made while
38          * displaying the rows of a table.
39          */
40         public static final int NO_SELECTION = -1;
41
42         /**
43          * Hide the SuggestBox's PopupPanel before updating the oracle suggestions.
44          * Since SuggestBox is not meant to be extended and its internals are
45          * inaccessible, we have to resort to JSNI to call the required methods.
46          *
47          * @param sb the SuggestBox whose panel will be hidden
48          */
49         public static final native void hideSuggestions(SuggestBox sb) /*-{
50                 sb.@com.google.gwt.user.client.ui.SuggestBox::suggestionPopup.@com.google.gwt.user.client.ui.PopupPanel::hide()();
51                 sb.@com.google.gwt.user.client.ui.SuggestBox::suggestionMenu.@com.google.gwt.user.client.ui.MenuBar::clearItems()();
52         }-*/;
53
54         /**
55          * Display the SuggestBox's PopupPanel after updating the oracle suggestions.
56          * Since SuggestBox is not meant to be extended and its internals are
57          * inaccessible, we have to resort to JSNI to call the required method.
58          *
59          * @param sb the SuggestBox whose panel will be shown
60          * @param query the query whose suggestions will be shown
61          */
62         public static final native void showSuggestions(SuggestBox sb, String query) /*-{
63                 sb.@com.google.gwt.user.client.ui.SuggestBox::showSuggestions(Ljava/lang/String;)(query);
64         }-*/;
65
66         /**
67          * Clear any current selection in the specified ListBox.
68          */
69         public static void clearSelections(ListBox listBox) {
70                 for (int i=0; i<listBox.getItemCount(); i++)
71                         if (listBox.isItemSelected(i))
72                                 listBox.setItemSelected(i, false);
73         }
74
75         /**
76          * Select the item in the listBox whose value matches the provided
77          * value.
78          */
79         public static void selectMatch(ListBox listBox, String value) {
80                 for (int i=0; i<listBox.getItemCount(); i++)
81                         if (listBox.getValue(i).equals(value))
82                                 listBox.setItemSelected(i, true);
83                         else
84                                 listBox.setItemSelected(i, false);
85         }
86
87         /**
88          * Select the items in the listBox whose value matches the provided
89          * value list. Every value that is matched in the listBox is removed
90          * from the value list, in order to let the caller know what values
91          * were not matched. Therefore the caller must be prepared for the
92          * value list to be modified.
93          *
94          * @param listBox the ListBox
95          * @param values the list of values to be selected
96          */
97         public static void selectMultiMatch(ListBox listBox, List values) {
98                 for (int i=0; i<listBox.getItemCount(); i++)
99                         if (values.contains(listBox.getValue(i))) {
100                                 listBox.setItemSelected(i, true);
101                                 values.remove(listBox.getValue(i));
102                         } else
103                                 listBox.setItemSelected(i, false);
104         }
105
106         public static native void log(String message) /*-{
107                 $wnd.console.debug(message);
108         }-*/;
109
110         /**
111          * Make the specified row look like selected or not, according to the
112          * <code>selected</code> flag.
113          *
114          * @param row the row number in the list of entries (i.e. ignoring the header line)
115          * @param selected the flag that denotes whether the <code>styleName</code> should
116          *                              be added or removed
117          * @param styleName the name of the CSS style
118          */
119         public static void styleRow(final FlexTable table, final int row, final boolean selected, String styleName) {
120                 if (row != -1)
121                         if (selected)
122                                 table.getRowFormatter().addStyleName(row + 1, styleName);
123                         else
124                                 table.getRowFormatter().removeStyleName(row + 1, styleName);
125         }
126
127         /**
128          * Select the specified row in the table. This entails modifying its style
129          * as well as the style of the previously selected row.
130          *
131          * @param table the FlexTable widget
132          * @param row the newly selected row
133          * @param previousRow the previously selected row
134          * @param styleName the name of the CSS style
135          * @return the newly selected row
136          */
137         public static int selectRow(final FlexTable table, final int row, final int previousRow, String styleName) {
138                 // Reset the style of the previously selected row.
139                 styleRow(table, previousRow, false, styleName);
140                 // Select the row that was clicked.
141                 styleRow(table, row, true, styleName);
142                 return row;
143         }
144
145 }