svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / test / java / org / gvsig / gui / beans / incrementabletask / TestIncrementableTask.java @ 40561
History | View | Annotate | Download (5.9 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.gui.beans.incrementabletask; |
25 |
|
26 |
import org.gvsig.gui.beans.progresspanel.LogControl; |
27 |
|
28 |
/* gvSIG. Geographic Information System of the Valencian Government
|
29 |
*
|
30 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
31 |
* of the Valencian Government (CIT)
|
32 |
*
|
33 |
* This program is free software; you can redistribute it and/or
|
34 |
* modify it under the terms of the GNU General Public License
|
35 |
* as published by the Free Software Foundation; either version 2
|
36 |
* of the License, or (at your option) any later version.
|
37 |
*
|
38 |
* This program is distributed in the hope that it will be useful,
|
39 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
40 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
41 |
* GNU General Public License for more details.
|
42 |
*
|
43 |
* You should have received a copy of the GNU General Public License
|
44 |
* along with this program; if not, write to the Free Software
|
45 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
46 |
* MA 02110-1301, USA.
|
47 |
*
|
48 |
*/
|
49 |
|
50 |
/**
|
51 |
* Test del IncrementableTask
|
52 |
*
|
53 |
* @version 27/05/2007
|
54 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
55 |
*/
|
56 |
public class TestIncrementableTask { |
57 |
class ClassProcess implements Runnable, IIncrementable, IncrementableListener { |
58 |
int i = 0; |
59 |
long j = 0; |
60 |
LogControl log = new LogControl();
|
61 |
IncrementableTask incrementableTask = null;
|
62 |
|
63 |
private volatile Thread blinker; |
64 |
// private boolean ended = false;
|
65 |
// private boolean threadSuspended = false;
|
66 |
|
67 |
public ClassProcess() {
|
68 |
} |
69 |
|
70 |
public void start() { |
71 |
blinker = new Thread(this); |
72 |
blinker.start(); |
73 |
} |
74 |
|
75 |
public synchronized void stop() { |
76 |
// ended = true;
|
77 |
blinker = null;
|
78 |
notify(); |
79 |
} |
80 |
|
81 |
public boolean isAlive() { |
82 |
return blinker.isAlive();
|
83 |
} |
84 |
|
85 |
public void procesoDuro() throws InterruptedException { |
86 |
try {
|
87 |
for (long k = 0; k <= 8000; k++) |
88 |
for (long n = 0; n <= 5000; n++) { |
89 |
for (long l = 0; l <= 100; l++); |
90 |
if (Thread.currentThread().isInterrupted()) |
91 |
throw new InterruptedException(); |
92 |
} |
93 |
System.out.println("b"); |
94 |
} finally {
|
95 |
System.out.println("c"); |
96 |
} |
97 |
} |
98 |
|
99 |
public synchronized void run() { |
100 |
try {
|
101 |
procesoDuro(); |
102 |
} catch (InterruptedException e1) { |
103 |
System.out.println("Se ha salido"); |
104 |
} |
105 |
/*
|
106 |
for (long k=0; k<=65535; k++)
|
107 |
for (long n=0; n<=5000; n++);
|
108 |
for (j=0; j<=65535; j++) {
|
109 |
if (Thread.currentThread().isInterrupted())
|
110 |
break;
|
111 |
for (long i=0; i<=65535; i++);
|
112 |
log.replaceLastLine(j + "");
|
113 |
System.out.println(Thread.currentThread().isInterrupted());
|
114 |
if ((j%1000)==0) {
|
115 |
log.addLine(j + "");
|
116 |
}
|
117 |
synchronized(this) {
|
118 |
while (threadSuspended && !Thread.currentThread().isInterrupted())
|
119 |
try {
|
120 |
wait(500);
|
121 |
} catch (InterruptedException e) {
|
122 |
e.printStackTrace();
|
123 |
}
|
124 |
}
|
125 |
}*/
|
126 |
incrementableTask.processFinalize(); |
127 |
} |
128 |
|
129 |
public String getLabel() { |
130 |
return "Generando estad?sticas, por favor, espere..."; |
131 |
} |
132 |
|
133 |
public String getLog() { |
134 |
return log.getText();
|
135 |
} |
136 |
|
137 |
public int getPercent() { |
138 |
return (int) ((j*100)/65535); |
139 |
} |
140 |
|
141 |
public String getTitle() { |
142 |
return "Barra de progreso"; |
143 |
} |
144 |
|
145 |
public void setIncrementableTask(IncrementableTask value) { |
146 |
incrementableTask = value; |
147 |
} |
148 |
|
149 |
public void actionCanceled(IncrementableEvent e) { |
150 |
// ended = true;
|
151 |
blinker.interrupt(); |
152 |
} |
153 |
|
154 |
public void actionResumed(IncrementableEvent e) { |
155 |
// threadSuspended = false;
|
156 |
} |
157 |
|
158 |
public void actionSuspended(IncrementableEvent e) { |
159 |
// threadSuspended = true;
|
160 |
} |
161 |
|
162 |
|
163 |
public void setLabel(String label) { |
164 |
// TODO Auto-generated method stub
|
165 |
|
166 |
} |
167 |
|
168 |
public boolean isSuspended() { |
169 |
// TODO Auto-generated method stub
|
170 |
return false; |
171 |
} |
172 |
|
173 |
public void resume() { |
174 |
// TODO Auto-generated method stub
|
175 |
|
176 |
} |
177 |
|
178 |
public void suspend() { |
179 |
// TODO Auto-generated method stub
|
180 |
|
181 |
} |
182 |
|
183 |
public boolean isCancelable() { |
184 |
// TODO Auto-generated method stub
|
185 |
return false; |
186 |
} |
187 |
|
188 |
public void process() throws InterruptedException, Exception { |
189 |
// TODO Auto-generated method stub
|
190 |
|
191 |
} |
192 |
|
193 |
public void setCancelable(boolean b) { |
194 |
// TODO Auto-generated method stub
|
195 |
|
196 |
} |
197 |
|
198 |
public boolean isPausable() { |
199 |
// TODO Auto-generated method stub
|
200 |
return false; |
201 |
} |
202 |
|
203 |
public void setPausable(boolean b) { |
204 |
// TODO Auto-generated method stub
|
205 |
|
206 |
} |
207 |
} |
208 |
|
209 |
ClassProcess classProcess = null;
|
210 |
|
211 |
public TestIncrementableTask() {
|
212 |
super();
|
213 |
initialize(); |
214 |
} |
215 |
|
216 |
private void initialize() { |
217 |
classProcess = new ClassProcess();
|
218 |
IncrementableTask incrementableTask = new IncrementableTask(classProcess);
|
219 |
classProcess.setIncrementableTask(incrementableTask); |
220 |
incrementableTask.showWindow(); |
221 |
incrementableTask.addIncrementableListener(classProcess); |
222 |
|
223 |
incrementableTask.start(); |
224 |
classProcess.start(); |
225 |
while (classProcess.isAlive()) {
|
226 |
try {
|
227 |
Thread.sleep(500); |
228 |
} catch (InterruptedException e) { |
229 |
e.printStackTrace(); |
230 |
} |
231 |
} |
232 |
classProcess.stop(); |
233 |
classProcess = null;
|
234 |
incrementableTask = null;
|
235 |
} |
236 |
|
237 |
public static void main(String[] args) { |
238 |
new TestIncrementableTask();
|
239 |
} |
240 |
} |