Skip to content

Commit

Permalink
SEBSERV-541 column width now sticks to user session
Browse files Browse the repository at this point in the history
  • Loading branch information
anhefti committed Jul 18, 2024
1 parent 6c4902d commit 3166152
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public void putAttribute(final String name, final String value) {
this.attributes.put(name, value);
}

public void deleteAttribute(final String name) {
this.attributes.remove(name);
}

public String getAttribute(final String name) {
return this.attributes.get(name);
}
Expand Down
48 changes: 41 additions & 7 deletions src/main/java/ch/ethz/seb/sebserver/gui/table/EntityTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class EntityTable<ROW extends ModelIdAware> {
private final String sortAttrName;
private final String sortOrderAttrName;
private final String currentPageAttrName;
private final String columnWidthAttrName;
private final boolean markupEnabled;
private final Consumer<EntityTable<ROW>> pageReloadListener;

Expand Down Expand Up @@ -138,6 +139,7 @@ public class EntityTable<ROW extends ModelIdAware> {
this.sortAttrName = name + "_sort";
this.sortOrderAttrName = name + "_sortOrder";
this.currentPageAttrName = name + "_currentPage";
this.columnWidthAttrName = name + "_columnWidth";
this.markupEnabled = markupEnabled;

this.defaultSortColumn = defaultSortColumn;
Expand Down Expand Up @@ -236,6 +238,7 @@ public class EntityTable<ROW extends ModelIdAware> {
this.navigator = new TableNavigator(this);

createTableColumns();

this.pageNumber = initCurrentPageFromUserAttr();
initFilterFromUserAttrs();
initSortFromUserAttr();
Expand Down Expand Up @@ -317,6 +320,8 @@ public void selectPage(final int pageSelection) {
}

public void reset() {
deleteColumnWidths();
adaptColumnWidth(null);
this.sortColumn = this.defaultSortColumn;
this.sortOrder = this.defaultSortOrder;
updateSortUserAttr();
Expand Down Expand Up @@ -595,7 +600,7 @@ private void adaptColumnWidth(final Event event) {
try {
int currentTableWidth = this.table.getParent().getClientArea().width;
// If we have all columns with filter we need some more space for the
// filter actions in the right hand side. This tweak gives enough space for that
// filter actions on the right hand side. This tweak gives enough space for that
if (this.filter != null && this.columns.size() == this.filter.size()) {
currentTableWidth -= 60;
}
Expand All @@ -620,7 +625,7 @@ private void adaptColumnWidth(final Event event) {
final int newWidth = (pSize > 0)
? columnUnitSize * column.getWidthProportion()
: columnUnitSize;
tableColumn.setWidth(newWidth);
loadOrSetColumnWidth(index, newWidth);
if (this.filter != null) {
this.filter.adaptColumnWidth(this.table.indexOf(tableColumn), newWidth);
}
Expand All @@ -639,11 +644,7 @@ private void adaptColumnWidthChange(final Event event) {
final Widget widget = event.widget;
if (widget instanceof TableColumn) {
final TableColumn tableColumn = ((TableColumn) widget);
if (this.filter != null) {
this.filter.adaptColumnWidth(
this.table.indexOf(tableColumn),
tableColumn.getWidth());
}
this.saveColumnSize(table.indexOf(tableColumn));
}
}

Expand Down Expand Up @@ -906,4 +907,37 @@ public void refreshPageSize() {
}
}

private void saveColumnSize(final int columnIndex) {
final TableColumn column = table.getColumn(columnIndex);
if (column.getWidth() > 0) {
this.pageService
.getCurrentUser()
.putAttribute(
columnWidthAttrName + columnIndex,
String.valueOf(table.getColumn(columnIndex).getWidth()));
}
}

private void loadOrSetColumnWidth(final int columnIndex, final int defaultWidth) {
int width = defaultWidth;
try {
width = Integer.parseInt(this.pageService
.getCurrentUser()
.getAttribute(columnWidthAttrName + columnIndex));
} catch (final Exception e) {
// ignore
}

table.getColumn(columnIndex).setWidth(width);
saveColumnSize(columnIndex);
}

private void deleteColumnWidths() {
for (int i = 0; i < this.columns.size(); i++) {
this.pageService
.getCurrentUser()
.deleteAttribute(columnWidthAttrName + i);
}
}

}

0 comments on commit 3166152

Please sign in to comment.