Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / Search.java @ 1206:292dec4eae08

History | View | Annotate | Download (4.6 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 org.gss_project.gss.web.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("org/gss_project/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.getElement().setId("textBox.search");
76
                tb.addKeyPressHandler(new KeyPressHandler() {
77

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

    
92
                Button b = new Button(createHeaderHTML(images.searchButton(), "Search"), new ClickHandler() {
93
                        @Override
94
                        public void onClick(ClickEvent event) {
95
                                GSS.get().showSearchResults(tb.getText());
96
                        }
97
                });
98
                b.getElement().setId("button.search");
99
                
100
                HorizontalPanel panel = new HorizontalPanel();
101
                panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
102
                panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
103
                panel.add(tb);
104
                panel.add(b);
105
                initWidget(panel);
106
        }
107

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

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

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

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