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;
35
36 import static info.magnolia.templating.editor.client.PageEditor.getDictionary;
37
38 import info.magnolia.rendering.template.AreaDefinition;
39 import info.magnolia.templating.editor.client.dom.CMSBoundary;
40 import info.magnolia.templating.editor.client.dom.CMSComment;
41
42 import com.google.gwt.core.client.GWT;
43 import com.google.gwt.dom.client.Style.Float;
44 import com.google.gwt.event.dom.client.ClickEvent;
45 import com.google.gwt.event.dom.client.ClickHandler;
46 import com.google.gwt.dom.client.Element;
47 import com.google.gwt.user.client.ui.Button;
48
49
50
51
52
53 public class AreaBarWidget extends AbstractBarWidget {
54
55 private String workspace;
56 private String path;
57
58 private String areaWorkspace;
59 private String areaPath;
60
61 private String name;
62 private String availableComponents;
63 private String type;
64 private String dialog;
65 private boolean showAddButton = true;
66 private boolean optional = false;
67 private boolean created = true;
68
69 public AreaBarWidget(CMSBoundary boundary, final PageEditor pageEditor) {
70 super(boundary);
71
72 String content = boundary.getComment().getAttribute("content");
73 int i = content.indexOf(':');
74 this.workspace = content.substring(0, i);
75 this.path = content.substring(i + 1);
76
77 CMSBoundary area = boundary.getParentArea();
78 String areaContent = area.getComment().getAttribute("content");
79 i = areaContent.indexOf(':');
80 this.areaWorkspace = areaContent.substring(0, i);
81 this.areaPath = areaContent.substring(i + 1);
82
83 this.name = area.getComment().getAttribute("name");
84 this.type = area.getComment().getAttribute("type");
85
86 GWT.log("Area ["+this.name+"] is of type " + this.type);
87
88 if(AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
89 this.availableComponents = "";
90 } else {
91 this.availableComponents = area.getComment().getAttribute("availableComponents");
92 }
93
94 this.dialog = boundary.getComment().getAttribute("dialog");
95 if (area.getComment().hasAttribute("showAddButton")) {
96 this.showAddButton = Boolean.parseBoolean(area.getComment().getAttribute("showAddButton"));
97 }
98 if (area.getComment().hasAttribute("optional")) {
99 this.optional = Boolean.parseBoolean(area.getComment().getAttribute("optional"));
100 this.created = Boolean.parseBoolean(area.getComment().getAttribute("created"));
101 }
102
103
104 createButtons(pageEditor, boundary.getComment());
105 if (hasControls) {
106 setClassName("mgnlAreaEditBar");
107 }
108 else {
109 setClassName("mgnlAreaBar");
110 }
111 }
112 @Override
113 protected void select() {
114
115 if (getBoundary() != null) {
116
117 CMSBoundary parentBoundary = getBoundary().getParentBoundary();
118 if (parentBoundary != null) {
119 for (CMSBoundary boundary : parentBoundary.getDescendants()) {
120 if (boundary.getWidget() != null) {
121 boundary.getWidget().addStyleName("selected");
122 }
123 }
124 }
125 if (getBoundary().getParentArea() != null) {
126 Element element = getBoundary().getParentArea().getFirstElement();
127 if (element != null) {
128 element.addClassName("mgnlSelected");
129 }
130 }
131 }
132 super.select();
133
134 }
135
136 @Override
137 protected void deSelect() {
138 if (getBoundary() != null) {
139
140 CMSBoundary parentBoundary = getBoundary().getParentBoundary();
141 if (parentBoundary != null) {
142 for (CMSBoundary boundary : parentBoundary.getDescendants()) {
143 if (boundary.getWidget() != null) {
144 boundary.getWidget().removeStyleName("selected");
145 }
146 }
147 }
148 if (getBoundary().getParentArea() != null) {
149 Element element = getBoundary().getParentArea().getFirstElement();
150 if (element != null) {
151 element.removeClassName("mgnlSelected");
152 }
153 }
154
155 }
156 super.deSelect();
157 }
158 public String getAvailableComponents() {
159 return availableComponents;
160 }
161
162 public String getType() {
163 return type;
164 }
165
166 private void createButtons(final PageEditor pageEditor, final CMSComment comment) {
167 if(this.optional) {
168 if(!this.created) {
169 Button createButton = new Button(getDictionary().get("buttons.createarea.js"));
170 createButton.addClickHandler(new ClickHandler() {
171 @Override
172 public void onClick(ClickEvent event) {
173 pageEditor.createComponent(workspace, path, name, "mgnl:area");
174 }
175 });
176 addButton(createButton, Float.RIGHT);
177
178 } else {
179 createEditAndAddComponentButtons(pageEditor, comment);
180
181 Button removeButton = new Button(getDictionary().get("buttons.removearea.js"));
182 removeButton.addClickHandler(new ClickHandler() {
183 @Override
184 public void onClick(ClickEvent event) {
185 pageEditor.deleteComponent(path + "/" + name);
186 }
187 });
188 removeButton.addStyleName("mgnlRemoveButton");
189 addButton(removeButton, Float.RIGHT);
190 }
191 } else {
192 createEditAndAddComponentButtons(pageEditor, comment);
193 }
194 }
195
196 private void createEditAndAddComponentButtons(final PageEditor pageEditor, final CMSComment comment) {
197 if (comment.hasAttribute("dialog")) {
198 Button editButton = new Button(getDictionary().get("buttons.editarea.js"));
199 editButton.addClickHandler(new ClickHandler() {
200 @Override
201 public void onClick(ClickEvent event) {
202 pageEditor.openDialog(dialog, workspace, path, null, null);
203 }
204 });
205 addButton(editButton, Float.RIGHT);
206 }
207
208 if (this.showAddButton) {
209 Button addButton = new Button(getDictionary().get("buttons.new.js"));
210 addButton.addClickHandler(new ClickHandler() {
211 @Override
212 public void onClick(ClickEvent event) {
213 if (!AreaDefinition.TYPE_NO_COMPONENT.equals(type)) {
214 pageEditor.addComponent(areaWorkspace, areaPath, name, null, availableComponents);
215 }
216 }
217 });
218 addButton(addButton, Float.RIGHT);
219 }
220 }
221 }