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.ui.vaadin.gwt.client.editor.widget.controlbar;
35
36 import com.google.gwt.core.client.GWT;
37 import com.google.gwt.event.dom.client.ClickEvent;
38 import com.google.gwt.event.dom.client.ClickHandler;
39 import com.google.gwt.event.shared.EventBus;
40 import com.google.gwt.user.client.ui.Label;
41 import com.google.gwt.user.client.ui.PushButton;
42 import info.magnolia.rendering.template.AreaDefinition;
43 import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlElement;
44 import info.magnolia.ui.vaadin.gwt.client.editor.event.DeleteComponentEvent;
45 import info.magnolia.ui.vaadin.gwt.client.editor.event.EditComponentEvent;
46 import info.magnolia.ui.vaadin.gwt.client.editor.event.NewAreaEvent;
47
48 import java.util.Map;
49
50 import static info.magnolia.ui.vaadin.gwt.client.editor.jsni.JavascriptUtils.getI18nMessage;
51
52
53
54
55
56 public class AreaBar extends AbstractBar {
57
58 private String name;
59
60 private String type;
61
62 private String dialog;
63
64 private String availableComponents;
65
66 private boolean optional;
67
68 private boolean created;
69
70 private boolean editable = true;
71
72 private static final String NODE_TYPE = "mgnl:area";
73
74 public AreaBar(EventBus eventBus, MgnlElement mgnlElement) {
75 super(eventBus, mgnlElement);
76
77 setFields(mgnlElement.getAttributes());
78
79 GWT.log("Area [" + this.name + "] is of type " + this.type);
80
81 this.addStyleName("area");
82
83 setVisible(false);
84 createControls();
85
86 }
87
88 private void createButtons() {
89
90 if (this.dialog != null) {
91
92 if (!this.optional || (this.optional && this.created)) {
93 PushButton editButton = new PushButton();
94 editButton.addClickHandler(new ClickHandler() {
95
96 @Override
97 public void onClick(ClickEvent event) {
98 getEventBus().fireEvent(new EditComponentEvent(dialog, getWorkspace(), getPath()));
99 }
100 });
101 editButton.setTitle(getI18nMessage("buttons.area.edit.js"));
102 editButton.setStylePrimaryName("mgnlEditorPushButton");
103 editButton.addStyleName("edit");
104 addPrimaryButton(editButton);
105 }
106 }
107 if (this.optional) {
108 if (this.created) {
109 PushButton removeButton = new PushButton();
110 removeButton.addClickHandler(new ClickHandler() {
111
112 @Override
113 public void onClick(ClickEvent event) {
114 getEventBus().fireEvent(new DeleteComponentEvent(getWorkspace(), getPath()));
115 }
116 });
117 removeButton.setTitle(getI18nMessage("buttons.area.delete.js"));
118 removeButton.setStylePrimaryName("mgnlEditorPushButton");
119 removeButton.addStyleName("remove");
120 addSecondaryButton(removeButton);
121 }
122 else {
123 PushButton createbutton = new PushButton();
124 createbutton.setStylePrimaryName("mgnlEditorButton");
125
126 createbutton.addClickHandler(new ClickHandler() {
127
128 @Override
129 public void onClick(ClickEvent event) {
130 getEventBus().fireEvent(new NewAreaEvent(getWorkspace(), NODE_TYPE, getPath()));
131 }
132 });
133 createbutton.setTitle(getI18nMessage("buttons.area.add.js"));
134 createbutton.setStylePrimaryName("mgnlEditorPushButton");
135 createbutton.addStyleName("add");
136 addSecondaryButton(createbutton);
137 }
138 }
139
140 }
141
142 private void createControls() {
143 if (this.optional) {
144 if (this.created) {
145 final Label remove = new Label();
146 remove.setStyleName(ICON_CLASSNAME);
147 remove.addStyleName(REMOVE_CLASSNAME);
148 remove.addClickHandler(new ClickHandler() {
149 @Override
150 public void onClick(ClickEvent event) {
151 getEventBus().fireEvent(new DeleteComponentEvent(getWorkspace(), getPath()));
152 }
153 });
154 addSecondaryButton(remove);
155
156 }
157 else {
158 final Label add = new Label();
159 add.setStyleName(ICON_CLASSNAME);
160 add.addStyleName(ADD_CLASSNAME);
161 add.addClickHandler(new ClickHandler() {
162 @Override
163 public void onClick(ClickEvent event) {
164 getEventBus().fireEvent(new NewAreaEvent(getWorkspace(), NODE_TYPE, getPath()));
165 }
166 });
167 addSecondaryButton(add);
168 }
169 }
170
171 if (this.dialog != null) {
172
173 if (!this.optional || (this.optional && this.created)) {
174 final Label edit = new Label();
175 edit.setStyleName(ICON_CLASSNAME);
176 edit.addStyleName(EDIT_CLASSNAME);
177 edit.addClickHandler(new ClickHandler() {
178
179 @Override
180 public void onClick(ClickEvent event) {
181 getEventBus().fireEvent(new EditComponentEvent(getWorkspace(), getPath(), dialog));
182 }
183 });
184 addPrimaryButton(edit);
185 }
186 }
187
188 }
189
190 private void setFields(Map<String, String> attributes) throws IllegalArgumentException {
191
192 setWorkspace(attributes.get("workspace"));
193 setPath(attributes.get("path"));
194
195 this.name = attributes.get("name");
196 this.type = attributes.get("type");
197
198 this.dialog = attributes.get("dialog");
199
200 if (attributes.containsKey("editable")) {
201 this.editable = Boolean.parseBoolean(attributes.get("editable"));
202 }
203
204 availableComponents = "";
205 if (!AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
206 availableComponents = attributes.get("availableComponents");
207 }
208
209 boolean showAddButton = Boolean.parseBoolean(attributes.get("showAddButton"));
210 this.optional = Boolean.parseBoolean(attributes.get("optional"));
211 this.created = Boolean.parseBoolean(attributes.get("created"));
212 }
213
214 @Override
215 public String getDialog() {
216 return editable ? dialog : null;
217 }
218
219 public String getAvailableComponents() {
220 return availableComponents;
221 }
222
223 }