Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / Search.java @ 6e6e914e

History | View | Annotate | Download (4.5 kB)

1
/*
2
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package gr.ebs.gss.client;
20

    
21
import com.google.gwt.event.dom.client.BlurEvent;
22
import com.google.gwt.event.dom.client.BlurHandler;
23
import com.google.gwt.event.dom.client.ClickEvent;
24
import com.google.gwt.event.dom.client.ClickHandler;
25
import com.google.gwt.event.dom.client.FocusEvent;
26
import com.google.gwt.event.dom.client.FocusHandler;
27
import com.google.gwt.event.dom.client.KeyPressEvent;
28
import com.google.gwt.event.dom.client.KeyPressHandler;
29
import com.google.gwt.resources.client.ClientBundle;
30
import com.google.gwt.resources.client.ImageResource;
31
import com.google.gwt.user.client.ui.AbstractImagePrototype;
32
import com.google.gwt.user.client.ui.Button;
33
import com.google.gwt.user.client.ui.Composite;
34
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
35
import com.google.gwt.user.client.ui.HasVerticalAlignment;
36
import com.google.gwt.user.client.ui.HorizontalPanel;
37
import com.google.gwt.user.client.ui.TextBox;
38
import com.google.gwt.user.client.ui.Widget;
39

    
40
/**
41
 * A component that contains the search form.
42
 */
43
public class Search extends Composite implements FocusHandler,BlurHandler {
44

    
45
        /**
46
         * The text hint that is displayed in the empty search box.
47
         */
48
        private static final String TEXT_HINT = "Search for files...";
49

    
50
        /**
51
         * Specifies the images that will be bundled for this Composite.
52
         */
53
        public interface Images extends ClientBundle {
54
                @Source("gr/ebs/gss/resources/search_16.png")
55
                ImageResource searchButton();
56
        }
57

    
58
        /**
59
         * The embedded text box widget that contains the search query.
60
         */
61
        private TextBox tb = new TextBox();
62

    
63
        /**
64
         * The search widget constructor.
65
         *
66
         * @param images the image bundle
67
         */
68
        public Search(final Images images) {
69
                tb.setWidth("200px");
70
                tb.setText(TEXT_HINT);
71
                tb.setStylePrimaryName("gss-search");
72
                tb.addStyleDependentName("empty");
73
                tb.addFocusHandler(this);
74
                tb.addBlurHandler(this);
75
                tb.addKeyPressHandler(new KeyPressHandler() {
76

    
77
                        @Override
78
                        public void onKeyPress(KeyPressEvent event) {
79
                                char keyCode = event.getCharCode();
80
                                if (keyCode == '\r')
81
                                        GSS.get().showSearchResults(tb.getText());
82
                                else if (keyCode == 27) {
83
                                        // Simulate the proper behavior for the escape key
84
                                        // (27 == ESC).
85
                                        onLostFocus((Widget)event.getSource());
86
                                        tb.setFocus(false);
87
                                }
88
                        }
89
                });
90

    
91
                Button b = new Button(createHeaderHTML(images.searchButton(), "Search"), new ClickHandler() {
92
                        @Override
93
                        public void onClick(ClickEvent event) {
94
                                GSS.get().showSearchResults(tb.getText());
95
                        }
96
                });
97

    
98
                HorizontalPanel panel = new HorizontalPanel();
99
                panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
100
                panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
101
                panel.add(tb);
102
                panel.add(b);
103
                initWidget(panel);
104
        }
105

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

    
120
        public void onLostFocus(Widget sender) {
121
                TextBox b = (TextBox) sender;
122
                if (b.getText().equals("")) {
123
                        b.addStyleDependentName("empty");
124
                        b.setText(TEXT_HINT);
125
                }
126
        }
127

    
128
        @Override
129
        public void onFocus(FocusEvent event) {
130
                TextBox b = (TextBox) event.getSource();
131
                if (b.getText().equals(TEXT_HINT))
132
                        b.setText("");
133
                b.removeStyleDependentName("empty");
134
        }
135

    
136
        @Override
137
        public void onBlur(BlurEvent event) {
138
                TextBox b = (TextBox) event.getSource();
139
                onLostFocus(b);
140
        }
141
}