Statistics
| Revision:

root / trunk / libraries / libUIComponent / src-test / org / gvsig / gui / beans / incrementableTask / TestIncrementableTask.java @ 11179

History | View | Annotate | Download (3.66 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 (long k=0; k<=65535; k++)
59
                                for (long n=0; n<=5000; n++);
60
                        for (j=0; j<=65535; j++) {
61
                                if (ended) break;
62
                                for (long i=0; i<=65535; i++);
63
                                log.replaceLastLine(j + "");
64
                                if ((j%1000)==0) {
65
                                        log.addLine(j + "");
66
                                }
67
                                synchronized(this) {
68
                                        while (threadSuspended && !ended)
69
                                                try {
70
                                                        wait(500);
71
                                                } catch (InterruptedException e) {
72
                                                        e.printStackTrace();
73
                                                }
74
                                }
75
                        }
76
                        while (incrementableTask.isAlive());
77
                        incrementableTask.Hide();
78
                        incrementableTask = null;
79
                }
80

    
81
                public String getLabel() {
82
                        return "Generando estad?sticas, por favor, espere...";
83
                }
84

    
85
                public String getLog() {
86
                        return log.getText();
87
                }
88

    
89
                public int getPercent() {
90
                        return (int) ((j*100)/65535);
91
                }
92

    
93
                public String getTitle() {
94
                        return "Barra de progreso";
95
                }
96

    
97
                public void setIncrementableTask(IncrementableTask value) {
98
                        incrementableTask = value;
99
                }
100

    
101
                public void actionCanceled(IncrementableEvent e) {
102
                        ended = true;
103
                }
104

    
105
                public void actionResumed(IncrementableEvent e) {
106
                        threadSuspended = false;
107
                }
108

    
109
                public void actionSuspended(IncrementableEvent e) {
110
                        threadSuspended = true;
111
                }
112
        }
113

    
114
  ClassProcess classProcess = null;
115

    
116
        public TestIncrementableTask() {
117
                super();
118
                initialize();
119
        }
120

    
121
        private void initialize() {
122
                classProcess = new ClassProcess();
123
                IncrementableTask incrementableTask = new IncrementableTask(classProcess);
124
                classProcess.setIncrementableTask(incrementableTask);
125
                incrementableTask.showWindow();
126
                incrementableTask.addIncrementableListener(classProcess);
127

    
128
                incrementableTask.start();
129
                classProcess.start();
130
                while (classProcess.isAlive()) {
131
                        try {
132
                                Thread.sleep(500);
133
                        } catch (InterruptedException e) {
134
                                e.printStackTrace();
135
                        }
136
                }
137
                classProcess.stop();
138
                classProcess = null;
139
                incrementableTask = null;
140
        }
141

    
142
        public static void main(String[] args) {
143
                new TestIncrementableTask();
144
        }
145
}