Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / swing / threads / MonitorableTaskQueue.java @ 40561

History | View | Annotate | Download (3.73 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
/* CVS MESSAGES:
25
*
26
* $Id: MonitorableTaskQueue.java 29631 2009-06-29 16:56:19Z jpiera $
27
* $Log$
28
* Revision 1.2  2007-05-15 07:21:20  cesar
29
* Add the finished method for execution from Event Dispatch Thread
30
*
31
* Revision 1.1  2006/03/14 19:23:42  azabala
32
* *** empty log message ***
33
*
34
*
35
*/
36
package org.gvsig.utils.swing.threads;
37

    
38
import java.util.ArrayList;
39
import java.util.Iterator;
40

    
41

    
42
/**
43
 * A task that could enqueue tasks.
44
 * @author azabala
45
 *
46
 */
47
public class MonitorableTaskQueue implements IMonitorableTask {
48

    
49
        private ArrayList taskQueue;
50
        private boolean canceled = false;
51
        private IMonitorableTask currentTask = null;
52
        
53
        public MonitorableTaskQueue(){
54
                taskQueue = new ArrayList();
55
        }
56
        
57
        public void addTask(IMonitorableTask task){
58
                taskQueue.add(task);
59
        }
60
        
61
        
62
        public int getInitialStep() {
63
                return 0;
64
        }
65

    
66
        public int getFinishStep() {
67
                int lastStep = 0;
68
                if(isDefined()){
69
                        for(int i = 0; i < taskQueue.size(); i++){
70
                                IMonitorableTask task = 
71
                                        (IMonitorableTask) taskQueue.get(i);
72
                                lastStep += task.getFinishStep();
73
                        }
74
                }else{
75
                        lastStep += taskQueue.size();
76
                }
77
                return lastStep;
78
        }
79

    
80
        public int getCurrentStep() {
81
                int currentStep = 0;
82
                
83
                        for(int i = 0; i < taskQueue.size(); i++){
84
                                IMonitorableTask task = 
85
                                        (IMonitorableTask) taskQueue.get(i);
86
                                
87
                                        if(task == currentTask)
88
                                        {
89
                                                if(isDefined()){
90
                                                        currentStep += currentTask.getCurrentStep();
91
                                                        
92
                                                }else{
93
                                                        currentStep = i;
94
                                                }        
95
                                                break;
96
                                        }else{
97
                                                if(isDefined())
98
                                                        currentStep += currentTask.getFinishStep();
99
                                        }
100
                }
101
                return currentStep++;
102
        }
103

    
104
        public String getStatusMessage() {
105
                if(currentTask != null){
106
                        return currentTask.getStatusMessage();
107
                }else{
108
                        return "Waiting for new tasks...";
109
                }
110
        }
111

    
112
        public String getNote() {
113
                if(currentTask != null){
114
                        return currentTask.getNote();
115
                }else{
116
                        return "";
117
                }
118
        }
119

    
120
        public boolean isDefined() {
121
                for(int i = 0; i < taskQueue.size(); i++){
122
                        IMonitorableTask task = 
123
                                (IMonitorableTask) taskQueue.get(i);
124
                        if(!task.isDefined())
125
                                return false;
126
                }
127
                return true;
128
        }
129

    
130
        public void cancel() {
131
                if(currentTask != null){
132
                        currentTask.cancel();
133
                }
134
                canceled = true;
135

    
136
        }
137

    
138
        public void run() throws Exception {
139
                System.out.println("lanzando procesos encolados...");
140
                Iterator taskIterator = taskQueue.iterator();
141
                while (taskIterator.hasNext() && (! canceled)){
142
                        currentTask = (IMonitorableTask) taskIterator.next();
143
                        System.out.println("proceso " + currentTask.toString());
144
                        currentTask.run();
145
                }
146
                System.out.println("Se finalizo la cola de procesos");
147
                
148

    
149
        }
150

    
151
        public boolean isCanceled() {
152
                if(currentTask != null){
153
                        return currentTask.isCanceled();
154
                }
155
                return false;
156
        }
157

    
158
        public boolean isFinished() {
159
                if(currentTask != null){
160
                        return currentTask.isFinished();
161
                }
162
                return false;
163
        }
164

    
165
        public void finished() {
166
        }
167
}
168