package org.gwtoolbox.widget.client.panel.dashboard;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.DOM;
import org.gwtoolbox.commons.util.client.UIUtils;
/**
* @author Uri Boness
*/
public class DashboardColumn extends Composite {
private VerticalPanel main;
private int placeHolderIndex = -1;
public DashboardColumn() {
main = new InternalVerticalPanel();
main.setSpacing(5);
initWidget(main);
setStylePrimaryName("DashboardColumn");
}
public void addWidget(DashboardWidget dashboardWidget) {
addWidget(dashboardWidget, main.getWidgetCount());
}
public void addWidget(DashboardWidget dashboardWidget, int index) {
SimplePanel wrapper = new SimplePanel();
wrapper.setWidget(dashboardWidget);
dashboardWidget.setWidth("100%");
wrapper.setWidth("100%");
main.insert(wrapper, index);
}
public void handleDashboardWidgetDrop(DashboardWidget dashboardWidget) throws DropVetoException {
if (placeHolderIndex < 0) {
throw new DropVetoException();
}
SimplePanel placeHolder = (SimplePanel) main.getWidget(placeHolderIndex);
placeHolder.removeFromParent();
placeHolder = new SimplePanel();
main.insert(placeHolder, placeHolderIndex);
placeHolder.setWidget(dashboardWidget);
dashboardWidget.setWidth("100%");
placeHolder.setWidth("100%");
placeHolderIndex = -1;
}
public void showDropIndication(DashboardWidget dashboardWidget) {
int y = dashboardWidget.getAbsoluteTop() + dashboardWidget.getOffsetHeight() / 2;
int index = 0;
for (Widget widget : main) {
int centerLine = widget.getAbsoluteTop() + widget.getOffsetHeight() / 2;
if (y < centerLine) {
break;
}
index++;
}
if (placeHolderIndex == index) {
return;
}
if (placeHolderIndex >= 0) {
main.remove(placeHolderIndex);
}
placeHolderIndex = index;
SimplePanel placeHolder = new SimplePanel();
placeHolder.setSize("100%", dashboardWidget.getOffsetHeight() + "px");
placeHolder.addStyleName("PlaceHolder");
main.insert(placeHolder, index);
}
public void clearDropIndication() {
if (placeHolderIndex >= 0) {
main.remove(placeHolderIndex);
placeHolderIndex = -1;
}
}
//============================================== Inner Classes =====================================================
private class InternalVerticalPanel extends VerticalPanel {
public void add(Widget w) {
Element tr = DOM.createTR();
Element td = createAlignedTd();
UIUtils.setStyleName(td, "Cell", true);
DOM.appendChild(tr, td);
DOM.appendChild(getBody(), tr);
super.add(w, td);
}
public void insert(Widget w, int beforeIndex) {
checkIndexBoundsForInsertion(beforeIndex);
Element tr = DOM.createTR();
Element td = createAlignedTd();
UIUtils.setStyleName(td, "Cell", true);
DOM.appendChild(tr, td);
/*
* The case where we reinsert an already existing child is tricky.
*
* For the WIDGET, it ultimately removes first and inserts second, so we
* have to adjust the index within ComplexPanel.insert(). But for the DOM,
* we insert first and remove second, which means we DON'T need to adjust
* the index.
*/
DOM.insertChild(getBody(), tr, beforeIndex);
super.insert(w, td, beforeIndex, false);
}
private Element createAlignedTd() {
Element td = DOM.createTD();
setCellHorizontalAlignment(td, getHorizontalAlignment());
setCellVerticalAlignment(td, getVerticalAlignment());
return td;
}
}
}