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.ui.field;
35
36 import com.vaadin.ui.Button;
37 import com.vaadin.ui.Component;
38 import com.vaadin.ui.CustomField;
39 import com.vaadin.ui.HorizontalLayout;
40 import com.vaadin.ui.NativeButton;
41 import com.vaadin.ui.TextField;
42 import com.vaadin.ui.VerticalLayout;
43
44
45
46
47
48 public class LinkField extends CustomField<String> {
49
50 private final VerticalLayout rootLayout = new VerticalLayout();
51 private final HorizontalLayout linkLayout = new HorizontalLayout();
52 private final TextField textField = new TextField();
53 private final Button selectButton = new NativeButton();
54
55 private boolean editable;
56 private Component contentPreview;
57 private String buttonCaptionNew;
58 private String buttonCaptionOther;
59 private String value;
60
61 @Override
62 protected Component initContent() {
63 addStyleName("linkfield");
64
65 rootLayout.setSizeFull();
66 rootLayout.setSpacing(true);
67
68 textField.setWidth(100, Unit.PERCENTAGE);
69 textField.addValueChangeListener(event -> {
70
71 setValue(event.getValue());
72 });
73
74 linkLayout.setSizeFull();
75 linkLayout.addComponent(textField);
76 linkLayout.setExpandRatio(textField, 1);
77
78 if (!textField.isReadOnly()) {
79 selectButton.addStyleName("magnoliabutton");
80 linkLayout.addComponent(selectButton);
81 linkLayout.setExpandRatio(selectButton, 0);
82 }
83
84 rootLayout.addComponent(linkLayout);
85
86 addValueChangeListener(event -> updateComponents());
87 updateComponents();
88
89 return rootLayout;
90 }
91
92 @Override
93 public void setReadOnly(boolean readOnly) {
94 super.setReadOnly(readOnly);
95 textField.setReadOnly(readOnly);
96 }
97
98 @Override
99 protected void doSetValue(String value) {
100 this.value = value;
101 this.textField.setValue(value);
102 }
103
104 @Override
105 public String getValue() {
106 return value;
107 }
108
109 @Override
110 public boolean isEmpty() {
111 return getValue() == null;
112 }
113
114 public boolean isEditable() {
115 return editable;
116 }
117
118 public void setEditable(boolean editable) {
119 this.editable = editable;
120 }
121
122 public void setButtonCaptionNew(String buttonCaptionNew) {
123 this.buttonCaptionNew = buttonCaptionNew;
124 }
125
126 public void setButtonCaptionOther(String buttonCaptionOther) {
127 this.buttonCaptionOther = buttonCaptionOther;
128 }
129
130 public void setPlaceholder(String placeholder) {
131 textField.setPlaceholder(placeholder);
132 }
133
134 public void setDisableOnClick(boolean disable) {
135 selectButton.setDisableOnClick(disable);
136 }
137
138 public void addClickListener(Button.ClickListener buttonClickListener) {
139 selectButton.addClickListener(buttonClickListener);
140 }
141
142 @Override
143 public void setEnabled(boolean enabled) {
144 super.setEnabled(enabled);
145 textField.setEnabled(enabled);
146 selectButton.setEnabled(enabled);
147 }
148
149 public void setContentPreview(Component contentPreviewComponent) {
150 if (contentPreview != null) {
151 rootLayout.removeComponent(contentPreview);
152 }
153 rootLayout.addComponentAsFirst(contentPreviewComponent);
154 contentPreview = contentPreviewComponent;
155 }
156
157
158
159
160 private void updateComponents() {
161 if (!isEditable() && !isEmpty()) {
162 textField.setReadOnly(true);
163 if (linkLayout.getComponentIndex(selectButton) != -1) {
164 linkLayout.removeComponent(selectButton);
165 }
166 } else {
167 setButtonCaptionAndDescription();
168
169 }
170 }
171
172 private void setButtonCaptionAndDescription() {
173 final String caption = isEmpty() ? buttonCaptionNew : buttonCaptionOther;
174 selectButton.setCaption(caption);
175 selectButton.setDescription(caption);
176 }
177 }