Statistics
| Revision:

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

History | View | Annotate | Download (3.56 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: MonitorableDecoratorMainFirst.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/05/08 15:52:30  azabala
32
* *** empty log message ***
33
*
34
* Revision 1.2  2006/04/18 15:17:20  azabala
35
* a?adidos comentarios a metodos
36
*
37
* Revision 1.1  2006/03/14 19:23:42  azabala
38
* *** empty log message ***
39
*
40
*
41
*/
42
package org.gvsig.utils.swing.threads;
43
/**
44
 * Task that wraps a main task, executing it and doing some preprocess
45
 * stuff after main task, by executing a secondary task
46
 * @author azabala
47
 *
48
 */
49
public class MonitorableDecoratorMainFirst implements IMonitorableTask {
50

    
51
        /**
52
         * main task
53
         */
54
        private IMonitorableTask mainTask;
55
        /**
56
         * preprocess task 
57
         */
58
        private IMonitorableTask secondaryTask;
59
        /**
60
         * task that is currently in execution
61
         */
62
        private IMonitorableTask currentTask;
63
        /**
64
         * flag for cancelations
65
         */
66
        private boolean canceled = false;
67
        /**
68
         * flag for finalization
69
         */
70
        private boolean finished = false;
71
        
72
        /**
73
         * Constructor
74
         * @param mainTask
75
         * @param secondaryTask
76
         */
77
        public MonitorableDecoratorMainFirst(IMonitorableTask mainTask, IMonitorableTask secondaryTask){
78
                this.mainTask = mainTask;
79
                this.secondaryTask = secondaryTask;
80
                this.currentTask = mainTask;
81
        }
82
        
83
        /**
84
         * Makes some preprocess steps, and return a boolean flag that
85
         * indicates if this task can be launched.
86
         * @return
87
         */
88
        public boolean preprocess(){
89
                if(mainTask != null  && secondaryTask != null)
90
                        return true;
91
                else 
92
                        return false;
93
        }
94
        
95
        
96
        public int getInitialStep() {
97
                return mainTask.getInitialStep();
98
        }
99

    
100
        public int getFinishStep() {
101
                //we add 1 because secondaryTask is consideered as
102
                //a step
103
                return mainTask.getFinishStep() + 1;
104
        }
105

    
106
        public int getCurrentStep() {
107
                if(currentTask == mainTask){
108
                        return mainTask.getCurrentStep();
109
                }else{
110
                        return getFinishStep();
111
                }
112
        }
113

    
114
        public String getStatusMessage() {
115
                return mainTask.getStatusMessage();
116
        }
117

    
118
        public String getNote() {
119
                return mainTask.getNote();
120
        }
121

    
122
        public boolean isDefined() {
123
                return mainTask.isDefined();
124
        }
125

    
126
        public void cancel() {
127
                canceled = true;
128
                currentTask.cancel();
129

    
130
        }
131

    
132
        public void run() throws Exception {
133
                currentTask = mainTask;
134
                if(! canceled)
135
                        mainTask.run();
136
                if(! canceled){
137
                        currentTask = secondaryTask;
138
                        secondaryTask.run();
139
                }
140
                finished  = true;
141
                
142

    
143
        }
144

    
145
        public boolean isCanceled() {
146
                return canceled == true;
147
        }
148

    
149
        public boolean isFinished() {
150
                if(currentTask == mainTask){
151
                        return currentTask.isFinished();
152
                }else
153
                        return finished == true;
154
        }
155
        
156
        public void finished() {
157
                
158
        }
159

    
160
}
161