root / trunk / libraries / libUIComponent / src-test / org / gvsig / gui / beans / incrementableTask / TestIncrementableTask.java @ 11042
History | View | Annotate | Download (3.56 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.gui.beans.incrementableTask; |
20 |
|
21 |
import org.gvsig.gui.beans.incrementabletask.IIncrementable; |
22 |
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent; |
23 |
import org.gvsig.gui.beans.incrementabletask.IncrementableListener; |
24 |
import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
25 |
import org.gvsig.gui.beans.progresspanel.LogControl; |
26 |
|
27 |
public class TestIncrementableTask { |
28 |
class ClassProcess implements Runnable, IIncrementable, IncrementableListener { |
29 |
int i = 0; |
30 |
long j = 0; |
31 |
LogControl log = new LogControl();
|
32 |
IncrementableTask incrementableTask = null;
|
33 |
|
34 |
private volatile Thread blinker; |
35 |
private boolean ended = false; |
36 |
private boolean threadSuspended = false; |
37 |
|
38 |
public ClassProcess() {
|
39 |
} |
40 |
|
41 |
public void start() { |
42 |
blinker = new Thread(this); |
43 |
blinker.start(); |
44 |
} |
45 |
|
46 |
public synchronized void stop() { |
47 |
ended = true;
|
48 |
blinker = null;
|
49 |
notify(); |
50 |
} |
51 |
|
52 |
public boolean isAlive() { |
53 |
return blinker.isAlive();
|
54 |
} |
55 |
|
56 |
|
57 |
public synchronized void run(){ |
58 |
for (j=0; j<=65535; j++) { |
59 |
if (ended) break; |
60 |
for (long i=0; i<=65535; i++); |
61 |
log.replaceLastLine(j + "");
|
62 |
if ((j%1000)==0) { |
63 |
log.addLine(j + "");
|
64 |
} |
65 |
synchronized(this) { |
66 |
while (threadSuspended && !ended)
|
67 |
try {
|
68 |
wait(500);
|
69 |
} catch (InterruptedException e) { |
70 |
e.printStackTrace(); |
71 |
} |
72 |
} |
73 |
} |
74 |
while (incrementableTask.isAlive());
|
75 |
incrementableTask.Hide(); |
76 |
incrementableTask = null;
|
77 |
} |
78 |
|
79 |
public String getLabel() { |
80 |
return "Etiqueta " + j; |
81 |
} |
82 |
|
83 |
public String getLog() { |
84 |
return log.getText();
|
85 |
} |
86 |
|
87 |
public int getPercent() { |
88 |
return (int) ((j*100)/65535); |
89 |
} |
90 |
|
91 |
public String getTitle() { |
92 |
return "TestIncrementTask"; |
93 |
} |
94 |
|
95 |
public void setIncrementableTask(IncrementableTask value) { |
96 |
incrementableTask = value; |
97 |
} |
98 |
|
99 |
public void actionCanceled(IncrementableEvent e) { |
100 |
ended = true;
|
101 |
} |
102 |
|
103 |
public void actionResumed(IncrementableEvent e) { |
104 |
threadSuspended = false;
|
105 |
} |
106 |
|
107 |
public void actionSuspended(IncrementableEvent e) { |
108 |
threadSuspended = true;
|
109 |
} |
110 |
} |
111 |
|
112 |
ClassProcess classProcess = null;
|
113 |
|
114 |
public TestIncrementableTask() {
|
115 |
super();
|
116 |
initialize(); |
117 |
} |
118 |
|
119 |
private void initialize() { |
120 |
classProcess = new ClassProcess();
|
121 |
IncrementableTask incrementableTask = new IncrementableTask(classProcess);
|
122 |
classProcess.setIncrementableTask(incrementableTask); |
123 |
incrementableTask.showWindow(); |
124 |
incrementableTask.addIncrementableListener(classProcess); |
125 |
|
126 |
incrementableTask.start(); |
127 |
classProcess.start(); |
128 |
while (classProcess.isAlive()) {
|
129 |
try {
|
130 |
Thread.sleep(500); |
131 |
} catch (InterruptedException e) { |
132 |
e.printStackTrace(); |
133 |
} |
134 |
} |
135 |
classProcess.stop(); |
136 |
classProcess = null;
|
137 |
incrementableTask = null;
|
138 |
} |
139 |
|
140 |
public static void main(String[] args) { |
141 |
new TestIncrementableTask();
|
142 |
} |
143 |
} |