Add Cut, Move to trash and Delete options to Other's shared context menu.
[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.user.client.ui.AbstractImagePrototype;\r
22 import com.google.gwt.user.client.ui.Button;\r
23 import com.google.gwt.user.client.ui.ClickListener;\r
24 import com.google.gwt.user.client.ui.Composite;\r
25 import com.google.gwt.user.client.ui.FocusListener;\r
26 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
27 import com.google.gwt.user.client.ui.HasVerticalAlignment;\r
28 import com.google.gwt.user.client.ui.HorizontalPanel;\r
29 import com.google.gwt.user.client.ui.ImageBundle;\r
30 import com.google.gwt.user.client.ui.KeyboardListenerAdapter;\r
31 import com.google.gwt.user.client.ui.TextBox;\r
32 import com.google.gwt.user.client.ui.Widget;\r
33 \r
34 /**\r
35  * A component that contains the search form.\r
36  */\r
37 public class Search extends Composite implements FocusListener {\r
38 \r
39         /**\r
40          * The text hint that is displayed in the empty search box.\r
41          */\r
42         private static final String TEXT_HINT = "Search for files...";\r
43 \r
44         /**\r
45          * Specifies the images that will be bundled for this Composite.\r
46          */\r
47         public interface Images extends ImageBundle {\r
48                 @Resource("gr/ebs/gss/resources/search_16.png")\r
49                 AbstractImagePrototype searchButton();\r
50         }\r
51 \r
52         /**\r
53          * The embedded text box widget that contains the search query.\r
54          */\r
55         private TextBox tb = new TextBox();\r
56 \r
57         /**\r
58          * The search widget constructor.\r
59          *\r
60          * @param images the image bundle\r
61          */\r
62         public Search(final Images images) {\r
63                 tb.setWidth("200px");\r
64                 tb.setText(TEXT_HINT);\r
65                 tb.setStylePrimaryName("gss-search");\r
66                 tb.addStyleDependentName("empty");\r
67                 tb.addFocusListener(this);\r
68                 tb.addKeyboardListener(new KeyboardListenerAdapter() {\r
69 \r
70                         @Override\r
71                         public void onKeyPress(Widget sender, char keyCode, int modifiers) {\r
72                                 if (keyCode == '\r')\r
73                                         GSS.get().showSearchResults(tb.getText());\r
74                                 else if (keyCode == 27) {\r
75                                         // Simulate the proper behavior for the escape key (27 ==\r
76                                         // ESC).\r
77                                         onLostFocus(sender);\r
78                                         tb.setFocus(false);\r
79                                 }\r
80                         }\r
81                 });\r
82 \r
83                 Button b = new Button(createHeaderHTML(images.searchButton(), "Search"), new ClickListener() {\r
84 \r
85                         public void onClick(Widget sender) {\r
86                                 GSS.get().showSearchResults(tb.getText());\r
87                         }\r
88                 });\r
89 \r
90                 HorizontalPanel panel = new HorizontalPanel();\r
91                 panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);\r
92                 panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);\r
93                 panel.add(tb);\r
94                 panel.add(b);\r
95                 initWidget(panel);\r
96         }\r
97 \r
98         /**\r
99          * Creates an HTML fragment that places an image & caption together.\r
100          *\r
101          * @param imageProto an image prototype for an image\r
102          * @param caption the caption\r
103          * @return the HTML fragment\r
104          */\r
105         private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {\r
106                 String captionHTML = "<table cellpadding='0' cellspacing='0'>" + "<tr><td>" +\r
107                         imageProto.getHTML() + "</td><td style='font-size: 90%;'>&nbsp;" +\r
108                         caption + "</td></tr></table>";\r
109                 return captionHTML;\r
110         }\r
111 \r
112         public void onFocus(Widget sender) {\r
113                 TextBox b = (TextBox) sender;\r
114                 if (b.getText().equals(TEXT_HINT))\r
115                         b.setText("");\r
116                 sender.removeStyleDependentName("empty");\r
117         }\r
118 \r
119         public void onLostFocus(Widget sender) {\r
120                 TextBox b = (TextBox) sender;\r
121                 if (b.getText().equals("")) {\r
122                         b.addStyleDependentName("empty");\r
123                         b.setText(TEXT_HINT);\r
124                 }\r
125         }\r
126 }\r