1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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 }