root / web_client / src / gr / grnet / pithos / web / client / DisplayHelper.java @ ba0078a6
History | View | Annotate | Download (5.3 kB)
1 | ab1eb3f8 | Christos Stathis | /*
|
---|---|---|---|
2 | 58777026 | Christos Stathis | * Copyright 2011 GRNET S.A. All rights reserved.
|
3 | 58777026 | Christos Stathis | *
|
4 | 58777026 | Christos Stathis | * Redistribution and use in source and binary forms, with or
|
5 | 58777026 | Christos Stathis | * without modification, are permitted provided that the following
|
6 | 58777026 | Christos Stathis | * conditions are met:
|
7 | 58777026 | Christos Stathis | *
|
8 | 58777026 | Christos Stathis | * 1. Redistributions of source code must retain the above
|
9 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
10 | 58777026 | Christos Stathis | * disclaimer.
|
11 | 58777026 | Christos Stathis | *
|
12 | 58777026 | Christos Stathis | * 2. Redistributions in binary form must reproduce the above
|
13 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
14 | 58777026 | Christos Stathis | * disclaimer in the documentation and/or other materials
|
15 | 58777026 | Christos Stathis | * provided with the distribution.
|
16 | 58777026 | Christos Stathis | *
|
17 | 58777026 | Christos Stathis | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 | 58777026 | Christos Stathis | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 | 58777026 | Christos Stathis | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 | 58777026 | Christos Stathis | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 | 58777026 | Christos Stathis | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 | 58777026 | Christos Stathis | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 | 58777026 | Christos Stathis | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 | 58777026 | Christos Stathis | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 | 58777026 | Christos Stathis | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 | 58777026 | Christos Stathis | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 | 58777026 | Christos Stathis | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 | 58777026 | Christos Stathis | * POSSIBILITY OF SUCH DAMAGE.
|
29 | 58777026 | Christos Stathis | *
|
30 | 58777026 | Christos Stathis | * The views and conclusions contained in the software and
|
31 | 58777026 | Christos Stathis | * documentation are those of the authors and should not be
|
32 | 58777026 | Christos Stathis | * interpreted as representing official policies, either expressed
|
33 | 58777026 | Christos Stathis | * or implied, of GRNET S.A.
|
34 | ab1eb3f8 | Christos Stathis | */
|
35 | ab1eb3f8 | Christos Stathis | package gr.grnet.pithos.web.client; |
36 | ab1eb3f8 | Christos Stathis | |
37 | ab1eb3f8 | Christos Stathis | import java.util.List; |
38 | ab1eb3f8 | Christos Stathis | |
39 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.FlexTable; |
40 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.ListBox; |
41 | ab1eb3f8 | Christos Stathis | |
42 | ab1eb3f8 | Christos Stathis | |
43 | ab1eb3f8 | Christos Stathis | /**
|
44 | ab1eb3f8 | Christos Stathis | * A helper class with static methods that manipulate display
|
45 | ab1eb3f8 | Christos Stathis | * widgets in various useful ways, not available in GWT.
|
46 | ab1eb3f8 | Christos Stathis | *
|
47 | ab1eb3f8 | Christos Stathis | */
|
48 | ab1eb3f8 | Christos Stathis | public class DisplayHelper { |
49 | ab1eb3f8 | Christos Stathis | |
50 | ab1eb3f8 | Christos Stathis | /**
|
51 | ab1eb3f8 | Christos Stathis | * A flag that denotes that no selection should be made while
|
52 | ab1eb3f8 | Christos Stathis | * displaying the rows of a table.
|
53 | ab1eb3f8 | Christos Stathis | */
|
54 | ab1eb3f8 | Christos Stathis | public static final int NO_SELECTION = -1; |
55 | ab1eb3f8 | Christos Stathis | |
56 | ab1eb3f8 | Christos Stathis | /**
|
57 | ab1eb3f8 | Christos Stathis | * Clear any current selection in the specified ListBox.
|
58 | ab1eb3f8 | Christos Stathis | */
|
59 | ab1eb3f8 | Christos Stathis | public static void clearSelections(ListBox listBox) { |
60 | ab1eb3f8 | Christos Stathis | for (int i=0; i<listBox.getItemCount(); i++) |
61 | ab1eb3f8 | Christos Stathis | if (listBox.isItemSelected(i))
|
62 | ab1eb3f8 | Christos Stathis | listBox.setItemSelected(i, false);
|
63 | ab1eb3f8 | Christos Stathis | } |
64 | ab1eb3f8 | Christos Stathis | |
65 | ab1eb3f8 | Christos Stathis | /**
|
66 | ab1eb3f8 | Christos Stathis | * Select the item in the listBox whose value matches the provided
|
67 | ab1eb3f8 | Christos Stathis | * value.
|
68 | ab1eb3f8 | Christos Stathis | */
|
69 | ab1eb3f8 | Christos Stathis | public static void selectMatch(ListBox listBox, String value) { |
70 | ab1eb3f8 | Christos Stathis | for (int i=0; i<listBox.getItemCount(); i++) |
71 | ab1eb3f8 | Christos Stathis | if (listBox.getValue(i).equals(value))
|
72 | ab1eb3f8 | Christos Stathis | listBox.setItemSelected(i, true);
|
73 | ab1eb3f8 | Christos Stathis | else
|
74 | ab1eb3f8 | Christos Stathis | listBox.setItemSelected(i, false);
|
75 | ab1eb3f8 | Christos Stathis | } |
76 | ab1eb3f8 | Christos Stathis | |
77 | ab1eb3f8 | Christos Stathis | /**
|
78 | ab1eb3f8 | Christos Stathis | * Select the items in the listBox whose value matches the provided
|
79 | ab1eb3f8 | Christos Stathis | * value list. Every value that is matched in the listBox is removed
|
80 | ab1eb3f8 | Christos Stathis | * from the value list, in order to let the caller know what values
|
81 | ab1eb3f8 | Christos Stathis | * were not matched. Therefore the caller must be prepared for the
|
82 | ab1eb3f8 | Christos Stathis | * value list to be modified.
|
83 | ab1eb3f8 | Christos Stathis | *
|
84 | ab1eb3f8 | Christos Stathis | * @param listBox the ListBox
|
85 | ab1eb3f8 | Christos Stathis | * @param values the list of values to be selected
|
86 | ab1eb3f8 | Christos Stathis | */
|
87 | ab1eb3f8 | Christos Stathis | public static void selectMultiMatch(ListBox listBox, List values) { |
88 | ab1eb3f8 | Christos Stathis | for (int i=0; i<listBox.getItemCount(); i++) |
89 | ab1eb3f8 | Christos Stathis | if (values.contains(listBox.getValue(i))) {
|
90 | ab1eb3f8 | Christos Stathis | listBox.setItemSelected(i, true);
|
91 | ab1eb3f8 | Christos Stathis | values.remove(listBox.getValue(i)); |
92 | ab1eb3f8 | Christos Stathis | } else
|
93 | ab1eb3f8 | Christos Stathis | listBox.setItemSelected(i, false);
|
94 | ab1eb3f8 | Christos Stathis | } |
95 | ab1eb3f8 | Christos Stathis | |
96 | ab1eb3f8 | Christos Stathis | public static native void log(String message) /*-{ |
97 | ab1eb3f8 | Christos Stathis | var logger = $wnd.console;
|
98 | ab1eb3f8 | Christos Stathis | if (logger && logger.debug)
|
99 | ab1eb3f8 | Christos Stathis | logger.debug(message);
|
100 | ab1eb3f8 | Christos Stathis | else if (logger && logger.log)
|
101 | ab1eb3f8 | Christos Stathis | logger.log(message);
|
102 | ab1eb3f8 | Christos Stathis | }-*/;
|
103 | ab1eb3f8 | Christos Stathis | |
104 | ab1eb3f8 | Christos Stathis | /**
|
105 | ab1eb3f8 | Christos Stathis | * Make the specified row look like selected or not, according to the
|
106 | ab1eb3f8 | Christos Stathis | * <code>selected</code> flag.
|
107 | ab1eb3f8 | Christos Stathis | *
|
108 | ab1eb3f8 | Christos Stathis | * @param row the row number in the list of entries (i.e. ignoring the header line)
|
109 | ab1eb3f8 | Christos Stathis | * @param selected the flag that denotes whether the <code>styleName</code> should
|
110 | ab1eb3f8 | Christos Stathis | * be added or removed
|
111 | ab1eb3f8 | Christos Stathis | * @param styleName the name of the CSS style
|
112 | ab1eb3f8 | Christos Stathis | */
|
113 | ab1eb3f8 | Christos Stathis | public static void styleRow(final FlexTable table, final int row, final boolean selected, String styleName) { |
114 | ab1eb3f8 | Christos Stathis | if (row != -1) |
115 | ab1eb3f8 | Christos Stathis | if (selected)
|
116 | ab1eb3f8 | Christos Stathis | table.getRowFormatter().addStyleName(row + 1, styleName);
|
117 | ab1eb3f8 | Christos Stathis | else
|
118 | ab1eb3f8 | Christos Stathis | table.getRowFormatter().removeStyleName(row + 1, styleName);
|
119 | ab1eb3f8 | Christos Stathis | } |
120 | ab1eb3f8 | Christos Stathis | |
121 | ab1eb3f8 | Christos Stathis | /**
|
122 | ab1eb3f8 | Christos Stathis | * Select the specified row in the table. This entails modifying its style
|
123 | ab1eb3f8 | Christos Stathis | * as well as the style of the previously selected row.
|
124 | ab1eb3f8 | Christos Stathis | *
|
125 | ab1eb3f8 | Christos Stathis | * @param table the FlexTable widget
|
126 | ab1eb3f8 | Christos Stathis | * @param row the newly selected row
|
127 | ab1eb3f8 | Christos Stathis | * @param previousRow the previously selected row
|
128 | ab1eb3f8 | Christos Stathis | * @param styleName the name of the CSS style
|
129 | ab1eb3f8 | Christos Stathis | * @return the newly selected row
|
130 | ab1eb3f8 | Christos Stathis | */
|
131 | ab1eb3f8 | Christos Stathis | public static int selectRow(final FlexTable table, final int row, final int previousRow, String styleName) { |
132 | ab1eb3f8 | Christos Stathis | // Reset the style of the previously selected row.
|
133 | ab1eb3f8 | Christos Stathis | styleRow(table, previousRow, false, styleName);
|
134 | ab1eb3f8 | Christos Stathis | // Select the row that was clicked.
|
135 | ab1eb3f8 | Christos Stathis | styleRow(table, row, true, styleName);
|
136 | ab1eb3f8 | Christos Stathis | return row;
|
137 | ab1eb3f8 | Christos Stathis | } |
138 | ab1eb3f8 | Christos Stathis | /**
|
139 | ab1eb3f8 | Christos Stathis | * The implementation of this trim method also checks for
|
140 | ab1eb3f8 | Christos Stathis | * no brake space characters (nbsp) = '\00A0'
|
141 | ab1eb3f8 | Christos Stathis | * and removes them
|
142 | ab1eb3f8 | Christos Stathis | *
|
143 | ab1eb3f8 | Christos Stathis | * @param input
|
144 | ab1eb3f8 | Christos Stathis | * @return the new trimmed string without whitespace or no brake space
|
145 | ab1eb3f8 | Christos Stathis | */
|
146 | ab1eb3f8 | Christos Stathis | public static native String trim(String input) /*-{ |
147 | ab1eb3f8 | Christos Stathis | if(input.length == 0)
|
148 | ab1eb3f8 | Christos Stathis | return input;
|
149 | ab1eb3f8 | Christos Stathis | if((input[0]||input[input.length-1]) != '\u0020' && (input[0]||input[input.length-1]) != '\u00A0')
|
150 | ab1eb3f8 | Christos Stathis | return input;
|
151 | ab1eb3f8 | Christos Stathis | var r1 = input.replace(/^(\s*)/, '');
|
152 | ab1eb3f8 | Christos Stathis | var r2 = r1.replace(/\s*$/, '');
|
153 | ab1eb3f8 | Christos Stathis | return r2;
|
154 | ab1eb3f8 | Christos Stathis | }-*/;
|
155 | ab1eb3f8 | Christos Stathis | |
156 | ab1eb3f8 | Christos Stathis | } |