Moved all web client files to their folder. Updated ignored files
[pithos-web-client] / src / org / gss_project / gss / web / 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 org.gss_project.gss.web.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("org/gss_project/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.getElement().setId("textBox.search");\r
76                 tb.addKeyPressHandler(new KeyPressHandler() {\r
77 \r
78                         @Override\r
79                         public void onKeyPress(KeyPressEvent event) {\r
80                                 char keyCode = event.getCharCode();\r
81                                 if (keyCode == '\r')\r
82                                         GSS.get().showSearchResults(tb.getText());\r
83                                 else if (keyCode == 27) {\r
84                                         // Simulate the proper behavior for the escape key\r
85                                         // (27 == ESC).\r
86                                         onLostFocus((Widget)event.getSource());\r
87                                         tb.setFocus(false);\r
88                                 }\r
89                         }\r
90                 });\r
91 \r
92                 Button b = new Button(createHeaderHTML(images.searchButton(), "Search"), new ClickHandler() {\r
93                         @Override\r
94                         public void onClick(ClickEvent event) {\r
95                                 GSS.get().showSearchResults(tb.getText());\r
96                         }\r
97                 });\r
98                 b.getElement().setId("button.search");\r
99                 \r
100                 HorizontalPanel panel = new HorizontalPanel();\r
101                 panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);\r
102                 panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);\r
103                 panel.add(tb);\r
104                 panel.add(b);\r
105                 initWidget(panel);\r
106         }\r
107 \r
108         /**\r
109          * Creates an HTML fragment that places an image & caption together.\r
110          *\r
111          * @param imageProto an image prototype for an image\r
112          * @param caption the caption\r
113          * @return the HTML fragment\r
114          */\r
115         private String createHeaderHTML(ImageResource imageProto, String caption) {\r
116                 String captionHTML = "<table cellpadding='0' cellspacing='0'>" + "<tr><td>" +\r
117                 AbstractImagePrototype.create(imageProto).getHTML() + "</td><td style='font-size: 90%;'>&nbsp;" +\r
118                         caption + "</td></tr></table>";\r
119                 return captionHTML;\r
120         }\r
121 \r
122         public void onLostFocus(Widget sender) {\r
123                 TextBox b = (TextBox) sender;\r
124                 if (b.getText().equals("")) {\r
125                         b.addStyleDependentName("empty");\r
126                         b.setText(TEXT_HINT);\r
127                 }\r
128         }\r
129 \r
130         @Override\r
131         public void onFocus(FocusEvent event) {\r
132                 TextBox b = (TextBox) event.getSource();\r
133                 if (b.getText().equals(TEXT_HINT))\r
134                         b.setText("");\r
135                 b.removeStyleDependentName("empty");\r
136         }\r
137 \r
138         @Override\r
139         public void onBlur(BlurEvent event) {\r
140                 TextBox b = (TextBox) event.getSource();\r
141                 onLostFocus(b);\r
142         }\r
143 }\r