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
42
43
44 import com.google.gwt.dom.client.Style.Cursor;
45 import com.google.gwt.event.dom.client.ClickEvent;
46 import com.google.gwt.event.dom.client.ClickHandler;
47
48 import com.google.gwt.event.dom.client.DragDropEventBase;
49 import com.google.gwt.event.dom.client.MouseDownEvent;
50 import com.google.gwt.event.dom.client.MouseDownHandler;
51 import com.google.gwt.event.dom.client.MouseOutEvent;
52 import com.google.gwt.event.dom.client.MouseOutHandler;
53 import com.google.gwt.event.dom.client.MouseOverEvent;
54 import com.google.gwt.event.dom.client.MouseOverHandler;
55 import com.google.gwt.user.client.ui.PushButton;
56
57
58
59
60
61
62
63 public class ComponentBar extends AbstractBar {
64
65 private String workspace;
66 private String path;
67 private String dialog;
68 private String nodeName;
69 private boolean isInherited;
70 private boolean editable = true;
71
72 public ComponentBar(MgnlElement mgnlElement) throws IllegalArgumentException {
73
74 super(mgnlElement);
75
76 checkMandatories(mgnlElement.getAttributes());
77 addStyleName("component");
78
79 if(DragDropEventBase.isSupported()) {
80 getStyle().setCursor(Cursor.MOVE);
81 createButtons(false);
82 createDragAndDropHandlers();
83 } else {
84 createButtons(true);
85 createMouseEventsHandlers();
86 }
87
88 setVisible(false);
89 attach();
90
91 }
92
93 private void checkMandatories(Map<String, String> attributes) {
94 String content = attributes.get("content");
95 int i = content.indexOf(':');
96 this.workspace = content.substring(0, i);
97 this.path = content.substring(i + 1);
98
99 this.nodeName = path.substring(path.lastIndexOf("/") + 1);
100
101 setId("__" + nodeName);
102
103 this.dialog = attributes.get("dialog");
104
105 this.isInherited = Boolean.parseBoolean(attributes.get("inherited"));
106
107 if (attributes.containsKey("editable")) {
108 this.editable = Boolean.parseBoolean(attributes.get("editable"));
109 }
110
111 if (this.isInherited || !this.editable) {
112 throw new IllegalArgumentException();
113 }
114
115 }
116
117 public String getNodeName() {
118 return nodeName;
119 }
120
121 public String getPath() {
122 return path;
123 }
124
125 private void createDragAndDropHandlers() {
126 DragAndDrop.dragAndDrop(this);
127 }
128
129 private void createMouseEventsHandlers() {
130
131 addDomHandler(new MouseDownHandler() {
132
133 @Override
134 public void onMouseDown(MouseDownEvent event) {
135 String parentPath = path.substring(0, path.lastIndexOf("/"));
136 PageEditor.moveComponentEnd((ComponentBar)event.getSource(), parentPath);
137 }
138 }, MouseDownEvent.getType());
139
140 addDomHandler(new MouseOverHandler() {
141
142 @Override
143 public void onMouseOver(MouseOverEvent event) {
144 PageEditor.moveComponentOver((ComponentBar)event.getSource());
145
146 }
147 }, MouseOverEvent.getType());
148
149 addDomHandler(new MouseOutHandler() {
150
151 @Override
152 public void onMouseOut(MouseOutEvent event) {
153 PageEditor.moveComponentOut((ComponentBar)event.getSource());
154 }
155 }, MouseOutEvent.getType());
156 }
157
158 private void createButtons(boolean createMoveButton) {
159
160 if (dialog != null && !this.isInherited) {
161 final PushButton edit = new PushButton();
162 edit.addClickHandler(new ClickHandler() {
163 @Override
164 public void onClick(ClickEvent event) {
165 PageEditor.openDialog(dialog, workspace, path, null, null);
166 }
167 });
168 edit.setStylePrimaryName("mgnlEditorPushButton");
169 edit.addStyleName("edit");
170 addPrimaryButton(edit);
171 }
172
173 if(createMoveButton && !this.isInherited) {
174 final PushButton move = new PushButton();
175 move.addClickHandler(new ClickHandler() {
176 @Override
177 public void onClick(ClickEvent event) {
178 toggleButtons(false);
179 PageEditor.moveComponentStart(nodeName);
180 }
181 });
182 move.setStylePrimaryName("mgnlEditorPushButton");
183 move.addStyleName("move");
184 addPrimaryButton(move);
185 }
186
187 if (!this.isInherited) {
188 final PushButton remove = new PushButton();
189 remove.addClickHandler(new ClickHandler() {
190 @Override
191 public void onClick(ClickEvent event) {
192 PageEditor.deleteComponent(path);
193 }
194 });
195 remove.setStylePrimaryName("mgnlEditorPushButton");
196 remove.addStyleName("remove");
197
198 addSecondaryButton(remove);
199 }
200 }
201 }