From af0029d44fc9b3145e16e98690533f5c79521f0f Mon Sep 17 00:00:00 2001 From: Urs Reupke Date: Wed, 12 Jun 2013 19:22:44 +0200 Subject: [PATCH] ListSelection replaces Selection with ComboBox where necessary. * Fixes #332 on OS X, but looks really ugly. * Spinner might be an alternative, but is hard to use for different reasons * Maybe I can find a way to limit the Lists to 3 items each --- .../item/view/fx/FxEquipmentDatabaseView.java | 6 +- .../fx/selection/ListSelectionView.java | 108 ++++++++++++++++++ .../selection/PopUpLessSelectionFactory.java | 11 ++ 3 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 Platform_FX/src/net/sf/anathema/platform/fx/selection/ListSelectionView.java create mode 100644 Platform_FX/src/net/sf/anathema/platform/fx/selection/PopUpLessSelectionFactory.java diff --git a/Character_Equipment/src/net/sf/anathema/character/equipment/item/view/fx/FxEquipmentDatabaseView.java b/Character_Equipment/src/net/sf/anathema/character/equipment/item/view/fx/FxEquipmentDatabaseView.java index d1ed3f9042..b980482e43 100644 --- a/Character_Equipment/src/net/sf/anathema/character/equipment/item/view/fx/FxEquipmentDatabaseView.java +++ b/Character_Equipment/src/net/sf/anathema/character/equipment/item/view/fx/FxEquipmentDatabaseView.java @@ -3,6 +3,8 @@ import net.sf.anathema.character.equipment.item.view.AgnosticEquipmentDatabaseView; import net.sf.anathema.platform.fx.PerspectivePane; import net.sf.anathema.platform.fx.selection.ComboBoxSelectionFactory; +import net.sf.anathema.platform.fx.selection.PopUpLessSelectionFactory; +import net.sf.anathema.platform.fx.selection.SelectionViewFactory; import static net.sf.anathema.platform.fx.FxThreading.runOnCorrectThread; import static net.sf.anathema.platform.fx.FxUtilities.systemSupportsPopUpsWhileEmbeddingFxIntoSwing; @@ -31,11 +33,11 @@ public void run() { }); } - private ComboBoxSelectionFactory selectionFactory() { + private SelectionViewFactory selectionFactory() { if (systemSupportsPopUpsWhileEmbeddingFxIntoSwing()) { return new ComboBoxSelectionFactory(); } else { - return new ComboBoxSelectionFactory(); + return new PopUpLessSelectionFactory(); } } } \ No newline at end of file diff --git a/Platform_FX/src/net/sf/anathema/platform/fx/selection/ListSelectionView.java b/Platform_FX/src/net/sf/anathema/platform/fx/selection/ListSelectionView.java new file mode 100644 index 0000000000..b47b7f109b --- /dev/null +++ b/Platform_FX/src/net/sf/anathema/platform/fx/selection/ListSelectionView.java @@ -0,0 +1,108 @@ +package net.sf.anathema.platform.fx.selection; + +import com.sun.javafx.collections.ObservableListWrapper; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.scene.Node; +import javafx.scene.control.Label; +import javafx.scene.control.ListView; +import net.sf.anathema.lib.control.ObjectValueListener; +import net.sf.anathema.lib.gui.AgnosticUIConfiguration; +import net.sf.anathema.platform.fx.ConfigurableListCellFactory; +import net.sf.anathema.platform.fx.FxObjectSelectionView; +import net.sf.anathema.platform.fx.FxThreading; +import org.jmock.example.announcer.Announcer; +import org.tbee.javafx.scene.layout.MigPane; + +import java.util.Arrays; + +import static net.sf.anathema.lib.gui.layout.LayoutUtils.withoutInsets; + +public class ListSelectionView implements FxObjectSelectionView { + private ListView list; + private Label label; + private MigPane pane; + private final Announcer announcer = Announcer.to(ObjectValueListener.class); + + @SuppressWarnings("unchecked") + public ListSelectionView(final String description, final AgnosticUIConfiguration ui) { + FxThreading.runOnCorrectThread(new Runnable() { + @Override + public void run() { + list = new ListView<>(); + label = new Label(description); + pane = new MigPane(withoutInsets()); + pane.add(label); + pane.add(list); + } + }); + FxThreading.runOnCorrectThread(new Runnable() { + @Override + public void run() { + list.setCellFactory(new ConfigurableListCellFactory<>(ui)); + list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observableValue, T v, T newValue) { + announcer.announce().valueChanged(newValue); + } + }); + } + }); + } + + @Override + public void setSelectedObject(final T object) { + FxThreading.runOnCorrectThread(new Runnable() { + @Override + public void run() { + list.getSelectionModel().select(object); + } + }); + } + + @Override + public void addObjectSelectionChangedListener(ObjectValueListener listener) { + announcer.addListener(listener); + } + + @Override + public void removeObjectSelectionChangedListener(ObjectValueListener listener) { + announcer.removeListener(listener); + } + + @Override + public void setObjects(T[] objects) { + waitForContent(); + list.setItems(new ObservableListWrapper<>(Arrays.asList(objects))); + } + + @Override + public T getSelectedObject() { + return list.getSelectionModel().getSelectedItem(); + } + + @Override + public boolean isObjectSelected() { + return getSelectedObject() != null; + } + + @Override + public void setEnabled(boolean enabled) { + list.setDisable(!enabled); + } + + @Override + public Node getNode() { + return pane; + } + + private void waitForContent() { + try { + while (pane == null) { + Thread.sleep(50); + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } +} diff --git a/Platform_FX/src/net/sf/anathema/platform/fx/selection/PopUpLessSelectionFactory.java b/Platform_FX/src/net/sf/anathema/platform/fx/selection/PopUpLessSelectionFactory.java new file mode 100644 index 0000000000..6ebda031c9 --- /dev/null +++ b/Platform_FX/src/net/sf/anathema/platform/fx/selection/PopUpLessSelectionFactory.java @@ -0,0 +1,11 @@ +package net.sf.anathema.platform.fx.selection; + +import net.sf.anathema.lib.gui.AgnosticUIConfiguration; +import net.sf.anathema.platform.fx.FxObjectSelectionView; + +public class PopUpLessSelectionFactory implements SelectionViewFactory{ + @Override + public FxObjectSelectionView create(String label, AgnosticUIConfiguration ui) { + return new ListSelectionView<>(label, ui); + } +}