ZK show progress percentage - event-handling

I have a ZK event handler in Java that may work slowly, and I want to show the progress on the client like this:
public void onClick$btnAddOrders() {
List<Order> orders = getOrders();
int numberOfOrder = orders.size();
for (int i=0; i< numberOfOrder; i++){
addOrder(orders.get(i));
showOnTheGui(i + " of " + numberOfOrder + " orders are processed");
}
}
As I observed, these kind of notifications will displayed only when my event handler returned.
Is there any way in the free ZK editions that solves it?

Add a timer to your screen. Start your timer before your for loop and update the screen via the onTimer event. Stop your timer before exiting your method.

Related

Part of my PlayerExpChangeEvent is being overridden by vanilla

I'm making a spigot plugin (version 1.8.8) that has an function that I know works because it fires flawlessly through my command. However, when I call it at the end of a PlayerExpChangeEvent, it seems like vanilla leveling overrides the bar, making it go up way more that it is supposed to. Running the command/function after this happens makes the bar go back to how it is supposed to be. I've tried setting my event's priority to highest (and when that didn't work, to lowest) but no matter what my function appears to be completely ignored when called inside the event.
Here is some code:
#EventHandler(priority=EventPriority.HIGHEST)
public void onXpGain(PlayerExpChangeEvent event)
{
// Load custom levels from config
ArrayList<String> levelList = new ArrayList<String>(plugin.getConfig().getStringList("levels"));
if (!((String)levelList.get(0)).equals("none"))
{
Player player = event.getPlayer();
Iterator<String> var4 = levelList.iterator();
while (var4.hasNext())
{
String s = (String)var4.next();
String[] splits = s.split(" ");
int levelCompare = Integer.parseInt(splits[0]);
int playerLvl = player.getLevel();
// Detect if on correct tier, else continue iteration
if (playerLvl == levelCompare - 1)
{
// Calculate the player's new XP amount
int totalXp = player.getTotalExperience() + event.getAmount();
player.setTotalExperience(totalXp);
updateBar(event.getPlayer()); // <-- THIS IS THE FUNCTION
return;
}
}
// At max level
player.setTotalExperience(player.getTotalExperience() + event.getAmount());
player.setLevel(getHighestLevel(levelList));
player.setExp(1.0f);
}
}
And here is the function itself. Keep in mind that it works fine when called through a command and not an event. It's purpose is to use the player's total XP to set the level and bar. Neither set correctly in the event; it instead embraces vanilla leveling.
public static void updateBar(Player player) {
ArrayList<String> levelList = new ArrayList<String>(plugin.getConfig().getStringList("levels"));
int totalXp = player.getTotalExperience();
player.setLevel(getHighestLevelForXp(totalXp, levelList));
if (player.getLevel() < getHighestLevel(levelList)) {
int lvlDiff = getTotalXpToLevel(player.getLevel() + 1,levelList) - getTotalXpToLevel(player.getLevel(),levelList);
int xpDiff = totalXp - getTotalXpToLevel(player.getLevel(),levelList);
player.setExp((float)xpDiff/lvlDiff);
} else {
player.setLevel(getHighestLevel(levelList));
player.setExp(0.0f);
}
return;
}
The command where the function works correctly is a bare-bones call to the function and doesn't need a mention here. Does anyone know how to get my event to override vanilla xp gain? The update works through the command, just not natural xp gain. It is already confirmed that the event DOES fire, as the rest of the event changes the internal xp amount, but the visual effects are overridden by vanilla. Can anyone help? Thanks.
Only setting the Player's EXP won't be enough for your desired behaviour. The Vanilla behaviour will still complete, as you're not changing how the event will add EXP to the player.
Currently, your event is working like this:
And PlayerExpGainEvent isn't cancellable, so you cannot undo it's addition of EXP.
What you can do instead is to set the EXP the event will add to 0, therefore not changing the player's EXP after your interception.
event.setAmount(0); //Cancelling the EXP addition
I would recommend to set your event to a high priority, so that other events that depend on Experience gain won't trigger when you set the amount gained to 0.

How to detect the scroll panel is still scrolling or not in GWT?

I need checkpoint to detect the scrollpanel is still scrolling or not. I am sending the server request while scrolling the scroll bar with the delay of 100 milliseconds.
I meant, every 100 milliseconds i am sending the request to server while scrolling. This is fine when i do scroll slowly in the grid. But, when I drag from page top to bottom
I could see there are multiple request is going to server. I want to know is there any flag to delete the long scroll. Please find the code
#Override
public void onScroll(final ScrollEvent scrollEvent)
{
int delay = 100;
super.onScroll(scrollEvent, delay);
}
I am using GWT class com.google.gwt.dom.client.BrowserEvents.ScrollEvent. I need below kind of logic
#Override
public void onScroll(final ScrollEvent scrollEvent)
{
int delay = 100;
if(stillScollbarScrolling) {
return;
} else {
super.onScroll(scrollEvent, delay);
}
}
So, I need to consider last request only as valid request. All the previous requests are invalid. I have logic to cancel all the previous logic. But, I need check point to still scroll bar is scrolling without release the bar.
Please help me..
If I understand you well, you need to cancel repeating scroll events. If so, you can use a Timer to do that.
Once a scroll event occurs you start a timer. If next scroll event is fired you check if a timer is still running. If so, it means that you have a repeating event that should be cancelled. If a timer is not running than it is not a repeating event and a request should be done.
Here is the code:
Timer timer = new Timer() {
#Override
public void run() {
// do nothing...
}
};
#Override
public void onScroll(final ScrollEvent scrollEvent) {
if(timer.isRunning()) {
// timer is running - stop the timer and do nothing (cancel event)
timer.cancel();
}
else
// timer is not running - do request
super.onScroll(scrollEvent);
// restart the timer with 100ms delay
timer.schedule(100);
}
Please, notice that the first scroll event will cause doing the request because the timer is not started yet.

Updating ListView after animation

My app has a sliding drawer for notifications. I've managed to get it to function like the android notifications including the "clear all" button.
When the clear all button is clicked my database is cleared, my list adapter is refreshed, and my list adapter gets set to the list. The view updates and the list is cleared.
When I added slide-out animation (just like jelly bean) I got a NullPointerException. The issue crops up when my adapter is set. If I comment out setting the adapter the animation runs without a problem.
// Animation
int count = drawer_list.getCount();
for (int i = 0; i < count; i++) {
View view = drawer_list.getChildAt(i);
if (view != null) {
// create an Animation for each item
Animation animation = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
animation.setDuration(300);
// ensure animation final state is "persistent"
animation.setFillAfter(true);
// calculate offset (bottom ones first, like in notification panel)
animation.setStartOffset(300 * (count - 1 - i));
// animation listener to execute action code
if (i == 0) {
animation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
// NOT USED
}
public void onAnimationRepeat(Animation animation) {
// NOT USED
}
public void onAnimationEnd(Animation animation) {
// Clear table
notifications.flushNotificationTempTable_ActiveOnly();
// Update list adapter
refreshNotifications();
// Close drawer
notification_drawer.close();
}
});
}
view.startAnimation(animation);
}
}
The trouble spot is in the refreshNotifications() method when the line drawer_list.setAdapter(notificationAdapter); executes. I use this method and adapter all throughout my app and, as I said above, it works flawlessly without the animations.
I was able to solve this by eliminating the animation listener and adding a delayed runnable after it that ran my post animation code.
I was unable to find a way to set a new adapter (required because i used a custom adapter) inside the animation listener. If anyone knows how to do this I would like to know.

Best practices Implementing ProgressMonitorDialog in Eclipse RCP

I wish to Show progress of a long running operation(process) in UI, so that the user can understand the status of the background job. This is the way I have implemented and I feel like the code is absurd. Below is my code
dialog.run(true,false, new IRunnableWithProgress() {
#Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("Main process", 10);
for (int i = 0; i < 10; i++) {
if (monitor.isCanceled()) return;
monitor.subTask("Status message");
sleep(1000);
// worked increases the monitor, the values are added to the existing ones
monitor.worked(1);
if(i == 3) {
sleep(3000);
callMe();//calling a long running function
}
if(i == 9) {
monitor.subTask("finishing setup..... please wait ");
sleep(2000);
}
}
monitor.done();
}
});
Note: There is a sleep method somewhere in the code
here at i == 3 an operation/function is called that takes a minimum of 5 minutes, post execution of the function the progress continues.
I don't want the progress to be stopped while executing the function(long running operation) rather progress must be shown even while executing it.
can someone show the correct programming practices in showing progress
The reason your code feels absurd is that wrapping the long-running method in a IRunnableWithProgress.run() really does not add much in itself, there is no magic.
To make the ProgressMonitorDialog (or e.g. the related Job API) work for you, you need to change "callMe()" so it takes "IProgressMonitor monitor" as a parameter, and use that to listen for cancel-requests, and use it also for reporting progress.
To say the same again, using different wording: You need to recursively add "IProgressMonitor monitor" as a parameter to all long-running method calls. All long-running operations must be build with this (IProgressMonitor) in mind if any progress is to be reported and/or you want it to be cancelable.

Android Dynamic Checkbox

I am in the process of trying to add dynamic checkbox to my activity. However being a beginner i cant get round the basics of being able to add checkboxes and remove them. Here is my code....
private void createCheckbox() {
for(int i=0; i<5; i++){
cb = new CheckBox(this);
ll.addView(cb);
cb.setText("Test");
}
ll.addView(submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < 5; i++) {
ll.removeView(cb);
}
ll.removeView(submit);
Questions();
}});
}
ll is a linerlayout object. The idea is when the code runs, 5 checkboxes appear and then once the user clicks on the submit button they get removed.
Currently the boxes are being seen, but when the submit button is pressed only one of the five is being removed. I don't understand what i am doing wrong?
For loop not ending correctly, curly bracket wrong place