-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
IntegerProperty selectedIndexProperty(JList<?> list)
.
- Loading branch information
Showing
5 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
src/main/java/io/github/parubok/fxprop/ListSelectedIndexPropertyImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/java/io/github/parubok/fxprop/SelectedIndexPropertyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
} | ||
} |