From 5ed8a499fbaa845b8f42fbb2a82f14a2722c1b21 Mon Sep 17 00:00:00 2001 From: Roman Pinyazhin Date: Fri, 5 Oct 2018 13:57:58 +0400 Subject: [PATCH] Save UI component settings only if users change them #753 --- .../cuba/gui/components/Collapsable.java | 11 ++- .../gui/components/filter/FilterDelegate.java | 12 ++- .../components/filter/FilterDelegateImpl.java | 57 ++++++++----- .../cuba/web/widgets/CubaGroupBox.java | 10 ++- .../gui/components/WebAbstractDataGrid.java | 45 +++++++---- .../web/gui/components/WebAbstractTable.java | 80 ++++++++++++------- .../cuba/web/gui/components/WebFilter.java | 6 +- .../cuba/web/gui/components/WebGroupBox.java | 22 ++--- .../web/gui/components/WebGroupTable.java | 28 +++++-- .../gui/components/WebResizableTextArea.java | 27 +------ .../web/gui/components/WebSplitPanel.java | 34 ++------ 11 files changed, 188 insertions(+), 144 deletions(-) diff --git a/modules/gui/src/com/haulmont/cuba/gui/components/Collapsable.java b/modules/gui/src/com/haulmont/cuba/gui/components/Collapsable.java index c58efc8b4d..dca96e3a20 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/components/Collapsable.java +++ b/modules/gui/src/com/haulmont/cuba/gui/components/Collapsable.java @@ -39,13 +39,15 @@ public interface Collapsable extends Component { @Deprecated void removeExpandedStateChangeListener(Consumer listener); - class ExpandedStateChangeEvent { + class ExpandedStateChangeEvent implements HasUserOriginated { private final Collapsable component; private final boolean expanded; + private final boolean userOriginated; - public ExpandedStateChangeEvent(Collapsable component, boolean expanded) { + public ExpandedStateChangeEvent(Collapsable component, boolean expanded, boolean userOriginated) { this.component = component; this.expanded = expanded; + this.userOriginated = userOriginated; } public Collapsable getComponent() { @@ -58,5 +60,10 @@ public interface Collapsable extends Component { public boolean isExpanded() { return expanded; } + + @Override + public boolean isUserOriginated() { + return userOriginated; + } } } \ No newline at end of file diff --git a/modules/gui/src/com/haulmont/cuba/gui/components/filter/FilterDelegate.java b/modules/gui/src/com/haulmont/cuba/gui/components/filter/FilterDelegate.java index d40bdc522f..5dc5c3959b 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/components/filter/FilterDelegate.java +++ b/modules/gui/src/com/haulmont/cuba/gui/components/filter/FilterDelegate.java @@ -21,6 +21,7 @@ import com.haulmont.chile.core.model.MetaClass; import com.haulmont.cuba.gui.components.Component; import com.haulmont.cuba.gui.components.ComponentContainer; import com.haulmont.cuba.gui.components.Filter; +import com.haulmont.cuba.gui.components.HasUserOriginated; import com.haulmont.cuba.gui.data.CollectionDatasource; import com.haulmont.cuba.gui.model.CollectionLoader; import com.haulmont.cuba.security.entity.FilterEntity; @@ -145,13 +146,15 @@ public interface FilterDelegate { void setCaptionChangedListener(Consumer captionChangedListener); - class FDExpandedStateChangeEvent { + class FDExpandedStateChangeEvent implements HasUserOriginated { private final FilterDelegate delegate; private final boolean expanded; + private final boolean userOriginated; - public FDExpandedStateChangeEvent(FilterDelegate delegate, boolean expanded) { + public FDExpandedStateChangeEvent(FilterDelegate delegate, boolean expanded, boolean userOriginated) { this.delegate = delegate; this.expanded = expanded; + this.userOriginated = userOriginated; } public FilterDelegate getComponent() { @@ -164,6 +167,11 @@ public interface FilterDelegate { public boolean isExpanded() { return expanded; } + + @Override + public boolean isUserOriginated() { + return userOriginated; + } } void setExpandedStateChangeListener(Consumer listener); diff --git a/modules/gui/src/com/haulmont/cuba/gui/components/filter/FilterDelegateImpl.java b/modules/gui/src/com/haulmont/cuba/gui/components/filter/FilterDelegateImpl.java index b6b70069ab..4392677594 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/components/filter/FilterDelegateImpl.java +++ b/modules/gui/src/com/haulmont/cuba/gui/components/filter/FilterDelegateImpl.java @@ -171,6 +171,8 @@ public class FilterDelegateImpl implements FilterDelegate { protected int maxResults = -1; protected boolean useMaxResults; protected boolean textMaxResults; + protected boolean maxResultValueChanged = false; + protected boolean groupBoxExpandedChanged = false; protected Boolean manualApplyRequired; protected boolean folderActionsEnabled = true; protected boolean filtersLookupListenerEnabled = true; @@ -233,7 +235,7 @@ public class FilterDelegateImpl implements FilterDelegate { protected void createLayout() { if (layout == null) { groupBoxLayout = componentsFactory.createComponent(GroupBoxLayout.class); - groupBoxLayout.addExpandedStateChangeListener(e -> fireExpandStateChange()); + groupBoxLayout.addExpandedStateChangeListener(e -> fireExpandStateChange(e.isUserOriginated())); groupBoxLayout.setOrientation(GroupBoxLayout.Orientation.VERTICAL); groupBoxLayout.setWidth("100%"); @@ -470,6 +472,7 @@ public class FilterDelegateImpl implements FilterDelegate { maxResultsLookupField.setStyleName("c-maxresults-select"); maxResultsField = textMaxResults ? maxResultsTextField : maxResultsLookupField; + maxResultsField.addValueChangeListener(integerValueChangeEvent -> maxResultValueChanged = true); maxResultsLayout.add(maxResultsField); } @@ -1375,6 +1378,9 @@ public class FilterDelegateImpl implements FilterDelegate { setUseMaxResults(false); } else if (useMaxResults) { initMaxResults(); + + // set to false because it's initial value + maxResultValueChanged = false; } if (ftsSwitch != null && !isEntityAvailableForFts()) { @@ -1401,6 +1407,9 @@ public class FilterDelegateImpl implements FilterDelegate { if (useMaxResults) { initMaxResults(); + + // set to false because it's initial value + maxResultValueChanged = false; } } @@ -1781,6 +1790,9 @@ public class FilterDelegateImpl implements FilterDelegate { Integer maxResultsFromSettings = Integer.valueOf(maxResultsEl.getText()); adapter.setMaxResults(maxResultsFromSettings); initMaxResults(); + + // set to false cause it's initial value from settings + maxResultValueChanged = false; } catch (NumberFormatException ex) { log.error("Error on parsing maxResults setting value", ex); } @@ -1831,31 +1843,32 @@ public class FilterDelegateImpl implements FilterDelegate { changed = true; } - Element groupBoxExpandedEl = element.element("groupBoxExpanded"); - if (groupBoxExpandedEl == null) - groupBoxExpandedEl = element.addElement("groupBoxExpanded"); + if (groupBoxExpandedChanged) { + Element groupBoxExpandedEl = element.element("groupBoxExpanded"); + if (groupBoxExpandedEl == null) + groupBoxExpandedEl = element.addElement("groupBoxExpanded"); - Boolean oldGroupBoxExpandedValue = Boolean.valueOf(groupBoxExpandedEl.getText()); - Boolean newGroupBoxExpandedValue = groupBoxLayout.isExpanded(); - if (!Objects.equals(oldGroupBoxExpandedValue, newGroupBoxExpandedValue)) { - groupBoxExpandedEl.setText(newGroupBoxExpandedValue.toString()); - changed = true; + Boolean oldGroupBoxExpandedValue = + groupBoxExpandedEl.getText().isEmpty() ? true : Boolean.valueOf(groupBoxExpandedEl.getText()); + Boolean newGroupBoxExpandedValue = groupBoxLayout.isExpanded(); + if (!Objects.equals(oldGroupBoxExpandedValue, newGroupBoxExpandedValue)) { + groupBoxExpandedEl.setText(newGroupBoxExpandedValue.toString()); + changed = true; + } } if (isMaxResultsLayoutVisible()) { - Element maxResultsEl = element.element("maxResults"); - if (maxResultsEl == null) - maxResultsEl = element.addElement("maxResults"); - try { - Integer oldMaxResultsValue = !Strings.isNullOrEmpty(maxResultsEl.getText()) ? - Integer.valueOf(maxResultsEl.getText()) : null; + if (maxResultValueChanged) { + Element maxResultsEl = element.element("maxResults"); + if (maxResultsEl == null) { + maxResultsEl = element.addElement("maxResults"); + } + Integer newMaxResultsValue = maxResultsField.getValue(); - if (newMaxResultsValue != null && !Objects.equals(oldMaxResultsValue, newMaxResultsValue)) { + if (newMaxResultsValue != null) { maxResultsEl.setText(newMaxResultsValue.toString()); changed = true; } - } catch (NumberFormatException ex) { - log.error("Error on parsing maxResults setting value", ex); } } @@ -2080,10 +2093,14 @@ public class FilterDelegateImpl implements FilterDelegate { expandedStateChangeListener = listener; } - protected void fireExpandStateChange() { + protected void fireExpandStateChange(boolean userOriginated) { if (expandedStateChangeListener != null) { - FDExpandedStateChangeEvent event = new FDExpandedStateChangeEvent(this, isExpanded()); + FDExpandedStateChangeEvent event = new FDExpandedStateChangeEvent(this, isExpanded(), userOriginated); expandedStateChangeListener.accept(event); + + if (userOriginated) { + groupBoxExpandedChanged = true; + } } } diff --git a/modules/web-widgets/src/com/haulmont/cuba/web/widgets/CubaGroupBox.java b/modules/web-widgets/src/com/haulmont/cuba/web/widgets/CubaGroupBox.java index 8539ead74b..d19717bdce 100644 --- a/modules/web-widgets/src/com/haulmont/cuba/web/widgets/CubaGroupBox.java +++ b/modules/web-widgets/src/com/haulmont/cuba/web/widgets/CubaGroupBox.java @@ -33,7 +33,7 @@ public class CubaGroupBox extends Panel implements ComponentContainer { public CubaGroupBox() { registerRpc((CubaGroupBoxServerRpc) expanded -> { if (getState().collapsable) { - setExpanded(expanded); + setExpanded(expanded, true); } }); @@ -103,6 +103,10 @@ public class CubaGroupBox extends Panel implements ComponentContainer { } public void setExpanded(boolean expanded) { + setExpanded(expanded, false); + } + + public void setExpanded(boolean expanded, boolean invokedByUser) { if (expanded != getState(false).expanded) { getContent().setVisible(expanded); markAsDirtyRecursive(); @@ -110,7 +114,7 @@ public class CubaGroupBox extends Panel implements ComponentContainer { getState().expanded = expanded; if (expandChangeHandler != null) - expandChangeHandler.expandStateChanged(expanded); + expandChangeHandler.expandStateChanged(expanded, invokedByUser); } public boolean isCollapsable() { @@ -182,7 +186,7 @@ public class CubaGroupBox extends Panel implements ComponentContainer { } public interface ExpandChangeHandler { - void expandStateChanged(boolean expanded); + void expandStateChanged(boolean expanded, boolean invokedByUser); } public void setShowAsPanel(boolean showAsPanel) { diff --git a/modules/web/src/com/haulmont/cuba/web/gui/components/WebAbstractDataGrid.java b/modules/web/src/com/haulmont/cuba/web/gui/components/WebAbstractDataGrid.java index b7e30598ba..54e8518e65 100644 --- a/modules/web/src/com/haulmont/cuba/web/gui/components/WebAbstractDataGrid.java +++ b/modules/web/src/com/haulmont/cuba/web/gui/components/WebAbstractDataGrid.java @@ -82,6 +82,8 @@ import com.vaadin.ui.components.grid.Header; import com.vaadin.ui.components.grid.StaticSection; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.dom4j.Document; +import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -155,6 +157,8 @@ public abstract class WebAbstractDataGrid & CubaEnhancedGrid & CubaEnhancedGrid> modelColumns = getVisibleColumns(); @@ -2045,8 +2056,6 @@ public abstract class WebAbstractDataGrid & CubaEnhancedGrid & CubaEnhancedGrid & CubaEnhancedGrid> sortOrders = component.getSortOrder(); if (!sortOrders.isEmpty()) { GridSortOrder sortOrder = sortOrders.get(0); - if (columnsElem != null) { - columnsElem.addAttribute("sortColumnId", sortOrder.getSorted().getId()); - columnsElem.addAttribute("sortDirection", sortOrder.getDirection().toString()); - } + columnsElem.addAttribute("sortColumnId", sortOrder.getSorted().getId()); + columnsElem.addAttribute("sortDirection", sortOrder.getDirection().toString()); } - - settingsChanged = true; } return settingsChanged; @@ -2100,7 +2104,7 @@ public abstract class WebAbstractDataGrid & CubaEnhancedGrid settingsColumnList = columnsElem.elements("columns"); @@ -2136,6 +2140,19 @@ public abstract class WebAbstractDataGrid & CubaEnhancedGrid> sortOrders = component.getSortOrder(); diff --git a/modules/web/src/com/haulmont/cuba/web/gui/components/WebAbstractTable.java b/modules/web/src/com/haulmont/cuba/web/gui/components/WebAbstractTable.java index 5fe5202651..a334f27f0a 100644 --- a/modules/web/src/com/haulmont/cuba/web/gui/components/WebAbstractTable.java +++ b/modules/web/src/com/haulmont/cuba/web/gui/components/WebAbstractTable.java @@ -1829,9 +1829,9 @@ public abstract class WebAbstractTable -1) - colElem.addAttribute("width", String.valueOf(width)); + MetaPropertyPath sortProperty = (MetaPropertyPath) component.getSortContainerPropertyId(); + boolean sortAscending = component.isSortAscending(); - boolean visible = !component.isColumnCollapsed(column); - colElem.addAttribute("visible", Boolean.toString(visible)); - } + columnsElem.addAttribute("sortProperty", sortProperty == null ? "" : sortProperty.toString()); + columnsElem.addAttribute("sortAscending", Boolean.toString(sortAscending)); settingsChanged = true; } - if (isSettingsSortPropertyChanged(settingsSortProperty, settingsSortAscending) || commonSettingsChanged) { - MetaPropertyPath sortProperty = (MetaPropertyPath) component.getSortContainerPropertyId(); - if (sortProperty != null) { - boolean sortAscending = component.isSortAscending(); - - if (columnsElem != null) { - columnsElem.addAttribute("sortProperty", sortProperty.toString()); - columnsElem.addAttribute("sortAscending", Boolean.toString(sortAscending)); - - settingsChanged = true; - } - } + String settingsDefaultPresentation = Strings.nullToEmpty(element.attributeValue("presentation")); + String defaultPresentation = getDefaultPresentationId() == null ? "" : String.valueOf(getDefaultPresentationId()); + if (!Objects.equals(settingsDefaultPresentation, defaultPresentation)) { + settingsChanged = true; } return settingsChanged; } + /** + * Saves common table column settings (width, visible, id). + * + * @param columnsElem setting element for the columns + */ + protected void saveCommonTableColumnSettings(Element columnsElem) { + Object[] visibleColumns = component.getVisibleColumns(); + for (Object column : visibleColumns) { + Element colElem = columnsElem.addElement("columns"); + colElem.addAttribute("id", column.toString()); + + int width = component.getColumnWidth(column); + if (width > -1) + colElem.addAttribute("width", String.valueOf(width)); + + boolean visible = !component.isColumnCollapsed(column); + colElem.addAttribute("visible", Boolean.toString(visible)); + } + } + protected boolean isCommonTableSettingsChanged(Element columnsElem) { if (columnsElem == null) { - return true; + return isDefaultTableSettingsChanged(); } List settingsColumnList = columnsElem.elements("columns"); @@ -1926,11 +1934,23 @@ public abstract class WebAbstractTable imp component.setWidth(100, Sizeable.Unit.PERCENTAGE); component.addStyleName(FILTER_STYLENAME); - delegate.setExpandedStateChangeListener(e -> fireExpandStateChange(e.isExpanded())); + delegate.setExpandedStateChangeListener(e -> fireExpandStateChange(e.isExpanded(), e.isUserOriginated())); delegate.setCaptionChangedListener(this::updateCaptions); } @@ -269,8 +269,8 @@ public class WebFilter extends WebAbstractComponent imp unsubscribe(ExpandedStateChangeEvent.class, listener); } - protected void fireExpandStateChange(boolean expanded) { - ExpandedStateChangeEvent event = new ExpandedStateChangeEvent(this, expanded); + protected void fireExpandStateChange(boolean expanded, boolean userOriginated) { + ExpandedStateChangeEvent event = new ExpandedStateChangeEvent(this, expanded, userOriginated); publish(ExpandedStateChangeEvent.class, event); } diff --git a/modules/web/src/com/haulmont/cuba/web/gui/components/WebGroupBox.java b/modules/web/src/com/haulmont/cuba/web/gui/components/WebGroupBox.java index eaeb3e4952..7250ed68dd 100644 --- a/modules/web/src/com/haulmont/cuba/web/gui/components/WebGroupBox.java +++ b/modules/web/src/com/haulmont/cuba/web/gui/components/WebGroupBox.java @@ -45,6 +45,7 @@ public class WebGroupBox extends WebAbstractComponent implements G protected Orientation orientation = Orientation.VERTICAL; protected boolean settingsEnabled = true; + protected boolean settingsChanged = false; protected Map shortcuts; @@ -258,9 +259,13 @@ public class WebGroupBox extends WebAbstractComponent implements G unsubscribe(ExpandedStateChangeEvent.class, listener); } - protected void fireExpandStateChange(boolean expanded) { - ExpandedStateChangeEvent event = new ExpandedStateChangeEvent(this, expanded); + protected void fireExpandStateChange(boolean expanded, boolean invokedByUser) { + ExpandedStateChangeEvent event = new ExpandedStateChangeEvent(this, expanded, invokedByUser); publish(ExpandedStateChangeEvent.class, event); + + if (invokedByUser) { + settingsChanged = true; + } } @Override @@ -282,7 +287,7 @@ public class WebGroupBox extends WebAbstractComponent implements G return false; } - if (!isSettingsChanged(element)) { + if (!settingsChanged) { return false; } @@ -400,15 +405,4 @@ public class WebGroupBox extends WebAbstractComponent implements G return new com.haulmont.cuba.gui.components.MarginInfo(vMargin.hasTop(), vMargin.hasRight(), vMargin.hasBottom(), vMargin.hasLeft()); } - - protected boolean isSettingsChanged(Element element) { - Element groupBoxElement = element.element("groupBox"); - - if (groupBoxElement == null) { - return true; - } - - String expanded = groupBoxElement.attributeValue("expanded"); // old value - return isExpanded() != Boolean.parseBoolean(expanded); - } } \ No newline at end of file diff --git a/modules/web/src/com/haulmont/cuba/web/gui/components/WebGroupTable.java b/modules/web/src/com/haulmont/cuba/web/gui/components/WebGroupTable.java index f326accba4..0ac9549a9c 100644 --- a/modules/web/src/com/haulmont/cuba/web/gui/components/WebGroupTable.java +++ b/modules/web/src/com/haulmont/cuba/web/gui/components/WebGroupTable.java @@ -113,13 +113,20 @@ public class WebGroupTable extends WebAbstractTable extends WebAbstractTable settingsProperties = groupPropertiesElement.elements("property"); @@ -173,6 +178,19 @@ public class WebGroupTable extends WebAbstractTable extends WebAbstractTextArea extends WebAbstractTextArea { ResizeEvent e = new ResizeEvent(this, oldWidth, width, oldHeight, height); publish(ResizeEvent.class, e); + + settingsChanged = true; }); } @@ -146,7 +149,7 @@ public class WebResizableTextArea extends WebAbstractTextArea extends WebAbstractTextArea impl protected int orientation; protected boolean settingsEnabled = true; + protected boolean settingsChanged = false; protected float currentPosition = 0; protected boolean inverse = false; @@ -109,6 +110,10 @@ public class WebSplitPanel extends WebAbstractComponent impl SplitPositionChangeEvent cubaEvent = new SplitPositionChangeEvent(this, currentPosition, event.getSplitPosition(), event.isUserOriginated()); publish(SplitPositionChangeEvent.class, cubaEvent); + + if (event.isUserOriginated()) { + settingsChanged = true; + } } @Override @@ -200,7 +205,7 @@ public class WebSplitPanel extends WebAbstractComponent impl return false; } - if (!isSplitPanelSettingsChanged(element)) { + if (!settingsChanged) { return false; } @@ -366,31 +371,4 @@ public class WebSplitPanel extends WebAbstractComponent impl public void removeSplitPositionChangeListener(Consumer listener) { unsubscribe(SplitPositionChangeEvent.class, listener); } - - protected boolean isSplitPanelSettingsChanged(Element splitPanelElement) { - if (splitPanelElement == null) { - return true; - } - - Element positionElement = splitPanelElement.element("position"); - if (positionElement == null) { - return true; - } - - String settingsUnit = positionElement.attributeValue("unit"); - String unit = String.valueOf(component.getSplitPositionUnit()); - - if (!unit.equals(settingsUnit)) { - return true; - } - - String settingsValue = positionElement.attributeValue("value"); - String value = String.valueOf(component.getSplitPosition()); - - if (!value.equals(settingsValue)) { - return true; - } - - return false; - } } \ No newline at end of file