Skip to content

Commit

Permalink
Add IntegerProperty selectedIndexProperty(JList<?> list).
Browse files Browse the repository at this point in the history
  • Loading branch information
parubok committed Jan 4, 2024
1 parent 50cc061 commit bb4ce5c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.github.parubok.fxprop;

import io.github.parubok.swingfx.beans.property.IntegerProperty;
import io.github.parubok.swingfx.beans.property.SimpleIntegerProperty;
import io.github.parubok.swingfx.beans.value.ChangeListener;

import javax.swing.JList;
import javax.swing.event.ListSelectionListener;
import java.util.Objects;

import static io.github.parubok.fxprop.ClientProps.PROP_SELECTED_INDEX;

/**
* Selected index of {@link JList}.
*/
final class ListSelectedIndexPropertyImpl {
private static final ListSelectionListener SELECTION_LISTENER = e -> {
JList<?> list = (JList) e.getSource();
int selectedIndex = list.getSelectedIndex();
IntegerProperty p = (IntegerProperty) list.getClientProperty(PROP_SELECTED_INDEX);
if (p.get() != selectedIndex) {
p.set(selectedIndex);
}
};

private static final ChangeListener<Number> FX_PROP_LISTENER = (observable, oldValue, newValue) -> {
IntegerProperty p = (IntegerProperty) observable;
JList<?> list = (JList) p.getBean();
if (newValue.intValue() != list.getSelectedIndex()) {
list.setSelectedIndex(newValue.intValue());
}
};

static IntegerProperty getProperty(JList<?> list) {
Objects.requireNonNull(list, "list");
SimpleIntegerProperty p = (SimpleIntegerProperty) list.getClientProperty(PROP_SELECTED_INDEX);
if (p == null) {
p = new SimpleIntegerProperty(list, "selectedIndex", list.getSelectedIndex());
list.putClientProperty(PROP_SELECTED_INDEX, p);
list.addListSelectionListener(SELECTION_LISTENER);
p.addListener(FX_PROP_LISTENER);
}
return p;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Selected value of {@link JList}.
*/
final class SelectedValuePropertyImpl {
final class ListSelectedValuePropertyImpl {
private static final ListSelectionListener SELECTION_LISTENER = e -> {
JList list = (JList) e.getSource();
Object selectedValue = list.getSelectedValue();
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/io/github/parubok/fxprop/SwingPropertySupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,17 @@ public static <E> ObjectProperty<E> selectedItemProperty(JComboBox<E> comboBox)
* @implNote The property always passes {@code false} as a value of {@code shouldScroll} argument of {@link JList#setSelectedValue(Object, boolean)}.
*/
public static <E> ObjectProperty<E> selectedValueProperty(JList<E> list) {
return SelectedValuePropertyImpl.getProperty(list);
return ListSelectedValuePropertyImpl.getProperty(list);
}

/**
* @param list List component. Not null.
* @return Integer property for 'selectedIndex' property of the specified list. Value {@code -1} of the property means no selection.
* @see JList#setSelectedIndex(int)
* @see JList#getSelectedIndex()
*/
public static IntegerProperty selectedIndexProperty(JList<?> list) {
return ListSelectedIndexPropertyImpl.getProperty(list);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.parubok.fxprop;

import io.github.parubok.swingfx.beans.property.IntegerProperty;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.SwingUtilities;

class SelectedIndexPropertyTest {
@Test
public void basicTest() throws Exception {
SwingUtilities.invokeAndWait(() -> {
DefaultListModel<String> model = new DefaultListModel<>();
model.addElement("a1");
model.addElement("b2");
model.addElement("c3");
JList<String> list = new JList<>();
list.setModel(model);

IntegerProperty p = SwingPropertySupport.selectedIndexProperty(list);
Assertions.assertEquals(-1, list.getSelectedIndex());
Assertions.assertEquals(-1, p.getValue());

list.setSelectedIndex(1);
Assertions.assertEquals(1, list.getSelectedIndex());
Assertions.assertEquals(1, p.getValue());

list.clearSelection();
Assertions.assertEquals(-1, list.getSelectedIndex());
Assertions.assertEquals(-1, p.getValue());
});
}
}

0 comments on commit bb4ce5c

Please sign in to comment.