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