Statistics
| Revision:

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

History | View | Annotate | Download (3.66 KB)

1 10970 bsanchez
/* 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 10945 bsanchez
package org.gvsig.gui.beans.incrementableTask;
20
21 10948 nacho
import org.gvsig.gui.beans.incrementabletask.IIncrementable;
22 10970 bsanchez
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent;
23
import org.gvsig.gui.beans.incrementabletask.IncrementableListener;
24 10948 nacho
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
25 10962 bsanchez
import org.gvsig.gui.beans.progresspanel.LogControl;
26 10945 bsanchez
27
public class TestIncrementableTask {
28 11042 bsanchez
        class ClassProcess implements Runnable, IIncrementable, IncrementableListener {
29 10945 bsanchez
                int i = 0;
30 10962 bsanchez
                long j = 0;
31
                LogControl log = new LogControl();
32 11042 bsanchez
                IncrementableTask incrementableTask = null;
33 10945 bsanchez
34 11042 bsanchez
                private volatile Thread blinker;
35
                private boolean ended = false;
36
                private boolean threadSuspended = false;
37
38 10962 bsanchez
                public ClassProcess() {
39
                }
40
41 11042 bsanchez
                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 10962 bsanchez
                public synchronized void run(){
58 11179 bsanchez
                        for (long k=0; k<=65535; k++)
59
                                for (long n=0; n<=5000; n++);
60 10962 bsanchez
                        for (j=0; j<=65535; j++) {
61 11042 bsanchez
                                if (ended) break;
62 10962 bsanchez
                                for (long i=0; i<=65535; i++);
63
                                log.replaceLastLine(j + "");
64
                                if ((j%1000)==0) {
65
                                        log.addLine(j + "");
66
                                }
67 11042 bsanchez
                                synchronized(this) {
68
                                        while (threadSuspended && !ended)
69
                                                try {
70
                                                        wait(500);
71
                                                } catch (InterruptedException e) {
72
                                                        e.printStackTrace();
73
                                                }
74
                                }
75 10962 bsanchez
                        }
76 11042 bsanchez
                        while (incrementableTask.isAlive());
77
                        incrementableTask.Hide();
78
                        incrementableTask = null;
79 10962 bsanchez
                }
80
81 10945 bsanchez
                public String getLabel() {
82 11179 bsanchez
                        return "Generando estad?sticas, por favor, espere...";
83 10945 bsanchez
                }
84
85
                public String getLog() {
86 10962 bsanchez
                        return log.getText();
87 10945 bsanchez
                }
88
89
                public int getPercent() {
90 10962 bsanchez
                        return (int) ((j*100)/65535);
91 10945 bsanchez
                }
92
93
                public String getTitle() {
94 11179 bsanchez
                        return "Barra de progreso";
95 10945 bsanchez
                }
96 10962 bsanchez
97 11042 bsanchez
                public void setIncrementableTask(IncrementableTask value) {
98
                        incrementableTask = value;
99 10962 bsanchez
                }
100
101 11042 bsanchez
                public void actionCanceled(IncrementableEvent e) {
102
                        ended = true;
103 10962 bsanchez
                }
104
105 10970 bsanchez
                public void actionResumed(IncrementableEvent e) {
106 11042 bsanchez
                        threadSuspended = false;
107 10962 bsanchez
                }
108
109 10970 bsanchez
                public void actionSuspended(IncrementableEvent e) {
110 11042 bsanchez
                        threadSuspended = true;
111 10962 bsanchez
                }
112 10945 bsanchez
        }
113
114 11042 bsanchez
  ClassProcess classProcess = null;
115 10945 bsanchez
116
        public TestIncrementableTask() {
117
                super();
118
                initialize();
119
        }
120
121
        private void initialize() {
122 11042 bsanchez
                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 10945 bsanchez
        }
141
142
        public static void main(String[] args) {
143
                new TestIncrementableTask();
144
        }
145
}