Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / Search.java @ 58777026

History | View | Annotate | Download (5.1 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import com.google.gwt.event.dom.client.BlurEvent;
38
import com.google.gwt.event.dom.client.BlurHandler;
39
import com.google.gwt.event.dom.client.ClickEvent;
40
import com.google.gwt.event.dom.client.ClickHandler;
41
import com.google.gwt.event.dom.client.FocusEvent;
42
import com.google.gwt.event.dom.client.FocusHandler;
43
import com.google.gwt.event.dom.client.KeyPressEvent;
44
import com.google.gwt.event.dom.client.KeyPressHandler;
45
import com.google.gwt.resources.client.ClientBundle;
46
import com.google.gwt.resources.client.ImageResource;
47
import com.google.gwt.user.client.ui.AbstractImagePrototype;
48
import com.google.gwt.user.client.ui.Button;
49
import com.google.gwt.user.client.ui.Composite;
50
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
51
import com.google.gwt.user.client.ui.HasVerticalAlignment;
52
import com.google.gwt.user.client.ui.HorizontalPanel;
53
import com.google.gwt.user.client.ui.TextBox;
54
import com.google.gwt.user.client.ui.Widget;
55

    
56
/**
57
 * A component that contains the search form.
58
 */
59
public class Search extends Composite implements FocusHandler,BlurHandler {
60

    
61
        /**
62
         * The text hint that is displayed in the empty search box.
63
         */
64
        private static final String TEXT_HINT = "Search for files...";
65

    
66
        /**
67
         * Specifies the images that will be bundled for this Composite.
68
         */
69
        public interface Images extends ClientBundle {
70
                @Source("gr/grnet/pithos/resources/search_16.png")
71
                ImageResource searchButton();
72
        }
73

    
74
        /**
75
         * The embedded text box widget that contains the search query.
76
         */
77
        private TextBox tb = new TextBox();
78

    
79
        /**
80
         * The search widget constructor.
81
         *
82
         * @param images the image bundle
83
         */
84
        public Search(final Images images) {
85
                tb.setWidth("200px");
86
                tb.setText(TEXT_HINT);
87
                tb.setStylePrimaryName("pithos-search");
88
                tb.addStyleDependentName("empty");
89
                tb.addFocusHandler(this);
90
                tb.addBlurHandler(this);
91
                tb.getElement().setId("textBox.search");
92
                tb.addKeyPressHandler(new KeyPressHandler() {
93

    
94
                        @Override
95
                        public void onKeyPress(KeyPressEvent event) {
96
                                char keyCode = event.getCharCode();
97
                if (keyCode == 27) {
98
                                        // Simulate the proper behavior for the escape key
99
                                        // (27 == ESC).
100
                                        onLostFocus((Widget)event.getSource());
101
                                        tb.setFocus(false);
102
                                }
103
                        }
104
                });
105

    
106
                HorizontalPanel panel = new HorizontalPanel();
107
                panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
108
                panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
109
                panel.add(tb);
110
                initWidget(panel);
111
        }
112

    
113
        /**
114
         * Creates an HTML fragment that places an image & caption together.
115
         *
116
         * @param imageProto an image prototype for an image
117
         * @param caption the caption
118
         * @return the HTML fragment
119
         */
120
        private String createHeaderHTML(ImageResource imageProto, String caption) {
121
                String captionHTML = "<table cellpadding='0' cellspacing='0'>" + "<tr><td>" +
122
                AbstractImagePrototype.create(imageProto).getHTML() + "</td><td style='font-size: 90%;'>&nbsp;" +
123
                        caption + "</td></tr></table>";
124
                return captionHTML;
125
        }
126

    
127
        public void onLostFocus(Widget sender) {
128
                TextBox b = (TextBox) sender;
129
                if (b.getText().equals("")) {
130
                        b.addStyleDependentName("empty");
131
                        b.setText(TEXT_HINT);
132
                }
133
        }
134

    
135
        @Override
136
        public void onFocus(FocusEvent event) {
137
                TextBox b = (TextBox) event.getSource();
138
                if (b.getText().equals(TEXT_HINT))
139
                        b.setText("");
140
                b.removeStyleDependentName("empty");
141
        }
142

    
143
        @Override
144
        public void onBlur(BlurEvent event) {
145
                TextBox b = (TextBox) event.getSource();
146
                onLostFocus(b);
147
        }
148
}