sync edit menu checkbox state with the selected shape
This commit is contained in:
@@ -1,15 +1,22 @@
|
||||
package ovh.gasser.newshapes;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ovh.gasser.newshapes.attributes.SelectionAttributes;
|
||||
import ovh.gasser.newshapes.shapes.Shape;
|
||||
import ovh.gasser.newshapes.ui.listeners.SelectionListener;
|
||||
import ovh.gasser.newshapes.util.Streamable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class Selection implements Streamable<Shape> {
|
||||
private final static Logger logger = LoggerFactory.getLogger(Selection.class);
|
||||
|
||||
private final List<Shape> selectedShapes = new ArrayList<>();
|
||||
private final List<SelectionListener> listeners = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public Iterator<Shape> iterator() {
|
||||
@@ -20,12 +27,26 @@ public class Selection implements Streamable<Shape> {
|
||||
for (Shape shape : selectedShapes) {
|
||||
shape.addAttributes(new SelectionAttributes(false));
|
||||
}
|
||||
|
||||
selectedShapes.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public void add(Shape s) {
|
||||
logger.info("Selection.add");
|
||||
selectedShapes.add(s);
|
||||
s.addAttributes(new SelectionAttributes(true));
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
public List<Shape> getSelectedShapes() {
|
||||
return List.copyOf(selectedShapes);
|
||||
}
|
||||
|
||||
public void addListener(SelectionListener listener) {
|
||||
this.listeners.add(listener);
|
||||
}
|
||||
|
||||
private void notifyListeners(){
|
||||
listeners.forEach(l -> l.onSelectionChanged(Collections.unmodifiableCollection(selectedShapes)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user