View Javadoc
1   /**
2    * This file Copyright (c) 2015-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.workbench.contenttool.search;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.ui.vaadin.extension.ShortcutProtector;
38  import info.magnolia.ui.vaadin.icon.Icon;
39  
40  import java.util.Arrays;
41  
42  import javax.inject.Inject;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import com.vaadin.event.FieldEvents.FocusListener;
47  import com.vaadin.event.ShortcutAction;
48  import com.vaadin.ui.Button;
49  import com.vaadin.ui.Component;
50  import com.vaadin.ui.CssLayout;
51  import com.vaadin.v7.data.Property;
52  import com.vaadin.v7.ui.TextField;
53  
54  /**
55   * Implementation of {@link SearchContentToolView}.
56   */
57  public class SearchContentToolViewImpl extends CssLayout implements SearchContentToolView {
58  
59      private final SimpleTranslator i18n;
60  
61      private TextField searchField;
62  
63      private SearchContentToolView.Listener listener;
64  
65      private final Property.ValueChangeListener searchFieldListener = new Property.ValueChangeListener() {
66  
67          @Override
68          public void valueChange(Property.ValueChangeEvent event) {
69              listener.onSearch(searchField.getValue());
70  
71              boolean hasSearchContent = !searchField.getValue().isEmpty();
72              if (hasSearchContent) {
73                  addStyleName("has-content");
74              } else {
75                  removeStyleName("has-content");
76              }
77              searchField.focus();
78          }
79      };
80  
81      @Inject
82      public SearchContentToolViewImpl(SimpleTranslator i18n) {
83          this.i18n = i18n;
84          Button clearSearchBoxButton = new Button();
85          clearSearchBoxButton.setStyleName("m-closebutton");
86          clearSearchBoxButton.addStyleName("icon-delete-search");
87          clearSearchBoxButton.addStyleName("searchbox-clearbutton");
88          // Preventing the button to spoil the tab-navigation due to its changing display value.
89          clearSearchBoxButton.setTabIndex(-1);
90          clearSearchBoxButton.addClickListener((Button.ClickListener) event -> searchField.clear());
91  
92          Icon searchIcon = new Icon("search");
93          searchIcon.addStyleName("searchbox-icon");
94  
95          searchField = buildSearchField();
96  
97          setVisible(true);
98          addComponent(searchIcon);
99          addComponent(searchField);
100         addComponent(clearSearchBoxButton);
101         setStyleName("searchbox");
102     }
103 
104     private TextField buildSearchField() {
105         final TextField field = new TextField();
106         ShortcutProtector.extend(field, Arrays.asList(ShortcutAction.KeyCode.ENTER));
107         final String inputPrompt = i18n.translate("toolbar.search.prompt");
108 
109         field.setInputPrompt(inputPrompt);
110         field.setSizeUndefined();
111         field.addStyleName("searchfield");
112 
113         // TextField has to be immediate to fire value changes when pressing Enter, avoiding ShortcutListener overkill.
114         field.addValueChangeListener(searchFieldListener);
115 
116         field.addFocusListener((FocusListener) event -> {
117             // put the cursor at the end of the field
118             TextField tf = (TextField) event.getSource();
119             tf.setCursorPosition(tf.getValue().length());
120         });
121 
122         // No blur handler.
123         return field;
124     }
125 
126     @Override
127     public void setListener(SearchContentToolView.Listener listener) {
128         this.listener = listener;
129     }
130 
131     @Override
132     public void setSearchQuery(String searchQuery) {
133         if (searchField == null) {
134             return;
135         }
136         // turn off value change listener, so that presenter does not think there was user input and searches again
137         searchField.removeValueChangeListener(searchFieldListener);
138         if (StringUtils.isNotBlank(searchQuery)) {
139             searchField.setValue(searchQuery);
140             searchField.focus();
141         } else {
142             searchField.setValue("");
143             removeStyleName("has-content");
144         }
145         searchField.addValueChangeListener(searchFieldListener);
146     }
147 
148     @Override
149     public Component asVaadinComponent() {
150         return this;
151     }
152 }