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.contentapp.field;
35
36 import info.magnolia.event.EventBus;
37 import info.magnolia.jcr.util.SessionUtil;
38 import info.magnolia.objectfactory.Components;
39 import info.magnolia.ui.api.context.UiContext;
40 import info.magnolia.ui.api.event.ChooseDialogEventBus;
41 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
42 import info.magnolia.ui.form.field.factory.AbstractFieldFactory;
43 import info.magnolia.ui.form.field.factory.LinkFieldFactory;
44 import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
45 import info.magnolia.ui.workbench.WorkbenchPresenter;
46 import info.magnolia.ui.workbench.WorkbenchView;
47 import info.magnolia.ui.workbench.event.SelectionChangedEvent;
48
49 import javax.inject.Inject;
50 import javax.inject.Named;
51 import javax.jcr.Node;
52 import javax.jcr.RepositoryException;
53
54 import org.apache.commons.lang3.StringUtils;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 import com.vaadin.v7.data.Item;
59 import com.vaadin.v7.ui.Field;
60
61
62
63
64
65
66
67
68 public class LinkFieldSelectionFactory extends AbstractFieldFactory<LinkFieldSelectionDefinition, String> {
69
70 private static final Logger log = LoggerFactory.getLogger(LinkFieldSelectionFactory.class);
71
72 private final EventBus chooseDialogEventBus;
73
74 private final WorkbenchPresenter workbenchPresenter;
75
76 private final String propertyName;
77
78 private TextAndContentViewField textContent;
79
80 @Inject
81 public LinkFieldSelectionFactory(
82 LinkFieldSelectionDefinition definition, Item relatedFieldItem, UiContext uiContext, I18NAuthoringSupport i18nAuthoringSupport,
83 WorkbenchPresenter workbenchPresenter, @Named(ChooseDialogEventBus.NAME) final EventBus chooseDialogEventBus) {
84 super(definition, relatedFieldItem, uiContext, i18nAuthoringSupport);
85 this.workbenchPresenter = workbenchPresenter;
86 this.chooseDialogEventBus = chooseDialogEventBus;
87
88
89 propertyName = String.valueOf(relatedFieldItem.getItemPropertyIds().iterator().next());
90
91
92 definition.setName(propertyName);
93 }
94
95
96
97
98 @Deprecated
99 public LinkFieldSelectionFactory(LinkFieldSelectionDefinition definition, Item relatedFieldItem, WorkbenchPresenter workbenchPresenter, @Named(ChooseDialogEventBus.NAME) final EventBus chooseDialogEventBus) {
100 this(definition, relatedFieldItem, null, Components.getComponent(I18NAuthoringSupport.class), workbenchPresenter, chooseDialogEventBus);
101 }
102
103 @Override
104 protected Field<String> createFieldComponent() {
105 textContent = new TextAndContentViewField(definition.isDisplayTextField(), definition.isDisplayTextFieldOnTop());
106
107
108 WorkbenchView workbenchView = workbenchPresenter.start(null, null, null);
109 textContent.setContentView(workbenchView);
110
111
112 restoreContentSelection();
113
114
115 chooseDialogEventBus.addHandler(SelectionChangedEvent.class, new SelectionChangedEvent.Handler() {
116 @Override
117 public void onSelectionChanged(SelectionChangedEvent event) {
118 JcrItemId firstItemId = (JcrItemId) event.getFirstItemId();
119 final Node selected = SessionUtil.getNodeByIdentifier(firstItemId.getWorkspace(), firstItemId.getUuid());
120 if (selected != null) {
121 try {
122 boolean isPropertyExisting = StringUtils.isNotBlank(propertyName)
123 && !LinkFieldFactory.PATH_PROPERTY_NAME.equals(propertyName) && selected.hasProperty(propertyName);
124 textContent.setValue(isPropertyExisting ? selected.getProperty(propertyName).getString() : selected.getPath());
125 } catch (RepositoryException e) {
126 log.error("Not able to access the configured property. Value will not be set.", e);
127 }
128 }
129 }
130 });
131 return textContent;
132 }
133
134 @Override
135 protected Class<String> getDefaultFieldType() {
136 return String.class;
137 }
138
139
140
141
142 private void restoreContentSelection() {
143 final String propertyValue = String.valueOf(item.getItemProperty(propertyName).getValue());
144
145 final String path = LinkFieldFactory.PATH_PROPERTY_NAME.equals(propertyName) && StringUtils.isNotBlank(propertyValue) ? propertyValue : "/";
146 workbenchPresenter.select(path);
147 }
148 }