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.form.field;
35
36 import info.magnolia.annotation.deprecation.MgnlDeprecated;
37 import info.magnolia.objectfactory.ComponentProvider;
38 import info.magnolia.ui.api.app.AppController;
39 import info.magnolia.ui.api.context.UiContext;
40 import info.magnolia.ui.form.field.definition.LinkFieldDefinition;
41
42 import java.util.Optional;
43
44 import org.apache.commons.lang3.StringUtils;
45
46 import com.vaadin.ui.Alignment;
47 import com.vaadin.ui.Button;
48 import com.vaadin.ui.Component;
49 import com.vaadin.ui.NativeButton;
50 import com.vaadin.v7.data.util.converter.Converter;
51 import com.vaadin.v7.ui.CustomField;
52 import com.vaadin.v7.ui.HorizontalLayout;
53 import com.vaadin.v7.ui.TextField;
54 import com.vaadin.v7.ui.VerticalLayout;
55
56
57
58
59
60
61
62 @MgnlDeprecated(since = "6.0")
63 public class LinkField extends CustomField<String> {
64
65 private final VerticalLayout rootLayout = new VerticalLayout();
66 private final HorizontalLayout linkLayout = new HorizontalLayout();
67 private final TextField textField = new TextField();
68 private final Button selectButton = new NativeButton();
69 private Component contentPreview;
70 private String buttonCaptionNew;
71 private String buttonCaptionOther;
72 private boolean isFieldEditable;
73
74 public LinkField() {
75 }
76
77
78
79
80 @Deprecated
81 public LinkField(LinkFieldDefinition linkFieldDefinition, ComponentProvider componentProvider) {
82 this();
83 }
84
85 @Deprecated
86 public LinkField(LinkFieldDefinition linkFieldDefinition, AppController appController, UiContext uiContext, ComponentProvider componentProvider) {
87 this();
88 }
89
90 @Override
91 protected Component initContent() {
92 addStyleName("linkfield");
93
94 rootLayout.setSizeFull();
95 rootLayout.setSpacing(true);
96
97 textField.setWidth(100, Unit.PERCENTAGE);
98 textField.setNullRepresentation("");
99 textField.setNullSettingAllowed(true);
100 textField.addValueChangeListener(event ->
101 setValue(String.valueOf(Optional.ofNullable(event.getProperty().getValue()).orElse(""))));
102
103
104 linkLayout.setSizeFull();
105 linkLayout.addComponent(textField);
106 linkLayout.setExpandRatio(textField, 1);
107 linkLayout.setComponentAlignment(textField, Alignment.MIDDLE_LEFT);
108
109 if (!textField.isReadOnly()) {
110 selectButton.addStyleName("magnoliabutton");
111 linkLayout.addComponent(selectButton);
112 linkLayout.setExpandRatio(selectButton, 0);
113 linkLayout.setComponentAlignment(selectButton, Alignment.MIDDLE_RIGHT);
114 }
115 setButtonCaption(StringUtils.EMPTY);
116 rootLayout.addComponent(linkLayout);
117
118 this.addValueChangeListener(event -> updateComponents(getValue()));
119
120 updateComponents(getValue());
121 return rootLayout;
122 }
123
124 public TextField getTextField() {
125 return this.textField;
126 }
127
128 public Button getSelectButton() {
129 return this.selectButton;
130 }
131
132 @Override
133 public String getValue() {
134 return super.getValue();
135 }
136
137 @Override
138 public void setValue(String newValue) throws ReadOnlyException, Converter.ConversionException {
139 super.setValue(newValue);
140 }
141
142 @Override
143 protected void setInternalValue(String newValue) {
144 super.setInternalValue(newValue);
145 boolean readFlag = textField.isReadOnly();
146 textField.setReadOnly(false);
147 textField.setValue(newValue);
148 textField.setReadOnly(readFlag);
149 }
150
151 @Override
152 public void setReadOnly(boolean readOnly) {
153 super.setReadOnly(readOnly);
154 textField.setReadOnly(readOnly);
155 }
156
157
158
159
160
161
162 private void updateComponents(String currentValue) {
163 if (!isFieldEditable && StringUtils.isNotBlank(currentValue)) {
164 textField.setReadOnly(true);
165 if (linkLayout.getComponentIndex(selectButton) != -1) {
166 linkLayout.removeComponent(selectButton);
167 }
168 } else {
169 setButtonCaption(currentValue);
170 }
171 }
172
173 @Override
174 public Class<String> getType() {
175 return String.class;
176 }
177
178 private void setButtonCaption(String value) {
179 if (StringUtils.isNotBlank(value)) {
180 selectButton.setCaption(buttonCaptionOther);
181 selectButton.setDescription(buttonCaptionOther);
182 } else {
183 selectButton.setCaption(buttonCaptionNew);
184 selectButton.setDescription(buttonCaptionNew);
185 }
186 }
187
188 public void setContentPreview(Component contentPreviewComponent) {
189 if (contentPreview != null) {
190 rootLayout.removeComponent(contentPreview);
191 }
192 contentPreviewComponent.setVisible(StringUtils.isNotBlank(textField.getValue()));
193 rootLayout.addComponentAsFirst(contentPreviewComponent);
194 contentPreview = contentPreviewComponent;
195 }
196
197
198
199
200 public void setButtonCaptionNew(String buttonCaptionNew) {
201 this.buttonCaptionNew = buttonCaptionNew;
202 }
203
204 public void setButtonCaptionOther(String buttonCaptionOther) {
205 this.buttonCaptionOther = buttonCaptionOther;
206 }
207
208 public void setTextFieldConverter(Converter textFieldConverter) {
209 this.setConverter(textFieldConverter);
210 }
211
212 public void setFieldEditable(boolean isFieldEditable) {
213 this.isFieldEditable = isFieldEditable;
214 }
215
216 @Override
217 public boolean isEmpty() {
218 return StringUtils.isEmpty(getValue());
219 }
220 }