How to create Drag n Drop for Cells in a DataGrid - gwt

I'm trying to add drag-n-drop to cell widgets. More specifically, I want to drag and drop ClickableTextCells, but they don't have the specific methods and not even .addDomHandler, so I can't just create something like .addDomHandler(new DragStartHandler() { ... }
Can someone give some help on how to DnD cells, preferably with pure GWT?

I do not know how to implement a DnD with GWT, but I know how to implement a CnC (Clic 'n Clic), which might interest you. DnD are cool, fun and beautiful, but I think that some times they are not very convenient. For instance if you have a big screen requiring a scroll, and if you want to DnD an item from the top to the bottom, it is not so convenient to have to hold the mouse... But this just a personnal feeling...
Anyway, I would recommand you to use a ListDataProvider along with simple events, in order to move your items: the first clic selects the source item, and the second clic selects the destination. Once the source and the destination are known, you can move your item.
It works well for me... and if you add some styles to highlight source and destination, it is very nice.
Example:
This is the main class:
import java.util.ArrayList;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
public class Clic_Clic {
private static final Integer LEFT = 0;
private static final Integer CENTER = 1;
private static final Integer RIGHT = 2;
private ClicClicSelectionModel<Item> selectionModel = new ClicClicSelectionModel<Item>();
ListDataProvider<Item> leftLDP = new ListDataProvider<Item>();
ListDataProvider<Item> centerLDP = new ListDataProvider<Item>();
ListDataProvider<Item> rightLDP = new ListDataProvider<Item>();
FocusPanel left = new FocusPanel(), center = new FocusPanel(), right = new FocusPanel();
Item selected = null;
public Clic_Clic() {
// --- Builds the GUI
DialogBox clic_clic = buildGUI();
clic_clic.center();
clic_clic.show();
// --- Initializes data
configureSelectionManagement();
initCellLists();
configureAddAndRemove();
// --- Fills the left LDP
leftLDP.getList().add(new Item("Fraternité", LEFT));
leftLDP.refresh();
// --- Fills the center LDP
centerLDP.getList().add(new Item("Egalité", LEFT));
centerLDP.refresh();
// --- Fills the right LDP
rightLDP.getList().add(new Item("Liberté", RIGHT));
rightLDP.refresh();
}
private DialogBox buildGUI() {
DialogBox db = new DialogBox();
db.setText("A simple example for Clic 'n Clic");
db.setSize("300px", "200px");
db.setGlassEnabled(true);
db.setModal(true);
FlowPanel fp = buildContent();
db.add(fp);
return db;
}
private FlowPanel buildContent() {
Grid g = new Grid(1, 3);
g.setSize("300px", "200px");
g.setWidget(0, 0, left);
left.setSize("100px", "100px");
left.getElement().getStyle().setBackgroundColor("blue");
g.setWidget(0, 1, center);
center.setSize("100px", "100px");
g.setWidget(0, 2, right);
right.setSize("100px", "100px");
right.getElement().getStyle().setBackgroundColor("red");
FlowPanel fp = new FlowPanel();
fp.setSize("300px", "200px");
fp.add(new Label("Do you know the correct order ?"));
fp.add(g);
return fp;
}
private void initCellLists() {
// --- Associates the left panel with the leftLDP ListDataProvider
CellList<Item> cellListLeft = new CellList<Item>(new MyCell());
cellListLeft.setSelectionModel(selectionModel);
left.add(cellListLeft);
leftLDP = new ListDataProvider<Item>(new ArrayList<Item>());
leftLDP.addDataDisplay(cellListLeft);
// --- Associates the center panel with the centerLDP ListDataProvider
CellList<Item> cellListCenter = new CellList<Item>(new MyCell());
cellListCenter.setSelectionModel(selectionModel);
center.add(cellListCenter);
centerLDP = new ListDataProvider<Item>(new ArrayList<Item>());
centerLDP.addDataDisplay(cellListCenter);
// --- Associates the right panel with the rightLDP ListDataProvider
CellList<Item> cellListRight = new CellList<Item>(new MyCell());
cellListRight.setSelectionModel(selectionModel);
right.add(cellListRight);
rightLDP = new ListDataProvider<Item>(new ArrayList<Item>());
rightLDP.addDataDisplay(cellListRight);
}
private void configureAddAndRemove() {
// --- If the user clic on the left
left.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
if (selected != null) {
// --- If the selected item comes from the right
if (selected.getContainerIndex() == RIGHT) {
rightLDP.getList().remove(selected);
rightLDP.refresh();
selected.setContainerIndex(LEFT);
leftLDP.getList().add(selected);
leftLDP.refresh();
selected = null;
}
// --- If the selected item comes from the center
if (selected.getContainerIndex() == CENTER) {
centerLDP.getList().remove(selected);
centerLDP.refresh();
selected.setContainerIndex(LEFT);
leftLDP.getList().add(selected);
leftLDP.refresh();
selected = null;
}
}
}
});
// --- If the user clic on the center
center.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
if (selected != null) {
// --- If the selected item comes from the right
if (selected.getContainerIndex() == RIGHT) {
rightLDP.getList().remove(selected);
rightLDP.refresh();
selected.setContainerIndex(CENTER);
centerLDP.getList().add(selected);
centerLDP.refresh();
selected = null;
}
// --- If the selected item comes from the left
if (selected.getContainerIndex() == LEFT) {
leftLDP.getList().remove(selected);
leftLDP.refresh();
selected.setContainerIndex(CENTER);
centerLDP.getList().add(selected);
centerLDP.refresh();
selected = null;
}
}
}
});
// --- If the user clic on the right
right.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// --- If the selected item comes from the left
if (selected.getContainerIndex() == LEFT) {
leftLDP.getList().remove(selected);
leftLDP.refresh();
selected.setContainerIndex(RIGHT);
rightLDP.getList().add(selected);
rightLDP.refresh();
selected = null;
}
// --- If the selected item comes from the center
if (selected.getContainerIndex() == CENTER) {
centerLDP.getList().remove(selected);
centerLDP.refresh();
selected.setContainerIndex(RIGHT);
rightLDP.getList().add(selected);
rightLDP.refresh();
selected = null;
}
}
});
}
#SuppressWarnings("hiding")
class ClicClicSelectionModel<Item> extends SingleSelectionModel<Item> {
#Override
public void setSelected(Item object, boolean selected) {
if (getSelectedObject() != null && getSelectedObject().equals(object)) {
super.setSelected(object, !selected);
} else {
super.setSelected(object, selected);
}
};
}
private void configureSelectionManagement() {
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
Item selected = selectionModel.getSelectedObject();
updateSelected(selected);
}
});
}
private void updateSelected(Item item) {
// --- If no item has been selected, update the selected item
if (selected == null) {
selected = item;
}
}
}
You also need this one:
public class Item {
private String label;
private Integer containerIndex;
public Item(String l, Integer id) {
this.label = l;
this.containerIndex = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public Integer getContainerIndex() {
return containerIndex;
}
public void setContainerIndex(Integer containerIndex) {
this.containerIndex = containerIndex;
}
}
And finally, this one:
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
public class MyCell extends AbstractCell<Item> {
#Override
public void render(com.google.gwt.cell.client.Cell.Context context, Item value, SafeHtmlBuilder sb) {
if (value != null) {
// --- If the value comes from the user, we escape it to avoid XSS
// attacks.
SafeHtml safeValue = SafeHtmlUtils.fromString(value.getLabel());
sb.append(safeValue);
}
}
}
Here you go.
Next time I'll try to make a fun example :)
Hope it helps.

Related

JavaFX custom ListCell

I'm trying to animate ListCell when they appear.
Specially I try to animate a new cell when it was just added to the list.
For now it's working pretty OK except when I scroll the ListView, then indexes get messed up and the wrong cell is animated.
I use a boolean flag (entering) in my item model to detect when a cell is used for a brand new item.
public class TimeListCell extends ListCell<MarkItem> {
private static final String BUTTON_GOTO_MARK_CLASS = "but-markgoto";
private static final String LABEL_TIME_MARK_CLASS = "track-time";
private static final String BUTTON_DELETE_MARK_CLASS = "but-markdel";
private static final String MARK_HIGHLIGHT_CURRENT_CLASS = "highlighted";
private Instant time;
private MarkItem markItem;
protected ListCellAnimation anim;
private HBox root = new HBox();
private Button go = new Button();
private Label track = new Label();;
private Button del = new Button();
private ChangeListener<? super Boolean> highlightChange = (e, o, n) -> { setHighlighted(n); };
public TimeListCell (Consumer<MarkItem> onGoto, Consumer<MarkItem> onDelete) {
root.setAlignment(Pos.CENTER);
go.getStyleClass().add(BUTTON_GOTO_MARK_CLASS);
go.setOnAction( e -> {
if (onGoto != null) {
// Trigger GOTO consumer function
onGoto.accept(markItem);
}
});
track.getStyleClass().add(LABEL_TIME_MARK_CLASS);
del.getStyleClass().add(BUTTON_DELETE_MARK_CLASS);
del.setOnAction( e -> {
// First trigger exit animation then delete item
this.animateExit(onDelete);
});
root.getChildren().add(go);
root.getChildren().add(track);
root.getChildren().add(del);
}
#Override
protected void updateItem (final MarkItem item, boolean empty) {
super.updateItem(item, empty);
if (markItem != null) {
markItem.highlightedProperty().removeListener(highlightChange);
}
if (!empty && item != null) {
markItem = item;
time = item.getTime();
track.setText(DateUtil.format(time, DateUtil.Pattern.TIME));
setGraphic(root);
item.highlightedProperty().addListener(highlightChange);
setHighlighted(item.isHighlighted());
if (anim == null) {
//Adding Animation to the ListCell
anim = new ListCellAnimation(this);
//KeyFrame[] f = getKeyFrames(types);
KeyFrame[] frames = null;
if (anim.getKeyFrames().size() == 0) {
KeyFrame[] f = anim.getPopIn(frames);
if (f != null) {
anim.getKeyFrames().addAll(f);
}
}
}
if (item.isEntering()) {
//Checking when to play Animation
animateEnter();
item.setEntering(false);
}
} else {
setGraphic(null);
}
}
/**
* Set/unset cell highlighted style for display.
*
* #param highlighted
* Whether or not to highlight the cell
*/
public void setHighlighted (boolean highlighted) {
track.getStyleClass().remove(MARK_HIGHLIGHT_CURRENT_CLASS);
if (highlighted)
track.getStyleClass().add(MARK_HIGHLIGHT_CURRENT_CLASS);
}
/**
* Animate entering cell.
*/
private void animateEnter() {
if (anim != null && anim.getKeyFrames().size() >= 0
&& (anim.getTimeline().getStatus() == Timeline.Status.STOPPED
|| anim.getTimeline().getStatus() == Timeline.Status.PAUSED)) {
anim.getTimeline().playFromStart();
}
}
/**
* Animate exiting cell.
* Trigger DELETE consumer function when animation is complete.
*/
private void animateExit (Consumer<MarkItem> onDelete) {
anim.getReversedTimeline().setOnFinished( t -> {
// Remove item from list
if (onDelete != null) {
onDelete.accept(markItem);
}
// Prepare cell for next item to use it
scaleXProperty().set(1);
scaleYProperty().set(1);
});
anim.getReversedTimeline().playFromStart();
}
public Instant getTime () {
return time;
}
}
Has anyone any idea of what could mess up the cell indexing ?
Thanks.
If a cell which is animating is reused to display an item that is not "entering", then you need to stop the current animation:
if (item.isEntering()) {
//Checking when to play Animation
animateEnter();
item.setEntering(false);
} else {
anim.getTimeline().stop();
}
In general, you seem to be assuming that any given cell is only ever used for a single item, which is certainly not the case. There may be other bugs in your code that are consequences of this assumption, but this is the main one I can see.

GWT Cell Table Clicking on a new row gives value of Previously selected row

In GWT 2.6 CellTable, I'm writing a single click event to perform some operation. I can not get the correct Row Index while clicking on the CellTable Row; only a double click event returns the row correctly.
final SingleSelectionModel<PatientDTO> selectionModel =
new SingleSelectionModel<PatientDTO>();
patientsTable.setSelectionModel(selectionModel);
patientsTable.addDomHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
PatientDTO selected = selectionModel.getSelectedObject();
if (selected != null)
{
RootLayoutPanel.get().clear();
RootLayoutPanel.get().add(new PatientPanel(selected));
}
}
}, ClickEvent.getType());
Either use SingleSelectionModel or MultiSelectionModel and add SelectionChangeHandler on it that will be fired when selection is changed in the CellTable
Sample code:
final SingleSelectionModel<Contact> selectionModel = new SingleSelectionModel<Contact>();
//final MultiSelectionModel<Contact> selectionModel = new MultiSelectionModel<Contact>();
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
Set<Contact> selected = selectionModel.getSelectedSet();
if (selected != null) {
for (Contact contact : selected) {
System.out.println("You selected: " + contact.name);
}
}
}
});
OR alternatively try with CellPreviewHandler
table.addCellPreviewHandler(new Handler<Contact>() {
#Override
public void onCellPreview(CellPreviewEvent<Contact> event) {
int row = event.getIndex();
int column = event.getColumn();
if ("focus".equals(event.getNativeEvent().getType())) {
//..
}
if ("blur".equals(event.getNativeEvent().getType())) {
//...
}
if ("mousedown".equals(event.getNativeEvent().getType())) {
//..
}
if ("mouseover".equals(event.getNativeEvent().getType())) {
//..
}
}
});

How to make GWT Datagrid have its first column fixed and scroll horizontally and vertically

Currently GWT DataGrid header does this trick with a fixed header row during a vertical scroll. Is there a way to acheive the same on an entire (first) column?
I have implemented ScrolledGrid that freezes first column in DataGrid. You need to use it instead of DataGrid in order to make first column be frozen.
import com.google.gwt.dom.client.*;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.client.ui.HeaderPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
/**
*
* #author Yuri Plaksyuk
*/
public class ScrolledGrid extends DataGrid {
private final Text cssText;
private boolean addedClass = false;
private int currentScrollLeft = 0;
public ScrolledGrid() {
cssText = Document.get().createTextNode("");
StyleElement styleElement = Document.get().createStyleElement();
styleElement.setType("text/css");
styleElement.appendChild(cssText);
HeaderPanel headerPanel = (HeaderPanel) getWidget();
headerPanel.getElement().insertFirst(styleElement);
final ScrollPanel scrollPanel = (ScrollPanel) headerPanel.getContentWidget();
scrollPanel.addScrollHandler(new ScrollHandler() {
#Override
public void onScroll(ScrollEvent event) {
int scrollLeft = scrollPanel.getHorizontalScrollPosition();
if (scrollLeft != currentScrollLeft) {
StringBuilder css = new StringBuilder();
if (scrollLeft > 0) {
css.append(".ScrolledGrid-frozen {");
css.append("background-color: inherit;");
css.append("}");
css.append(".ScrolledGrid-frozen div {");
css.append("position: absolute;");
css.append("left: ").append(scrollLeft).append("px;");
css.append("width: ").append(getColumnWidth(getColumn(0))).append(";");
css.append("padding-left: 1.3em;");
css.append("padding-right: 0.5em;");
css.append("margin-top: -0.7em;");
css.append("white-space: nowrap;");
css.append("background-color: inherit;");
css.append("}");
}
else
css.append(".ScrolledGrid-frozen { }");
css.append("th.ScrolledGrid-frozen { background-color: white; }");
cssText.setData(css.toString());
if (!addedClass) {
NodeList<TableRowElement> rows;
TableRowElement row;
TableCellElement cell;
rows = getTableHeadElement().getRows();
for (int i = 0; i < rows.getLength(); ++i) {
row = rows.getItem(i);
cell = row.getCells().getItem(0);
cell.setInnerHTML("<div>" + cell.getInnerHTML() + "</div>");
cell.addClassName("ScrolledGrid-frozen");
}
rows = getTableBodyElement().getRows();
for (int i = 0; i < rows.getLength(); ++i) {
row = rows.getItem(i);
cell = row.getCells().getItem(0);
cell.addClassName("ScrolledGrid-frozen");
}
addedClass = true;
}
currentScrollLeft = scrollLeft;
}
}
});
}
}
Unfortunately, some CSS values are hard-coded.
I adapted Yuri's solution to achieve the following goals:
does not flicker
copes with arbitrary row-heights
works with SelectionModel
more uniform solution
It does not mess with the columns itself, but instead shows arbitrary "frozen" information on row-level.
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.*;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.DefaultCellTableBuilder;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HeaderPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
/**
* #author Daniel Lintner
*
* A DataGrid extension with the ability to display some row-level-information
* when scrolling left (horizontal), hence important columns out of sight of the user.
*/
public class FrozenDataGrid extends DataGrid
{
//textnode getting updated dynamically when scolling horizontally
private Text cssText;
//the latest scroll-position
private int currentScrollLeft = 0;
//an object extracting String-info from your rowdata
private FrozenValueProvider valueProvider;
//inject basic styling into the document - once
//this is how the frozen row-info looks like
static
{
Text baseCss = Document.get().createTextNode("");
StyleElement styleElement = Document.get().createStyleElement();
styleElement.setType("text/css");
styleElement.appendChild(baseCss);
StringBuilder css = new StringBuilder();
css.append(".ScrolledGrid-base {");
css.append("position: absolute;");
css.append("background-color: gray;");
css.append("padding: .3em;");
css.append("padding-left: .5em;");
css.append("padding-right: .5em;");
css.append("border-radius: 3px 3px;");
css.append("transition: opacity 500ms;");
css.append("color: white;");
css.append("margin-top: 2px;");
css.append("white-space: nowrap;");
css.append("}");
baseCss.setData(css.toString());
Document.get().getBody().insertFirst(styleElement);
}
public FrozenDataGrid()
{
super();
init();
}
public FrozenDataGrid(int pageSize, DataGrid.Resources resources)
{
super(pageSize, resources);
init();
}
public void init()
{
//create a css textnode
cssText = Document.get().createTextNode("");
//create dynamic css Style
StyleElement styleElement = Document.get().createStyleElement();
styleElement.setType("text/css");
styleElement.appendChild(cssText);
//append the initial style condition
//todo the name of this style might be built dynamically per instance - if multiple grid-instances exist/not the use-case by now
StringBuilder css = new StringBuilder();
css.append(".ScrolledGrid-frozen {");
css.append("opacity:0;");
css.append("}");
cssText.setData(css.toString());
//set a custom CellTableBuilder in order to inject the info-div to the row
setTableBuilder(new DefaultCellTableBuilder(this)
{
#Override
public void buildRowImpl(final Object rowValue, final int absRowIndex)
{
//do what DefaultCellTableBuilder does
super.buildRowImpl(rowValue, absRowIndex);
//only do something if there is a valueProvider
if(valueProvider != null) {
//we do this deferred because this row has to created first in order to access it
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand()
{
#Override
public void execute()
{
createInfoDiv(getTableBodyElement().getRows().getItem(absRowIndex % getPageSize()), rowValue);
}
});
}
}
});
//fetch the ScrollPanel from the grid
HeaderPanel headerPanel = (HeaderPanel) getWidget();
headerPanel.getElement().insertFirst(styleElement);
final ScrollPanel scrollPanel = (ScrollPanel) headerPanel.getContentWidget();
//setup a timer handling the left-offset-css thing
//we use a timer to be able to cancel this operation -> e.g. continuous scroll
final Timer timer = new Timer(){
#Override
public void run() {
StringBuilder css = new StringBuilder();
//we need to left-offset the info-divs
if (scrollPanel.getHorizontalScrollPosition() > 100)
{
css.append(".ScrolledGrid-frozen {");
css.append("left: ").append(3 + scrollPanel.getHorizontalScrollPosition()).append("px;");
css.append("opacity: 1;");
css.append("}");
}
//we are close to the leftmost scroll position: info hidden
else
{
css.append(".ScrolledGrid-frozen {");
css.append("opacity:0;");
css.append("}");
}
cssText.setData(css.toString());
}
};
//track scrolling
scrollPanel.addScrollHandler(new ScrollHandler()
{
#Override
public void onScroll(ScrollEvent event)
{
//cancel previous actions to scroll events
if(timer.isRunning())
timer.cancel();
//actual horizontal scrollposition
int scrollLeft = scrollPanel.getHorizontalScrollPosition();
//a horizontal scroll takes places
if (scrollLeft != currentScrollLeft)
{
//first we hide the row-info
StringBuilder css = new StringBuilder();
css.append(".ScrolledGrid-frozen {");
css.append("opacity:0;");
css.append("}");
cssText.setData(css.toString());
//render left offset after a delay
timer.schedule(500);
//remember the current horizontal position
currentScrollLeft = scrollLeft;
}
}
});
}
private void createInfoDiv(TableRowElement row, Object value)
{
//create a div element and add value and style to it
DivElement div = Document.get().createDivElement();
div.setInnerText(valueProvider.getFrozenValue(value));
div.addClassName("ScrolledGrid-base");
div.addClassName("ScrolledGrid-frozen");
//we add it to the first child of the row, because added as child of the row directly
// confuses the CellTable with coordinating header positions
row.getFirstChildElement().insertFirst(div);
}
public void setFrozenValueProvider(FrozenValueProvider valueProvider) {
this.valueProvider = valueProvider;
}
public interface FrozenValueProvider<T>{
String getFrozenValue(T data);
}
}
Hope this helps developers on this rarely and unsatisfactorily solved problem.
And... there is still room for improvement left.
Cheers Dan

GXT: LayoutContainer does not respond to ESC Key or "X" button to close

I have a GXT 2.x application with a Menubar Item that renders a separate LayoutContainer.
Here's the hierarchy
MainUI.java -> MenuBar.java -> ReservationPopUp.java
I have replaced my contents of ReservationPopUp.java with KNOWN working examples of LayoutContainer implementations and they respond to the ESC key and "X" button.
Here's how the MenuItem renders the ReservationPopUp.java
MenuItem mntmReserve = new MenuItem("Reserve");
mntmReserve.addSelectionListener(new SelectionListener<MenuEvent>() {
public void componentSelected(MenuEvent ce) {
RootPanel.get().add(new ReservationPopUp());
}
Here's a slimmed down version of my ReservationPopUp.java
public class ReservationPopUp extends LayoutContainer {
public ReservationPopUp() {
}
#Override
protected void onRender(Element parent, int pos) {
super.onRender(parent, pos);
setSize("1024", "809");
final Window window = new Window();
window.setDraggable(false);
window.setSize(537, 399);
window.setPlain(true);
window.setModal(true);
window.setBlinkModal(true);
window.setHeading("Reserve A Server");
window.setClosable(true);
window.setOnEsc(true);
window.setSize("465", "345");
window.setLayout(new AbsoluteLayout());
LabelField lblfldUsers = new LabelField("Users");
window.add(lblfldUsers, new AbsoluteData(43, 218));
final ComboBox<AsyncUser> userList = new ComboBox<AsyncUser>();
window.add(userList, new AbsoluteData(81, 218));
userList.setEmptyText("Select a User...");
userList.setSize("347px", "24px");
LabelField labelServers = new LabelField("Servers");
window.add(labelServers, new AbsoluteData(32, 6));
final DualListField<AsyncServer> serverList = new DualListField<AsyncServer>();
....
window.add(serverList, new AbsoluteData(81, 6));
serverList.setSize("347px", "206px");
window.addButton(new Button("Cancel", new SelectionListener<ButtonEvent>() {
#Override
public void componentSelected(ButtonEvent ce) {
ReservationPopUp.this.hide();
}
}));
window.addButton(new Button("Reserve", new SelectionListener<ButtonEvent>() {
#Override
public void componentSelected(ButtonEvent ce) {
if (serverList.getToList().getListView().getItemCount() == 0 ) {
MessageBox.alert("Invalid Selection","No Server(s) Selected", null);
} else if ( userList.getValue() == null) {
} else {
// DO some stuff
ReservationPopUp.this.hide();
}
}
}));
window.addWindowListener(new WindowListener() {
#Override
public void windowHide(WindowEvent we) {
ReservationPopUp.this.hide();
}
});
window.setFocusWidget(window.getButtonBar().getItem(0));
add(window);
}
}
Window is a popup, it doesn't need to be (and shouldn't be) added to anything. Extend the Window class instead of the LayoutContainer, and instead of adding the ReservationPopup to the page, just call Window.show().

GWT: In a TabLayoutPanel, how do I make tabs wrap if there are too many?

I'm using GWT 2.4. I have a TabLayoutPanel to which I add tabs. Each tab contains a ScrollPanel. My question is, how do I make the tabs in the tab bar wrap to the next line if the width of the tab bar exceeds the visible width?
Thanks, - Dave
GWT's TabLayoutPanel intentionally never wraps tabs. See lines 246-248 in TabLayoutPanel.java - (line 217 defines private static final int BIG_ENOUGH_TO_NOT_WRAP = 16384). You might be able to override this, but as #milan says, it's probably not good design.
Having multiple lines is, indeed, not recommended...
However, to be able to navigate left/right on a single tab bar with many tabs, you can use this recipe:
http://devnotesblog.wordpress.com/2010/06/17/scrollable-gwt-tablayoutpanel/
And an updated implementation that doesn't use the deprecated DeferredCommand:
package whatever.you.want;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
/**
* A {#link TabLayoutPanel} that shows scroll buttons if necessary
*/
public class ScrolledTabLayoutPanel extends TabLayoutPanel {
private static final int IMAGE_PADDING_PIXELS = 4;
private LayoutPanel panel;
private FlowPanel tabBar;
private Image scrollLeftButton;
private Image scrollRightButton;
private HandlerRegistration windowResizeHandler;
private ImageResource leftArrowImage;
private ImageResource rightArrowImage;
public ScrolledTabLayoutPanel(double barHeight, Unit barUnit,
ImageResource leftArrowImage, ImageResource rightArrowImage) {
super(barHeight, barUnit);
this.leftArrowImage = leftArrowImage;
this.rightArrowImage = rightArrowImage;
// The main widget wrapped by this composite, which is a LayoutPanel with the tab bar & the tab content
panel = (LayoutPanel) getWidget();
// Find the tab bar, which is the first flow panel in the LayoutPanel
for (int i = 0; i < panel.getWidgetCount(); ++i) {
Widget widget = panel.getWidget(i);
if (widget instanceof FlowPanel) {
tabBar = (FlowPanel) widget;
break; // tab bar found
}
}
initScrollButtons();
}
#Override
public void add(Widget child, Widget tab) {
super.add(child, tab);
checkIfScrollButtonsNecessary();
}
#Override
public boolean remove(Widget w) {
boolean b = super.remove(w);
checkIfScrollButtonsNecessary();
return b;
}
#Override
protected void onLoad() {
super.onLoad();
if (windowResizeHandler == null) {
windowResizeHandler = Window.addResizeHandler(new ResizeHandler() {
#Override
public void onResize(ResizeEvent event) {
checkIfScrollButtonsNecessary();
}
});
}
}
#Override
protected void onUnload() {
super.onUnload();
if (windowResizeHandler != null) {
windowResizeHandler.removeHandler();
windowResizeHandler = null;
}
}
private ClickHandler createScrollClickHandler(final int diff) {
return new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Widget lastTab = getLastTab();
if (lastTab == null)
return;
int newLeft = parsePosition(tabBar.getElement().getStyle().getLeft()) + diff;
int rightOfLastTab = getRightOfWidget(lastTab);
// Prevent scrolling the last tab too far away form the right border,
// or the first tab further than the left border position
if (newLeft <= 0 && (getTabBarWidth() - newLeft < (rightOfLastTab + 20))) {
scrollTo(newLeft);
}
}
};
}
/** Create and attach the scroll button images with a click handler */
private void initScrollButtons() {
scrollLeftButton = new Image(leftArrowImage);
int leftImageWidth = scrollLeftButton.getWidth();
panel.insert(scrollLeftButton, 0);
panel.setWidgetLeftWidth(scrollLeftButton, 0, Unit.PX, leftImageWidth, Unit.PX);
panel.setWidgetTopHeight(scrollLeftButton, 0, Unit.PX, scrollLeftButton.getWidth(), Unit.PX);
scrollLeftButton.addClickHandler(createScrollClickHandler(+20));
scrollLeftButton.setVisible(false);
scrollRightButton = new Image(rightArrowImage);
panel.insert(scrollRightButton, 0);
panel.setWidgetLeftWidth(scrollRightButton, leftImageWidth + IMAGE_PADDING_PIXELS, Unit.PX, scrollRightButton.getWidth(), Unit.PX);
panel.setWidgetTopHeight(scrollRightButton, 0, Unit.PX, scrollRightButton.getHeight(), Unit.PX);
scrollRightButton.addClickHandler(createScrollClickHandler(-20));
scrollRightButton.setVisible(false);
}
private void checkIfScrollButtonsNecessary() {
// Defer size calculations until sizes are available, when calculating immediately after
// add(), all size methods return zero
Scheduler.get().scheduleDeferred( new Scheduler.ScheduledCommand() {
#Override
public void execute() {
boolean isScrolling = isScrollingNecessary();
// When the scroll buttons are being hidden, reset the scroll position to zero to
// make sure no tabs are still out of sight
if (scrollRightButton.isVisible() && !isScrolling) {
resetScrollPosition();
}
scrollRightButton.setVisible(isScrolling);
scrollLeftButton.setVisible(isScrolling);
}
}
);
}
private void resetScrollPosition() {
scrollTo(0);
}
private void scrollTo(int pos) {
tabBar.getElement().getStyle().setLeft(pos, Unit.PX);
}
private boolean isScrollingNecessary() {
Widget lastTab = getLastTab();
if (lastTab == null)
return false;
return getRightOfWidget(lastTab) > getTabBarWidth();
}
private int getRightOfWidget(Widget widget) {
return widget.getElement().getOffsetLeft() + widget.getElement().getOffsetWidth();
}
private int getTabBarWidth() {
return tabBar.getElement().getParentElement().getClientWidth();
}
private Widget getLastTab() {
if (tabBar.getWidgetCount() == 0)
return null;
return tabBar.getWidget(tabBar.getWidgetCount() - 1);
}
private static int parsePosition(String positionString) {
int position;
try {
for (int i = 0; i < positionString.length(); i++) {
char c = positionString.charAt(i);
if (c != '-' && !(c >= '0' && c <= '9')) {
positionString = positionString.substring(0, i);
}
}
position = Integer.parseInt(positionString);
} catch (NumberFormatException ex) {
position = 0;
}
return position;
}
}