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