How to dismiss Vaadin 8 confirmation dialog while performing lengthy operation - java

I have Vaadin 8 confirmation code there it performs a long lasting task. I would like to dismiss the dialog and show a progress bar:-
ConfirmDialog.show(UI.getCurrent(), "Dynamic Report warning caption",
"More than " +rows+ " rows in this table. It will take long time to generate this report.\nDo you still want to continue? ",
SalkkuTM.getI18N("PortfolioManagementMenuBar.deleteData.confirmDialog.yes.caption"),
SalkkuTM.getI18N("PortfolioManagementMenuBar.deleteData.confirmDialog.no.caption"), new ConfirmDialog.Listener() {
public void onClose(ConfirmDialog dialog) {
ProgressBar bar = new ProgressBar();
bar.setIndeterminate(true);
if (dialog.isConfirmed()) {
layout.addComponent(bar);
// this method does a long lasting task.
dynamicReportParameterGenerator();
bar.setVisible(false);
}
}
});
I would like to dismiss this dialog as soon as user select yes. And I would like to show an Indeterminate progress bar. I couldn't manage it. How to do it? Please let me know how can I do it?

This is FAQ stuff, you need to use Server Push and update progress bar in background thread. There is a blog post and video about it here
https://vaadin.com/blog/community-answer-processing-a-file-in-a-background-thread
There is a good discussion about the topic here too
Update Vaadin Progressbar with push asynchronously
And I would like to show an Indeterminate progress bar.
This means that you can simplify things a little. If you do not want to update progress bar during the operation, you just need to put progress bar in UI in Indeterminate mode and once complete, update the UI again.

Related

ProgressMonitorDialog and user interaction

I'm using a ProgressMonitorDialog with an IRunnableWithProgress to read a file in the background.
If an error occurs during this file processing (the data isn't what I'm expecting), I would like to ask the user if s/he wants to continue with the next line.
The problem is now that in order to ask the user if they want to continue, I'd have to show a dialog. Showing a dialog from a non-UI thread involves using Display.asyncExec() or Display.syncExec(). In order to return the result (user decision) to the background thread I'd have to use a callback.
Now, the problem is, that when I get the result in a callback in the background thread, how can I continue reading the file? Or, in other words, how can I pause the execution of the background thread until the feedback returns and then continue it?
I'm open to suggestions and willing to restructure my environment to accommodate this behaviour.
Dislay.syncExec blocks the thread you call it from so you can do something like:
final int[] result = new int[] {0};
display.syncExec(new Runnable() {
public void run() {
Shell shell = display.getActiveShell();
MessageDialog dialog = .... your message dialog
result[0] = dialog.open();
}
});
... dialog return code in result[0]
(heavily adapted from code in org.eclipse.equinox.internal.p2.ui.ValidationDialogServiceUI)

Dialog box that disappear automatically

I am developing a application. I want when I exit it, a message box appears having message, for example, "Thanks for using Soft Attendance". Then disappear automatically say after few seconds.
I have write code for this as following:
public void windowClosing(WindowEvent e){
int whichOption;
whichOption=JOptionPane.showConfirmDialog(f1,"Are you Serious?","Soft Attendence",JOptionPane.YES_NO_OPTION);
if(whichOption==JOptionPane.YES_OPTION){
f1.dispose();
JOptionPane.showMessageDialog(null,"Thanks for using Soft Attendence");
}
}
When i click on exit button a confirmation dialog box appear and after clicking yes button
application is exited and a another message box appears.
Now I have two questions in my mind :
First question is I have dispose application already and then message box is shown. Is it fine to show message box after its parent is killed?
When second message box appears I don't want to click on "OK" button. I want it should automatically disappear. So my second question is How to shown message box that disappear automatically after some time?
Is it fine to show message box after its parent is killed?
I think this would not be a ideal case. But you can open dialog if the parentComponent has no Frame JOptionPane.showConfirmDialog(null,"...");
How to shown message box that disappear automatically after some time?
Auto disposable you can get it by a trick, you can create a SwingWorker which normally performs GUI-interaction tasks in a background thread. Set a timer and which will execute after time period and close your dialog explicitly.
class AutoDisposer extends SwingWorker<Boolean, Object> {
#Override
public String doInBackground() {
try{
JOptionPane.getRootFrame().dispose();
return Boolean.True;
}catch(Exception ex){...}
return Boolean.False;
}
}
...
new Timer(seconds * 1000, autoDisposer).start();

How can I test the result of a button click that changes the Activity's view asynchronously?

I am trying to write some Activity tests for an app, and one particular scenario that I want to test is that when I click a certain button, the Activity view updates accordingly. However, clicking the button causes a somewhat long running asynchronous task to start and only after that task is completed does the view change.
How can I test this? I'm currently trying to use the ActivityInstrumentationTestCase2 class to accomplish this, but am having trouble figuring out how to have the test 'wait' until the asynchronous part of the button click task is complete and the view updates.
The most common and simplest solution is to use Thread.sleep():
public void testFoo() {
TextView textView = (TextView) myActivity.findViewById(com.company.app.R.id.text);
assertEquals("text should be empty", "", textView.getText());
// simulate a button click, which start an AsyncTask and update TextView when done.
final Button button = (Button) myActivity.findViewById(com.company.app.R.id.refresh);
myActivity.runOnUiThread(new Runnable() {
public void run() {
button.performClick();
}
});
// assume AsyncTask will be finished in 6 seconds.
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
assertEquals("text should be refreshed", "refreshed", textView.getText());
}
Hope this helps.
If you're using Eclipse, you could use the debugger by setting a breakpoint in the code that updates the view. You could also set some breakpoints in the long running task to watch and ensure that all your code is executing.
An alternative, write some log or console outputs in your long-running task and the view updater code, so you can see the progress without interrupting the thread by a debugger.
As a piece of advise, if its a long-running process, you should be showing a progress bar of some description to the user, so they aren't stuck there thinking "Is something happening?". If you use a progress bar with a maximum value, you can update it in your long-running task as it is running, so the user can see the activity going from 10% to 20%... etc.
Sorry if you were expecting some kind of jUnit-specific answer.
I ended up solving this by using the Robotium library's Solo.waitForText method that takes a string and timeout period and blocks until either the expected text appears or the timeout occurs. Great UI testing library.

Showing Dialog after another Dialog: HANGS

I have a really weird problem, which I am unable to debug so far...
Thing is...my app needs to download something to work. So in the beginning of the onCreate() method, I check if that something is already downloaded. If not, I pop a dialog up asking the user to download it.
if (!isInstalled) {
showDialog(DIALOG_INSTALL);
} else {
start();
}
Where start() method performs some other action. Now, that showDialog calls this:
builder = new AlertDialog.Builder(MyApp.this);
builder.setMessage("Would you like to install...")
.setCancelable(false)
.setPositiveButton("Install", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
aManager.install(MyApp.this);
}
});
dialog = builder.create();
return dialog;
My dialog is shown and I am clicking, so aManager.install() is called. I am passing the context because that aManager.install() pops up a ProgressDialog to show downloading progress and spawns a new thread in which everything is downloaded. So obviously before creating my dialog I make a Handler to receive the response from that aManager.install(). And the response MAY vary, because for example the internet connection isn't available (Exception raised and catched and listener called with different code).
Now, when that happens (Exception) I would like to call another dialog saying "something went wrong, would you like to retry?"...so another call to showDialog(DIALOG_REINSTALL) (this time with another code).
Thing is...the showDialog() gets called (I can verify this by logging) but the dialogs doesn't show up. Instead my application JUST HANGS!?!?!?
Does someone have a clue why it's doing this???? No exception raised, absolutely nothing from logcat, I can't tell WHERE it's hanging...just see that the method is called and the dialog should be displayed...
Thank you very much!!
Looks like you have a deadlock. I would put the download code on the separate thread e.g. use AsyncTask. In task.onPreExecute() you can dismiss 1st dialog and pop-up your progress dialog which you update by overwriting task.onProgressUpdate()
Use .show() instead of .create().

How to use Rotating Progress Bar?

Hi i'm using Rotating Progress Bar in my Android Music Plyer Application....I'm not able to stop it. While working with horizontal Progress bar i used handler to stop and start it.
But while working with Rotating One, The progress bar goes into Infinite Loop.....
Can you please suggest method to stop the indefinite loop. Thanks in advance.
How about using ProgressBar#dismiss() method?
EDIT: dismiss() is only for ProgressDialog. For ProgressBar you should toggle the Visibilty of the View.
If mHandler is a Handler bound to your UI thread and mProgress is your ProgressBar, you can have something like the following from inside the run method of your background thread:
mHandler.post(new Runnable() {
public void run() {
mProgress.setVisibility(View.INVISIBLE);
}
});
You can dismiss a ProgressDialog. A progressBar is just a view you can make set its visibility as visible or invisible based on your requirement
Drawable d = yourActivity.this.getResources().getDrawable(android.R.drawable.ic_dialog_alert);
d.setBounds(progressbar.getIndeterminateDrawable().getBounds());
progressbar.setIndeterminateDrawable(d);

Categories

Resources