From 137c02ee882f2d6ecbba44b5f65035e58ab68a9e Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Fri, 19 Dec 2014 18:02:37 +0000 Subject: [PATCH] Minor update git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-widgetx@102174 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../tdwx/client/TabularDataXGridPanel.java | 9 +- .../client/config/ExtendedLiveGridView.java | 213 +++++++++++++++++- .../config/GridAndCellSelectionModel.java | 18 ++ 3 files changed, 229 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/tdwx/client/TabularDataXGridPanel.java b/src/main/java/org/gcube/portlets/user/tdwx/client/TabularDataXGridPanel.java index 7738f5d..f050505 100644 --- a/src/main/java/org/gcube/portlets/user/tdwx/client/TabularDataXGridPanel.java +++ b/src/main/java/org/gcube/portlets/user/tdwx/client/TabularDataXGridPanel.java @@ -134,6 +134,8 @@ public class TabularDataXGridPanel extends ContentPanel { private ColumnModel columnModel; + private GridAndCellSelectionModel sm; + /** * @param eventBus */ @@ -766,7 +768,7 @@ public class TabularDataXGridPanel extends ContentPanel { // liveGridView.setRowHeight(19); grid = new Grid(store, columnModel); - GridAndCellSelectionModel sm = new GridAndCellSelectionModel(); + sm = new GridAndCellSelectionModel(); grid.setSelectionModel(sm); grid.setLoadMask(true); @@ -826,11 +828,12 @@ public class TabularDataXGridPanel extends ContentPanel { } else { Log.debug("Setup grid not null"); // TODO we need to manually reset the sort + sm.onChangeNumberOfRows(); loader.clearSortInfo(); reader.setDefinition(tableDefinition); - grid.reconfigure(store, columnModel); - + + ArrayList> filters = FiltersGenerator .generate(columnModel); gridFilters.removeAll(); diff --git a/src/main/java/org/gcube/portlets/user/tdwx/client/config/ExtendedLiveGridView.java b/src/main/java/org/gcube/portlets/user/tdwx/client/config/ExtendedLiveGridView.java index c902608..a35f19e 100644 --- a/src/main/java/org/gcube/portlets/user/tdwx/client/config/ExtendedLiveGridView.java +++ b/src/main/java/org/gcube/portlets/user/tdwx/client/config/ExtendedLiveGridView.java @@ -1,6 +1,13 @@ package org.gcube.portlets.user.tdwx.client.config; +import com.allen_sauer.gwt.log.client.Log; +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.EventTarget; +import com.google.gwt.user.client.Event; +import com.sencha.gxt.core.client.GXTLogConfiguration; import com.sencha.gxt.data.shared.ListStore; +import com.sencha.gxt.messages.client.DefaultMessages; +import com.sencha.gxt.widget.core.client.event.LiveGridViewUpdateEvent; import com.sencha.gxt.widget.core.client.grid.LiveGridView; /** @@ -11,9 +18,21 @@ import com.sencha.gxt.widget.core.client.grid.LiveGridView; * @param */ public class ExtendedLiveGridView extends LiveGridView { - // TODO bug in gxt3 3.0.0 fixed in future - - + + private boolean isLoading; + private boolean isMasked; + + @SuppressWarnings("unused") + private int viewIndexReload = -1; + + private int lastViewIndex = -1; + private int lastScrollDirection = 0; + private int rowHeightAdjust; + private boolean measureRowHeight = true; + private boolean ignoreScroll; + + // private boolean adjustScrollHeight; + @Override public void refresh(boolean headerToo) { preventScrollToTopOnRefresh = true; @@ -35,11 +54,189 @@ public class ExtendedLiveGridView extends LiveGridView { public void setCacheStore(ListStore list) { cacheStore = list; } - - - - + /** + * Returns the total number of rows that are visible given the current grid + * height. + * + * @return the visible row count + */ + @Override + public int getVisibleRowCount() { + int rh = getRowHeight(); + int visibleHeight = getLiveScrollerHeight(); + int result = (int) ((visibleHeight < 1) ? 0 : Math + .floor((double) visibleHeight / rh)); + + int calcHeight = rh * result; + + while (calcHeight < visibleHeight) { + result++; + calcHeight = rh * result; + } + + /* + * Log.debug("GetVisibleRowCount(): row height: " + rh + + * " visibleHeight: " + visibleHeight + " visible rows: " + result + + * " calcHeight: " + calcHeight); + */ + + return result; + } + + @Override + protected void handleComponentEvent(Event ge) { + super.handleComponentEvent(ge); + int type = ge.getTypeInt(); + EventTarget t = ge.getEventTarget(); + + if (ignoreScroll || !Element.is(t)) { + return; + } + + Element target = Element.as(t); + + if (type == Event.ONMOUSEWHEEL && dataTable.isOrHasChild(target)) { + int v = ge.getMouseWheelVelocityY() * getRowHeight(); + liveScroller.setScrollTop(liveScroller.getScrollTop() + v); + } else if (type == Event.ONSCROLL && liveScroller.isOrHasChild(target)) { + ge.stopPropagation(); + ge.preventDefault(); + Log.debug("HandleComponentEvent: scrollTop:"+liveScroller.getScrollTop()+", rowHeight:"+getRowHeight()+", newIndex:"+(int) Math.ceil((double) liveScroller.getScrollTop() + / getRowHeight())); + updateRows( + (int) Math.ceil((double) liveScroller.getScrollTop() + / getRowHeight()), false); + } + } + + private void maskView() { + if (!isMasked && grid.isLoadMask()) { + grid.mask(DefaultMessages.getMessages().loadMask_msg()); + isMasked = true; + } + } + + /** + * Updates the rows based on the new index. + * + * @param newIndex + * the new index + * @param reload + * true to reload the data + */ + @Override + protected void updateRows(int newIndex, boolean reload) { + Log.debug("updateRows(): newIndex: " + newIndex + " reload:" + reload); + + // the number of rows visible within the grid's viewport + int rowCount = getVisibleRowCount(); + + newIndex = Math.min(newIndex, Math.max(0, totalCount - rowCount)); + + int diff = newIndex - viewIndex; + + // the difference from the view index and the new index + int delta = Math.abs(diff); + + Log.debug("updateRows(): totalCount:" + totalCount + " newIndex: " + + newIndex + " viewIndex: " + viewIndex + " visibleRows: " + + rowCount + " diff: " + diff); + + // nothing has changed and we are not forcing a reload + if (delta == 0 && !reload) { + Log.debug("updateRows() nothing changed and not forcing reload"); + return; + } + + viewIndex = newIndex; + int liveStoreIndex = Math.max(0, viewIndex - liveStoreOffset); + + // load data if not already cached + if (!isCached(viewIndex)) { + Log.debug("updateRows() not cached, loading data"); + + maskView(); + if (loadLiveStore(getLiveStoreCalculatedIndex(viewIndex))) { + viewIndexReload = viewIndex; + } + return; + } + + // do pre caching + if (shouldCache(viewIndex) && !isLoading) { + loadLiveStore(getLiveStoreCalculatedIndex(viewIndex)); + } + + int rc = getVisibleRowCount(); + if (delta > rc - 1) { + reload = true; + } + + if (reload) { + Log.debug("updateRows() newIndex: " + newIndex + " viewIndex: " + + viewIndex + " visible rows: " + rowCount + " diff: " + + diff); + + delta = diff = rc; + if (ds.size() > 0) { + boolean p = preventScrollToTopOnRefresh; + preventScrollToTopOnRefresh = true; + ds.clear(); + preventScrollToTopOnRefresh = p; + } + } + + if (delta == 0) { + return; + } + + int count = ds.size(); + if (diff > 0) { + // rolling forward + for (int c = 0; c < delta && c < count; c++) { + ds.remove(0); + } + count = ds.size(); + Log.debug("updateRows() forward adding to store from cache, liveStoreIndex: " + + liveStoreIndex + " count: " + count); + + ds.addAll(cacheStore.subList(liveStoreIndex + count, liveStoreIndex + + count + delta)); + } else { + // rolling back + for (int c = 0; c < delta && c < count; c++) { + ds.remove(count - c - 1); + } + + if (GXTLogConfiguration.loggingIsEnabled()) { + Log.debug("updateRows() reverse adding to store from cache, liveStoreIndex: " + + liveStoreIndex + " count: " + count); + } + + ds.addAll(0, + cacheStore.subList(liveStoreIndex, liveStoreIndex + delta)); + } + + if (!measureRowHeight) { + if (viewIndex > lastViewIndex) { + scroller.setScrollTop(rowHeightAdjust); + lastScrollDirection = 1; + } else if (viewIndex < lastViewIndex) { + lastScrollDirection = 0; + scroller.setScrollTop(0); + } else { + scroller.setScrollTop(lastScrollDirection == 0 ? 0 + : rowHeightAdjust); + } + } + + Log.debug("updateRows() fireEvent Update: offset:" + liveStoreOffset + + " viewIndex:" + viewIndex + " totalCount:" + totalCount + + " visibleRowCount:" + getVisibleRowCount()); + fireEvent(new LiveGridViewUpdateEvent(liveStoreOffset, viewIndex, + totalCount, getVisibleRowCount())); + lastViewIndex = viewIndex; + } - } \ No newline at end of file diff --git a/src/main/java/org/gcube/portlets/user/tdwx/client/config/GridAndCellSelectionModel.java b/src/main/java/org/gcube/portlets/user/tdwx/client/config/GridAndCellSelectionModel.java index aca8e28..3c95a14 100644 --- a/src/main/java/org/gcube/portlets/user/tdwx/client/config/GridAndCellSelectionModel.java +++ b/src/main/java/org/gcube/portlets/user/tdwx/client/config/GridAndCellSelectionModel.java @@ -277,4 +277,22 @@ public class GridAndCellSelectionModel extends GridSelectionModel { } + public void onChangeNumberOfRows(){ + Log.debug("Selection Model Called OnChangeNumberOfRows()"); + if(selected!=null){ + Log.debug("Rows Selected: "+selected.size()); + } + + deselectAll(); + fireSelectionChange(); + + if(selected!=null){ + Log.debug("After deselect Rows Selected: "+selected.size()); + } else{ + Log.debug("After deselect No Rows Selected"); + } + + + } + }