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.widget.controlbar;
35
36 import static info.magnolia.templating.editor.client.jsni.JavascriptUtils.getI18nMessage;
37
38 import info.magnolia.cms.security.operations.OperationPermissionDefinition;
39 import info.magnolia.templating.editor.client.PageEditor;
40 import info.magnolia.templating.editor.client.dom.MgnlElement;
41 import info.magnolia.templating.editor.client.widget.dnd.DragAndDrop;
42 import info.magnolia.templating.editor.client.widget.dnd.LegacyDragAndDrop;
43
44 import java.util.Map;
45
46 import com.google.gwt.dom.client.Element;
47 import com.google.gwt.dom.client.Style.Cursor;
48 import com.google.gwt.event.dom.client.ClickEvent;
49 import com.google.gwt.event.dom.client.ClickHandler;
50 import com.google.gwt.event.dom.client.DragDropEventBase;
51 import com.google.gwt.event.dom.client.MouseDownEvent;
52 import com.google.gwt.event.dom.client.MouseDownHandler;
53 import com.google.gwt.event.dom.client.MouseOutEvent;
54 import com.google.gwt.event.dom.client.MouseOutHandler;
55 import com.google.gwt.event.dom.client.MouseOverEvent;
56 import com.google.gwt.event.dom.client.MouseOverHandler;
57 import com.google.gwt.user.client.ui.PushButton;
58
59
60
61
62
63
64
65 public class ComponentBar extends AbstractBar {
66
67 private String workspace;
68 private String path;
69 private String dialog;
70 private String nodeName;
71 private boolean isInherited;
72 private boolean editable = true;
73
74 private boolean moveable = true;
75 private boolean writable = true;
76 private boolean deletable = true;
77
78 public ComponentBar(MgnlElement mgnlElement) throws IllegalArgumentException {
79
80 super(mgnlElement);
81
82 checkMandatories(mgnlElement.getAttributes());
83 addStyleName("component");
84
85 if(DragDropEventBase.isSupported()) {
86 createDragAndDropHandlers();
87
88 }
89 if (!this.isInherited) {
90 createButtons();
91 createMouseEventsHandlers();
92 }
93
94 setVisible(false);
95 attach();
96 }
97
98 public void setDraggable(boolean draggable) {
99 if(DragDropEventBase.isSupported()) {
100 if (draggable) {
101 this.getElement().setDraggable(Element.DRAGGABLE_TRUE);
102 getStyle().setCursor(Cursor.MOVE);
103 }
104 else {
105 this.getElement().setDraggable(Element.DRAGGABLE_FALSE);
106 getStyle().setCursor(Cursor.DEFAULT);
107 }
108 }
109 }
110
111 private void checkMandatories(Map<String, String> attributes) {
112 String content = attributes.get("content");
113 int i = content.indexOf(':');
114 this.workspace = content.substring(0, i);
115 this.path = content.substring(i + 1);
116
117 this.nodeName = path.substring(path.lastIndexOf("/") + 1);
118
119 setId("__" + nodeName);
120
121 this.dialog = attributes.get("dialog");
122
123 this.isInherited = getMgnlElement().isInherited();
124
125 if (attributes.containsKey("editable")) {
126 this.editable = Boolean.parseBoolean(attributes.get("editable"));
127 }
128
129 if (this.isInherited || !this.editable) {
130 throw new IllegalArgumentException();
131 }
132
133 if (attributes.containsKey(OperationPermissionDefinition.MOVEABLE)) {
134 this.moveable = Boolean.parseBoolean(attributes.get(OperationPermissionDefinition.MOVEABLE));
135 }
136 if (attributes.containsKey(OperationPermissionDefinition.WRITABLE)) {
137 this.writable = Boolean.parseBoolean(attributes.get(OperationPermissionDefinition.WRITABLE));
138 }
139 if (attributes.containsKey(OperationPermissionDefinition.DELETABLE)) {
140 this.deletable = Boolean.parseBoolean(attributes.get(OperationPermissionDefinition.DELETABLE));
141 }
142 }
143
144 public String getNodeName() {
145 return nodeName;
146 }
147
148 public String getPath() {
149 return path;
150 }
151
152 private void createDragAndDropHandlers() {
153 DragAndDrop.dragAndDrop(this);
154 }
155
156 private void createMouseEventsHandlers() {
157
158 addDomHandler(new MouseDownHandler() {
159
160 @Override
161 public void onMouseDown(MouseDownEvent event) {
162 LegacyDragAndDrop.moveComponentEnd(ComponentBar.this);
163 }
164 }, MouseDownEvent.getType());
165
166 addDomHandler(new MouseOverHandler() {
167
168 @Override
169 public void onMouseOver(MouseOverEvent event) {
170 LegacyDragAndDrop.moveComponentOver(ComponentBar.this);
171
172 }
173 }, MouseOverEvent.getType());
174
175 addDomHandler(new MouseOutHandler() {
176
177 @Override
178 public void onMouseOut(MouseOutEvent event) {
179 LegacyDragAndDrop.moveComponentOut(ComponentBar.this);
180 }
181 }, MouseOutEvent.getType());
182 }
183
184 private void createButtons() {
185
186 final PushButton remove = new PushButton();
187 remove.addClickHandler(new ClickHandler() {
188 @Override
189 public void onClick(ClickEvent event) {
190 PageEditor.deleteComponent(path);
191 }
192 });
193 remove.setTitle(getI18nMessage("buttons.component.delete.js"));
194 remove.setStylePrimaryName("mgnlEditorPushButton");
195 remove.addStyleName("remove");
196 if (!deletable) {
197 remove.setVisible(false);
198 }
199
200 addSecondaryButton(remove);
201
202 final PushButton move = new PushButton();
203 move.addClickHandler(new ClickHandler() {
204 @Override
205 public void onClick(ClickEvent event) {
206 toggleButtons(false);
207 LegacyDragAndDrop.moveComponentStart(ComponentBar.this);
208 }
209 });
210 move.setTitle(getI18nMessage("buttons.component.move.js"));
211 move.setStylePrimaryName("mgnlEditorPushButton");
212 move.addStyleName("move");
213 if (!moveable) {
214 move.setVisible(false);
215 }
216 addPrimaryButton(move);
217
218 if (dialog != null) {
219 final PushButton edit = new PushButton();
220 edit.addClickHandler(new ClickHandler() {
221 @Override
222 public void onClick(ClickEvent event) {
223 PageEditor.openDialog(dialog, workspace, path);
224 }
225 });
226 edit.setTitle(getI18nMessage("buttons.component.edit.js"));
227 edit.setStylePrimaryName("mgnlEditorPushButton");
228 edit.addStyleName("edit");
229 addPrimaryButton(edit);
230 if (!writable) {
231 edit.setVisible(false);
232 }
233 }
234 }
235 }