Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.1 kB)

1 ab1eb3f8 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 ab1eb3f8 Christos Stathis
 */
35 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client;
36 ab1eb3f8 Christos Stathis
37 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.BlurEvent;
38 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.BlurHandler;
39 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
40 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
41 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.FocusEvent;
42 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.FocusHandler;
43 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.KeyPressEvent;
44 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.KeyPressHandler;
45 ab1eb3f8 Christos Stathis
import com.google.gwt.resources.client.ClientBundle;
46 ab1eb3f8 Christos Stathis
import com.google.gwt.resources.client.ImageResource;
47 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
48 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Button;
49 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Composite;
50 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
51 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HasVerticalAlignment;
52 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
53 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.TextBox;
54 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Widget;
55 ab1eb3f8 Christos Stathis
56 ab1eb3f8 Christos Stathis
/**
57 ab1eb3f8 Christos Stathis
 * A component that contains the search form.
58 ab1eb3f8 Christos Stathis
 */
59 ab1eb3f8 Christos Stathis
public class Search extends Composite implements FocusHandler,BlurHandler {
60 ab1eb3f8 Christos Stathis
61 ab1eb3f8 Christos Stathis
        /**
62 ab1eb3f8 Christos Stathis
         * The text hint that is displayed in the empty search box.
63 ab1eb3f8 Christos Stathis
         */
64 ab1eb3f8 Christos Stathis
        private static final String TEXT_HINT = "Search for files...";
65 ab1eb3f8 Christos Stathis
66 ab1eb3f8 Christos Stathis
        /**
67 ab1eb3f8 Christos Stathis
         * Specifies the images that will be bundled for this Composite.
68 ab1eb3f8 Christos Stathis
         */
69 ab1eb3f8 Christos Stathis
        public interface Images extends ClientBundle {
70 ab1eb3f8 Christos Stathis
                @Source("gr/grnet/pithos/resources/search_16.png")
71 ab1eb3f8 Christos Stathis
                ImageResource searchButton();
72 ab1eb3f8 Christos Stathis
        }
73 ab1eb3f8 Christos Stathis
74 ab1eb3f8 Christos Stathis
        /**
75 ab1eb3f8 Christos Stathis
         * The embedded text box widget that contains the search query.
76 ab1eb3f8 Christos Stathis
         */
77 ab1eb3f8 Christos Stathis
        private TextBox tb = new TextBox();
78 ab1eb3f8 Christos Stathis
79 ab1eb3f8 Christos Stathis
        /**
80 ab1eb3f8 Christos Stathis
         * The search widget constructor.
81 ab1eb3f8 Christos Stathis
         *
82 ab1eb3f8 Christos Stathis
         * @param images the image bundle
83 ab1eb3f8 Christos Stathis
         */
84 ab1eb3f8 Christos Stathis
        public Search(final Images images) {
85 ab1eb3f8 Christos Stathis
                tb.setWidth("200px");
86 ab1eb3f8 Christos Stathis
                tb.setText(TEXT_HINT);
87 ab1eb3f8 Christos Stathis
                tb.setStylePrimaryName("pithos-search");
88 ab1eb3f8 Christos Stathis
                tb.addStyleDependentName("empty");
89 ab1eb3f8 Christos Stathis
                tb.addFocusHandler(this);
90 ab1eb3f8 Christos Stathis
                tb.addBlurHandler(this);
91 ab1eb3f8 Christos Stathis
                tb.getElement().setId("textBox.search");
92 ab1eb3f8 Christos Stathis
                tb.addKeyPressHandler(new KeyPressHandler() {
93 ab1eb3f8 Christos Stathis
94 ab1eb3f8 Christos Stathis
                        @Override
95 ab1eb3f8 Christos Stathis
                        public void onKeyPress(KeyPressEvent event) {
96 ab1eb3f8 Christos Stathis
                                char keyCode = event.getCharCode();
97 9e8e14e4 Christos Stathis
                if (keyCode == 27) {
98 ab1eb3f8 Christos Stathis
                                        // Simulate the proper behavior for the escape key
99 ab1eb3f8 Christos Stathis
                                        // (27 == ESC).
100 ab1eb3f8 Christos Stathis
                                        onLostFocus((Widget)event.getSource());
101 ab1eb3f8 Christos Stathis
                                        tb.setFocus(false);
102 ab1eb3f8 Christos Stathis
                                }
103 ab1eb3f8 Christos Stathis
                        }
104 ab1eb3f8 Christos Stathis
                });
105 ab1eb3f8 Christos Stathis
106 ab1eb3f8 Christos Stathis
                HorizontalPanel panel = new HorizontalPanel();
107 ab1eb3f8 Christos Stathis
                panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
108 ab1eb3f8 Christos Stathis
                panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
109 ab1eb3f8 Christos Stathis
                panel.add(tb);
110 ab1eb3f8 Christos Stathis
                initWidget(panel);
111 ab1eb3f8 Christos Stathis
        }
112 ab1eb3f8 Christos Stathis
113 ab1eb3f8 Christos Stathis
        /**
114 ab1eb3f8 Christos Stathis
         * Creates an HTML fragment that places an image & caption together.
115 ab1eb3f8 Christos Stathis
         *
116 ab1eb3f8 Christos Stathis
         * @param imageProto an image prototype for an image
117 ab1eb3f8 Christos Stathis
         * @param caption the caption
118 ab1eb3f8 Christos Stathis
         * @return the HTML fragment
119 ab1eb3f8 Christos Stathis
         */
120 ab1eb3f8 Christos Stathis
        private String createHeaderHTML(ImageResource imageProto, String caption) {
121 ab1eb3f8 Christos Stathis
                String captionHTML = "<table cellpadding='0' cellspacing='0'>" + "<tr><td>" +
122 ab1eb3f8 Christos Stathis
                AbstractImagePrototype.create(imageProto).getHTML() + "</td><td style='font-size: 90%;'>&nbsp;" +
123 ab1eb3f8 Christos Stathis
                        caption + "</td></tr></table>";
124 ab1eb3f8 Christos Stathis
                return captionHTML;
125 ab1eb3f8 Christos Stathis
        }
126 ab1eb3f8 Christos Stathis
127 ab1eb3f8 Christos Stathis
        public void onLostFocus(Widget sender) {
128 ab1eb3f8 Christos Stathis
                TextBox b = (TextBox) sender;
129 ab1eb3f8 Christos Stathis
                if (b.getText().equals("")) {
130 ab1eb3f8 Christos Stathis
                        b.addStyleDependentName("empty");
131 ab1eb3f8 Christos Stathis
                        b.setText(TEXT_HINT);
132 ab1eb3f8 Christos Stathis
                }
133 ab1eb3f8 Christos Stathis
        }
134 ab1eb3f8 Christos Stathis
135 ab1eb3f8 Christos Stathis
        @Override
136 ab1eb3f8 Christos Stathis
        public void onFocus(FocusEvent event) {
137 ab1eb3f8 Christos Stathis
                TextBox b = (TextBox) event.getSource();
138 ab1eb3f8 Christos Stathis
                if (b.getText().equals(TEXT_HINT))
139 ab1eb3f8 Christos Stathis
                        b.setText("");
140 ab1eb3f8 Christos Stathis
                b.removeStyleDependentName("empty");
141 ab1eb3f8 Christos Stathis
        }
142 ab1eb3f8 Christos Stathis
143 ab1eb3f8 Christos Stathis
        @Override
144 ab1eb3f8 Christos Stathis
        public void onBlur(BlurEvent event) {
145 ab1eb3f8 Christos Stathis
                TextBox b = (TextBox) event.getSource();
146 ab1eb3f8 Christos Stathis
                onLostFocus(b);
147 ab1eb3f8 Christos Stathis
        }
148 ab1eb3f8 Christos Stathis
}