Commit c01efdc8 authored by Nico Mack's avatar Nico Mack

Implementation of SelectionGroup mechanism

parent 3e8b8e22
......@@ -150,6 +150,8 @@ SCREEN_ID_NODE=screenId
SECTION_NODE=section
SECTIONS_NODE=sections
SELECTED_ELEMENT=selected
SELECTION_GROUPS_NODE=selectionGroups
SELECTION_GROUP_NODE=selectionGroup
SHADE_ATTRIBUTE=shade
SHADE_BRIGHTER_VALUE=brighter
SHADE_EXTRA_BRIGHT_VALUE=extra bright
......
......@@ -16,6 +16,8 @@
*/
package lu.list.itis.dkd.tui.event;
import lu.list.itis.dkd.tui.utility.selection.Selectable;
/**
* @author Nico Mack [nico.mack@list.lu]
* @since 2.5
......
......@@ -185,6 +185,8 @@ public class Externalization extends NLS {
public static String SECTION_NODE;
public static String SECTIONS_NODE;
public static String SELECTED_ELEMENT;
public static String SELECTION_GROUPS_NODE;
public static String SELECTION_GROUP_NODE;
public static String SHADE_ATTRIBUTE;
public static String SHADE_BRIGHTER_VALUE;
public static String SHADE_EXTRA_BRIGHT_VALUE;
......
......@@ -14,7 +14,9 @@
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.event;
package lu.list.itis.dkd.tui.utility.selection;
import lu.list.itis.dkd.tui.event.SelectionListener;
/**
* @author Nico Mack [nico.mack@list.lu]
......@@ -23,6 +25,17 @@ package lu.list.itis.dkd.tui.event;
*/
public interface Selectable {
/**
* @param listener
*/
public void addSelectionListener(SelectionListener listener);
/**
* @param listener
*/
public void removeSelectionListener(SelectionListener listener);
/**
* @param selectIt
*/
......
/**
* Copyright Luxembourg Institute of Science and Technology, 2018. All rights reserved.
*
* This file is part of TULIP.
*
* TULIP is free software: you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation, version 3 of the
* License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.utility.selection;
import lu.list.itis.dkd.dbc.annotation.Nullable;
import lu.list.itis.dkd.tui.bootstrapping.BootstrapCallback;
import lu.list.itis.dkd.tui.bootstrapping.BootstrapContext;
import lu.list.itis.dkd.tui.bootstrapping.BootstrappingUtils;
import lu.list.itis.dkd.tui.event.SelectionEvent;
import lu.list.itis.dkd.tui.event.SelectionListener;
import lu.list.itis.dkd.tui.exception.BuildException;
import lu.list.itis.dkd.tui.utility.Externalization;
import org.jdom2.Element;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Provides a way to logically organize and label a set of coronas. Bundles can also be altered
* dynamically, i.e. coronas may be added or removed at runtime.
*
* @author Nico Mack [nico.mack@list.lu]
* @since 2.5
* @version 2.5.0
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class SelectionGroup implements SelectionListener {
private String name;
private boolean isExclusive;
private List<Selectable> group;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
*
*/
// ---------------------------------------------------------------------------
public SelectionGroup() {
this.name = Externalization.EMPTY_STRING;
this.isExclusive = false;
this.group = new ArrayList<>();
}
// ---------------------------------------------------------------------------
/**
* @param rootElement
* @throws BuildException
*/
// ---------------------------------------------------------------------------
public SelectionGroup(Element rootElement) throws BuildException {
this.buildFromBootstrap(rootElement, null, null);
}
// ---------------------------------------------------------------------------
/**
* @param rootElement
* @param context
* @param callback
* @throws BuildException
*/
// ---------------------------------------------------------------------------
public SelectionGroup(Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
this.buildFromBootstrap(rootElement, context, callback);
}
// ---------------------------------------------------------------------------
public SelectionGroup(SelectionGroup original) {
this.group = new ArrayList<>();
this.name = original.name;
this.isExclusive = original.isExclusive;
this.setGroup(original.group);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitive(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
*
* @param rootElement
* @param context
* @param callback
* @throws BuildException
*/
// ---------------------------------------------------------------------------
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<>();
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* Simple getter method for name.
*
* @return The value of name.
*/
// ---------------------------------------------------------------------------
public String getName() {
return name;
}
// ---------------------------------------------------------------------------
/**
* Simple setter method for name.
*
* @param name
* The value to set name to.
*/
// ---------------------------------------------------------------------------
public void setName(String name) {
this.name = name;
}
// ---------------------------------------------------------------------------
/**
* Simple getter method for exclusive.
*
* @return The value of exclusive.
*/
// ---------------------------------------------------------------------------
public boolean isExclusive() {
return isExclusive;
}
// ---------------------------------------------------------------------------
public boolean addToGroup(Selectable selectable) {
if (!this.group.contains(selectable)) {
selectable.addSelectionListener(this);
return this.group.add(selectable);
}
return false;
}
// ---------------------------------------------------------------------------
public void setGroup(Collection<Selectable> selectables) {
this.group.clear();
selectables.forEach(selectable -> addToGroup(selectable));
}
// ---------------------------------------------------------------------------
/**
* @param candidate
* @return
*/
// ---------------------------------------------------------------------------
public boolean select(Selectable candidate) {
int found = this.group.indexOf(candidate);
if (found < 0)
return false;
if (isExclusive) {
for (int index = 0; index < this.group.size(); index++) {
this.group.get(index).setSelected(index == found);
}
} else {
this.group.get(found).setSelected(true);
}
return true;
}
// ---------------------------------------------------------------------------
/**
* @param candidate
* @return
*/
// ---------------------------------------------------------------------------
public boolean deselect(Selectable candidate) {
int found = this.group.indexOf(candidate);
if (found >= 0) {
this.group.get(found).setSelected(false);
return true;
}
return false;
}
// ---------------------------------------------------------------------------
@Override
public SelectionGroup clone() {
return new SelectionGroup(this);
}
/** {@inheritDoc} */
@Override
public void selectionStateChanged(SelectionEvent event) {
if (event.isSelected()) {
this.select(event.getSource());
} else {
this.deselect(event.getSource());
}
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class *
// ***************************************************************************
// ---------------------------------------------------------------------------
}
......@@ -17,11 +17,11 @@
package lu.list.itis.dkd.tui.widget.corona;
import lu.list.itis.dkd.dbc.annotation.Nullable;
import lu.list.itis.dkd.tui.event.Selectable;
import lu.list.itis.dkd.tui.event.SelectionEvent;
import lu.list.itis.dkd.tui.event.SelectionListener;
import lu.list.itis.dkd.tui.event.conditional.Condition;
import lu.list.itis.dkd.tui.event.conditional.Conditional;
import lu.list.itis.dkd.tui.utility.selection.Selectable;
import lu.list.itis.dkd.tui.widget.corona.builder.BaseSelectableCoronaBuilder;
import java.util.ArrayList;
......
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