package org.gwtoolbox.commons.ui.client.notification.tray;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootPanel;
import org.gwtoolbox.commons.ui.client.notification.Notification;
import org.gwtoolbox.commons.ui.client.notification.Notifier;
/**
* @author Uri Boness
*/
public class TrayNotifier implements Notifier {
private static TrayNotifier instance;
private Orientation orientation;
private Offset offset;
private final FlowPanel main;
private TrayNotifier() {
main = new FlowPanel();
main.setStylePrimaryName("gtx-TrayNotifier");
orientation = Orientation.BOTTOM_RIGHT;
offset = Offset.create(10, 10);
resetPosition();
RootPanel.get().add(main);
}
public void setOffset(Offset offset) {
this.offset = offset;
resetPosition();
}
public void setOrientation(Orientation orientation) {
this.orientation = orientation;
resetPosition();
}
public static TrayNotifier get() {
if (instance == null) {
instance = new TrayNotifier();
}
return instance;
}
@Override
public void notify(Notification notification) {
TrayNotificationBox box = new TrayNotificationBox(notification);
if (orientation.isTop()) {
box.getElement().getStyle().clearMarginTop();
box.getElement().getStyle().setMarginBottom(5, Style.Unit.PX);
main.add(box);
} else {
box.getElement().getStyle().clearMarginBottom();
box.getElement().getStyle().setMarginTop(5, Style.Unit.PX);
main.insert(box, 0);
}
}
//================================================ Helper Methods ==================================================
private void resetPosition() {
switch (orientation) {
case TOP_LEFT:
main.getElement().getStyle().clearRight();
main.getElement().getStyle().clearBottom();
main.getElement().getStyle().setLeft(offset.x(), Style.Unit.PX);
main.getElement().getStyle().setTop(offset.y(), Style.Unit.PX);
break;
case TOP_RIGHT:
main.getElement().getStyle().clearLeft();
main.getElement().getStyle().clearBottom();
main.getElement().getStyle().setRight(offset.x(), Style.Unit.PX);
main.getElement().getStyle().setTop(offset.y(), Style.Unit.PX);
break;
case BOTTOM_LEFT:
main.getElement().getStyle().clearRight();
main.getElement().getStyle().clearTop();
main.getElement().getStyle().setLeft(offset.x(), Style.Unit.PX);
main.getElement().getStyle().setBottom(offset.y(), Style.Unit.PX);
break;
case BOTTOM_RIGHT:
main.getElement().getStyle().clearLeft();
main.getElement().getStyle().clearTop();
main.getElement().getStyle().setRight(offset.x(), Style.Unit.PX);
main.getElement().getStyle().setBottom(offset.y(), Style.Unit.PX);
break;
}
}
}