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.ui.vaadin.gwt.client.widget.controlbar;
35
36 import info.magnolia.jcr.util.NodeTypes;
37 import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlElement;
38 import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.eventmanager.ControlBarEventManager;
39 import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.listener.ControlBarListener;
40
41 import java.util.Arrays;
42
43 import com.google.gwt.core.client.GWT;
44 import com.google.gwt.dom.client.Element;
45 import com.google.gwt.user.client.DOM;
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public abstract class AbstractBar<LISTENER extends ControlBarListener> extends SimpleBar {
69
70
71 private final static String EDITOR_BAR_LABEL_CLASS_NAME = "mgnlEditorBarLabel";
72 private final static String EDITOR_BAR_BUTTONS_CLASS_NAME = "mgnlEditorBarButtons";
73 private final static String EDITOR_BAR_WITH_STATUS_CLASS_NAME = "mgnlEditorBarStatusIndicator";
74
75 private final static String MGNL_LEVEL_CLASS_NAME = "mgnlLevel-";
76
77 protected final static String AREA_CLASS_NAME = "area";
78 protected final static String COMPONENT_CLASS_NAME = "component";
79
80 protected final static String ICON_CLASS_NAME = "editorIcon";
81 protected final static String EDIT_CLASS_NAME = "icon-edit";
82 protected final static String ADD_CLASS_NAME = "icon-add-item";
83
84 protected final static String STATIC_ICON_CLASS_NAME = "staticIcon";
85
86 private final static String STATUS_INDICATOR_CLASS_NAME = "status-indicator";
87 private final static String STATUS_INACTIVATED_CLASS_NAME = "background-color-red icon-status-red";
88 private final static String STATUS_MODIFIED_CLASS_NAME = "background-color-yellow icon-status-orange";
89
90 private final static int MAX_LEVEL = 6;
91
92 protected ControlBarEventManager eventManager = GWT.create(ControlBarEventManager.class);
93 protected LISTENER listener;
94 protected boolean editable;
95
96 private FlowPanel buttons;
97 private Element label;
98 private Element activationIndicator;
99
100
101
102
103
104
105 protected void initLayout() {
106 this.buttons = new FlowPanel();
107 this.activationIndicator = DOM.createDiv();
108 this.label = DOM.createDiv();
109
110 buttons.setStylePrimaryName(EDITOR_BAR_BUTTONS_CLASS_NAME);
111 insert(buttons, 0);
112
113 getElement().appendChild(activationIndicator);
114 getElement().appendChild(label);
115 }
116
117
118
119
120 protected void addLabel(String text, int level) {
121 label.setClassName(EDITOR_BAR_LABEL_CLASS_NAME);
122 label.setInnerText(text);
123
124 label.setTitle(text);
125
126 label.addClassName(MGNL_LEVEL_CLASS_NAME + (level > MAX_LEVEL ? "max" : String.valueOf(level)));
127 }
128
129
130
131
132
133 protected void addStatusIndicator(int activationStatus) {
134 switch (activationStatus) {
135 case NodeTypes.Activatable.ACTIVATION_STATUS_MODIFIED:
136 activationIndicator.setClassName(STATUS_INDICATOR_CLASS_NAME);
137 activationIndicator.addClassName(STATUS_MODIFIED_CLASS_NAME);
138 addStyleName(EDITOR_BAR_WITH_STATUS_CLASS_NAME);
139 break;
140 case NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED:
141 activationIndicator.setClassName(STATUS_INDICATOR_CLASS_NAME);
142 activationIndicator.addClassName(STATUS_INACTIVATED_CLASS_NAME);
143 addStyleName(EDITOR_BAR_WITH_STATUS_CLASS_NAME);
144 break;
145
146 case NodeTypes.Activatable.ACTIVATION_STATUS_ACTIVATED:
147 default:
148 break;
149 }
150 }
151
152 @Override
153 public void onAttach() {
154 super.onAttach();
155 }
156
157 protected void addButton(final Widget button) {
158 buttons.add(button);
159 }
160
161 protected void createControls() {
162 if (editable) {
163 final Label edit = new Label();
164 edit.setStyleName(ICON_CLASS_NAME);
165 edit.addStyleName(EDIT_CLASS_NAME);
166
167 eventManager.addClickOrTouchHandler(edit, event -> listener.edit());
168 addButton(edit);
169 }
170 }
171
172 protected void addStyles() {
173 if (listener instanceof MgnlElement) {
174 final String stylesAttribute = ((MgnlElement) listener).getAttribute("styles");
175 if (stylesAttribute != null && !stylesAttribute.isEmpty()) {
176 Arrays.asList(stylesAttribute.split(",")).forEach(this::addStyleName);
177 }
178 }
179 }
180
181 protected void addIcons() {
182 if (listener instanceof MgnlElement) {
183 final String iconClassNames = ((MgnlElement) listener).getAttribute("icons");
184 if (iconClassNames != null && !iconClassNames.isEmpty()) {
185 Arrays.asList(iconClassNames.split(","))
186 .forEach(iconClassName -> {
187 final Label label = new Label();
188 label.setStyleName(ICON_CLASS_NAME);
189 label.addStyleName(STATIC_ICON_CLASS_NAME);
190 label.addStyleName(iconClassName);
191 addButton(label);
192 });
193 }
194 }
195 }
196
197
198
199
200
201
202
203
204 public static class Builder<BUILDER extends Builder, T extends AbstractBar, LISTENER extends ControlBarListener> {
205
206 private LISTENER listener;
207 private int level;
208 private String label;
209 private int activationStatus;
210
211 public BUILDER withListener(LISTENER listener) {
212 this.listener = listener;
213 return (BUILDER) this;
214 }
215
216 public BUILDER withLevel(int level) {
217 this.level = level;
218 return (BUILDER) this;
219 }
220
221 public BUILDER withLabel(String label) {
222 this.label = label;
223 return (BUILDER) this;
224 }
225
226 public BUILDER withActivationStatus(int activationStatus) {
227 this.activationStatus = activationStatus;
228 return (BUILDER) this;
229 }
230
231 public T build(T controlBar) {
232 controlBar.listener = listener;
233 controlBar.initLayout();
234 controlBar.addLabel(label, level);
235 controlBar.addStatusIndicator(activationStatus);
236 controlBar.addStyles();
237
238 return controlBar;
239 }
240 }
241 }