I have a TableView which lists several addresses as a preview. The selected address can be edited in a form next to the TableView. In case that the user has made changes to the selected address and changes the selected address in the TableView, I want to ask him if he wants to proceed without saving changes. If no, I want the Tableview to stay at the currently selected position, if the user wants to discard the changes the TableView may perform the selection change.
My approach would have been to extend the SelectionModel of the TableView and only allow the selection to change if the user answers the described dialog or there are no changes. Basically I only want to add an if-clause before changing the selection in the Tableview. Unfortunately, I only found the abstract class “TableViewSelectionModel” and no concrete implementations of it.
Is there any way to obtain this desired behavior without implementing the whole class?
Solved it with James_D's recommendation:
public class ControllableTableSelectionModel<T> extends TableViewSelectionModel<T>{
private TableViewSelectionModel<T> originalModel;
private ControlsTableView controller;
public ControllableTableSelectionModel(TableViewSelectionModel<T> originalModel, ControlsTableView controller) {
super(originalModel.getTableView());
this.originalModel=originalModel;
this.controller=controller;
}
#Override
public boolean isSelected(int row, TableColumnBase<T, ?> column) {
return originalModel.isSelected(row, column);
}
#Override
public void selectLeftCell() {
if(controller.canSelect(this))
{
originalModel.selectLeftCell();
}
}
#Override
public void selectRightCell() {
if(controller.canSelect(this))
{
originalModel.selectRightCell();
}
}
#Override
public void selectAboveCell() {
if(controller.canSelect(this))
{
originalModel.selectAboveCell();
}
}
#Override
public void selectBelowCell() {
if(controller.canSelect(this))
{
originalModel.selectBelowCell();
}
}
#Override
public void selectRange(int minRow, TableColumnBase<T, ?> minColumn, int maxRow,
TableColumnBase<T, ?> maxColumn) {
if(controller.canSelect(this))
{
originalModel.selectRange(minRow, minColumn, maxRow, maxColumn);
}
}
#Override
protected int getItemCount() {
return originalModel.getTableView().getItems().size();
}
#Override
protected T getModelItem(int index) {
return originalModel.getTableView().getItems().get(index);
}
#Override
public void focus(int index) {
originalModel.getTableView().getFocusModel().focus(index);
}
#Override
public int getFocusedIndex() {
return originalModel.getTableView().getFocusModel().getFocusedIndex();
}
#Override
public ObservableList<TablePosition> getSelectedCells() {
return originalModel.getSelectedCells();
}
#Override
public boolean isSelected(int row, TableColumn<T, ?> column) {
return originalModel.isSelected(row, column);
}
#Override
public void select(int row, TableColumn<T, ?> column) {
if(controller.canSelect(this))
{
originalModel.select(row, column);
}
}
#Override
public void clearAndSelect(int row, TableColumn<T, ?> column) {
if(controller.canSelect(this))
{
originalModel.clearAndSelect(row, column);
}
}
#Override
public void clearSelection(int row, TableColumn<T, ?> column) {
if(controller.canSelect(this))
{
originalModel.clearSelection(row, column);
}
}
}
I'd like to add an improvement to your self-answer by making it more generic and applying the "tell, don't ask!" principle:
public class ControllableTableSelectionModel<T> extends TableViewSelectionModel<T> {
#FunctionalInterface
public interface VetoableSelectAction{
void execute();
}
#FunctionalInterface
public interface SelectionVeto {
<T> void allow(TableViewSelectionModel<T> tableViewSelectionModel, SelectionVeto vetoableSelectAction);
}
private final TableViewSelectionModel<T> originalModel;
private final SelectionVeto selectionVeto;
public ControllableTableSelectionModel(TableViewSelectionModel<T> originalModel, SelectionVeto selectionVeto) {
super(originalModel.getTableView());
this.originalModel = originalModel;
this.selectionVeto = selectionVeto;
}
#Override
public boolean isSelected(int row, TableColumnBase<T, ?> column) {
return originalModel.isSelected(row, column);
}
#Override
public void selectLeftCell() {
selectionVeto.allow(this, () -> originalModel.selectLeftCell());
}
// ...
}
I personally have no use for the second parameter in allow():
public class ControllableTableSelectionModel<T> extends TableViewSelectionModel<T> {
// ...
#FunctionalInterface
public interface SelectionVeto{
void allow(VetoableSelectAction vetoableSelectAction);
}
// ...
#Override
public void selectLeftCell() {
selectionVeto.allow(() -> originalModel.selectLeftCell());
}
// ...
}
Related
In my code, MyTab extendes Tab and until version 8 existed the method setEventHandler (...);
As I am now using the javaFX 12 version, this method is private and I can not use it anymore.
I also do not have access to the variable eventHandlerManager of Tab.
How can I access this functionality in JavaFX 12?
Here is an example of the code.
public class MyTab extends Tab {
...
protected ObjectProperty<EventHandler<EventAction>> onEventDockRequest=null;
public void setOnEventDockRequest(EventHandler<EventAction> value) {
onEventDockRequestProperty().set(value);
}
public final ObjectProperty<EventHandler<EventAction>> onEventDockRequestProperty() {
if (onEventDockRequest == null) {
onEventDockRequest = new ObjectPropertyBase<EventHandler<EventAction>>() {
#Override protected void invalidated() {
setEventHandler(EventAction.DOCK_REQUEST, get()); // here error
}
#Override public Object getBean() {
return DTab.this;
}
#Override public String getName() {
return "onEventDockRequest";
}
};
}
return onEventDockRequest;
}
}
I have a TreeTableView which allows multiselection. I got a ContextMenu for editing or deleting that selected items.
Delete and edit should only be enabled if there is at least one selection.
final BooleanBinding isTableSelectionEmpty = Bindings.isEmpty(this.table.getSelectionModel().getSelectedItems());
this.menuItemDelete.disableProperty().bind(isTableSelectionEmpty);
That is working as expected.
But now I have dependencies on different values of the selected rows. Like for example that the row is system-mandatory and should not be deleted.
I tried the following but it is not working
final BooleanBinding invalidSelection = Bindings.and(Bindings.isEmpty(tableSelection),
Bindings.isNotEmpty(tableSelection.filtered(item -> {
this.logger.trace("filtering :" + item);
return item.getValue().getSystemProperty().get();
})));
this.menuItemDelete.disableProperty().bind(invalidSelection);
Not even the debug-trace is printed and the value of the binding is always false (thus enabling the menu item). Now I am a bit lost. Where is my mistake?
FilteredList relies on a correct ListIterator, but currently there is a bug in the ListIterator the selectedItems list in MultipleSelectionModelBase. This prevents the filtering to properly work. To fix this you could create a ObservableList implementation delegating everything but the ListIterator creation to a source ObservableList. Most IDEs have a functionality to generate this kind of methods automatically, reducing the amount of work to a minimum (e.g.in NetBeans: Generate -> Delegate Method).
public class ObservableListIteratorFix<T> implements ObservableList<T> {
private final ObservableList<T> list;
public ObservableListIteratorFix(ObservableList<T> list) {
this.list = list;
}
#Override
public void addListener(ListChangeListener<? super T> listener) {
list.addListener(listener);
}
#Override
public void removeListener(ListChangeListener<? super T> listener) {
list.removeListener(listener);
}
#Override
public boolean addAll(T... elements) {
return list.addAll(elements);
}
...
private class CustomListIterator implements ListIterator<T> {
private final ListIterator<T> iterator;
private int index;
public CustomListIterator(int index) {
this.iterator = list.listIterator(index);
this.index = index;
}
#Override
public boolean hasNext() {
return iterator.hasNext();
}
#Override
public T next() {
T t = iterator.next();
index++;
return t;
}
#Override
public boolean hasPrevious() {
return iterator.hasPrevious();
}
#Override
public T previous() {
T t = iterator.previous();
index--;
return t;
}
#Override
public int nextIndex() {
return index;
}
#Override
public int previousIndex() {
return index-1;
}
#Override
public void remove() {
iterator.remove();
}
#Override
public void set(T e) {
iterator.set(e);
}
#Override
public void add(T e) {
iterator.add(e);
}
#Override
public void forEachRemaining(Consumer<? super T> action) {
iterator.forEachRemaining(action);
}
}
#Override
public ListIterator<T> listIterator() {
return listIterator(0);
}
#Override
public ListIterator<T> listIterator(int index) {
return new CustomListIterator(index);
}
#Override
public FilteredList<T> filtered(Predicate<T> predicate) {
return new FilteredList<>(this, predicate);
}
...
This allows you to use the class as wrapper the selectedItems which should fix the filtering...
new ObservableListIteratorFix<>(tableSelection).filtered(...)
I need to order the list of items based on a field say starredAt
I am loading the data in the recyclerview from Realm DB using RealmRecyclerView by thorbenprimke
The field changes it value on user's action i.e when user presses star button the item should be moved to top.
For this I am just updating the starredAt field of the object.
The items are already sorted by starredAt so realm loads the updated list but it randomly adds one more item to the recyclerview.
CheatSheet.java
public class CheatSheet extends RealmObject {
#PrimaryKey
private String id;
private RealmList<Item> items;
private String title;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public RealmList<Item> getItems() {
return items;
}
public void setItems(RealmList<Item> items) {
this.items = items;
}
}
Item.java
public class Item extends RealmObject {
#PrimaryKey
private String id;
private String description;
private Date starredAt;
public Item() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getStarredAt() {
return starredAt;
}
public void setStarredAt(Date starredAt) {
this.starredAt = starredAt;
}
}
CheatSheetActivity.java
public class MainActivity extends AppCompatActivity {
RealmRecyclerView revItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setData();
}
private void setData() {
rvItems = (RealmRecyclerView) findViewById(R.id.rev_items);
RealmResults<Item> items = Realm.getDefaultInstance().where(CheatSheet.class)
.equalTo("id", "some-id").findFirst().getItems()
.where()
.findAllSorted("starredAt", Sort.DESCENDING);
ItemRealmListAdapter itemRealmListAdapter =
new ItemRealmListAdapter(this, items,
true, true);
rvItems.setAdapter(itemRealmListAdapter);
}
ItemRealmListAdapter.java
public class ItemRealmListAdapter extends RealmBasedRecyclerViewAdapter<Item,
ItemRealmListAdapter.ItemViewHolder> {
RealmResults<Item> mItems;
public ItemRealmListAdapter(Context context, RealmResults<Item> realmResults,
boolean automaticUpdate, boolean animateResults) {
super(context, realmResults, automaticUpdate, animateResults);
this.mItems = realmResults;
}
#Override
public ItemViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int i) {
return new ItemViewHolder(LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_layout_cs_text, viewGroup, false));
}
public Item getItem(int position) {
return mItems.get(position);
}
#Override
public void onBindRealmViewHolder(ItemViewHolder itemViewHolder, int position) {
itemViewHolder.txtBody.setText(getItem(position).getDescription());
if (getItem(position).getStarredAt() != null) {
itemViewHolder.imvStar.setImageResource(R.drawable.ic_star_yellow);
}
itemViewHolder.imvStar.setOnClickListener(v -> handleStarClick(v,position));
}
private void handleStarClick(View v, int position) {
if (getItem(position).getStarredAt() != null) {
((ImageView) v).setImageResource(R.drawable.ic_star);
CheatSheetStorage.unStarItem("some-id", getItem(position));
} else {
((ImageView) v).setImageResource(R.drawable.ic_star_yellow);
CheatSheetStorage.starItem("some-id", getItem(position));
}
}
public static class ItemViewHolder extends RealmViewHolder {
#Bind(R.id.txt_cheat_sheet)
TextView txtBody;
#Bind(R.id.img_star)
ImageView imvStar;
public ItemViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
CheatSheetStorage.java
public class CheatSheetStorage {
public static void unStarItem(String cheatSheetId, Item item) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
CheatSheet cheatSheet = getCheatSheetById(cheatSheetId);
Item itemDB = cheatSheet.getItems().where().equalTo("id", item.getId()).findFirst();
itemDB.setStarredAt(null);
realm.commitTransaction();
}
public static void starItem(String cheatSheetId, Item item) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
CheatSheet cheatSheet = getCheatSheetById(cheatSheetId);
Item itemDB = cheatSheet.getItems().where().equalTo("id", item.getId()).findFirst();
itemDB.setStarredAt(new Date());
realm.commitTransaction();
}
}
Please refer following screenshots for clearer idea :
Screenshot before starring
Screenshot after starring the sixth item
#Rohan-Peshkar - You will have to provide a animateExtraColumnName value to the adapter. For the animations, the adapter keeps track of the items and since that item's id doesn't change, the list isn't updated. With an additional column (in your case that should be the starredAt column - as long as it is stored as an Integer), the diffing algorithm will detect a change and the order is updated.
For reference: https://github.com/thorbenprimke/realm-recyclerview/blob/2835a543dce20993d8f98a4f773fa0e67132ce52/library/src/main/java/io/realm/RealmBasedRecyclerViewAdapter.java#L177
You can also check out the MainActivity in the example folder. The example changes a row's text from "ABC" to "Updated ABC" and the list recognizes the change because both the primary key and the quote field are used to basically create a composite key for diffing purposes.
I have created a custom ObservableList implementation for a list of TreeItems. My custom implementation can listen to various notifications from inside my app (using OSGi EventAdmin), and update itself accordingly. I then expect its consumer (a TreeView widget) to be updated with the changes to the list. However, I can't see how to notify the consumer.
In the ObservableList subclass I am implementing addListener(ListChangeListener), which I would expect to get called when the object is added to the widget. However it is never called; I have no listeners thus no apparent way to notify anyone when the list changes. I must be missing something.
Here is a snippet from my TreeItem implementation, which returns an instance of my ObservableList in response to a getChildren call:
#Override
public ObservableList<TreeItem<DataObject>> getChildren() {
if (needChildren) {
needChildren = false;
children = new MyObservableList();
}
return children;
}
Here is an abridged version of my custom ObservableList implementation, which simply wraps an FXCollections.observableArrayList and adds an OSGi event handler. I listen to changes on the internal list so that I can pass those changes on to my listeners.
public class MyObservableList implements ObservableList<TreeItem<DataObject>>, EventHandler {
private List<ListChangeListener<? super TreeItem<DataObject>>> changeListeners = new ArrayList<>();
private List<InvalidationListener> invalidationListeners = new ArrayList<>();
private ObservableList<TreeItem<DataObject>> theList;
private int size;
public MyObservableList() {
theList = FXCollections.observableArrayList();
theList.addListener(new ListChangeListener<TreeItem<DataObject>>() {
#Override
public void onChanged(Change<? extends TreeItem<DataObject>> change) {
fireValueChangedEvent(change);
}
});
}
#Override
public int size() {
return theList.size();
}
#Override
public boolean isEmpty() {
return (size == 0);
}
#Override
public boolean contains(Object o) {
return theList.contains(o);
}
#Override
public Iterator iterator() {
return theList.iterator();
}
#Override
public boolean remove(Object o) {
return theList.remove(o);
}
#Override
public boolean addAll(Collection c) {
return theList.addAll(c);
}
#Override
public boolean addAll(int index, Collection c) {
return theList.addAll(index, c);
}
#Override
public void clear() {
theList.clear();
}
#Override
public TreeItem<DataObject> get(int index) {
return theList.get(index);
}
#Override
public int indexOf(Object o) {
return theList.indexOf(o);
}
#Override
public int lastIndexOf(Object o) {
return theList.lastIndexOf(o);
}
#Override
public ListIterator listIterator() {
return theList.listIterator();
}
#Override
public ListIterator listIterator(int index) {
return theList.listIterator(index);
}
#Override
public List<TreeItem<DataObject>> subList(int fromIndex, int toIndex) {
return theList.subList(fromIndex, toIndex);
}
#Override
public Object[] toArray(Object[] a) {
return theList.toArray(a);
}
#Override
public void addListener(ListChangeListener<? super TreeItem<DataObject>> listChangeListener) {
changeListeners.add(listChangeListener);
}
#Override
public void removeListener(ListChangeListener<? super TreeItem<DataObject>> listChangeListener) {
changeListeners.remove(listChangeListener);
}
#Override
public boolean addAll(TreeItem<DataObject>... treeItems) {
return theList.addAll(treeItems);
}
#Override
public boolean setAll(TreeItem<DataObject>... treeItems) {
return theList.setAll(treeItems);
}
#Override
public boolean setAll(Collection<? extends TreeItem<DataObject>> treeItems) {
return theList.setAll(treeItems);
}
#Override
public boolean removeAll(TreeItem<DataObject>... treeItems) {
return theList.removeAll(treeItems);
}
#Override
public boolean retainAll(TreeItem<DataObject>... treeItems) {
return theList.retainAll(treeItems);
}
#Override
public void remove(int i, int i2) {
theList.remove(i, i2);
}
#Override
public Object[] toArray() {
return theList.toArray();
}
#Override
public boolean add(TreeItem<DataObject> dataObjectTreeItem) {
return theList.add(dataObjectTreeItem);
}
#Override
public boolean containsAll(Collection<?> c) {
return theList.containsAll(c);
}
#Override
public boolean removeAll(Collection<?> c) {
return theList.removeAll(c);
}
#Override
public boolean retainAll(Collection<?> c) {
return theList.retainAll(c);
}
#Override
public TreeItem<DataObject> set(int index, TreeItem<DataObject> element) {
return theList.set(index, element);
}
#Override
public void add(int index, TreeItem<DataObject> element) {
theList.add(index, element);
}
#Override
public TreeItem<DataObject> remove(int index) {
return theList.remove(index);
}
#Override
public void addListener(InvalidationListener invalidationListener) {
invalidationListeners.add(invalidationListener);
}
#Override
public void removeListener(InvalidationListener invalidationListener) {
invalidationListeners.remove(invalidationListener);
}
private void fireValueChangedEvent(ListChangeListener.Change<? extends TreeItem<DataObject>> change) {
for (ListChangeListener<? super TreeItem<DataObject>> listener : changeListeners) {
listener.onChanged(change);
}
}
#Override
public void handleEvent(Event event) {
// Here I add or remove TreeItem<DataObject> instances to the list based on event.
//
// At this point, onChanged() gets called above in my listener, but my changeListeners list is empty. There is
// no one to pass the Change on to.
}
}
Thanks for any help.
I figured out what's going on here after looking through the JavaFX source.
It turns out that the listening on the ObservableList of children is all set up in the TreeItem itself, not the TreeView as I had somehow assumed. This means that any subclass of TreeView that overrides getChildren() must call super.getChildren() and add its children to the resulting list. This means that using a custom ObservableList implementation is not possible as TreeItem is hardcoded to use FXCollections.observableArrayList() to create the list.
I am taking a different approach to this now where I call super.getChildren(), add my children, and then instantiate another object that holds a reference to that list and does all of my app's event handling business, operating on the list as needed. So my getChildren() method looks something like this now.
private MyEventHandler eventHandler;
private ObservableList<TreeItem<DataObject>> children;
#Override
public ObservableList<TreeItem<DataObject>> getChildren() {
if (needChildren) {
needChildren = false;
children = super.getChildren();
eventHandler = new MyEventHandler(children); // handles app events
}
return children;
}
I have built a custom component button, but somehow the action is not invoked. When debugging the getAction-Method within the component and invoking the supplied MethodeExpression the Bean-Method is called as expected. But due to some reason, the Expression is not invoked when pressing the button in the browser.
Is there some kind of additional Interface necessary to pass the action to the embedded button-component?
Any help is very appreciated since I am stuck at this issue for some days now
MyClass:
public class MyClass extends UIPanel implements SystemEventListener
{
private UIForm form;
private HtmlCommandButton buttonOk;
public MyClass()
{
FacesContext context = getFacesContext();
UIViewRoot root = context.getViewRoot();
root.subscribeToViewEvent(PostAddToViewEvent.class, this);
}
#Override
public void processEvent(SystemEvent event)
{
this.form = new UIForm();
this.buttonOk = new HtmlCommandButton();
this.buttonOk.setId("okButtonId");
this.buttonOk.setActionExpression(getAction());
this.buttonOk.setValue("OK");
this.form.getChildren().add(this.buttonOk);
getChildren().add(this.form);
}
private enum PropertyKeys
{
action, text, titel
}
public MethodExpression getAction()
{
return (MethodExpression) getStateHelper().eval(PropertyKeys.action);
}
public void setAction(MethodExpression actionExpression)
{
getStateHelper().put(PropertyKeys.action, actionExpression);
}
public String getText()
{
return (String) getStateHelper().eval(PropertyKeys.text);
}
public void setText(String text)
{
getStateHelper().put(PropertyKeys.text, text);
}
public String getTitel()
{
return (String) getStateHelper().eval(PropertyKeys.titel);
}
public void setTitel(String titel)
{
getStateHelper().put(PropertyKeys.titel, titel);
}
#Override
public void encodeAll(FacesContext context) throws IOException
{
ResponseWriter writer = context.getResponseWriter();
writer.startElement(HTML.DIV_ELEM, this);
writer.writeText(getText(), null);
this.form.encodeAll(context);
writer.endElement(HTML.DIV_ELEM);
}
#Override
public void encodeChildren(FacesContext context) throws IOException
{
}
#Override
public boolean isListenerForSource(Object source)
{
return (source instanceof MyClass);
}
}
MyClassHandler:
public class MyClassHandler extends ComponentHandler
{
public MyClassHandler(ComponentConfig config)
{
super(config);
}
#SuppressWarnings("rawtypes")
#Override
protected MetaRuleset createMetaRuleset(Class type)
{
return super.createMetaRuleset(type).addRule(new MethodRule("action", String.class, new Class[] { ActionEvent.class }));
}
}
myView Method:
...
public String myMethod()
{
System.err.println("myMethod");
return "/some/path/yadayada.xhtml";
}
...
MyView.xhtml
<myTag action="#{myView.myMethod}" id="id1" titel="bla" text="bleh" />
Exdending UICommand is enough, since you only want one action to be executed.
You have to provide two additional MethodExpressions via the tag-attributes and within the decode-method you can check which button has been pressed and redirect the particular MethodExpression to the standard-action provided by UICommand. This way, you dont have to worry about the legacy-interface ActionSource, or how Events are broadcasted.
public void decode(FacesContext contex)
{
Map<String,String> map = context.getExternalContext.getRequestParameterMap();
// your rendered buttons need a name you check for
final boolean okPressed = map.containsKey( getClientId + ":ok" );
final boolean cancelPressed = map.containsKey( getClientId + ":cancel" );
if(okPressed || cancelPressed)
{
MethodExpression exp = null;
if(okPressed)
{
exp = getActionOk();
}
else
{
exp = getActionCancel();
}
// redirect to standard action
setActionExpression(exp);
queueEvent(new ActionEvent(this));
}
}
In order to make use of of this you need two attributes (actionOk and actionCancel) which use Method Expressions (setter and getter). Those have to be configured by a ComponentHandler as you did for the action-attribute.