Skip to content

Commit

Permalink
Changed AsyncTableCell's select all functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
SugaryLump committed Jan 17, 2025
1 parent 4667e26 commit 1a02593
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ private void cancelHandler() {
}

private void clearHandler() {
this.list.setSelectingAll(false);
this.list.setSelected(new HashSet<>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
Expand Down Expand Up @@ -103,16 +104,13 @@ protected enum TextAlign {

private FlowPanel mainPanel;
private FlowPanel sidePanel;
private FlowPanel selectAllPanel;
private FlowPanel selectAllPanelBody;
private Label selectAllLabel;
private CheckBox selectAllCheckBox;

private Column<T, Boolean> selectColumn;
private Set<T> selected = new HashSet<T>();
private boolean persistSelections = false;
private Set<String> persistedSelectedUUIDs = new HashSet<>();
private final List<CheckboxSelectionListener<T>> listeners = new ArrayList<AsyncTableCell.CheckboxSelectionListener<T>>();
private boolean selectingAll = false;

private Filter filter;
private boolean justActive;
Expand Down Expand Up @@ -204,8 +202,6 @@ protected void fireChangeEvent(IndexResult<T> result) {
pageSizePager = new RodaPageSizePager(getInitialPageSize(), getPageSizePagerIncrement());
pageSizePager.setDisplay(display);

createSelectAllPanel();

displayScroll = new ScrollPanel(display);
displayScroll.addStyleName("ms-scroll-fix");
displayScrollWrapper = new SimplePanel(displayScroll);
Expand Down Expand Up @@ -243,7 +239,6 @@ protected void fireChangeEvent(IndexResult<T> result) {

addValueChangeHandler(event -> {
selected = new HashSet<>();
hideSelectAllPanel();
});

sidePanel = new FlowPanel();
Expand All @@ -254,7 +249,6 @@ protected void fireChangeEvent(IndexResult<T> result) {
mainPanel.addStyleName("my-asyncdatagrid-main-panel");
add(mainPanel);

mainPanel.add(selectAllPanel);
mainPanel.add(displayScrollWrapper);
mainPanel.add(resultsPager);
if (exportButtons != null) {
Expand Down Expand Up @@ -312,17 +306,49 @@ protected void handleScrollChanges() {

private void configure(final CellTable<T> display) {
if (selectable) {
selectColumn = new Column<T, Boolean>(new CheckboxCell(true, false)) {
selectColumn = new Column<T, Boolean>(new CheckboxCell(true, false) {
private final SafeHtml INPUT_CHECKED = SafeHtmlUtils
.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\" checked/>");
private final SafeHtml INPUT_CHECKED_DISABLED = SafeHtmlUtils
.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\" checked disabled/>");
private final SafeHtml INPUT_UNCHECKED = SafeHtmlUtils
.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\"/>");

@Override
public Boolean getValue(T object) {
if (persistSelections) {
if (object == null) {
return false;
public void render(Context context, Boolean value, SafeHtmlBuilder sb) {
// Get the view data.
Object key = context.getKey();
Boolean viewData = getViewData(key);
if (viewData != null && viewData.equals(value)) {
clearViewData(key);
viewData = null;
}

if (selectingAll) {
sb.append(INPUT_CHECKED_DISABLED);
} else {
if (value != null && ((viewData != null) ? viewData : value)) {
sb.append(INPUT_CHECKED);
} else {
return persistedSelectedUUIDs.contains(object.getUuid());
sb.append(INPUT_UNCHECKED);
}
}
}
}) {
@Override
public Boolean getValue(T object) {
if (selectingAll) {
return true;
} else {
return selected.contains(object);
if (persistSelections) {
if (object == null) {
return false;
} else {
return persistedSelectedUUIDs.contains(object.getUuid());
}
} else {
return selected.contains(object);
}
}
}
};
Expand All @@ -341,60 +367,47 @@ public Boolean getValue(T object) {
fireOnCheckboxSelectionChanged();
});

Header<Boolean> selectHeader = new Header<Boolean>(new CheckboxCell(true, true)) {
Header<Boolean> selectHeader = new Header<Boolean>(new CheckboxCell(true, true) {
private final SafeHtml INPUT_OPEN = SafeHtmlUtils.fromSafeConstant("<");
private final SafeHtml INPUT_CLOSE = SafeHtmlUtils.fromSafeConstant("/>");
private final SafeHtml INPUT_CHECKED = SafeHtmlUtils
.fromSafeConstant("input type=\"checkbox\" tabindex=\"-1\" checked");
private final SafeHtml INPUT_UNCHECKED = SafeHtmlUtils
.fromSafeConstant("input type=\"checkbox\" tabindex=\"-1\"");

private SafeHtml makeTooltip() {
return SafeHtmlUtils.fromSafeConstant(" title=\"Select all " + dataProvider.getRowCount() + " items\" ");
}

@Override
public Boolean getValue() {
Boolean ret;

if (!persistSelections) {
if (selected.isEmpty()) {
ret = false;
} else if (selected.containsAll(getVisibleItems())) {
ret = true;
showSelectAllPanel();
} else {
// some are selected
ret = false;
hideSelectAllPanel();
}
public void render(Context context, Boolean value, SafeHtmlBuilder sb) {
// Get the view data.
Object key = context.getKey();
Boolean viewData = getViewData(key);
if (viewData != null && viewData.equals(value)) {
clearViewData(key);
viewData = null;
}

sb.append(INPUT_OPEN);
if (value != null && ((viewData != null) ? viewData : value)) {
sb.append(INPUT_CHECKED);
} else {
Collection<String> visibleUUIDs = new ArrayList<>();
for (T item : getVisibleItems()) {
if (item != null) {
visibleUUIDs.add(item.getUuid());
}
}
if (persistedSelectedUUIDs.isEmpty()) {
ret = false;
} else if (persistedSelectedUUIDs.containsAll(visibleUUIDs)) {
ret = true;
showSelectAllPanel();
} else {
// some are selected
ret = false;
hideSelectAllPanel();
}
sb.append(INPUT_UNCHECKED);
}
sb.append(makeTooltip());
sb.append(INPUT_CLOSE);
}
}) {

return ret;
@Override
public Boolean getValue() {
return selectingAll;
}
};

selectHeader.setUpdater(value -> {
if (value) {
for (T item : getVisibleItems()) {
persistedSelectedUUIDs.add(item.getUuid());
}
selected.addAll(getVisibleItems());
showSelectAllPanel();
} else {
for (T item : getVisibleItems()) {
persistedSelectedUUIDs.remove(item.getUuid());
}
selected.clear();
hideSelectAllPanel();
}
selectingAll = value;
redraw();
fireOnCheckboxSelectionChanged();
});
Expand Down Expand Up @@ -436,7 +449,6 @@ public SingleSelectionModel<T> getSelectionModel() {

public void refresh() {
selected = new HashSet<T>();
hideSelectAllPanel();
display.setVisibleRangeAndClearData(new Range(0, getInitialPageSize()), true);
getSelectionModel().clear();
}
Expand Down Expand Up @@ -661,7 +673,7 @@ public void setSelectable(boolean selectable) {

public SelectedItems<T> getSelected() {
SelectedItems<T> ret;
if (isAllSelected()) {
if (getSelectingAll()) {
ret = new SelectedItemsFilter<T>(getFilter(), selectedClass.getName(), getJustActive());
} else {
List<String> ids = new ArrayList<>();
Expand Down Expand Up @@ -735,48 +747,12 @@ public void fireOnCheckboxSelectionChanged() {
}
}

// SELECT ALL PANEL

public void createSelectAllPanel() {
selectAllPanel = new FlowPanel();
selectAllPanelBody = new FlowPanel();
selectAllCheckBox = new CheckBox();
selectAllLabel = new Label("Select all");

selectAllPanelBody.add(selectAllCheckBox);
selectAllPanelBody.add(selectAllLabel);
selectAllPanel.add(selectAllPanelBody);
selectAllPanel.setVisible(false);

selectAllPanel.addStyleName("panel");
selectAllPanelBody.addStyleName("panel-body");

selectAllCheckBox.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

@Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
fireOnCheckboxSelectionChanged();
}
});

}

public void showSelectAllPanel() {
if (!selectAllPanel.isVisible() && resultsPager.hasNextPage() || resultsPager.hasPreviousPage()) {
// selectAllLabel.setText(messages.listSelectAllMessage(dataProvider.getRowCount()));
selectAllLabel.setText("Select all " + dataProvider.getRowCount() + " items");
selectAllCheckBox.setValue(false);
selectAllPanel.setVisible(true);
}
}

public void hideSelectAllPanel() {
selectAllCheckBox.setValue(false);
selectAllPanel.setVisible(false);
public Boolean getSelectingAll() {
return selectingAll;
}

public Boolean isAllSelected() {
return selectAllCheckBox.getValue();
public void setSelectingAll(boolean value) {
this.selectingAll = value;
}

public O getObject() {
Expand Down

0 comments on commit 1a02593

Please sign in to comment.