Deleting multiple rows from TableView - javafx

#FXML
private void handleDeleteAction(ActionEvent event) {
for (Transaction transaction : transactionsTable.getSelectionModel().getSelectedItems()) {
RemoveTransactionsCommand removeTransactionsCommand = new RemoveTransactionsCommand(transaction, transactionsTable.getSelectionModel().getSelectedItems(), data);
commandRegistry.executeCommand(removeTransactionsCommand);
}
}
Why it won't work in case where I select more than one row, I mean it delete one row (sometimes two, but can't find what decide about it)
Here is command implementation:
public class RemoveTransactionsCommand implements Command {
private ObservableList<Transaction> selectedItems;
private Transaction transactionToRemove;
private Account account;
public RemoveTransactionsCommand(Transaction transactionToRemove, ObservableList<Transaction> selectedItems, Account account) {
this.account = account;
this.transactionToRemove = transactionToRemove;
this.selectedItems = selectedItems;
}
#Override
public String getName() {
int presentSize = selectedItems.size();
return presentSize + "transaction/s removed";
}
#Override
public void execute() {
account.removeTransaction(transactionToRemove);
}
}
And removal command:
public void removeTransaction(Transaction transaction) {
this.transactions.remove(transaction);
}
Additionally I wanted to know the size of actual delete operation but what I pass as a 2nd argument isn't static and for example when every row is deleted it will be 0.
Any advices how to improve it?
Full project can be found here

The problem is that the selected items list may change when the list of items in the table changes. So the list gets modified while you are trying to iterate through it.
You should create a copy of the list of selected items and iterate through it instead:
#FXML
private void handleDeleteAction(ActionEvent event) {
List<Transaction> selectedTransactions = new ArrayList<>(transactionTable.getSelectionModel().getSelectedItems());
for (Transaction transaction : selectedTransactions) {
RemoveTransactionsCommand removeTransactionsCommand = new RemoveTransactionsCommand(transaction, selectedTransactions, data);
commandRegistry.executeCommand(removeTransactionsCommand);
}
}
(and change the type of selectedItems in RemoveTransactionsCommand to List<Transaction>).

Related

How do I show an empty preferences category in Android?

I have a use case in which I have to show an empty preference category. I have tries setting the visibility flag value to true but the preference category does not seem to be displayed.
This is my logic
public class MyFragment extends PreferenceFragmentCompat {
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
final PreferenceScreen screen = getPreferenceScreen();
// ViewModelProviderFactory instance is injected using Dagger.
NotificationsViewModel viewModel = ViewModelProviders.of(Objects.requireNonNull(getActivity()), viewModelProviderFactory)
.get(NotificationsViewModel.class);
final SwitchPreferenceCompat notificationsEnabledPreference = preferencesFactory.create(
screen,
SwitchPreferenceCompat.class,
NotificationsViewModel.MASTER_SWITCH_PREFERENCE_KEY,
R.string.preferences_enable_notifications);
final PreferenceCategory orderUpdatesCategory = preferencesFactory.create(
screen,
PreferenceCategory.class,
NotificationsViewModel.ORDER_UPDATES_CATEGORY_PREFERENCE_KEY,
R.string.notifications_group_order_updates,
NotificationsViewModel.MASTER_SWITCH_PREFERENCE_KEY
);
orderUpdatesCategory.setVisible(true);
. . . . Some other logic ...
}
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootTag)
{
final PreferenceManager preferenceManager =
getPreferenceManager();
final PreferenceScreen screen =
preferenceManager.createPreferenceScreen(
preferenceManager.getContext());
setPreferenceScreen(screen);
}
}
Helper function PreferenceFactory#create
public <T extends Preference> T create(#NonNull PreferenceGroup parent, #NonNull Class<T> classInstance,
#Nullable String key, #StringRes int titleResId, #NonNull String dependencyKey)
{
final T preference = create(classInstance);
if (parent.addPreference(preference))
{
preference.setKey(key);
preference.setTitle(titleResId);
preference.setDependency(dependencyKey);
}
return preference;
}
How can I show the empty preference category?
Not sure what preferencesFactory is since it's not initialized in the code above and is not part of the PreferenceFragmentCompat API.
I was able to do what you wanted to do (display an empty Category) by adding categories and preferences to a PreferenceScreen object, as shown in the sample code here:
https://developer.android.com/guide/topics/ui/settings/programmatic-hierarchy

How to make always visible headers of rows in RowsFragment

Here is what i have now.
Simply my RowsFragment is look like this,
public static class SampleFragmentB extends RowsFragment {
private final ArrayObjectAdapter mRowsAdapter;
public SampleFragmentB() {
mRowsAdapter = new ArrayObjectAdapter(new ShadowRowPresenterSelector());
setAdapter(mRowsAdapter);
setOnItemViewClickedListener(new OnItemViewClickedListener() {
#Override
public void onItemClicked(
Presenter.ViewHolder itemViewHolder,
Object item,
RowPresenter.ViewHolder rowViewHolder,
Row row) {
Toast.makeText(getActivity(), "Implement click handler", Toast.LENGTH_SHORT)
.show();
}
});
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createRows();
getMainFragmentAdapter().getFragmentHost().notifyDataReady(getMainFragmentAdapter());
}
private void createRows() {
String json = Utils.inputStreamToString(getResources().openRawResource(
R.raw.page_row_example));
CardRow[] rows = new Gson().fromJson(json, CardRow[].class);
for (CardRow row : rows) {
mRowsAdapter.add(createCardRow(row));
}
}
private Row createCardRow(CardRow cardRow) {
PresenterSelector presenterSelector = new CardPresenterSelector(getActivity());
ArrayObjectAdapter adapter = new ArrayObjectAdapter(presenterSelector);
for (Card card : cardRow.getCards()) {
adapter.add(card);
}
HeaderItem headerItem = new HeaderItem(cardRow.getTitle());
return new CardListRow(headerItem, adapter, cardRow);
}
}
This is what i exactly want to do,
So I want to make always visible Header of each row without selecting into or focusing into RowsFragment. I'am using leanback v24 to add multiple rows into each header item.
You might want to check the Sofa library for Android TV that extends the Leanback library capabilities by offering a set of more powerful features. In BrowseSupportFragment, mRowsFragment.setExpand should be true. Additional reference: how to always show headers in RowsFragment

ListChangeListener.Change: how to properly handle updated and permutated items

From the JavaDoc:
ObservableList theList = ...;
theList.addListener(new ListChangeListener<Item>() {
public void onChanged(Change<tem> c) {
while (c.next()) {
if (c.wasPermutated()) {
for (int i = c.getFrom(); i < c.getTo(); ++i) {
//permutate
}
} else if (c.wasUpdated()) {
//update item
} else {
for (Item remitem : c.getRemoved()) {
remitem.remove(Outer.this);
}
for (Item additem : c.getAddedSubList()) {
additem.add(Outer.this);
}
}
}
}
});
}
Adding and removing items is straight forward, but what about //update item and // permutate?
How do I know which items have been permutated by which other items?
What does update mean exactly? Is it just adding the same item to the list again?
And what about
for (Item remitem : c.getRemoved()) {
remitem.remove(Outer.this);
}
or (Item additem : c.getAddedSubList()) {
additem.add(Outer.this);
}
What does Outer.this mean?
How do I know which items have been permutated by which other items?
The change has a getPermutation() method that describes how the elements were permuted.
What does update mean exactly?
A list is updated if properties belonging to an element change, though the same elements remain in the list (in the same order). For example, given a class
public class Item {
private final IntegerProperty value = new SimpleIntegerProperty();
public final IntegerProperty valueProperty() {
return value ;
}
public final int getValue() {
return valueProperty().get();
}
public final void setValue(int value) {
valueProperty().set(value);
}
public Item(int value) {
setValue(value);
}
}
calling setValue() on an element of the list may fire an update. Note that the documentation states that updates are "optional" and may not be fired by all lists. Specifically, to obtain a list that fires updates, create it with an extractor:
ObservableList<Item> list = FXCollections.observableArrayList(
item -> new Observable[] {item.valueProperty()});
list.addAll(new Item(1), new Item(2), new Item(3));
list.addListener((Change<? extends Item> c) -> {
while (c.next()) {
if (c.wasUpdated()) {
System.out.println("Items from "+c.getFrom()+" to "+c.getTo()+" changed");
}
}
});
list.get(1).setValue(42);
The last line of code doesn't change which elements are in the list, or which order they are in, but changes a property of one of the elements. So this change will fire an update.
What does Outer.this mean?
It is simply a reference to the current object of the surrounding class (which is assumed to have class name Outer); i.e. not the current object of the anonymous inner class implementation of ListChangeListener. See What is the difference between Class.this and this in Java (and many others). I think the context for the code snippet in the documentation is supposed to be a class that implements ObservableList and maintains its own ObservableList instance (decorator pattern). It observes the list instance and updates itself to keep in sync with it.

Concurrent task updating complex object JavaFX - swingworker equivalent?

I want to run a task in background updating intermediate results in the view.I am trying to implement MVC JavaFX application. The task is defined in the Model.
I want to send to the main threath partial results in order to show them in the View.
I use updateValue() to do so. Also, I define object property and a listener in the controller.
My problem: The method changed() from the listener, is not being fired each time that updateValue() is executed in the Task. Why? How can I force it to do this?.
I have not found much complex examples.
What I have so far:
Model.cpp
ComplexObject _complexO;
public Task<ComplexObject> getModelTask() {
return new Task<ComplexObject>() {
#Override
protected ComplexObject call() throws Exception {
int numberOfFiles = 0;
boolean filesToRead = true;
while (filesToRead){
// ....
_complexO = new ComplexObject();
try{
//..
if(f.exists()){
_complexO.initialize();
numberOfScans ++;
}
else{
_complexO.initializeToNull();
}
String stringNumber = Converter.toString(numberOfFiles);
updateMessage(stringNumber);
updateValue(_complexO );
} catch(Exception ex){
ex.printStackTrace();
_complexO = null;
return _complexO;
}
filesToRead = areThereFilesToRead();
}
return _complexO;
}
};
}
Controller.cpp
...
Task< ComplexObject> task = _model.getModelTask();
_AJavaFXTextField.textProperty().bind(task.messageProperty());
_AJavaFXTextField.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue observable, String oldValue, String newValue) {
System.out.println("Success with messageProperty!!" + newValue);
}
});
SimpleObjectProperty<ComplexObject> complexObjectProperty = new SimpleObjectProperty<>();
complexObjectProperty.bind(task.valueProperty());
complexObjectProperty.addListener(new ChangeListener<ComplexObject>(){
#Override
public void changed(ObservableValue<? extends ComplexObject> observable, ComplexObject oldValue, ComplexObject newValue) {
if(newValue.data == null ) {
System.out.println("value is new!!! " + scansNumber);
}
else if(newValue.isValid()){
System.out.println("I want to plot newValue data here");
}
}
});
Thread th= new Thread(task);
System.out.println("call TASK");
th.start();
}
My questions/conclusions here:
How to force to all times that I execute in the task updateValue() to really execute the listener - so execute the code where I want to plot data.
Why it is more times fire the bind for the messageProperty than the valueProperty? - it should be the same number of times.
Why I find that the code of the listener is fired more times when debug mode than normal execution?
Any recomendation of good sources about this topic (from a complex point of view) would be great.
I am looking from something in JavaFX to replace SwingWorker.
What I really whant at the end: To return a list of complexObjects from the task, and ideally, updateValue() would send the objects one per one (partial results)
I have followed:
https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html
Thanks very much for any contribuction
Task only guaranties that a value passes to updateValue or a value passed later will be set to the value property. This is done to increase performance of the application thread by limiting the number of changes the listeners are notified of.
Why it is more times fire the bind for the messageProperty than the valueProperty? - it should be the same number of times.
As described above there simply is no guaranty about the number of updates.
Why I find that the code of the listener is fired more times when debug mode than normal execution?
In general debugging makes your program smaller. The smaller the update frequency from the thread of your Task, the smaller the number of updates between the times the Task class updates the properties and the smaller the number of skipped. (The updates are probably executed every frame or every few frames.) If you even use a break-point/stepper in the task, you probably make the Task extremely slow while the application thread runs at normal speed.
It should be easy enough to implement publish on your own by using a List to buffer the updates
public abstract class JavaFXWorker<S, T> extends Task<S> {
private List<T> chunks = new ArrayList<>();
private final Object lock = new Object();
private boolean chunkUpdating = false;
protected final void publish(T... results) {
synchronized (lock) {
chunks.addAll(Arrays.asList(results));
if (!chunkUpdating) {
chunkUpdating = true;
Platform.runLater(() -> {
List<T> cs;
synchronized (lock) {
cs = chunks;
// create new list to not unnecessary lock worker thread
chunks = new ArrayList<>();
chunkUpdating = false;
}
try {
process(cs);
} catch (RuntimeException ex) {
}
});
}
}
}
protected void process(List<T> chunks) {
}
}
Sample use
#Override
public void start(Stage primaryStage) {
ListView<Integer> lv = new ListView<>();
Button btn = new Button("Run");
btn.setOnAction((ActionEvent event) -> {
JavaFXWorker<Void, Integer> worker = new JavaFXWorker<Void, Integer>() {
#Override
protected Void call() throws Exception {
final int maxCount = 100;
Random random = new Random();
int breakIndex = random.nextInt(maxCount-1)+1;
for (int i = 0; i < breakIndex; i++) {
publish(i);
}
// some break simulating a part long part of the task with no updates
Thread.sleep(3000);
for (int i = breakIndex; i <= maxCount; i++) {
publish(i);
}
return null;
}
#Override
protected void process(List<Integer> chunks) {
lv.getItems().addAll(chunks);
}
};
new Thread(worker).start();
});
Scene scene = new Scene(new VBox(btn, lv));
primaryStage.setScene(scene);
primaryStage.show();
}

Test case for fragment in android

In my application, I have multiple fragments on a single activity. Now I want to write a test case to check if these fragments are loading properly. To begin with, I passed some touch event to scroll to a particular fragment and then I am trying to fetch the name of this fragment. Below is my code for the test case:-
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity>
{
MainActivity mMainActivity;
ActionBar tactionbar;
Fragment tFragment;
public static final int TEST_POSITION = 2;
private static String mSelection ;
private int mPos = 0;
public MainActivityTest()
{
super(MainActivity.class);
}
protected void setUp() throws Exception
{
super.setUp();
mMainActivity = (MainActivity) getActivity();
tactionbar = mfoneclay.getActionBar();
}
public void testPreConditions()
{
assertNotNull(mMainActivity);
assertNotNull(tactionbar);
}
public void testFragmentUI()
{
mMainActivity.runOnUiThread(
new Runnable(){
public void run()
{
mMainActivity.getCurrentFocus();
}
});
for (int i = 1; i <= TEST_POSITION; i++)
{
this.sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
mPos = tactionbar.getSelectedNavigationIndex();
}
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
mSelection = (String)tactionbar.getTabAt(mPos).getText();
String resultText = "Exclusive";
assertEquals(resultText,mSelection);
}
}
Here, "Exclusive" is the name of one of my tab to which I am navigating to via the touch event. Now, while running the test case, I can see that it is properly navigating to the "Exclusive" fragment, but the result shows the value of the msection variable as the name of the activity and not the fragments name. What am I doing wrong?
Got the solution. It was so stupid of me to use the wrong components to fetch the fragment. It turns out that I have to use "ViewPager" to fetch the fragments.

Resources