PL-7250 DesktopComponents should use WeakListenerWrappers for ds listeners

This commit is contained in:
Daniil Tsarev 2016-06-22 14:55:03 +04:00
parent d5156c9401
commit 9a8708eb0d
16 changed files with 198 additions and 93 deletions

View File

@ -36,6 +36,8 @@ import com.haulmont.cuba.gui.components.CaptionMode;
import com.haulmont.cuba.gui.components.OptionsField;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.WeakItemChangeListener;
import com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@ -70,6 +72,10 @@ public abstract class DesktopAbstractOptionsField<C extends JComponent>
protected CaptionFormatter captionFormatter;
protected Datasource.ItemChangeListener itemChangeListener;
protected Datasource.ItemChangeListener securityItemChangeListener;
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
public interface CaptionFormatter<T> {
String formatValue(T value);
}
@ -160,22 +166,26 @@ public abstract class DesktopAbstractOptionsField<C extends JComponent>
MetaClass metaClass = datasource.getMetaClass();
resolveMetaPropertyPath(metaClass, property);
datasource.addItemChangeListener(e -> {
itemChangeListener = e -> {
if (updatingInstance)
return;
Object value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
updateComponent(value);
fireChangeListeners(value);
});
};
// noinspection unchecked
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
datasource.addItemPropertyChangeListener(e -> {
itemPropertyChangeListener = e -> {
if (updatingInstance)
return;
if (e.getProperty().equals(metaPropertyPath.toString())) {
updateComponent(e.getValue());
fireChangeListeners(e.getValue());
}
});
};
// noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
setRequired(metaProperty.isMandatory());
if (StringUtils.isEmpty(getRequiredMessage())) {
@ -209,7 +219,9 @@ public abstract class DesktopAbstractOptionsField<C extends JComponent>
}
handleFilteredAttributes(this, this.datasource, metaPropertyPath);
this.datasource.addItemChangeListener(e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath));
securityItemChangeListener = e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath);
// noinspection unchecked
this.datasource.addItemChangeListener(new WeakItemChangeListener(this.datasource, securityItemChangeListener));
}
protected void fireChangeListeners(Object newValue) {

View File

@ -40,6 +40,8 @@ import com.haulmont.cuba.gui.components.Window;
import com.haulmont.cuba.gui.data.*;
import com.haulmont.cuba.gui.data.impl.CollectionDsActionsNotifier;
import com.haulmont.cuba.gui.data.impl.DatasourceImplementation;
import com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener;
import com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener;
import com.haulmont.cuba.gui.presentations.Presentations;
import net.miginfocom.layout.CC;
import net.miginfocom.swing.MigLayout;
@ -143,6 +145,10 @@ public abstract class DesktopAbstractTable<C extends JXTable, E extends Entity>
protected boolean multiLineCells;
protected boolean settingsEnabled = true;
protected CollectionDatasource.CollectionChangeListener collectionChangeListener;
protected CollectionDatasource.CollectionChangeListener securityCollectionChangeListener;
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
protected CollectionDsActionsNotifier collectionDsActionsNotifier;
protected DesktopAbstractTable() {
@ -566,8 +572,7 @@ public abstract class DesktopAbstractTable<C extends JXTable, E extends Entity>
this.datasource = datasource;
//noinspection unchecked
datasource.addCollectionChangeListener(e -> {
collectionChangeListener = e -> {
switch (e.getOperation()) {
case CLEAR:
case REFRESH:
@ -581,7 +586,9 @@ public abstract class DesktopAbstractTable<C extends JXTable, E extends Entity>
}
break;
}
});
};
//noinspection unchecked
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
initTableModel(datasource);
@ -662,7 +669,7 @@ public abstract class DesktopAbstractTable<C extends JXTable, E extends Entity>
action.setDatasource(datasource);
}
datasource.addCollectionChangeListener(e -> {
securityCollectionChangeListener = e -> {
onDataChange();
packRows();
@ -690,9 +697,11 @@ public abstract class DesktopAbstractTable<C extends JXTable, E extends Entity>
} else {
setSelected(newSelection);
}
});
};
// noinspection unchecked
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, securityCollectionChangeListener));
datasource.addItemPropertyChangeListener(e -> {
itemPropertyChangeListener = e -> {
List<Column> columns1 = getColumns();
boolean find = false;
int i = 0;
@ -710,7 +719,9 @@ public abstract class DesktopAbstractTable<C extends JXTable, E extends Entity>
onDataChange();
}
packRows();
});
};
// noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
if (rowsCount != null) {
rowsCount.setDatasource(datasource);

View File

@ -33,6 +33,8 @@ import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.components.Frame;
import com.haulmont.cuba.gui.components.TextInputField;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.WeakItemChangeListener;
import com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@ -67,6 +69,9 @@ public abstract class DesktopAbstractTextField<T extends JTextComponent> extends
protected Locale locale = AppBeans.<UserSessionSource>get(UserSessionSource.NAME).getLocale();
protected DefaultValueFormatter valueFormatter;
protected Datasource.ItemChangeListener itemChangeListener;
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
protected DesktopAbstractTextField() {
doc = createDocument();
@ -210,8 +215,7 @@ public abstract class DesktopAbstractTextField<T extends JTextComponent> extends
resolveMetaPropertyPath(datasource.getMetaClass(), property);
valueFormatter.setMetaProperty(metaProperty);
//noinspection unchecked
datasource.addItemChangeListener(e -> {
itemChangeListener = e -> {
if (updatingInstance) {
return;
}
@ -219,10 +223,11 @@ public abstract class DesktopAbstractTextField<T extends JTextComponent> extends
Object value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
updateComponent(value);
fireChangeListeners(value);
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(e -> {
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
itemPropertyChangeListener = e -> {
if (updatingInstance) {
return;
}
@ -231,7 +236,9 @@ public abstract class DesktopAbstractTextField<T extends JTextComponent> extends
updateComponent(e.getValue());
fireChangeListeners(e.getValue());
}
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
setRequired(metaProperty.isMandatory());
if (StringUtils.isEmpty(getRequiredMessage())) {

View File

@ -24,6 +24,8 @@ import com.haulmont.cuba.desktop.gui.executors.impl.DesktopBackgroundWorker;
import com.haulmont.cuba.desktop.sys.DesktopToolTipManager;
import com.haulmont.cuba.gui.components.CheckBox;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.WeakItemChangeListener;
import com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.ObjectUtils;
@ -36,6 +38,10 @@ public class DesktopCheckBox extends DesktopAbstractField<JCheckBox> implements
protected boolean editable = true;
protected Datasource.ItemChangeListener itemChangeListener;
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
public DesktopCheckBox() {
impl = new JCheckBox();
impl.addActionListener(e -> {
@ -95,7 +101,7 @@ public class DesktopCheckBox extends DesktopAbstractField<JCheckBox> implements
}
resolveMetaPropertyPath(datasource.getMetaClass(), property);
datasource.addItemChangeListener(e -> {
itemChangeListener = e -> {
if (updatingInstance)
return;
Boolean value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
@ -105,9 +111,10 @@ public class DesktopCheckBox extends DesktopAbstractField<JCheckBox> implements
updateComponent(value);
fireChangeListeners(value);
});
};
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
datasource.addItemPropertyChangeListener(e -> {
itemPropertyChangeListener = e -> {
if (updatingInstance) {
return;
}
@ -121,7 +128,8 @@ public class DesktopCheckBox extends DesktopAbstractField<JCheckBox> implements
updateComponent(value);
fireChangeListeners(value);
}
});
};
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
if (datasource.getItemIfValid() != null) {
Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());

View File

@ -41,6 +41,8 @@ import com.haulmont.cuba.gui.components.DateField;
import com.haulmont.cuba.gui.components.RequiredValueMissingException;
import com.haulmont.cuba.gui.components.ValidationException;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.WeakItemChangeListener;
import com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener;
import com.haulmont.cuba.security.global.UserSession;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@ -73,6 +75,9 @@ public class DesktopDateField extends DesktopAbstractField<JPanel> implements Da
protected UserSession userSession;
protected TimeZones timeZones = AppBeans.get(TimeZones.NAME);
protected Datasource.ItemChangeListener itemChangeListener;
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
public DesktopDateField() {
impl = new FocusableComposition();
@ -288,18 +293,18 @@ public class DesktopDateField extends DesktopAbstractField<JPanel> implements Da
}
}
//noinspection unchecked
datasource.addItemChangeListener(e -> {
itemChangeListener = e -> {
if (updatingInstance) {
return;
}
Date value = getEntityValue(e.getItem());
updateComponent(toUserDate(value));
fireChangeListeners(value);
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(e -> {
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
itemPropertyChangeListener = e -> {
if (updatingInstance) {
return;
}
@ -307,7 +312,9 @@ public class DesktopDateField extends DesktopAbstractField<JPanel> implements Da
updateComponent(toUserDate((Date) e.getValue()));
fireChangeListeners(e.getValue());
}
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
if (datasource.getState() == Datasource.State.VALID && datasource.getItem() != null) {
if (property.equals(metaPropertyPath.toString())) {

View File

@ -34,6 +34,8 @@ import com.haulmont.cuba.gui.components.Label;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.ValueListener;
import com.haulmont.cuba.gui.components.compatibility.ComponentValueListenerWrapper;
import com.haulmont.cuba.gui.data.impl.WeakItemChangeListener;
import com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringEscapeUtils;
@ -60,6 +62,9 @@ public class DesktopLabel extends DesktopAbstractComponent<JLabel> implements La
protected String labelText = "";
protected Datasource.ItemChangeListener itemChangeListener;
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
public DesktopLabel() {
impl = new JLabel();
impl.setFocusable(false);
@ -98,8 +103,7 @@ public class DesktopLabel extends DesktopAbstractComponent<JLabel> implements La
valueFormatter.setMetaProperty(metaProperty);
//noinspection unchecked
datasource.addItemChangeListener(e -> {
itemChangeListener = e -> {
if (updatingInstance) {
return;
}
@ -107,10 +111,11 @@ public class DesktopLabel extends DesktopAbstractComponent<JLabel> implements La
Object value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
updateComponent(value);
fireChangeListeners(value);
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(e -> {
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
itemPropertyChangeListener = e -> {
if (updatingInstance) {
return;
}
@ -119,7 +124,9 @@ public class DesktopLabel extends DesktopAbstractComponent<JLabel> implements La
updateComponent(e.getValue());
fireChangeListeners(e.getValue());
}
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
if ((datasource.getState() == Datasource.State.VALID) && (datasource.getItem() != null)) {
Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());

View File

@ -35,7 +35,9 @@ import com.haulmont.cuba.desktop.sys.vcl.ExtendedComboBox;
import com.haulmont.cuba.desktop.sys.vcl.UserSelectionHandler;
import com.haulmont.cuba.gui.components.CaptionMode;
import com.haulmont.cuba.gui.components.LookupField;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@ -83,6 +85,8 @@ public class DesktopLookupField extends DesktopAbstractOptionsField<JComponent>
protected List<UserSelectionListener> userSelectionListeners = null; // lazy initialized list
protected CollectionDatasource.CollectionChangeListener collectionChangeListener;
public DesktopLookupField() {
composition = new JPanel();
composition.setLayout(new BorderLayout());
@ -317,12 +321,13 @@ public class DesktopLookupField extends DesktopAbstractOptionsField<JComponent>
items.add(new EntityWrapper(optionsDatasource.getItem(id)));
}
optionsDatasource.addCollectionChangeListener(e -> {
collectionChangeListener = e -> {
items.clear();
for (Entity item : optionsDatasource.getItems()) {
items.add(new EntityWrapper(item));
}
});
};
optionsDatasource.addCollectionChangeListener(new WeakCollectionChangeListener(optionsDatasource, collectionChangeListener));
} else if (optionsMap != null) {
for (String key : optionsMap.keySet()) {
items.add(new MapKeyWrapper(key));

View File

@ -23,6 +23,7 @@ import com.haulmont.cuba.gui.components.CaptionMode;
import com.haulmont.cuba.gui.components.OptionsGroup;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener;
import net.miginfocom.swing.MigLayout;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ObjectUtils;
@ -47,6 +48,8 @@ public class DesktopOptionsGroup extends DesktopAbstractOptionsField<JPanel> imp
private boolean enabled = true;
protected CollectionDatasource.CollectionChangeListener collectionChangeListener;
public DesktopOptionsGroup() {
layout = new MigLayout();
impl = new JPanel(layout);
@ -85,7 +88,7 @@ public class DesktopOptionsGroup extends DesktopAbstractOptionsField<JPanel> imp
addItem(new EntityWrapper(item));
}
optionsDatasource.addCollectionChangeListener(e -> {
collectionChangeListener = e -> {
Object value = getValue();
removeAllItems();
@ -98,7 +101,8 @@ public class DesktopOptionsGroup extends DesktopAbstractOptionsField<JPanel> imp
impl.revalidate();
impl.repaint();
});
};
optionsDatasource.addCollectionChangeListener(new WeakCollectionChangeListener(optionsDatasource, collectionChangeListener));
if ((datasource!= null) && (datasource.getState() == Datasource.State.VALID)) {
Entity newValue = datasource.getItem();

View File

@ -32,6 +32,8 @@ import com.haulmont.cuba.desktop.sys.vcl.Picker;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.WeakItemChangeListener;
import com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@ -76,6 +78,10 @@ public class DesktopPickerField extends DesktopAbstractField<Picker>
protected final ActionsPermissions actionsPermissions = new ActionsPermissions(this);
protected Datasource.ItemChangeListener itemChangeListener;
protected Datasource.ItemChangeListener securityItemChangeListener;
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
public DesktopPickerField() {
impl = new Picker();
initModifiersMask();
@ -284,8 +290,7 @@ public class DesktopPickerField extends DesktopAbstractField<Picker>
resolveMetaPropertyPath(datasource.getMetaClass(), property);
//noinspection unchecked
datasource.addItemChangeListener(e -> {
itemChangeListener = e -> {
if (updatingInstance) {
return;
}
@ -293,10 +298,11 @@ public class DesktopPickerField extends DesktopAbstractField<Picker>
Object value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
updateComponent(value);
fireChangeListeners(value);
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(e -> {
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
itemPropertyChangeListener = e -> {
if (updatingInstance) {
return;
}
@ -305,7 +311,9 @@ public class DesktopPickerField extends DesktopAbstractField<Picker>
updateComponent(e.getValue());
fireChangeListeners(e.getValue());
}
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
if ((datasource.getState() == Datasource.State.VALID) && (datasource.getItem() != null)) {
Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
@ -324,7 +332,9 @@ public class DesktopPickerField extends DesktopAbstractField<Picker>
}
handleFilteredAttributes(this, this.datasource, metaPropertyPath);
this.datasource.addItemChangeListener(e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath));
securityItemChangeListener = e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath);
// noinspection unchecked
this.datasource.addItemChangeListener(new WeakItemChangeListener(this.datasource, securityItemChangeListener));
}
protected void fireChangeListeners(Object newValue) {

View File

@ -25,6 +25,7 @@ import com.haulmont.cuba.gui.components.ListComponent;
import com.haulmont.cuba.gui.components.RowsCount;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;
import org.jdesktop.swingx.JXHyperlink;
@ -45,6 +46,8 @@ public class DesktopRowsCount extends DesktopAbstractComponent<DesktopRowsCount.
protected ListComponent owner;
protected boolean samePage;
protected CollectionDatasource.CollectionChangeListener collectionChangeListener;
public DesktopRowsCount() {
impl = new RowsCountComponent();
}
@ -58,11 +61,12 @@ public class DesktopRowsCount extends DesktopAbstractComponent<DesktopRowsCount.
public void setDatasource(CollectionDatasource datasource) {
this.datasource = datasource;
if (datasource != null) {
//noinspection unchecked
this.datasource.addCollectionChangeListener(e -> {
collectionChangeListener = e -> {
samePage = !CollectionDatasource.Operation.REFRESH.equals(e.getOperation());
onCollectionChanged();
});
};
//noinspection unchecked
this.datasource.addCollectionChangeListener(new WeakCollectionChangeListener(this.datasource, collectionChangeListener));
impl.getCountButton().addActionListener(e -> onLinkClick());

View File

@ -32,6 +32,8 @@ import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.components.DateField;
import com.haulmont.cuba.gui.components.TimeField;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.WeakItemChangeListener;
import com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@ -62,6 +64,9 @@ public class DesktopTimeField extends DesktopAbstractField<JFormattedTextField>
protected static final int DIGIT_WIDTH = 23;
protected Datasource.ItemChangeListener itemChangeListener;
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
public DesktopTimeField() {
UserSessionSource uss = AppBeans.get(UserSessionSource.NAME);
@ -199,18 +204,18 @@ public class DesktopTimeField extends DesktopAbstractField<JFormattedTextField>
resolveMetaPropertyPath(datasource.getMetaClass(), property);
//noinspection unchecked
datasource.addItemChangeListener(e -> {
itemChangeListener = e -> {
if (updatingInstance) {
return;
}
Date value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
updateComponent(value);
fireChangeListeners(value);
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(e -> {
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
itemPropertyChangeListener = e -> {
if (updatingInstance) {
return;
}
@ -218,7 +223,9 @@ public class DesktopTimeField extends DesktopAbstractField<JFormattedTextField>
updateComponent(e.getValue());
fireChangeListeners(e.getValue());
}
});
};
//noinspection unchecked;
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
if (datasource.getState() == Datasource.State.VALID && datasource.getItem() != null) {
if (property.equals(metaPropertyPath.toString())) {

View File

@ -40,6 +40,7 @@ import com.haulmont.cuba.gui.config.WindowInfo;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.ValueListener;
import com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener;
import net.miginfocom.layout.CC;
import net.miginfocom.swing.MigLayout;
import org.slf4j.LoggerFactory;
@ -97,6 +98,8 @@ public class DesktopTokenList extends DesktopAbstractField<JPanel> implements To
protected PickerField.LookupAction lookupAction;
protected CollectionDatasource.CollectionChangeListener collectionChangeListener;
protected final ValueChangeListener lookupSelectListener = e -> {
if (isEditable()) {
addValueFromLookupPickerField();
@ -168,7 +171,7 @@ public class DesktopTokenList extends DesktopAbstractField<JPanel> implements To
public void setDatasource(CollectionDatasource datasource) {
this.datasource = datasource;
datasource.addCollectionChangeListener(e -> {
collectionChangeListener = e -> {
if (lookupPickerField != null) {
if (isLookup()) {
if (getLookupScreen() != null)
@ -183,7 +186,8 @@ public class DesktopTokenList extends DesktopAbstractField<JPanel> implements To
}
rootPanel.refreshComponent();
rootPanel.refreshClickListeners(itemClickListener);
});
};
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
}
@Override

View File

@ -24,9 +24,11 @@ import com.haulmont.cuba.desktop.App;
import com.haulmont.cuba.desktop.gui.data.TreeModelAdapter;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.HierarchicalDatasource;
import com.haulmont.cuba.gui.data.impl.CollectionDsActionsNotifier;
import com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener;
import net.miginfocom.swing.MigLayout;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@ -60,6 +62,8 @@ public class DesktopTree<E extends Entity> extends DesktopAbstractActionsHolderC
protected MouseAdapter itemClickListener;
protected boolean editable = true;
protected CollectionDatasource.CollectionChangeListener collectionChangeListener;
protected CollectionDsActionsNotifier collectionDsActionsNotifier;
public DesktopTree() {
@ -295,8 +299,7 @@ public class DesktopTree<E extends Entity> extends DesktopAbstractActionsHolderC
action.setDatasource(datasource);
}
//noinspection unchecked
datasource.addCollectionChangeListener(e -> {
collectionChangeListener = e -> {
// #PL-2035, reload selection from ds
Set<E> selectedItems = getSelected();
if (selectedItems == null) {
@ -320,7 +323,9 @@ public class DesktopTree<E extends Entity> extends DesktopAbstractActionsHolderC
} else {
setSelected(newSelection);
}
});
};
//noinspection unchecked
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
collectionDsActionsNotifier = new CollectionDsActionsNotifier(this);
collectionDsActionsNotifier.bind(datasource);

View File

@ -25,7 +25,10 @@ import com.haulmont.cuba.core.global.MetadataTools;
import com.haulmont.cuba.core.global.View;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.CollectionDsHelper;
import com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener;
import com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
@ -44,6 +47,9 @@ public class TableModelAdapter extends AbstractTableModel implements AnyTableMod
protected boolean autoRefresh;
protected List<DataChangeListener> changeListeners = new ArrayList<>();
protected CollectionDatasource.CollectionChangeListener collectionChangeListener;
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
protected MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
public TableModelAdapter(CollectionDatasource datasource, List<Table.Column> columns, boolean autoRefresh) {
@ -63,8 +69,7 @@ public class TableModelAdapter extends AbstractTableModel implements AnyTableMod
}
}
//noinspection unchecked
datasource.addCollectionChangeListener(e -> {
collectionChangeListener = e -> {
switch (e.getOperation()) {
case ADD:
fireBeforeChangeListeners(true);
@ -96,10 +101,11 @@ public class TableModelAdapter extends AbstractTableModel implements AnyTableMod
fireAfterChangeListeners(true);
break;
}
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(e -> {
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
itemPropertyChangeListener = e -> {
int rowIndex = getRowIndex(e.getItem());
if (rowIndex >= 0) {
@ -107,7 +113,9 @@ public class TableModelAdapter extends AbstractTableModel implements AnyTableMod
fireTableRowsUpdated(rowIndex, rowIndex);
fireAfterChangeListeners(false);
}
});
};
//noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
}
protected void fireBeforeChangeListeners(boolean structureChanged) {

View File

@ -26,6 +26,7 @@ import com.haulmont.cuba.gui.components.CaptionMode;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.HierarchicalDatasource;
import com.haulmont.cuba.gui.data.impl.CollectionDsHelper;
import com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener;
import org.apache.commons.lang.ObjectUtils;
import javax.swing.event.TreeModelEvent;
@ -52,6 +53,8 @@ public class TreeModelAdapter implements TreeModel {
protected MetadataTools metadataTools;
protected CollectionDatasource.CollectionChangeListener collectionChangeListener;
public TreeModelAdapter(HierarchicalDatasource datasource, CaptionMode captionMode, String captionProperty,
boolean autoRefresh) {
@ -64,34 +67,32 @@ public class TreeModelAdapter implements TreeModel {
this.metadataTools = AppBeans.get(MetadataTools.NAME);
//noinspection unchecked
datasource.addCollectionChangeListener(new CollectionDatasource.CollectionChangeListener() {
@Override
public void collectionChanged(CollectionDatasource.CollectionChangeEvent e) {
switch (e.getOperation()) {
case CLEAR:
case REFRESH:
case ADD:
case REMOVE:
Object[] path = {getRoot()};
for (TreeModelListener listener : treeModelListeners) {
TreeModelEvent ev = new TreeModelEvent(this, path);
listener.treeStructureChanged(ev);
}
break;
collectionChangeListener = e -> {
switch (e.getOperation()) {
case CLEAR:
case REFRESH:
case ADD:
case REMOVE:
Object[] path = {getRoot()};
for (TreeModelListener listener : treeModelListeners) {
TreeModelEvent ev = new TreeModelEvent(this, path);
listener.treeStructureChanged(ev);
}
break;
case UPDATE:
for (Object item : e.getItems()) {
TreePath treePath = getTreePath(item);
for (TreeModelListener listener : treeModelListeners) {
TreeModelEvent ev = new TreeModelEvent(this, treePath.getPath());
listener.treeNodesChanged(ev);
}
case UPDATE:
for (Object item : e.getItems()) {
TreePath treePath = getTreePath(item);
for (TreeModelListener listener : treeModelListeners) {
TreeModelEvent ev = new TreeModelEvent(this, treePath.getPath());
listener.treeNodesChanged(ev);
}
break;
}
}
break;
}
});
};
//noinspection unchecked
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
}
@Override

View File

@ -21,7 +21,9 @@ import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.desktop.sys.vcl.JXTreeTableExt;
import com.haulmont.cuba.gui.components.CaptionMode;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.HierarchicalDatasource;
import com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;
@ -39,6 +41,8 @@ public class TreeTableModelAdapter extends AbstractTreeTableModel implements Any
protected List<DataChangeListener> changeListeners = new ArrayList<>();
protected CollectionDatasource.CollectionChangeListener collectionChangeListener;
public TreeTableModelAdapter(JXTreeTable treeTable, HierarchicalDatasource datasource, List<Table.Column> columns,
boolean autoRefresh) {
@ -46,8 +50,7 @@ public class TreeTableModelAdapter extends AbstractTreeTableModel implements Any
this.treeDelegate = createTreeModelAdapter(datasource, autoRefresh);
this.tableDelegate = new TableModelAdapter(datasource, columns, autoRefresh);
//noinspection unchecked
datasource.addCollectionChangeListener(e -> {
collectionChangeListener = e -> {
Object root1 = getRoot();
// Fixes #1160
JXTreeTableExt impl = (JXTreeTableExt) TreeTableModelAdapter.this.treeTable;
@ -59,7 +62,9 @@ public class TreeTableModelAdapter extends AbstractTreeTableModel implements Any
}
modelSupport.fireTreeStructureChanged(root1 == null ? null : new TreePath(root1));
});
};
//noinspection unchecked
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
}
protected TreeModelAdapter createTreeModelAdapter(HierarchicalDatasource datasource, boolean autoRefresh) {