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