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.framework.tools;
35
36 import static com.vaadin.server.Sizeable.Unit.PERCENTAGE;
37
38 import info.magnolia.ui.dialog.actionarea.ActionListener;
39 import info.magnolia.ui.vaadin.form.FormViewReduced;
40
41 import com.vaadin.ui.Button;
42 import com.vaadin.ui.Button.ClickListener;
43 import com.vaadin.ui.Component;
44 import com.vaadin.v7.ui.HorizontalLayout;
45 import com.vaadin.v7.ui.VerticalLayout;
46
47
48
49
50 public class DefaultFormToolView extends VerticalLayout implements FormToolView {
51 private FormViewReduced formView;
52 private final HorizontalLayout actionsLayout = new HorizontalLayout();
53
54 public DefaultFormToolView() {
55 setWidth(100, PERCENTAGE);
56 addStyleName("form-tool");
57 actionsLayout.addStyleName("action-wrapper");
58 actionsLayout.setSpacing(true);
59 }
60
61 @Override
62 public void setForm(FormViewReduced formView) {
63 Component newForm = formView.asVaadinComponent();
64 if (this.formView != null) {
65 Component oldForm = this.formView.asVaadinComponent();
66 replaceComponent(oldForm, newForm);
67 } else {
68 addComponentAsFirst(newForm);
69 }
70
71 this.formView = formView;
72 }
73
74 @Override
75 public void addAction(final String actionName, String buttonLabel, final ActionListener actionListener) {
76 Button button = new Button(buttonLabel, (ClickListener) event -> actionListener.onActionFired(actionName));
77 button.addStyleName(actionName);
78 actionsLayout.addComponent(button);
79
80 if (!actionsLayout.isAttached()) {
81 int actionsIndex = formView != null ? getComponentIndex(formView.asVaadinComponent()) + 1 : 0;
82 addComponent(actionsLayout, actionsIndex);
83 }
84 }
85
86 @Override
87 public Component asVaadinComponent() {
88 return this;
89 }
90 }