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 java.util.Map;
37
38 import info.magnolia.templating.editor.client.PageEditor;
39 import info.magnolia.templating.editor.client.dom.MgnlElement;
40 import info.magnolia.templating.editor.client.widget.dnd.DragAndDrop;
41 import info.magnolia.templating.editor.client.widget.dnd.LegacyDragAndDrop;
42
43
44
45 import com.google.gwt.dom.client.Element;
46 import com.google.gwt.dom.client.Style.Cursor;
47 import com.google.gwt.event.dom.client.ClickEvent;
48 import com.google.gwt.event.dom.client.ClickHandler;
49
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 public ComponentBar(MgnlElement mgnlElement) throws IllegalArgumentException {
75
76 super(mgnlElement);
77
78 checkMandatories(mgnlElement.getAttributes());
79 addStyleName("component");
80
81 if(DragDropEventBase.isSupported()) {
82 createDragAndDropHandlers();
83
84 }
85 if (!this.isInherited) {
86 createButtons();
87 createMouseEventsHandlers();
88 }
89
90 setVisible(false);
91 attach();
92
93 }
94
95 public void setDraggable(boolean draggable) {
96 if(DragDropEventBase.isSupported()) {
97 if (draggable) {
98 this.getElement().setDraggable(Element.DRAGGABLE_TRUE);
99 getStyle().setCursor(Cursor.MOVE);
100 }
101 else {
102 this.getElement().setDraggable(Element.DRAGGABLE_FALSE);
103 getStyle().setCursor(Cursor.DEFAULT);
104 }
105 }
106 }
107
108 private void checkMandatories(Map<String, String> attributes) {
109 String content = attributes.get("content");
110 int i = content.indexOf(':');
111 this.workspace = content.substring(0, i);
112 this.path = content.substring(i + 1);
113
114 this.nodeName = path.substring(path.lastIndexOf("/") + 1);
115
116 setId("__" + nodeName);
117
118 this.dialog = attributes.get("dialog");
119
120 this.isInherited = Boolean.parseBoolean(attributes.get("inherited"));
121
122 if (attributes.containsKey("editable")) {
123 this.editable = Boolean.parseBoolean(attributes.get("editable"));
124 }
125
126 if (this.isInherited || !this.editable) {
127 throw new IllegalArgumentException();
128 }
129
130 }
131
132 public String getNodeName() {
133 return nodeName;
134 }
135
136 public String getPath() {
137 return path;
138 }
139
140 private void createDragAndDropHandlers() {
141 DragAndDrop.dragAndDrop(this);
142 }
143
144 private void createMouseEventsHandlers() {
145
146 addDomHandler(new MouseDownHandler() {
147
148 @Override
149 public void onMouseDown(MouseDownEvent event) {
150 LegacyDragAndDrop.moveComponentEnd(ComponentBar.this);
151 }
152 }, MouseDownEvent.getType());
153
154 addDomHandler(new MouseOverHandler() {
155
156 @Override
157 public void onMouseOver(MouseOverEvent event) {
158 LegacyDragAndDrop.moveComponentOver(ComponentBar.this);
159
160 }
161 }, MouseOverEvent.getType());
162
163 addDomHandler(new MouseOutHandler() {
164
165 @Override
166 public void onMouseOut(MouseOutEvent event) {
167 LegacyDragAndDrop.moveComponentOut(ComponentBar.this);
168 }
169 }, MouseOutEvent.getType());
170 }
171
172 private void createButtons() {
173
174 final PushButton remove = new PushButton();
175 remove.addClickHandler(new ClickHandler() {
176 @Override
177 public void onClick(ClickEvent event) {
178 PageEditor.deleteComponent(path);
179 }
180 });
181 remove.setStylePrimaryName("mgnlEditorPushButton");
182 remove.addStyleName("remove");
183
184 addSecondaryButton(remove);
185
186 final PushButton move = new PushButton();
187 move.addClickHandler(new ClickHandler() {
188 @Override
189 public void onClick(ClickEvent event) {
190 toggleButtons(false);
191 LegacyDragAndDrop.moveComponentStart(ComponentBar.this);
192 }
193 });
194 move.setStylePrimaryName("mgnlEditorPushButton");
195 move.addStyleName("move");
196 addPrimaryButton(move);
197
198 if (dialog != null) {
199 final PushButton edit = new PushButton();
200 edit.addClickHandler(new ClickHandler() {
201 @Override
202 public void onClick(ClickEvent event) {
203 PageEditor.openDialog(dialog, workspace, path);
204 }
205 });
206 edit.setStylePrimaryName("mgnlEditorPushButton");
207 edit.addStyleName("edit");
208 addPrimaryButton(edit);
209 }
210 }
211 }