LongOperations"

From Documentation
Line 17: Line 17:
 
The operation creates a simple result which is added to the '''resultModel''' when it finishes. During the 3 seconds the default busy overlay is displayed, asking the user to wait.
 
The operation creates a simple result which is added to the '''resultModel''' when it finishes. During the 3 seconds the default busy overlay is displayed, asking the user to wait.
  
<source lang="java" high="10, 15, 25,26">
+
<source lang="java" high="10, 15, 26">
 
public class SimpleLongOperationViewModel {
 
public class SimpleLongOperationViewModel {
 
private ListModelList<String> resultModel = new ListModelList<String>();
 
private ListModelList<String> resultModel = new ListModelList<String>();
Line 53: Line 53:
  
 
* '''Line 10:''' Implement the '''execute''' callback to collecting the result asynchrously
 
* '''Line 10:''' Implement the '''execute''' callback to collecting the result asynchrously
* '''Line 15:''' Implement the '''onFinish''' callback to update the UI once the operation has finished successfully
+
* '''Line 15:''' Implement the '''onFinish''' callback to update the UI once the operation has finished '''successfully'''
 
* '''Line 26:''' Launch the operation  
 
* '''Line 26:''' Launch the operation  
 +
 +
In the ''''startLongOperation''''-command handler the "busy"-message is shown, in '''onCancel''' it is cleared, when the long operation terminates (successful or not).
  
 
Here the straight forward zul code using this '''SimpleLongOperationViewModel''' and posting the '''startLongOperation'''-command
 
Here the straight forward zul code using this '''SimpleLongOperationViewModel''' and posting the '''startLongOperation'''-command

Revision as of 07:54, 8 January 2015

Documentationobertwenzel
obertwenzel

Author
Robert Wenzel, Engineer, Potix Corporation
Date
January XX, 2015
Version
ZK 7.0.4


Introduction

Longoperations are useful - bla - leverage Java Threads - bla - here how to hide and reuse the technical details. support MVVM and MVC programming model

Long Operations de-mystified

A very Simple Example

This simple example shows a very simple use case of the LongOperation class. The operation creates a simple result which is added to the resultModel when it finishes. During the 3 seconds the default busy overlay is displayed, asking the user to wait.

public class SimpleLongOperationViewModel {
	private ListModelList<String> resultModel = new ListModelList<String>();

	@Command
	public void startLongOperation() {
		LongOperation longOperation = new LongOperation() {
			private List<String> result;

			@Override
			protected void execute() throws InterruptedException {
				Thread.sleep(3000); //simulate a long backend operation
				result = Arrays.asList("aaa", "bbb", "ccc");
			}

			protected void onFinish() {
				resultModel.addAll(result);
			};

			@Override
			protected void onCleanup() {
				Clients.clearBusy();
			}
		};

		Clients.showBusy("Result coming in 3 seconds, please wait!");
		longOperation.start();
	}

	public ListModelList<String> getResultModel() {
		return resultModel;
	}
}
  • Line 10: Implement the execute callback to collecting the result asynchrously
  • Line 15: Implement the onFinish callback to update the UI once the operation has finished successfully
  • Line 26: Launch the operation

In the 'startLongOperation'-command handler the "busy"-message is shown, in onCancel it is cleared, when the long operation terminates (successful or not).

Here the straight forward zul code using this SimpleLongOperationViewModel and posting the startLongOperation-command

    <div apply="org.zkoss.bind.BindComposer" 
    	 viewModel="@id('vm') @init('zk.example.longoperations.example.SimpleLongOperationViewModel')">
        <button onClick="@command('startLongOperation')" label="start"/>
        <grid model="@load(vm.resultModel)" height="300px"/>
    </div>

Showing Operation updates/progress

Aborting a Long Operation

Exception Handling

Parallel Tasks

Extending LongOperation

Resulting Demo

The video below demonstrates the results of the two advanced usages described above. For ease of demonstration here we use a PDF printer so the resulting screen is a PDF file, but you can definitely specify a real printer to print out the desired results on papers. ERROR: Link hasn't been found

Summary

With the printing utility explained in the article, you can print the desired sections in a ZK page with only little effort -- you can even include custom headers & footers or change the style easily for better readability. For your convenience we have wrapped this utility as a ready-to-use jar file. Refer to download section to download the jar file and put it in your project's WEB-INF/lib folder.

Download

  • The source code for this article can be found in github.
  • Download the packed jar file from github.


Comments



Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.