1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.examples.tutorial.bgtask;
17
18 import net.sf.jguiraffe.gui.builder.components.model.ProgressBarHandler;
19 import net.sf.jguiraffe.gui.builder.event.FormActionEvent;
20 import net.sf.jguiraffe.gui.builder.event.FormActionListener;
21 import net.sf.jguiraffe.gui.builder.utils.GUISynchronizer;
22 import net.sf.jguiraffe.gui.builder.window.Window;
23 import net.sf.jguiraffe.gui.cmd.CommandBase;
24
25 /**
26 * <p>
27 * A command class for executing the background task.
28 * </p>
29 * <p>
30 * This command class simulates the execution of a long-running background task.
31 * The {@link #execute()} only sleeps a configurable number of seconds, but it
32 * demonstrates how an information dialog with a progress bar can be used to
33 * give the user feedback about the progress of the background task. This visual
34 * feedback is optional; it can be disabled, then the command only sleeps.
35 * </p>
36 *
37 * @author Oliver Heger
38 * @version $Id: BgTaskCommand.java 205 2012-01-29 18:29:57Z oheger $
39 */
40 public class BgTaskCommand extends CommandBase implements FormActionListener
41 {
42 /** Constant for a second sleep time. */
43 private static final long SLEEP_SECOND = 1000;
44
45 /** Constant for the maximum of the progress bar. */
46 private static final int PROGRESS_MAX = 100;
47
48 /** The synchronizer. */
49 private final GUISynchronizer synchronizer;
50
51 /** Stores the information dialog. */
52 private final Window infoDialog;
53
54 /** The bean with information of the background task. */
55 private final BgTaskData data;
56
57 /** The component handler for the progress bar. */
58 private final ProgressBarHandler progressHandler;
59
60 /** A flag whether execution of the command should be canceled. */
61 private volatile boolean cancelExecution;
62
63 /**
64 * Creates a new instance of {@code BgTaskCommand} and initializes it.
65 *
66 * @param sync the {@code GUISynchronizer}
67 * @param infoWindow the reference to the information window
68 * @param taskData the data object with information about the background
69 * task
70 * @param handler the handler for the progress bar
71 */
72 public BgTaskCommand(GUISynchronizer sync, Window infoWindow,
73 BgTaskData taskData, ProgressBarHandler handler)
74 {
75 super(taskData.isVisual());
76 synchronizer = sync;
77 infoDialog = infoWindow;
78 data = taskData;
79 progressHandler = handler;
80 }
81
82 /**
83 * Notifies this object that the cancel button was pressed. This method sets
84 * the cancel flag, so that the execution of the command is canceled.
85 *
86 * @param e the action event
87 */
88 @Override
89 public void actionPerformed(FormActionEvent e)
90 {
91 // disable button to signal the user that the click was received
92 e.getHandler().setEnabled(false);
93
94 cancelExecution = true;
95 }
96
97 /**
98 * Executes this command. Sleeps the specified number of seconds. If visual
99 * feedback is enabled, the progress bar is updated.
100 */
101 @Override
102 public void execute() throws Exception
103 {
104 float step = 0;
105 if (data.getDuration() > 0 && data.isVisual())
106 {
107 step = (float) PROGRESS_MAX / data.getDuration();
108 infoDialog.open();
109 }
110 float progress = 0;
111
112 for (int i = 0; i < data.getDuration() && !cancelExecution; i++)
113 {
114 Thread.sleep(SLEEP_SECOND);
115
116 if (data.isVisual())
117 {
118 progress += step;
119 final int progressValue = Math.round(progress);
120 synchronizer.asyncInvoke(new Runnable()
121 {
122 @Override
123 public void run()
124 {
125 progressHandler.setValue(progressValue);
126 }
127 });
128 }
129 }
130 }
131
132 /**
133 * Performs UI-related updates after the execution of the command. This
134 * implementation closes the information window. Note that this method is
135 * only called if visual feedback is enabled.
136 */
137 @Override
138 protected void performGUIUpdate()
139 {
140 infoDialog.close(true);
141 }
142 }