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.vaadin.gwt.client.editor.dom;
35
36 import info.magnolia.cms.security.operations.OperationPermissionDefinition;
37 import info.magnolia.jcr.util.NodeTypes;
38 import info.magnolia.rendering.template.AreaDefinition;
39 import info.magnolia.ui.vaadin.gwt.client.editor.event.EditAreaEvent;
40 import info.magnolia.ui.vaadin.gwt.client.editor.event.NewAreaEvent;
41 import info.magnolia.ui.vaadin.gwt.client.editor.event.NewComponentEvent;
42 import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
43 import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.AreaEndBar;
44 import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.ComponentPlaceHolder;
45 import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.listener.AreaListener;
46
47 import com.google.gwt.dom.client.Element;
48 import com.google.gwt.event.shared.EventBus;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public class MgnlArea extends MgnlElement implements AreaListener {
66 public static final String EDITOR_INIT_CLASS_NAME = "init";
67 private AreaEndBar areaEndBar;
68 private ComponentPlaceHolder componentPlaceHolder;
69 private Element componentMarkerElement;
70 private EventBus eventBus;
71
72
73
74
75 public MgnlArea(MgnlElement parent, EventBus eventBus) {
76 super(parent);
77 this.eventBus = eventBus;
78 }
79
80 private AreaEndBar getAreaEndBar() {
81 return areaEndBar;
82 }
83
84 public void setAreaEndBar(AreaEndBar areaEndBar) {
85 this.areaEndBar = areaEndBar;
86 }
87
88 private ComponentPlaceHolder getComponentPlaceHolder() {
89 return componentPlaceHolder;
90 }
91
92 public void setComponentPlaceHolder(ComponentPlaceHolder componentPlaceHolder) {
93 this.componentPlaceHolder = componentPlaceHolder;
94 }
95
96 public void setComponentMarkerElement(Element componentElement) {
97 this.componentMarkerElement = componentElement;
98 }
99
100 public Element getComponentMarkerElement() {
101 return componentMarkerElement;
102 }
103
104 @Override
105 public AreaElement getTypedElement() {
106 String availableComponents = getAttribute("availableComponents");
107 AreaElement area = new AreaElement(getAttribute("workspace"), getAttribute("path"), getAttribute("dialog"), availableComponents);
108
109 boolean areaHasChildComponents = getComponents().size() > 0;
110 boolean optional = Boolean.parseBoolean(getAttribute("optional"));
111 boolean created = Boolean.parseBoolean(getAttribute("created"));
112 boolean hasAvailableComponents = availableComponents != null && !availableComponents.isEmpty();
113 boolean addible = true;
114 if (getAttributes().containsKey(OperationPermissionDefinition.ADDIBLE)) {
115 addible = Boolean.parseBoolean(getAttribute(OperationPermissionDefinition.ADDIBLE));
116 }
117
118 area.setOptional(optional);
119 area.setCreated(created);
120 area.setAddible(addible && hasAvailableComponents && !(optional && !created) && !(isTypeSingle() && areaHasChildComponents) && !isMaxComponentsReached());
121
122 return area;
123 }
124
125 public boolean isTypeSingle() {
126 return "single".equals(getAttribute("type"));
127 }
128
129 public boolean isMaxComponentsReached() {
130 boolean showAddButton = Boolean.parseBoolean(getAttribute("showAddButton"));
131 boolean showNewComponentArea = Boolean.parseBoolean(getAttribute("showNewComponentArea"));
132
133 return showNewComponentArea && !showAddButton;
134 }
135
136 @Override
137 public void createOptionalArea() {
138 eventBus.fireEvent(new NewAreaEvent(getTypedElement()));
139 }
140
141 @Override
142 public void editArea() {
143 eventBus.fireEvent(new EditAreaEvent(getTypedElement()));
144 }
145
146 @Override
147 public void createNewComponent() {
148 eventBus.fireEvent(new NewComponentEvent(getTypedElement()));
149 }
150
151 public boolean hasAddButton() {
152 boolean optional = Boolean.parseBoolean(getAttribute("optional"));
153 boolean created = Boolean.parseBoolean(getAttribute("created"));
154
155 return optional && !created;
156 }
157
158 public boolean hasEditButton() {
159 boolean optional = Boolean.parseBoolean(getAttribute("optional"));
160 boolean created = Boolean.parseBoolean(getAttribute("created"));
161 boolean dialog = null != getAttribute("dialog");
162
163 if (dialog) {
164
165 if (!optional || (optional && created)) {
166 return true;
167 }
168 }
169 return false;
170 }
171
172 public boolean hasAddComponentButton() {
173 return Boolean.parseBoolean(getAttribute("showAddButton"));
174 }
175
176 public String getLabel() {
177 String label = getAttribute("label");
178 boolean optional = Boolean.parseBoolean(getAttribute("optional"));
179 return label + ((optional) ? " (optional)" : "");
180 }
181
182 public boolean isBoxPlaceHolder() {
183 Element marker = getComponentMarkerElement();
184 boolean onlyBar = (marker != null && marker.getAttribute(AreaDefinition.CMS_ADD).equals("bar"));
185 return !onlyBar;
186 }
187
188 public String getPlaceHolderLabel() {
189 String label = getAttribute("label");
190 String labelString;
191
192
193 if (isMaxComponentsReached()) {
194 labelString = "Maximum of components reached.";
195 } else {
196 labelString = "New Component";
197 if (label != null && !label.isEmpty()) {
198 labelString = "New" + " " + label + " " + "Component";
199 }
200 }
201 return labelString;
202 }
203
204 public int getActivationStatus() {
205 boolean isOptionalArea = Boolean.parseBoolean(getAttribute("optional"));
206 boolean isCreatedArea = Boolean.parseBoolean(getAttribute("created"));
207
208 if (isOptionalArea && !isCreatedArea) {
209 return -1;
210 }else if (containsAttribute(ACTIVATION_STATUS_KEY)) {
211 return Integer.parseInt(getAttribute(ACTIVATION_STATUS_KEY));
212 }
213 return NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
214 }
215
216 public void removeFocus() {
217 if (getControlBar() != null) {
218 getControlBar().removeFocus();
219 }
220
221 if (getAreaEndBar() != null) {
222 getAreaEndBar().removeFocus();
223 }
224 }
225
226 public void setFocus(boolean child) {
227 if (getControlBar() != null) {
228 getControlBar().setFocus(child);
229 }
230 if (getAreaEndBar() != null) {
231 getAreaEndBar().setFocus(child);
232 }
233 }
234
235 public void setVisible(boolean visible) {
236 if (getControlBar() != null) {
237 getControlBar().setVisible(visible);
238 }
239 if (getAreaEndBar() != null) {
240 getAreaEndBar().setVisible(visible);
241 }
242 }
243
244 public void setPlaceHolderVisible(boolean visible) {
245 if (getComponentPlaceHolder() != null) {
246 getComponentPlaceHolder().setVisible(visible);
247 }
248 }
249
250 public void toggleInitFocus(boolean visible) {
251 if (visible) {
252 getControlBar().addStyleName(EDITOR_INIT_CLASS_NAME);
253 getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
254 getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
255 } else {
256 getControlBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
257 getAreaEndBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
258 }
259 }
260
261 public void onDragStart(boolean isDrag) {
262 if (getComponentPlaceHolder() != null) {
263 getComponentPlaceHolder().setStyleName("moveOngoing", isDrag);
264 }
265 }
266 }