Commit f149c6e4 authored by Nico Mack's avatar Nico Mack

Added SelectionListener support

parent b37b0704
......@@ -46,7 +46,8 @@ import java.util.List;
public class SelectionGroup implements SelectionListener {
private String name;
private boolean isExclusive;
private List<Selectable> group;
private List<Selectable> members;
protected List<SelectionListener> listeners;
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -61,7 +62,8 @@ public class SelectionGroup implements SelectionListener {
public SelectionGroup() {
this.name = Externalization.EMPTY_STRING;
this.isExclusive = false;
this.group = new ArrayList<>();
this.members = new ArrayList<>();
this.listeners = new ArrayList<>();
}
// ---------------------------------------------------------------------------
......@@ -72,6 +74,7 @@ public class SelectionGroup implements SelectionListener {
// ---------------------------------------------------------------------------
public SelectionGroup(Element rootElement) throws BuildException {
this.listeners = new ArrayList<>();
this.buildFromBootstrap(rootElement, null, null);
}
......@@ -85,16 +88,23 @@ public class SelectionGroup implements SelectionListener {
// ---------------------------------------------------------------------------
public SelectionGroup(Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
this.listeners = new ArrayList<>();
this.buildFromBootstrap(rootElement, context, callback);
}
// ---------------------------------------------------------------------------
/**
* @param original
*/
// ---------------------------------------------------------------------------
public SelectionGroup(SelectionGroup original) {
this.group = new ArrayList<>();
this.members = new ArrayList<>();
this.name = original.name;
this.isExclusive = original.isExclusive;
this.setGroup(original.group);
this.addAll(original.members);
this.listeners = new ArrayList<>();
}
// ---------------------------------------------------------------------------
......@@ -114,13 +124,50 @@ public class SelectionGroup implements SelectionListener {
private void buildFromBootstrap(@Nullable Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
name = BootstrappingUtils.getAttributeAsString(rootElement, Externalization.NAME_NODE, BootstrappingUtils.OPTIONAL, Externalization.EMPTY_STRING);
isExclusive = BootstrappingUtils.getAttributeAsBoolean(rootElement, Externalization.EXCLUSIVE_NODE, BootstrappingUtils.OPTIONAL, Boolean.FALSE);
this.group = new ArrayList<>();
this.members = new ArrayList<>();
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @param listener
*/
// ---------------------------------------------------------------------------
public void addSelectionListener(SelectionListener listener) {
if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
// ---------------------------------------------------------------------------
/**
* @param listener
*/
// ---------------------------------------------------------------------------
public void removeSelectionListener(SelectionListener listener) {
if (listeners.contains(listener)) {
listeners.remove(listener);
}
}
// ---------------------------------------------------------------------------
/**
*
*/
// ---------------------------------------------------------------------------
public void notifyListeners(SelectionEvent event) {
for (SelectionListener listener : listeners) {
listener.selectionStateChanged(event);
}
}
// ---------------------------------------------------------------------------
/**
* Simple getter method for name.
......@@ -160,19 +207,23 @@ public class SelectionGroup implements SelectionListener {
// ---------------------------------------------------------------------------
public boolean addToGroup(Selectable selectable) {
if (!this.group.contains(selectable)) {
public boolean add(Selectable selectable) {
if (!this.members.contains(selectable)) {
selectable.addSelectionListener(this);
return this.group.add(selectable);
return this.members.add(selectable);
}
return false;
}
// ---------------------------------------------------------------------------
/**
* @param selectables
*/
// ---------------------------------------------------------------------------
public void setGroup(Collection<Selectable> selectables) {
this.group.clear();
selectables.forEach(selectable -> addToGroup(selectable));
public void addAll(Collection<Selectable> selectables) {
this.members.clear();
selectables.forEach(selectable -> add(selectable));
}
// ---------------------------------------------------------------------------
......@@ -183,13 +234,13 @@ public class SelectionGroup implements SelectionListener {
// ---------------------------------------------------------------------------
public boolean select(Selectable candidate) {
int found = this.group.indexOf(candidate);
int found = this.members.indexOf(candidate);
if (found < 0)
return false;
if (isExclusive) {
for (int index = 0; index < this.group.size(); index++) {
Selectable current = this.group.get(index);
for (int index = 0; index < this.members.size(); index++) {
Selectable current = this.members.get(index);
if (index != found) {
current.setSelected(false);
}
......@@ -200,6 +251,9 @@ public class SelectionGroup implements SelectionListener {
candidate.setSelected(true);
}
SelectionEvent event = new SelectionEvent(candidate, true);
this.notifyListeners(event);
return true;
}
......@@ -211,21 +265,36 @@ public class SelectionGroup implements SelectionListener {
// ---------------------------------------------------------------------------
public boolean deselect(Selectable candidate) {
int found = this.group.indexOf(candidate);
int found = this.members.indexOf(candidate);
if (found >= 0) {
this.group.get(found).setSelected(false);
this.members.get(found).setSelected(false);
SelectionEvent event = new SelectionEvent(candidate, false);
this.notifyListeners(event);
return true;
}
return false;
}
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
@Override
public SelectionGroup clone() {
return new SelectionGroup(this);
public Collection<Selectable> getSelected() {
List<Selectable> selected = new ArrayList<>();
for (Selectable selectable : this.members) {
if (selectable.isSelected()) {
selected.add(selectable);
}
}
return selected;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
@Override
public void selectionStateChanged(SelectionEvent event) {
......@@ -234,7 +303,13 @@ public class SelectionGroup implements SelectionListener {
} else {
this.deselect(event.getSource());
}
}
// ---------------------------------------------------------------------------
@Override
public SelectionGroup clone() {
return new SelectionGroup(this);
}
// ---------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment