net.alantea.swing.keyvalue.KeyValueScroller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swingplus Show documentation
Show all versions of swingplus Show documentation
Addons over swing package.
The newest version!
package net.alantea.swing.keyvalue;
import java.awt.Color;
import java.awt.Component;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import javax.swing.BorderFactory;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import net.alantea.liteprops.Property;
import net.alantea.swing.text.LabeledLabel;
@SuppressWarnings("serial")
public class KeyValueScroller extends JScrollPane
{
private Property> selected = new Property<>();
private JList> content;
private KVListModel model = new KVListModel();
public KeyValueScroller()
{
super();
content = new JList<>();
setViewportView(content);
content.setModel(model);
content.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
content.addListSelectionListener((e) -> {
int i = content.getSelectedIndex();
KeyValue kv = model.get(i);
selected.set(kv);
});
KVListCellRenderer kvListCellRenderer= new KVListCellRenderer();
kvListCellRenderer.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,Color.BLACK));
content.setCellRenderer(kvListCellRenderer);
content.setBorder(new EmptyBorder(0, 5, 0, 5));
}
public Property> getSelectionProperty()
{
return selected;
}
public KeyValue getSelection()
{
return selected.get();
}
public void setInput(Collection> collection)
{
model.clear();
model.addAll(collection);
revalidate();
}
public void setInput(KeyValue[] collection)
{
setInput(Arrays.asList(collection));
}
class KVListCellRenderer extends LabeledLabel implements ListCellRenderer>
{
@Override
public Component getListCellRendererComponent(JList extends KeyValue> list,
KeyValue value, int index, boolean isSelected, boolean cellHasFocus)
{
this.setLabelText(value.getKey().toString());
if (value.getValue() != null)
{
this.setText(value.getValue().toString());
}
if (isSelected)
{
this.setBackground(Color.LIGHT_GRAY);
}
else
{
this.setBackground(Color.WHITE);
}
return this;
}
}
//--------------------------------------------
class KVListModel implements List>, ListModel>
{
private List> internal = new LinkedList<>();
private List listeners = new LinkedList<>();
@Override
public int getSize()
{
return internal.size();
}
@Override
public KeyValue getElementAt(int index)
{
return internal.get(index);
}
@Override
public void addListDataListener(ListDataListener l)
{
if (!listeners.contains(l))
{
listeners.add(l);
}
}
@Override
public void removeListDataListener(ListDataListener l)
{
if (listeners.contains(l))
{
listeners.remove(l);
}
}
@Override
public int size()
{
return internal.size();
}
@Override
public boolean isEmpty()
{
return internal.isEmpty();
}
@Override
public boolean contains(Object o)
{
return internal.contains(o);
}
@Override
public Iterator> iterator()
{
return internal.iterator();
}
@Override
public Object[] toArray()
{
return internal.toArray();
}
@Override
public T[] toArray(T[] a)
{
return internal.toArray(a);
}
@Override
public boolean add(KeyValue e)
{
boolean ret = internal.add(e);
fireEvent();
return ret;
}
@Override
public boolean remove(Object o)
{
boolean ret = internal.remove(o);
fireEvent();
return ret;
}
@Override
public boolean containsAll(Collection> c)
{
return internal.containsAll(c);
}
@Override
public boolean addAll(Collection extends KeyValue> c)
{
boolean ret = internal.addAll(c);
fireEvent();
return ret;
}
@Override
public boolean addAll(int index, Collection extends KeyValue> c)
{
boolean ret = internal.addAll(index, c);
fireEvent();
return ret;
}
@Override
public boolean removeAll(Collection> c)
{
boolean ret = internal.removeAll(c);
fireEvent();
return ret;
}
@Override
public boolean retainAll(Collection> c)
{
boolean ret = internal.retainAll(c);
fireEvent();
return ret;
}
@Override
public void clear()
{
internal.clear();
fireEvent();
}
@Override
public KeyValue get(int index)
{
return internal.get(index);
}
@Override
public KeyValue set(int index, KeyValue element)
{
KeyValue ret = internal.set(index, element);
fireEvent();
return ret;
}
@Override
public void add(int index, KeyValue element)
{
internal.add(element);
fireEvent();
}
@Override
public KeyValue remove(int index)
{
KeyValue ret = internal.remove(index);
fireEvent();
return ret;
}
@Override
public int indexOf(Object o)
{
return internal.indexOf(o);
}
@Override
public int lastIndexOf(Object o)
{
return internal.lastIndexOf(o);
}
@Override
public ListIterator> listIterator()
{
return internal.listIterator();
}
@Override
public ListIterator> listIterator(int index)
{
return internal.listIterator(index);
}
@Override
public List> subList(int fromIndex, int toIndex)
{
return internal.subList(fromIndex, toIndex);
}
private void fireEvent()
{
ListDataEvent event = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, 0);
for (ListDataListener listener : listeners)
{
listener.contentsChanged(event);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy