Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / DisplayHelper.java @ 63366925

History | View | Annotate | Download (5.3 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import java.util.List;
38

    
39
import com.google.gwt.user.client.ui.FlexTable;
40
import com.google.gwt.user.client.ui.ListBox;
41

    
42

    
43
/**
44
 * A helper class with static methods that manipulate display
45
 * widgets in various useful ways, not available in GWT.
46
 *
47
 */
48
public class DisplayHelper {
49

    
50
        /**
51
         * A flag that denotes that no selection should be made while
52
         * displaying the rows of a table.
53
         */
54
        public static final int NO_SELECTION = -1;
55

    
56
        /**
57
         * Clear any current selection in the specified ListBox.
58
         */
59
        public static void clearSelections(ListBox listBox) {
60
                for (int i=0; i<listBox.getItemCount(); i++)
61
                        if (listBox.isItemSelected(i))
62
                                listBox.setItemSelected(i, false);
63
        }
64

    
65
        /**
66
         * Select the item in the listBox whose value matches the provided
67
         * value.
68
         */
69
        public static void selectMatch(ListBox listBox, String value) {
70
                for (int i=0; i<listBox.getItemCount(); i++)
71
                        if (listBox.getValue(i).equals(value))
72
                                listBox.setItemSelected(i, true);
73
                        else
74
                                listBox.setItemSelected(i, false);
75
        }
76

    
77
        /**
78
         * Select the items in the listBox whose value matches the provided
79
         * value list. Every value that is matched in the listBox is removed
80
         * from the value list, in order to let the caller know what values
81
         * were not matched. Therefore the caller must be prepared for the
82
         * value list to be modified.
83
         *
84
         * @param listBox the ListBox
85
         * @param values the list of values to be selected
86
         */
87
        public static void selectMultiMatch(ListBox listBox, List values) {
88
                for (int i=0; i<listBox.getItemCount(); i++)
89
                        if (values.contains(listBox.getValue(i))) {
90
                                listBox.setItemSelected(i, true);
91
                                values.remove(listBox.getValue(i));
92
                        } else
93
                                listBox.setItemSelected(i, false);
94
        }
95

    
96
        public static native void log(String message) /*-{
97
                var logger = $wnd.console;
98
                  if (logger && logger.debug)
99
                        logger.debug(message);
100
                else if (logger && logger.log)
101
                        logger.log(message);
102
        }-*/;
103

    
104
        /**
105
         * Make the specified row look like selected or not, according to the
106
         * <code>selected</code> flag.
107
         *
108
         * @param row the row number in the list of entries (i.e. ignoring the header line)
109
         * @param selected the flag that denotes whether the <code>styleName</code> should
110
         *                                 be added or removed
111
         * @param styleName the name of the CSS style
112
         */
113
        public static void styleRow(final FlexTable table, final int row, final boolean selected, String styleName) {
114
                if (row != -1)
115
                        if (selected)
116
                                table.getRowFormatter().addStyleName(row + 1, styleName);
117
                        else
118
                                table.getRowFormatter().removeStyleName(row + 1, styleName);
119
        }
120

    
121
        /**
122
         * Select the specified row in the table. This entails modifying its style
123
         * as well as the style of the previously selected row.
124
         *
125
         * @param table the FlexTable widget
126
         * @param row the newly selected row
127
         * @param previousRow the previously selected row
128
         * @param styleName the name of the CSS style
129
         * @return the newly selected row
130
         */
131
        public static int selectRow(final FlexTable table, final int row, final int previousRow, String styleName) {
132
                // Reset the style of the previously selected row.
133
                styleRow(table, previousRow, false, styleName);
134
                // Select the row that was clicked.
135
                styleRow(table, row, true, styleName);
136
                return row;
137
        }
138
        /**
139
         * The implementation of this trim method also checks for
140
         * no brake space characters (nbsp) = '\00A0'
141
         * and removes them
142
         *
143
         * @param input
144
         * @return the new trimmed string without whitespace or no brake space
145
         */
146
        public static native String trim(String input) /*-{
147
    if(input.length == 0)
148
            return input;
149
        if((input[0]||input[input.length-1]) != '\u0020' && (input[0]||input[input.length-1]) != '\u00A0')
150
            return input;
151
    var r1 = input.replace(/^(\s*)/, '');
152
    var r2 = r1.replace(/\s*$/, '');
153
    return r2;
154
  }-*/;
155

    
156
}