57 lines
1.6 KiB
Java
57 lines
1.6 KiB
Java
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() {
|
|
return selectedShapes.iterator();
|
|
}
|
|
|
|
public void clear() {
|
|
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 boolean isEmpty() {
|
|
return selectedShapes.isEmpty();
|
|
}
|
|
|
|
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)));
|
|
}
|
|
}
|