Skip to content

Commit

Permalink
Use global isFocussing flag instead of per panel
Browse files Browse the repository at this point in the history
This prevents unnecessary UI updates triggered by individual components during databinding.
#430
  • Loading branch information
steffen-wilke committed Dec 28, 2021
1 parent e05d6ce commit 61aa52c
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ public void setFocus(IMapObject mapObject, boolean clearSelection) {
}
}

public boolean isFocussing() {
return this.isFocussing;
}

public void setSelection(IMapObject mapObject, boolean clearSelection) {
this.setSelection(
mapObject == null ? null : Collections.singletonList(mapObject), clearSelection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ public static String getCreatureSpriteName(String name) {

@Override
public void bind(IMapObject mapObject) {
this.isFocussing = true;

this.loadAvailableCreatureSprites();
if (mapObject != null) {
this.setControlValues(mapObject);
}

this.isFocussing = false;
super.bind(mapObject);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private void setupChangedListeners() {
}

private void updateCustomProperties() {
if (getDataSource() == null || isFocussing) {
if (getDataSource() == null || Editor.instance().getMapComponent().isFocussing()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.gurkenlabs.litiengine.entities.LightSource;
import de.gurkenlabs.litiengine.environment.tilemap.IMapObject;
import de.gurkenlabs.litiengine.environment.tilemap.MapObjectProperty;
import de.gurkenlabs.utiliti.components.Editor;
import de.gurkenlabs.utiliti.swing.ColorComponent;
import de.gurkenlabs.utiliti.swing.Icons;
import java.awt.LayoutManager;
Expand Down Expand Up @@ -109,7 +110,7 @@ private void setupChangedListeners() {
}

private void updateLighting() {
if (this.isFocussing) {
if (Editor.instance().getMapComponent().isFocussing()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public void refresh() {

@Override
public void bind(IMapObject mapObject) {
this.isFocussing = true;
super.bind(mapObject);

if (mapObject != null) {
Expand All @@ -153,7 +152,6 @@ public void bind(IMapObject mapObject) {
}

this.customPanel.bind(this.getDataSource());
this.isFocussing = false;
}

private LayoutManager createLayout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ public static String getIdentifierBySpriteName(String spriteName) {

@Override
public void bind(IMapObject mapObject) {
this.isFocussing = true;
this.loadAvailableProps();
if (mapObject != null) {
this.setControlValues(mapObject);
}
this.isFocussing = false;
super.bind(mapObject);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public abstract class PropertyPanel extends JPanel {
public static final float STEP_FINE = .05f;
public static final float STEP_FINEST = .01f;

protected boolean isFocussing;
protected transient IMapObject dataSource;
private String identifier;
private transient Icon icon;
Expand Down Expand Up @@ -121,15 +120,12 @@ public void setIcon(Icon icon) {
public void bind(IMapObject mapObject) {
this.dataSource = mapObject;

this.isFocussing = true;

if (this.dataSource == null) {
this.clearControls();
return;
}

this.setControlValues(mapObject);
this.isFocussing = false;
}

protected static void populateComboBoxWithSprites(
Expand Down Expand Up @@ -157,8 +153,8 @@ protected static void selectSpriteSheet(JComboBox<JLabel> comboBox, IMapObject m
JLabel label = comboBox.getModel().getElementAt(i);
if (label != null
&& label
.getText()
.equals(mapObject.getStringValue(MapObjectProperty.SPRITESHEETNAME))) {
.getText()
.equals(mapObject.getStringValue(MapObjectProperty.SPRITESHEETNAME))) {
comboBox.setSelectedItem(label);
break;
}
Expand Down Expand Up @@ -257,6 +253,7 @@ protected void setup(JTable table, String... properties) {
}

protected class MapObjectPropertyItemListener implements ItemListener {

private final Consumer<IMapObject> updateAction;

MapObjectPropertyItemListener(Consumer<IMapObject> updateAction) {
Expand All @@ -265,7 +262,7 @@ protected class MapObjectPropertyItemListener implements ItemListener {

@Override
public void itemStateChanged(ItemEvent arg0) {
if (getDataSource() == null || isFocussing) {
if (getDataSource() == null || Editor.instance().getMapComponent().isFocussing()) {
return;
}

Expand All @@ -274,6 +271,7 @@ public void itemStateChanged(ItemEvent arg0) {
}

protected class MapObjectPropertyActionListener implements ActionListener {

private final Consumer<IMapObject> updateAction;

MapObjectPropertyActionListener(Consumer<IMapObject> updateAction) {
Expand All @@ -282,7 +280,7 @@ protected class MapObjectPropertyActionListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
if (getDataSource() == null || isFocussing) {
if (getDataSource() == null || Editor.instance().getMapComponent().isFocussing()) {
return;
}

Expand All @@ -291,6 +289,7 @@ public void actionPerformed(ActionEvent e) {
}

protected class MabObjectPropertyTableModelListener implements TableModelListener {

private final Consumer<IMapObject> updateAction;

MabObjectPropertyTableModelListener(Consumer<IMapObject> updateAction) {
Expand All @@ -299,14 +298,15 @@ protected class MabObjectPropertyTableModelListener implements TableModelListene

@Override
public void tableChanged(TableModelEvent e) {
if (getDataSource() == null || isFocussing) {
if (getDataSource() == null || Editor.instance().getMapComponent().isFocussing()) {
return;
}
applyChanges(this.updateAction);
}
}

protected class MapObjectPropertyChangeListener implements ChangeListener {

private final Consumer<IMapObject> updateAction;

MapObjectPropertyChangeListener(Consumer<IMapObject> updateAction) {
Expand All @@ -315,7 +315,7 @@ protected class MapObjectPropertyChangeListener implements ChangeListener {

@Override
public void stateChanged(ChangeEvent e) {
if (getDataSource() == null || isFocussing) {
if (getDataSource() == null || Editor.instance().getMapComponent().isFocussing()) {
return;
}

Expand All @@ -324,12 +324,14 @@ public void stateChanged(ChangeEvent e) {
}

protected class SpinnerListener extends MapObjectPropertyChangeListener {

SpinnerListener(String mapObjectProperty, JSpinner spinner) {
super(m -> m.setValue(mapObjectProperty, spinner.getValue().toString()));
}
}

protected class SliderListener extends MapObjectPropertyChangeListener {

SliderListener(String mapObjectProperty, JSlider slider) {
super(m -> m.setValue(mapObjectProperty, slider.getValue()));
}
Expand All @@ -340,6 +342,7 @@ protected class SliderListener extends MapObjectPropertyChangeListener {
}

protected class TableListener extends MabObjectPropertyTableModelListener {

TableListener(JTable table, String... mapObjectProperties) {
super(
m -> {
Expand All @@ -357,6 +360,7 @@ protected class TableListener extends MabObjectPropertyTableModelListener {
}

protected class MapObjectPropteryFocusListener extends FocusAdapter {

private final Consumer<IMapObject> updateAction;

MapObjectPropteryFocusListener(Consumer<IMapObject> updateAction) {
Expand All @@ -365,7 +369,7 @@ protected class MapObjectPropteryFocusListener extends FocusAdapter {

@Override
public void focusLost(FocusEvent e) {
if (getDataSource() == null || isFocussing) {
if (getDataSource() == null || Editor.instance().getMapComponent().isFocussing()) {
return;
}

Expand Down Expand Up @@ -461,6 +465,7 @@ private void applyChanges(Consumer<IMapObject> updateAction) {
}

protected static class LayoutItem {

private final String caption;
private final Component component;
private final JLabel label;
Expand Down

0 comments on commit 61aa52c

Please sign in to comment.