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