Skip to content

Commit

Permalink
ListSelection replaces Selection with ComboBox where necessary.
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
UrsKR committed Jun 12, 2013
1 parent bb2f87e commit af0029d
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<T> implements FxObjectSelectionView<T> {
private ListView<T> list;
private Label label;
private MigPane pane;
private final Announcer<ObjectValueListener> announcer = Announcer.to(ObjectValueListener.class);

@SuppressWarnings("unchecked")
public ListSelectionView(final String description, final AgnosticUIConfiguration<T> 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<T>() {
@Override
public void changed(ObservableValue<? extends T> 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<T> listener) {
announcer.addListener(listener);
}

@Override
public void removeObjectSelectionChangedListener(ObjectValueListener<T> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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 <T> FxObjectSelectionView<T> create(String label, AgnosticUIConfiguration<T> ui) {
return new ListSelectionView<>(label, ui);
}
}

0 comments on commit af0029d

Please sign in to comment.