View Javadoc
1   /**
2    * This file Copyright (c) 2019 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.admincentral.findbar;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.periscope.operation.OperationRequest;
38  
39  import java.util.Optional;
40  import java.util.concurrent.atomic.AtomicBoolean;
41  
42  import com.vaadin.data.HasValue;
43  import com.vaadin.event.FieldEvents;
44  import com.vaadin.shared.Registration;
45  import com.vaadin.ui.Component;
46  import com.vaadin.ui.Label;
47  import com.vaadin.ui.Panel;
48  import com.vaadin.ui.TextField;
49  
50  /**
51   * A TextField-like component providing a visual aid when the query sniffer detects a valid command.
52   */
53  public class FindBarTextField extends Panel {
54  
55      private TextField textField = new TextField();
56      private AtomicBoolean textFieldHasFocus = new AtomicBoolean(false);
57      private OperationRequest command;
58      private final Label commandHint = new Label();
59  
60      FindBarTextField(SimpleTranslator i18n, Runnable search) {
61          this.textField.setSizeFull();
62          this.textField.addStyleNames("findbar");
63          this.textField.setPlaceholder(i18n.translate("findbar.placeholder"));
64          this.textField.addAttachListener(event -> search.run());
65          // be a bit more eager than the default 400 ms to make live type-ahead search feel more responsive
66          this.textField.setValueChangeTimeout(200);
67          this.textField.focus();
68          setContent(textField);
69          setSizeFull();
70  
71          this.commandHint.setStyleName("command");
72          this.commandHint.setValue((i18n.translate("findbar.command")));
73          this.commandHint.setVisible(false);
74      }
75  
76      @Override
77      public void focus() {
78          textField.focus();
79      }
80  
81      @SuppressWarnings("UnusedReturnValue")
82      Registration addFocusListener(FieldEvents.FocusListener listener) {
83          return textField.addFocusListener(listener);
84      }
85  
86      @SuppressWarnings("UnusedReturnValue")
87      Registration addBlurListener(FieldEvents.BlurListener listener) {
88          return textField.addBlurListener(listener);
89      }
90  
91      @SuppressWarnings("UnusedReturnValue")
92      Registration addValueChangeListener(HasValue.ValueChangeListener<String> listener) {
93          return textField.addValueChangeListener(listener);
94      }
95  
96      void clear() {
97          textField.clear();
98          clearCommand();
99      }
100 
101     String getValue() {
102         return textField.getValue();
103     }
104 
105     void setValue(String value) {
106         textField.setValue(value);
107     }
108 
109     boolean hasFocus() {
110         return textFieldHasFocus.get();
111     }
112 
113     void setFocusStatus(boolean focus) {
114         textFieldHasFocus.set(focus);
115         if (command == null && !focus) {
116             commandHint.setVisible(false);
117         }
118     }
119 
120     Component getCommandHint() {
121         return commandHint;
122     }
123 
124     Optional<OperationRequest> getCommand() {
125         return Optional.ofNullable(command);
126     }
127 
128     void setCommand(OperationRequest command) {
129         this.command = command;
130         commandHint.setVisible(true);
131     }
132 
133     void clearCommand() {
134         command = null;
135         commandHint.setVisible(false);
136     }
137 }