Enable focus inside the text box when creating a new group. This solves Issue 36.
[pithos] / src / gr / ebs / gss / client / Search.java
1 /*\r
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.client;\r
20 \r
21 import com.google.gwt.event.dom.client.BlurEvent;\r
22 import com.google.gwt.event.dom.client.BlurHandler;\r
23 import com.google.gwt.event.dom.client.ClickEvent;\r
24 import com.google.gwt.event.dom.client.ClickHandler;\r
25 import com.google.gwt.event.dom.client.FocusEvent;\r
26 import com.google.gwt.event.dom.client.FocusHandler;\r
27 import com.google.gwt.event.dom.client.KeyPressEvent;\r
28 import com.google.gwt.event.dom.client.KeyPressHandler;\r
29 import com.google.gwt.resources.client.ClientBundle;\r
30 import com.google.gwt.resources.client.ImageResource;\r
31 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
32 import com.google.gwt.user.client.ui.Button;\r
33 import com.google.gwt.user.client.ui.Composite;\r
34 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
35 import com.google.gwt.user.client.ui.HasVerticalAlignment;\r
36 import com.google.gwt.user.client.ui.HorizontalPanel;\r
37 import com.google.gwt.user.client.ui.TextBox;\r
38 import com.google.gwt.user.client.ui.Widget;\r
39 \r
40 /**\r
41  * A component that contains the search form.\r
42  */\r
43 public class Search extends Composite implements FocusHandler,BlurHandler {\r
44 \r
45         /**\r
46          * The text hint that is displayed in the empty search box.\r
47          */\r
48         private static final String TEXT_HINT = "Search for files...";\r
49 \r
50         /**\r
51          * Specifies the images that will be bundled for this Composite.\r
52          */\r
53         public interface Images extends ClientBundle {\r
54                 @Source("gr/ebs/gss/resources/search_16.png")\r
55                 ImageResource searchButton();\r
56         }\r
57 \r
58         /**\r
59          * The embedded text box widget that contains the search query.\r
60          */\r
61         private TextBox tb = new TextBox();\r
62 \r
63         /**\r
64          * The search widget constructor.\r
65          *\r
66          * @param images the image bundle\r
67          */\r
68         public Search(final Images images) {\r
69                 tb.setWidth("200px");\r
70                 tb.setText(TEXT_HINT);\r
71                 tb.setStylePrimaryName("gss-search");\r
72                 tb.addStyleDependentName("empty");\r
73                 tb.addFocusHandler(this);\r
74                 tb.addBlurHandler(this);\r
75                 tb.addKeyPressHandler(new KeyPressHandler() {\r
76 \r
77                         @Override\r
78                         public void onKeyPress(KeyPressEvent event) {\r
79                                 char keyCode = event.getCharCode();\r
80                                 if (keyCode == '\r')\r
81                                         GSS.get().showSearchResults(tb.getText());\r
82                                 else if (keyCode == 27) {\r
83                                         // Simulate the proper behavior for the escape key\r
84                                         // (27 == ESC).\r
85                                         onLostFocus((Widget)event.getSource());\r
86                                         tb.setFocus(false);\r
87                                 }\r
88                         }\r
89                 });\r
90 \r
91                 Button b = new Button(createHeaderHTML(images.searchButton(), "Search"), new ClickHandler() {\r
92                         @Override\r
93                         public void onClick(ClickEvent event) {\r
94                                 GSS.get().showSearchResults(tb.getText());\r
95                         }\r
96                 });\r
97 \r
98                 HorizontalPanel panel = new HorizontalPanel();\r
99                 panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);\r
100                 panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);\r
101                 panel.add(tb);\r
102                 panel.add(b);\r
103                 initWidget(panel);\r
104         }\r
105 \r
106         /**\r
107          * Creates an HTML fragment that places an image & caption together.\r
108          *\r
109          * @param imageProto an image prototype for an image\r
110          * @param caption the caption\r
111          * @return the HTML fragment\r
112          */\r
113         private String createHeaderHTML(ImageResource imageProto, String caption) {\r
114                 String captionHTML = "<table cellpadding='0' cellspacing='0'>" + "<tr><td>" +\r
115                 AbstractImagePrototype.create(imageProto).getHTML() + "</td><td style='font-size: 90%;'>&nbsp;" +\r
116                         caption + "</td></tr></table>";\r
117                 return captionHTML;\r
118         }\r
119 \r
120         public void onLostFocus(Widget sender) {\r
121                 TextBox b = (TextBox) sender;\r
122                 if (b.getText().equals("")) {\r
123                         b.addStyleDependentName("empty");\r
124                         b.setText(TEXT_HINT);\r
125                 }\r
126         }\r
127 \r
128         @Override\r
129         public void onFocus(FocusEvent event) {\r
130                 TextBox b = (TextBox) event.getSource();\r
131                 if (b.getText().equals(TEXT_HINT))\r
132                         b.setText("");\r
133                 b.removeStyleDependentName("empty");\r
134         }\r
135 \r
136         @Override\r
137         public void onBlur(BlurEvent event) {\r
138                 TextBox b = (TextBox) event.getSource();\r
139                 onLostFocus(b);\r
140         }\r
141 }\r