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.CMSComment;
40 import info.magnolia.templating.editor.client.dom.MgnlElement;
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
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(MgnlElement mgnlElement, CMSComment comment, final PageEditor pageEditor) {
70 super(mgnlElement, comment);
71
72 String content = mgnlElement.getComment().getAttribute("content");
73 if (content != null) {
74 int i = content.indexOf(':');
75
76 this.workspace = content.substring(0, i);
77 this.path = content.substring(i + 1);
78 }
79
80 setVisible(false);
81
82 String areaContent = mgnlElement.getComment().getAttribute("content");
83 int i = areaContent.indexOf(':');
84 this.areaWorkspace = areaContent.substring(0, i);
85 this.areaPath = areaContent.substring(i + 1);
86
87 this.name = mgnlElement.getComment().getAttribute("name");
88 this.type = mgnlElement.getComment().getAttribute("type");
89
90 GWT.log("Area ["+this.name+"] is of type " + this.type);
91
92 if(AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
93 this.availableComponents = "";
94 } else {
95 this.availableComponents = mgnlElement.getComment().getAttribute("availableComponents");
96 }
97
98 this.dialog = comment.getAttribute("dialog");
99 if (mgnlElement.getComment().hasAttribute("showAddButton")) {
100 this.showAddButton = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("showAddButton"));
101 }
102 if (comment.hasAttribute("optional")) {
103 this.optional = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("optional"));
104 this.created = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("created"));
105 }
106
107
108 createButtons(pageEditor, mgnlElement.getComment());
109 setClassName("mgnlAreaEditBar");
110 }
111
112 public String getAvailableComponents() {
113 return availableComponents;
114 }
115
116 public String getType() {
117 return type;
118 }
119
120 private void createButtons(final PageEditor pageEditor, final CMSComment comment) {
121 if(this.optional) {
122 if(!this.created) {
123 Button createButton = new Button(getDictionary().get("buttons.create.js"));
124 createButton.addClickHandler(new ClickHandler() {
125 @Override
126 public void onClick(ClickEvent event) {
127 pageEditor.createComponent(areaWorkspace, areaPath, name, "mgnl:area");
128 }
129 });
130 addButton(createButton, Float.RIGHT);
131
132 } else {
133 createEditAndAddComponentButtons(pageEditor, comment);
134
135 Button removeButton = new Button(getDictionary().get("buttons.remove.js"));
136 removeButton.addClickHandler(new ClickHandler() {
137 @Override
138 public void onClick(ClickEvent event) {
139 pageEditor.deleteComponent(path);
140 }
141 });
142 removeButton.addStyleName("mgnlRemoveButton");
143 addButton(removeButton, Float.RIGHT);
144 }
145 } else {
146 createEditAndAddComponentButtons(pageEditor, comment);
147 }
148 }
149
150 private void createEditAndAddComponentButtons(final PageEditor pageEditor, final CMSComment comment) {
151 if (comment.hasAttribute("dialog")) {
152 Button editButton = new Button(getDictionary().get("buttons.edit.js"));
153 editButton.addClickHandler(new ClickHandler() {
154 @Override
155 public void onClick(ClickEvent event) {
156 pageEditor.openDialog(dialog, workspace, path, null, null);
157 }
158 });
159 addButton(editButton, Float.RIGHT);
160 }
161
162 if (this.showAddButton) {
163 Button addButton = new Button(getDictionary().get("buttons.add.js"));
164 addButton.addClickHandler(new ClickHandler() {
165 @Override
166 public void onClick(ClickEvent event) {
167 if (!AreaDefinition.TYPE_NO_COMPONENT.equals(type)) {
168 pageEditor.addComponent(areaWorkspace, areaPath, name, null, availableComponents);
169 }
170 }
171 });
172 addButton(addButton, Float.RIGHT);
173 }
174 }
175 }