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
37 import info.magnolia.rendering.template.AreaDefinition;
38 import info.magnolia.templating.editor.client.dom.MgnlElement;
39 import static info.magnolia.templating.editor.client.jsni.LegacyJavascript.*;
40
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.dom.client.Style.Float;
43 import com.google.gwt.event.dom.client.ClickEvent;
44 import com.google.gwt.event.dom.client.ClickHandler;
45
46 import com.google.gwt.user.client.ui.Button;
47 import com.google.gwt.dom.client.Element;
48
49
50
51
52
53 public class AreaBarWidget extends AbstractBarWidget {
54
55 private String workspace;
56 private String path;
57
58 private String name;
59 private String availableComponents;
60 private String type;
61 private String dialog;
62 private boolean showAddButton = true;
63 private boolean optional = false;
64 private boolean created = true;
65
66 public AreaBarWidget(MgnlElement mgnlElement, final PageEditor pageEditor) {
67 super(mgnlElement);
68
69 String content = mgnlElement.getComment().getAttribute("content");
70 if (content != null) {
71 int i = content.indexOf(':');
72
73 this.workspace = content.substring(0, i);
74 this.path = content.substring(i + 1);
75 }
76
77 setVisible(false);
78
79 this.name = mgnlElement.getComment().getAttribute("name");
80 this.type = mgnlElement.getComment().getAttribute("type");
81
82 GWT.log("Area ["+this.name+"] is of type " + this.type);
83
84 if(AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
85 this.availableComponents = "";
86 } else {
87 this.availableComponents = mgnlElement.getComment().getAttribute("availableComponents");
88 }
89
90 this.dialog = mgnlElement.getComment().getAttribute("dialog");
91 if (mgnlElement.getComment().hasAttribute("showAddButton")) {
92 this.showAddButton = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("showAddButton"));
93 }
94 if (mgnlElement.getComment().hasAttribute("optional")) {
95 this.optional = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("optional"));
96 this.created = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("created"));
97 }
98
99
100 createButtons(pageEditor);
101
102 this.addStyleName("area");
103 }
104
105 public void attach(MgnlElement mgnlElement) {
106 Element element = mgnlElement.getFirstElement();
107 if (element != null) {
108 element.insertFirst(getElement());
109 }
110 onAttach();
111 }
112
113 public String getAvailableComponents() {
114 return availableComponents;
115 }
116
117 public String getType() {
118 return type;
119 }
120
121 private void createButtons(final PageEditor pageEditor) {
122 if(this.optional) {
123 if(!this.created) {
124 Button createButton = new Button(getI18nMessage("buttons.create.js"));
125 createButton.addClickHandler(new ClickHandler() {
126 @Override
127 public void onClick(ClickEvent event) {
128 pageEditor.createComponent(workspace, path, "mgnl:area");
129 }
130 });
131 addButton(createButton, Float.RIGHT);
132
133 } else {
134 createEditAndAddComponentButtons(pageEditor);
135
136 Button removeButton = new Button(getI18nMessage("buttons.remove.js"));
137 removeButton.addClickHandler(new ClickHandler() {
138 @Override
139 public void onClick(ClickEvent event) {
140 pageEditor.deleteComponent(path);
141 }
142 });
143 removeButton.addStyleName("mgnlRemoveButton");
144 addButton(removeButton, Float.RIGHT);
145 }
146 } else {
147 createEditAndAddComponentButtons(pageEditor);
148 }
149 }
150
151 private void createEditAndAddComponentButtons(final PageEditor pageEditor) {
152 if (this.dialog != null) {
153 Button editButton = new Button(getI18nMessage("buttons.edit.js"));
154 editButton.addClickHandler(new ClickHandler() {
155 @Override
156 public void onClick(ClickEvent event) {
157 pageEditor.openDialog(dialog, workspace, path, null, null);
158 }
159 });
160 addButton(editButton, Float.RIGHT);
161 }
162
163 if (this.showAddButton) {
164 Button addButton = new Button(getI18nMessage("buttons.add.js"));
165 addButton.addClickHandler(new ClickHandler() {
166 @Override
167 public void onClick(ClickEvent event) {
168 if (!AreaDefinition.TYPE_NO_COMPONENT.equals(type)) {
169 pageEditor.addComponent(workspace, path, null, availableComponents);
170 }
171 }
172 });
173 addButton(addButton, Float.RIGHT);
174 }
175 }
176
177
178 }