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