Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / task / impl / DefaultTaskStatusManager.java @ 1106

History | View | Annotate | Download (3.95 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 2
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
package org.gvsig.tools.task.impl;
25

    
26
import java.util.Collections;
27
import java.util.Iterator;
28
import java.util.LinkedHashMap;
29
import java.util.Map;
30

    
31
import org.gvsig.tools.observer.ObservableHelper;
32
import org.gvsig.tools.observer.Observer;
33
import org.gvsig.tools.task.SimpleTaskStatus;
34
import org.gvsig.tools.task.TaskStatus;
35
import org.gvsig.tools.task.TaskStatusManager;
36

    
37
/**
38
 * @author gvSIG Team
39
 * @version $Id$
40
 * 
41
 */
42
public class DefaultTaskStatusManager implements TaskStatusManager {
43

    
44
        private static final int MAXTASKS_TO_AUTOREMOVE = 15;
45

    
46
        private static long codeCounter = 1;
47

    
48
        private Map tasksStatus = null;
49
        private ObservableHelper observers = null;
50

    
51
        public DefaultTaskStatusManager() {
52
                this.tasksStatus = new LinkedHashMap();
53
                this.observers = new ObservableHelper();
54
        }
55

    
56
        synchronized public void add(TaskStatus taskStatus) {
57
                this.tasksStatus.put(taskStatus.getCode(), taskStatus);
58
                this.update(taskStatus);
59
        }
60

    
61
        synchronized public TaskStatus get(String code) {
62
                return (TaskStatus) (this.tasksStatus.get(code));
63
        }
64

    
65
        synchronized public void remove(String code) {
66
                this.tasksStatus.remove(code);
67
                this.update((TaskStatus) this.tasksStatus.get(code));
68
        }
69

    
70
        synchronized public void remove(TaskStatus taskStatus) {
71
                this.tasksStatus.remove(taskStatus.getCode());
72
                this.update(null);
73
        }
74

    
75
        public String getNewCode() {
76
                return Long.toBinaryString(codeCounter++);
77
        }
78

    
79
        synchronized public Map getAll() {
80
                return Collections.unmodifiableMap(this.tasksStatus);
81
        }
82

    
83
        synchronized public void addObserver(Observer o) {
84
                this.observers.addObserver(o);
85
        }
86

    
87
        synchronized public void deleteObserver(Observer o) {
88
                this.observers.deleteObserver(o);
89
        }
90

    
91
        synchronized public void deleteObservers() {
92
                this.observers.deleteObservers();
93
        }
94

    
95
        synchronized public void update(TaskStatus taskstatus) {
96
                if (taskstatus != null
97
                                && this.tasksStatus.get(taskstatus.getCode()) == null) {
98
                        // This TaskStatus not is registered in this manager.
99
                        return;
100
                }
101
                this.observers.notifyObservers(this, taskstatus);
102
                if (taskstatus != null) {
103
                        if (!taskstatus.isRunning()) {
104
                                if (taskstatus instanceof SimpleTaskStatus) {
105
                                        if (((SimpleTaskStatus) taskstatus).getAutoRemove()) {
106
                                                if (this.tasksStatus.size() > MAXTASKS_TO_AUTOREMOVE) {
107
                                                        ((SimpleTaskStatus) taskstatus).remove();
108
                                                }
109
                                        }
110
                                }
111
                        }
112
                }
113
        }
114

    
115
        public SimpleTaskStatus creteDefaultSimpleTaskStatus(String tittle) {
116
        return createDefaultSimpleTaskStatus(tittle);
117
    }
118

    
119
    public SimpleTaskStatus createDefaultSimpleTaskStatus(String title) {
120
        return new BaseTaskStatus(title);
121
        }
122

    
123
        synchronized public TaskStatus getRunningTaskStatusMostRecent() {
124
                TaskStatus mostRecent = null;
125
                Iterator taskStatusIterator = this.tasksStatus.values().iterator();
126
                while( taskStatusIterator.hasNext() ) {
127
                        TaskStatus current = (TaskStatus) taskStatusIterator.next();
128
                        if ( current.isRunning() ) {
129
                                if( mostRecent==null || current.getLastModification().compareTo(mostRecent.getLastModification())>0 ) {
130
                                        mostRecent = current;
131
                                }
132
                        }
133
                }
134
                return mostRecent;
135
        }
136

    
137
}