|
|
@@ -1,6 +1,8 @@
|
|
|
package org.leumasjaffe.recipe.view;
|
|
|
|
|
|
import java.awt.Component;
|
|
|
+import java.awt.event.FocusEvent;
|
|
|
+import java.awt.event.FocusListener;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.function.Consumer;
|
|
|
@@ -10,6 +12,7 @@ import java.util.function.Supplier;
|
|
|
import javax.swing.JPanel;
|
|
|
import javax.swing.event.DocumentEvent;
|
|
|
import javax.swing.event.DocumentListener;
|
|
|
+import javax.swing.text.JTextComponent;
|
|
|
|
|
|
import org.jdesktop.swingx.VerticalLayout;
|
|
|
|
|
|
@@ -31,8 +34,10 @@ public class AutoGrowPanel<C extends Component & AutoGrowPanel.ChildComponent, T
|
|
|
private static interface SetGap { void setGap(int gap); }
|
|
|
|
|
|
public static interface ChildComponent {
|
|
|
- void addGrowShrinkListener(DocumentListener dl);
|
|
|
- void removeGrowShrinkListener(DocumentListener dl);
|
|
|
+ void addGrowListener(DocumentListener dl);
|
|
|
+ void removeGrowListener(DocumentListener dl);
|
|
|
+ void addShrinkListener(FocusListener fl);
|
|
|
+ void removeShrinkListener(FocusListener fl);
|
|
|
default void setListPosition(int zeroIndex) {}
|
|
|
}
|
|
|
|
|
|
@@ -50,8 +55,8 @@ public class AutoGrowPanel<C extends Component & AutoGrowPanel.ChildComponent, T
|
|
|
public void insertUpdate(DocumentEvent e) {
|
|
|
if (model != null) {
|
|
|
models.add(model);
|
|
|
- last().removeGrowShrinkListener(this);
|
|
|
- last().addGrowShrinkListener(new ShrinkOnEmpty(last()));
|
|
|
+ last().removeGrowListener(this);
|
|
|
+ last().addShrinkListener(new ShrinkOnEmpty(last()));
|
|
|
}
|
|
|
|
|
|
model = makeEmptyModel.get();
|
|
|
@@ -59,28 +64,27 @@ public class AutoGrowPanel<C extends Component & AutoGrowPanel.ChildComponent, T
|
|
|
|
|
|
members.add(comp);
|
|
|
add(comp);
|
|
|
- comp.addGrowShrinkListener(this);
|
|
|
+ comp.addGrowListener(this);
|
|
|
comp.setListPosition(lastIndex());
|
|
|
revalidate();
|
|
|
callback.accept(true);
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@AllArgsConstructor
|
|
|
- private class ShrinkOnEmpty implements DocumentListener {
|
|
|
+ private class ShrinkOnEmpty implements FocusListener {
|
|
|
C component;
|
|
|
|
|
|
- @Override public void insertUpdate(DocumentEvent e) {}
|
|
|
- @Override public void changedUpdate(DocumentEvent e) {}
|
|
|
+ @Override public void focusGained(FocusEvent e) {}
|
|
|
|
|
|
@Override
|
|
|
- public void removeUpdate(DocumentEvent e) {
|
|
|
- if (e.getDocument().getLength() > 0) {
|
|
|
+ public void focusLost(FocusEvent e) {
|
|
|
+ if (!((JTextComponent) e.getComponent()).getText().isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
component.setVisible(false);
|
|
|
- component.removeGrowShrinkListener(this);
|
|
|
+ component.removeShrinkListener(this);
|
|
|
remove(members.indexOf(component));
|
|
|
callback.accept(false);
|
|
|
}
|
|
|
@@ -135,7 +139,7 @@ public class AutoGrowPanel<C extends Component & AutoGrowPanel.ChildComponent, T
|
|
|
this.members.subList(0, lastIndex()).clear();
|
|
|
models.forEach(model -> {
|
|
|
final C comp = this.grow.makeComponent.apply(model);
|
|
|
- comp.addGrowShrinkListener(new ShrinkOnEmpty(comp));
|
|
|
+ comp.addShrinkListener(new ShrinkOnEmpty(comp));
|
|
|
add(comp, lastIndex());
|
|
|
comp.setListPosition(lastIndex());
|
|
|
this.members.add(lastIndex(), comp);
|