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.templating.editor.client.widget.controlbar;
35
36
37 import static info.magnolia.templating.editor.client.jsni.JavascriptUtils.getI18nMessage;
38
39 import info.magnolia.rendering.template.AreaDefinition;
40 import info.magnolia.templating.editor.client.PageEditor;
41 import info.magnolia.templating.editor.client.dom.MgnlElement;
42 import info.magnolia.templating.elements.AreaElement;
43
44 import java.util.Map;
45
46 import com.google.gwt.core.client.GWT;
47 import com.google.gwt.event.dom.client.ClickEvent;
48 import com.google.gwt.event.dom.client.ClickHandler;
49 import com.google.gwt.user.client.ui.PushButton;
50
51
52
53
54
55 public class AreaBar extends AbstractBar {
56
57 private String workspace;
58 private String path;
59
60 private String name;
61 private String type;
62 private String dialog;
63 private boolean optional;
64 private boolean created;
65 private boolean isInherited;
66 private boolean editable = true;
67
68 public AreaBar(MgnlElement mgnlElement) throws IllegalArgumentException {
69 super(mgnlElement);
70
71 checkMandatories(mgnlElement.getAttributes());
72
73 GWT.log("Area ["+this.name+"] is of type " + this.type);
74
75 this.addStyleName("area");
76
77 setVisible(false);
78 createButtons();
79
80 attach();
81
82 }
83
84 private void createButtons() {
85
86 if (this.dialog != null) {
87
88 if (!this.optional || (this.optional && this.created)) {
89 PushButton editButton = new PushButton();
90 editButton.addClickHandler(new ClickHandler() {
91 @Override
92 public void onClick(ClickEvent event) {
93 PageEditor.openDialog(dialog, workspace, path);
94 }
95 });
96 editButton.setTitle(getI18nMessage("buttons.area.edit.js"));
97 editButton.setStylePrimaryName("mgnlEditorPushButton");
98 editButton.addStyleName("edit");
99 addPrimaryButton(editButton);
100 }
101 }
102 if(this.optional) {
103 if (this.created) {
104 PushButton removeButton = new PushButton();
105 removeButton.addClickHandler(new ClickHandler() {
106 @Override
107 public void onClick(ClickEvent event) {
108 PageEditor.deleteComponent(path);
109 }
110 });
111 removeButton.setTitle(getI18nMessage("buttons.area.delete.js"));
112 removeButton.setStylePrimaryName("mgnlEditorPushButton");
113 removeButton.addStyleName("remove");
114 addSecondaryButton(removeButton);
115 }
116 else {
117 PushButton createbutton = new PushButton();
118 createbutton.setStylePrimaryName("mgnlEditorButton");
119
120 createbutton.addClickHandler(new ClickHandler() {
121 @Override
122 public void onClick(ClickEvent event) {
123 PageEditor.createComponent(workspace, path, "mgnl:area");
124 }
125 });
126 createbutton.setTitle(getI18nMessage("buttons.area.add.js"));
127 createbutton.setStylePrimaryName("mgnlEditorPushButton");
128 createbutton.addStyleName("add");
129 addSecondaryButton(createbutton);
130 }
131 }
132
133 }
134
135 private void checkMandatories(Map<String, String> attributes) throws IllegalArgumentException {
136
137 String content = attributes.get("content");
138 if (content != null) {
139 int i = content.indexOf(':');
140
141 this.workspace = content.substring(0, i);
142 this.path = content.substring(i + 1);
143 }
144
145 this.name = attributes.get("name");
146 this.type = attributes.get("type");
147
148 this.dialog = attributes.get("dialog");
149
150 this.isInherited = getMgnlElement().isInherited();
151
152 if (attributes.containsKey("editable")) {
153 this.editable = Boolean.parseBoolean(attributes.get("editable"));
154 }
155
156 String availableComponents = "";
157 if(!AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
158 availableComponents = attributes.get("availableComponents");
159 }
160
161 boolean showAddButton = Boolean.parseBoolean(attributes.get(AreaElement.SHOW_ADD_BUTTON));
162 boolean showNewComponentArea = Boolean.parseBoolean(attributes.get(AreaElement.SHOW_NEW_COMPONENT_AREA));
163
164 this.optional = Boolean.parseBoolean(attributes.get("optional"));
165 this.created = Boolean.parseBoolean(attributes.get("created"));
166
167
168 if (this.isInherited || !this.editable) {
169 throw new IllegalArgumentException("Not injecting any Areabar");
170 }
171
172
173 if (this.optional) {
174 return;
175 }
176
177 else if (this.type.equals(AreaDefinition.TYPE_SINGLE)) {
178 return;
179 }
180
181
182 else if (showNewComponentArea || (showAddButton && !availableComponents.isEmpty())) {
183 return;
184 }
185
186
187 else if (dialog != null && !dialog.isEmpty()) {
188 return;
189 }
190
191 else {
192 throw new IllegalArgumentException("Not injecting any Areabar");
193 }
194 }
195
196 }