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 PageEditor.model.addEditBar(getMgnlElement(), this);
92 }
93
94 private void checkMandatories(Map<String, String> attributes) {
95 String content = attributes.get("content");
96 int i = content.indexOf(':');
97 this.workspace = content.substring(0, i);
98 this.path = content.substring(i + 1);
99
100 this.nodeName = path.substring(path.lastIndexOf("/") + 1);
101
102 setId("__" + nodeName);
103
104 this.dialog = attributes.get("dialog");
105
106 this.isInherited = Boolean.parseBoolean(attributes.get("inherited"));
107
108 if (attributes.containsKey("editable")) {
109 this.editable = Boolean.parseBoolean(attributes.get("editable"));
110 }
111
112 if (this.isInherited || !this.editable) {
113 throw new IllegalArgumentException();
114 }
115
116 }
117
118 public String getNodeName() {
119 return nodeName;
120 }
121
122 public String getPath() {
123 return path;
124 }
125
126 private void createDragAndDropHandlers() {
127 DragAndDrop.dragAndDrop(this);
128 }
129
130 private void createMouseEventsHandlers() {
131
132 addDomHandler(new MouseDownHandler() {
133
134 @Override
135 public void onMouseDown(MouseDownEvent event) {
136 String parentPath = path.substring(0, path.lastIndexOf("/"));
137 PageEditor.moveComponentEnd((ComponentBar)event.getSource(), parentPath);
138 }
139 }, MouseDownEvent.getType());
140
141 addDomHandler(new MouseOverHandler() {
142
143 @Override
144 public void onMouseOver(MouseOverEvent event) {
145 PageEditor.moveComponentOver((ComponentBar)event.getSource());
146
147 }
148 }, MouseOverEvent.getType());
149
150 addDomHandler(new MouseOutHandler() {
151
152 @Override
153 public void onMouseOut(MouseOutEvent event) {
154 PageEditor.moveComponentOut((ComponentBar)event.getSource());
155 }
156 }, MouseOutEvent.getType());
157 }
158
159 private void createButtons(boolean createMoveButton) {
160
161 if (dialog != null && !this.isInherited) {
162 final PushButton edit = new PushButton();
163 edit.addClickHandler(new ClickHandler() {
164 @Override
165 public void onClick(ClickEvent event) {
166 PageEditor.openDialog(dialog, workspace, path, null, null);
167 }
168 });
169 edit.setStylePrimaryName("mgnlEditorPushButton");
170 edit.addStyleName("edit");
171 addPrimaryButton(edit);
172 }
173
174 if(createMoveButton && !this.isInherited) {
175 final PushButton move = new PushButton();
176 move.addClickHandler(new ClickHandler() {
177 @Override
178 public void onClick(ClickEvent event) {
179 toggleButtons(false);
180 PageEditor.moveComponentStart(nodeName);
181 }
182 });
183 move.setStylePrimaryName("mgnlEditorPushButton");
184 move.addStyleName("move");
185 addPrimaryButton(move);
186 }
187
188 if (!this.isInherited) {
189 final PushButton remove = new PushButton();
190 remove.addClickHandler(new ClickHandler() {
191 @Override
192 public void onClick(ClickEvent event) {
193 PageEditor.deleteComponent(path);
194 }
195 });
196 remove.setStylePrimaryName("mgnlEditorPushButton");
197 remove.addStyleName("remove");
198
199 addSecondaryButton(remove);
200 }
201 }
202 }