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
37 import info.magnolia.templating.editor.client.PageEditor;
38 import info.magnolia.templating.editor.client.dom.MgnlElement;
39 import info.magnolia.templating.editor.client.jsni.JavascriptUtils;
40
41 import com.google.gwt.dom.client.Element;
42 import com.google.gwt.dom.client.Node;
43 import com.google.gwt.dom.client.Style;
44 import com.google.gwt.dom.client.Style.Float;
45
46 import com.google.gwt.user.client.ui.FlowPanel;
47 import com.google.gwt.user.client.ui.Label;
48 import com.google.gwt.user.client.ui.Widget;
49
50
51
52
53 public abstract class AbstractBar extends FlowPanel {
54
55 private String label = "";
56 private FlowPanel buttonWrapper;
57 private MgnlElement mgnlElement;
58
59 private FlowPanel primaryButtons;
60 private FlowPanel secondaryButtons;
61
62 public AbstractBar(MgnlElement mgnlElement) {
63
64 this.setMgnlElement(mgnlElement);
65
66 buttonWrapper = new FlowPanel();
67 buttonWrapper.setStylePrimaryName("mgnlEditorBarButtons");
68
69 add(buttonWrapper);
70
71 if (mgnlElement != null) {
72 this.label = mgnlElement.getAttribute("label");
73 if (label != null && !label.isEmpty()) {
74 Label areaName = new Label(this.label);
75
76 areaName.setTitle(this.label);
77 areaName.setStylePrimaryName("mgnlEditorBarLabel");
78
79
80 add(areaName);
81 }
82 }
83
84 setStyleName("mgnlEditor mgnlEditorBar");
85 }
86
87 protected void setId(String id){
88 getElement().setId(id);
89 }
90
91
92
93
94 protected void addButton(final Widget button, final Float cssFloat) {
95 button.setStylePrimaryName("mgnlEditorButton");
96 button.getElement().getStyle().setFloat(cssFloat);
97
98 buttonWrapper.add(button);
99 }
100
101
102
103
104 protected void addSecondaryButton(final Widget button) {
105 if (secondaryButtons == null) {
106 secondaryButtons = new FlowPanel();
107 secondaryButtons.setStylePrimaryName("mgnlEditorBarSecondaryButtons");
108 buttonWrapper.add(secondaryButtons);
109 }
110 secondaryButtons.add(button);
111 }
112
113
114
115
116 protected void addPrimaryButton(final Widget button) {
117 if (primaryButtons == null) {
118 primaryButtons = new FlowPanel();
119 primaryButtons.setStylePrimaryName("mgnlEditorBarPrimaryButtons");
120 buttonWrapper.add(primaryButtons);
121 }
122
123 primaryButtons.add(button);
124 }
125
126
127
128
129 protected void addButton(final Widget button, final Float cssFloat, final String primaryStyleName) {
130 if(JavascriptUtils.isEmpty(primaryStyleName)) {
131 addButton(button, cssFloat);
132 return;
133 }
134 button.setStylePrimaryName(primaryStyleName);
135 button.getElement().getStyle().setFloat(cssFloat);
136
137 buttonWrapper.add(button);
138 }
139
140
141
142
143
144 protected Style getStyle() {
145 return getElement().getStyle();
146 }
147
148
149
150
151 public void attach() {
152 if (getMgnlElement().getEditElement() != null) {
153 Element parent = getMgnlElement().getEditElement();
154 parent.insertFirst(getElement());
155 onAttach();
156 }
157 else if (getMgnlElement().getFirstElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
158 attach(getMgnlElement());
159 }
160 else {
161 attach(getMgnlElement().getComment().getElement());
162 }
163 }
164
165 public void attach(MgnlElement mgnlElement) {
166 Element element = mgnlElement.getFirstElement();
167 if (element != null) {
168 element.insertFirst(getElement());
169 }
170 onAttach();
171 }
172
173 public void attach(Element element) {
174 final Node parentNode = element.getParentNode();
175 parentNode.insertAfter(getElement(), element);
176 onAttach();
177 }
178
179 public void toggleVisible() {
180 setVisible(!isVisible());
181 }
182
183 @Override
184 protected void onAttach() {
185 PageEditor.model.addElements(this.getMgnlElement(), getElement());
186 PageEditor.model.addEditBar(this.getMgnlElement(), this);
187 super.onAttach();
188 }
189
190 public void setMgnlElement(MgnlElement mgnlElement) {
191 this.mgnlElement = mgnlElement;
192 }
193
194 public MgnlElement getMgnlElement() {
195 return mgnlElement;
196 }
197
198 public void toggleButtons(boolean visible) {
199 MgnlElement parent = mgnlElement.getParent();
200 if (parent != null) {
201 for (MgnlElement component : parent.getComponents()) {
202 PageEditor.model.getEditBar(component).primaryButtons.setVisible(visible);
203 PageEditor.model.getEditBar(component).secondaryButtons.setVisible(visible);
204 }
205 }
206
207 }
208 }