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 java.util.Map;
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
43 import com.google.gwt.core.client.GWT;
44 import com.google.gwt.event.dom.client.ClickEvent;
45 import com.google.gwt.event.dom.client.ClickHandler;
46
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.setStylePrimaryName("mgnlEditorPushButton");
94 editButton.addStyleName("edit");
95 addPrimaryButton(editButton);
96 }
97 }
98 if(this.optional) {
99 if (this.created) {
100 PushButton removeButton = new PushButton();
101 removeButton.addClickHandler(new ClickHandler() {
102 @Override
103 public void onClick(ClickEvent event) {
104 PageEditor.deleteComponent(path);
105 }
106 });
107 removeButton.setStylePrimaryName("mgnlEditorPushButton");
108 removeButton.addStyleName("remove");
109 addSecondaryButton(removeButton);
110 }
111 else {
112 PushButton createbutton = new PushButton();
113 createbutton.setStylePrimaryName("mgnlEditorButton");
114
115 createbutton.addClickHandler(new ClickHandler() {
116 @Override
117 public void onClick(ClickEvent event) {
118 PageEditor.createComponent(workspace, path, "mgnl:area");
119 }
120 });
121 createbutton.setStylePrimaryName("mgnlEditorPushButton");
122 createbutton.addStyleName("add");
123 addSecondaryButton(createbutton);
124 }
125 }
126
127 }
128
129 private void checkMandatories(Map<String, String> attributes) throws IllegalArgumentException {
130
131 String content = attributes.get("content");
132 if (content != null) {
133 int i = content.indexOf(':');
134
135 this.workspace = content.substring(0, i);
136 this.path = content.substring(i + 1);
137 }
138
139 this.name = attributes.get("name");
140 this.type = attributes.get("type");
141
142 this.dialog = attributes.get("dialog");
143
144 if (attributes.containsKey("editable")) {
145 this.editable = Boolean.parseBoolean(attributes.get("editable"));
146 }
147
148 String availableComponents = "";
149 if(!AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
150 availableComponents = attributes.get("availableComponents");
151 }
152
153 boolean showAddButton = Boolean.parseBoolean(attributes.get("showAddButton"));
154 this.optional = Boolean.parseBoolean(attributes.get("optional"));
155 this.created = Boolean.parseBoolean(attributes.get("created"));
156
157
158 if (!this.editable) {
159 throw new IllegalArgumentException("Not injecting any Areabar");
160 }
161
162
163 if (this.optional) {
164 return;
165 }
166
167 else if (this.type.equals(AreaDefinition.TYPE_SINGLE)) {
168 return;
169 }
170
171
172 else if (showAddButton && !availableComponents.isEmpty()) {
173 return;
174 }
175
176
177 else if (dialog != null && !dialog.isEmpty()) {
178 return;
179 }
180
181 else {
182 throw new IllegalArgumentException("Not injecting any Areabar");
183 }
184 }
185
186 }