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.pages.app.editor;
35
36 import info.magnolia.event.EventBus;
37 import info.magnolia.i18nsystem.SimpleTranslator;
38 import info.magnolia.pages.app.editor.event.ComponentMoveEvent;
39 import info.magnolia.pages.app.editor.event.NodeSelectedEvent;
40 import info.magnolia.ui.api.action.ActionExecutionException;
41 import info.magnolia.ui.api.action.ActionExecutor;
42 import info.magnolia.ui.api.app.SubAppContext;
43 import info.magnolia.ui.api.app.SubAppEventBus;
44 import info.magnolia.ui.api.event.ContentChangedEvent;
45 import info.magnolia.ui.api.message.Message;
46 import info.magnolia.ui.api.message.MessageType;
47 import info.magnolia.ui.vaadin.editor.PageEditorListener;
48 import info.magnolia.ui.vaadin.editor.PageEditorView;
49 import info.magnolia.ui.vaadin.gwt.client.shared.AbstractElement;
50 import info.magnolia.ui.vaadin.gwt.client.shared.PageEditorParameters;
51
52 import javax.inject.Inject;
53 import javax.inject.Named;
54 import javax.inject.Singleton;
55
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59
60
61
62
63 @Singleton
64 public class PageEditorPresenter implements PageEditorListener {
65
66 private static final Logger log = LoggerFactory.getLogger(PageEditorPresenter.class);
67
68 private final ActionExecutor actionExecutor;
69 private final PageEditorView view;
70 private final EventBus subAppEventBus;
71 private final SubAppContext subAppContext;
72 private final SimpleTranslator i18n;
73
74 private AbstractElement selectedElement;
75 private boolean moving = false;
76 private Listener listener;
77
78 @Inject
79 public PageEditorPresenter(final ActionExecutor actionExecutor, PageEditorView view, final @Named(SubAppEventBus.NAME) EventBus subAppEventBus,
80 SubAppContext subAppContext, SimpleTranslator i18n) {
81 this.actionExecutor = actionExecutor;
82 this.view = view;
83 this.subAppEventBus = subAppEventBus;
84 this.subAppContext = subAppContext;
85 this.i18n = i18n;
86 registerHandlers();
87 }
88
89 public PageEditorView start() {
90 view.setListener(this);
91 return view;
92 }
93
94 private void registerHandlers() {
95 subAppEventBus.addHandler(ContentChangedEvent.class, new ContentChangedEvent.Handler() {
96
97 @Override
98 public void onContentChanged(ContentChangedEvent event) {
99 view.refresh();
100 }
101 });
102 subAppEventBus.addHandler(ComponentMoveEvent.class, new ComponentMoveEvent.Handler() {
103 @Override
104 public void onMove(ComponentMoveEvent event) {
105 moving = event.isStart();
106 if (moving) {
107 view.startMoveComponent();
108 } else if (event.isServerSide()) {
109 view.cancelMoveComponent();
110 }
111
112 listener.onMove();
113 }
114 });
115
116 }
117
118 @Override
119 public void onElementSelect(AbstractElement selectedElement) {
120 this.selectedElement = selectedElement;
121 subAppEventBus.fireEvent(new NodeSelectedEvent(selectedElement));
122 }
123
124 @Override
125 public void onAction(String actionName, Object... args) {
126 try {
127 actionExecutor.execute(actionName, args);
128 } catch (ActionExecutionException e) {
129 Message error = new Message(MessageType.ERROR, i18n.translate("pages.pageEditorPresenter.actionExecutionError.message"), e.getMessage());
130 log.error("An error occurred while executing action [{}]", actionName, e);
131 subAppContext.getAppContext().sendLocalMessage(error);
132 }
133 }
134
135 public AbstractElement getSelectedElement() {
136 return selectedElement;
137 }
138
139 public void loadPageEditor(PageEditorParameters parameters) {
140 view.load(parameters);
141 }
142
143 public void updateParameters(PageEditorParameters parameters) {
144 view.update(parameters);
145 }
146
147 public boolean isMoving() {
148 return moving;
149 }
150
151 public void setListener(Listener listener) {
152 this.listener = listener;
153 }
154
155
156
157
158 interface Listener {
159 void onMove();
160 }
161 }