Added component list classes (unfinished)

This commit is contained in:
Kai S. K. Engelbart 2020-01-25 11:33:28 +01:00
parent 40adb54a32
commit 2f4583861e
3 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package envoy.client.ui.list;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>ComponentList.java</strong><br>
* Created: <strong>25.01.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.3-alpha
*/
public class ComponentList<E> extends JPanel {
private ComponentListCellRenderer<E> renderer;
private static final long serialVersionUID = 1759644503942876737L;
public ComponentList(ComponentListModel<E> model, ComponentListCellRenderer<E> renderer) {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
model.setComponentList(this);
this.renderer = renderer;
for (E elem : model)
add(renderer.getListCellComponent(this, elem, false));
}
void add(E elem) {
add(renderer.getListCellComponent(this, elem, false));
}
}

View File

@ -0,0 +1,16 @@
package envoy.client.ui.list;
import java.awt.Component;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>ComponentListCellRenderer.java</strong><br>
* Created: <strong>25.01.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.3-alpha
*/
public interface ComponentListCellRenderer<E> {
Component getListCellComponent(ComponentList<E> list, E value, boolean isSelected);
}

View File

@ -0,0 +1,83 @@
package envoy.client.ui.list;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>ComponentListModel.java</strong><br>
* Created: <strong>25.01.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.3-alpha
*/
public final class ComponentListModel<E> implements Iterable<E> {
private List<E> elements = new ArrayList<>();
private ComponentList<E> componentList;
/**
* Adds an element to this model and notifies the associated
* {@link ComponentList} to add the corresponding component.
*
* @param e the element to add
* @return {@code true}
* @see java.util.List#add(java.lang.Object)
*/
public boolean add(E e) {
componentList.add(e);
return elements.add(e);
}
/**
* Removes all elements from this model and clears the associated
* {@link ComponentList}.
*
* @see java.util.List#clear()
* @since Envoy v0.3-alpha
*/
public void clear() {
elements.clear();
componentList.removeAll();
}
/**
* @param index
* @return
* @see java.util.List#get(int)
*/
public E get(int index) { return elements.get(index); }
/**
* Removes the element at a specific index from this model and the corresponding
* component from the {@link ComponentList}.
*
* @param index
* @return the removed element
* @see java.util.List#remove(int)
*/
public E remove(int index) {
componentList.remove(index);
return elements.remove(index);
}
/**
* @return
* @see java.util.List#iterator()
*/
public Iterator<E> iterator() {
return new Iterator<E>() {
Iterator<E> iter = elements.iterator();
@Override
public boolean hasNext() { return iter.hasNext(); }
@Override
public E next() { return iter.next(); }
};
}
void setComponentList(ComponentList<E> componentList) { this.componentList = componentList; }
}