root / web_client / src / gr / grnet / pithos / web / client / Search.java @ 9e8e14e4
History | View | Annotate | Download (3.6 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2011 Greek Research and Technology Network
|
3 |
*/
|
4 |
package gr.grnet.pithos.web.client; |
5 |
|
6 |
import com.google.gwt.event.dom.client.BlurEvent; |
7 |
import com.google.gwt.event.dom.client.BlurHandler; |
8 |
import com.google.gwt.event.dom.client.ClickEvent; |
9 |
import com.google.gwt.event.dom.client.ClickHandler; |
10 |
import com.google.gwt.event.dom.client.FocusEvent; |
11 |
import com.google.gwt.event.dom.client.FocusHandler; |
12 |
import com.google.gwt.event.dom.client.KeyPressEvent; |
13 |
import com.google.gwt.event.dom.client.KeyPressHandler; |
14 |
import com.google.gwt.resources.client.ClientBundle; |
15 |
import com.google.gwt.resources.client.ImageResource; |
16 |
import com.google.gwt.user.client.ui.AbstractImagePrototype; |
17 |
import com.google.gwt.user.client.ui.Button; |
18 |
import com.google.gwt.user.client.ui.Composite; |
19 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
20 |
import com.google.gwt.user.client.ui.HasVerticalAlignment; |
21 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
22 |
import com.google.gwt.user.client.ui.TextBox; |
23 |
import com.google.gwt.user.client.ui.Widget; |
24 |
|
25 |
/**
|
26 |
* A component that contains the search form.
|
27 |
*/
|
28 |
public class Search extends Composite implements FocusHandler,BlurHandler { |
29 |
|
30 |
/**
|
31 |
* The text hint that is displayed in the empty search box.
|
32 |
*/
|
33 |
private static final String TEXT_HINT = "Search for files..."; |
34 |
|
35 |
/**
|
36 |
* Specifies the images that will be bundled for this Composite.
|
37 |
*/
|
38 |
public interface Images extends ClientBundle { |
39 |
@Source("gr/grnet/pithos/resources/search_16.png") |
40 |
ImageResource searchButton(); |
41 |
} |
42 |
|
43 |
/**
|
44 |
* The embedded text box widget that contains the search query.
|
45 |
*/
|
46 |
private TextBox tb = new TextBox(); |
47 |
|
48 |
/**
|
49 |
* The search widget constructor.
|
50 |
*
|
51 |
* @param images the image bundle
|
52 |
*/
|
53 |
public Search(final Images images) { |
54 |
tb.setWidth("200px");
|
55 |
tb.setText(TEXT_HINT); |
56 |
tb.setStylePrimaryName("pithos-search");
|
57 |
tb.addStyleDependentName("empty");
|
58 |
tb.addFocusHandler(this);
|
59 |
tb.addBlurHandler(this);
|
60 |
tb.getElement().setId("textBox.search");
|
61 |
tb.addKeyPressHandler(new KeyPressHandler() {
|
62 |
|
63 |
@Override
|
64 |
public void onKeyPress(KeyPressEvent event) { |
65 |
char keyCode = event.getCharCode();
|
66 |
if (keyCode == 27) { |
67 |
// Simulate the proper behavior for the escape key
|
68 |
// (27 == ESC).
|
69 |
onLostFocus((Widget)event.getSource()); |
70 |
tb.setFocus(false);
|
71 |
} |
72 |
} |
73 |
}); |
74 |
|
75 |
HorizontalPanel panel = new HorizontalPanel();
|
76 |
panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE); |
77 |
panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER); |
78 |
panel.add(tb); |
79 |
initWidget(panel); |
80 |
} |
81 |
|
82 |
/**
|
83 |
* Creates an HTML fragment that places an image & caption together.
|
84 |
*
|
85 |
* @param imageProto an image prototype for an image
|
86 |
* @param caption the caption
|
87 |
* @return the HTML fragment
|
88 |
*/
|
89 |
private String createHeaderHTML(ImageResource imageProto, String caption) { |
90 |
String captionHTML = "<table cellpadding='0' cellspacing='0'>" + "<tr><td>" + |
91 |
AbstractImagePrototype.create(imageProto).getHTML() + "</td><td style='font-size: 90%;'> " +
|
92 |
caption + "</td></tr></table>";
|
93 |
return captionHTML;
|
94 |
} |
95 |
|
96 |
public void onLostFocus(Widget sender) { |
97 |
TextBox b = (TextBox) sender; |
98 |
if (b.getText().equals("")) { |
99 |
b.addStyleDependentName("empty");
|
100 |
b.setText(TEXT_HINT); |
101 |
} |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public void onFocus(FocusEvent event) { |
106 |
TextBox b = (TextBox) event.getSource(); |
107 |
if (b.getText().equals(TEXT_HINT))
|
108 |
b.setText("");
|
109 |
b.removeStyleDependentName("empty");
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public void onBlur(BlurEvent event) { |
114 |
TextBox b = (TextBox) event.getSource(); |
115 |
onLostFocus(b); |
116 |
} |
117 |
} |