Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / incrementabletask / IncrementableTask.java @ 11042

History | View | Annotate | Download (5.17 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 java.awt.event.WindowAdapter;
22
import java.awt.event.WindowEvent;
23
import java.util.ArrayList;
24
import java.util.Iterator;
25

    
26
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
27
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
28
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
29
import org.gvsig.gui.beans.progresspanel.ProgressPanel;
30
/**
31
 * <code>IncrementableTask</code>. Es un dialogo que contiene un ProgressPanel.
32
 * Se ejecuta bajo un Thread y va consultando a un objeto de tipo IIncrementable
33
 * para modificar sus valores.
34
 *
35
 * @version 29/03/2007
36
 * @author Borja S?nchez Zamorano (borja.sanchez@iver.es)
37
 */
38
public class IncrementableTask implements Runnable, ButtonsPanelListener {
39
        IIncrementable iIncrementable = null;
40
        private volatile ProgressPanel progressPanel = null;
41
        private volatile Thread blinker = null;
42
        private boolean threadSuspended = false;
43
        private boolean ended = false;
44
        private boolean started = false;
45
        
46
        private ArrayList actionCommandListeners = new ArrayList();
47
        private boolean bDoCallListeners = true;
48
        static private int eventId = Integer.MIN_VALUE;
49

    
50
        public IncrementableTask(IIncrementable incrementable) {
51
                progressPanel = getProgressPanel();
52
                iIncrementable = incrementable;
53
        }
54

    
55
        public void start() {
56
                blinker = new Thread(this);
57
                blinker.start();
58
        }
59
        
60
        public synchronized void stop() {
61
                ended = true;
62
                blinker = null;
63
                notify();
64
        }        
65

    
66
        /**
67
         * Este thread va leyendo el porcentaje hasta que se completa el histograma.
68
         */
69
        public synchronized void run() {
70
                getProgressPanel().setPercent(0);
71
                while (!ended && (iIncrementable.getPercent() < 100)) {
72
                        try {
73
                                getProgressPanel().setLabel(iIncrementable.getLabel());
74
                                getProgressPanel().setPercent(iIncrementable.getPercent());
75
                                getProgressPanel().setTitle(iIncrementable.getTitle());
76
                                getProgressPanel().setLog(iIncrementable.getLog());
77
                                started = true;
78
                                Thread.sleep(100);
79
                                synchronized(this) {
80
                                        while (threadSuspended && !ended)
81
                                                wait(500);
82
                                }
83
                        } catch (InterruptedException e) {
84
                        }
85
                }
86
        }
87
        
88
        public boolean getStarted() {
89
                return started;
90
        }
91

    
92
        public void Hide() {
93
                getProgressPanel().setVisible(false);
94
                getProgressPanel().hide();
95
                progressPanel = null;
96
                this.stop();
97
        }
98
        
99
        public boolean isAlive() {
100
                return blinker.isAlive();
101
        }
102

    
103
        /**
104
         * Muestra la ventana de incremento con el porcentaje de la construcci?n del
105
         * histograma.
106
         */
107
        public void showWindow(){
108
                getProgressPanel().setTitle(iIncrementable.getTitle());
109
                getProgressPanel().showLog(false);
110
                getProgressPanel().show();
111
        }
112
        
113
        private ProgressPanel getProgressPanel() {
114
                if (progressPanel == null) {
115
                        progressPanel = new ProgressPanel();
116
                        progressPanel.addButtonPressedListener(this);
117
                        progressPanel.addWindowListener( new WindowAdapter() {
118
        public void windowClosing(WindowEvent e)
119
        {
120
                ended = true;
121
                                  callActionCommandListeners(IncrementableEvent.CANCELED);
122
        }
123
      });
124
                }
125
                return progressPanel;
126
        }
127

    
128
        private void callActionCommandListeners(int actions) {
129
                if (!bDoCallListeners)
130
                        return;
131
                Iterator acIterator = actionCommandListeners.iterator();
132
                while (acIterator.hasNext()) {
133
                        IncrementableListener listener = (IncrementableListener) acIterator.next();
134
                        switch (actions) {
135
                                case IncrementableEvent.RESUMED:
136
                                        listener.actionResumed(new IncrementableEvent(this));
137
                                        break;
138
                                case IncrementableEvent.SUSPENDED:
139
                                        listener.actionSuspended(new IncrementableEvent(this));
140
                                        break;
141
                                case IncrementableEvent.CANCELED:
142
                                        listener.actionCanceled(new IncrementableEvent(this));
143
                                        break;
144
                        }
145
                }
146
                eventId++;
147
        }
148

    
149
        public void addIncrementableListener(IncrementableListener listener) {
150
                if (!actionCommandListeners.contains(listener))
151
                        actionCommandListeners.add(listener);
152
        }
153

    
154
        public void removeIncrementableListener(IncrementableListener listener) {
155
                actionCommandListeners.remove(listener);
156
        }
157

    
158
        public void actionButtonPressed(ButtonsPanelEvent e) {
159
                switch (e.getButton()) {
160
                        case ButtonsPanel.BUTTON_CANCEL:
161
                                ended = true;
162
                                callActionCommandListeners(IncrementableEvent.CANCELED);
163
                                break;
164
                        case ButtonsPanel.BUTTON_PAUSE:
165
                                threadSuspended = true;
166
                                callActionCommandListeners(IncrementableEvent.SUSPENDED);
167
                                break;
168
                        case ButtonsPanel.BUTTON_RESTART:
169
                                threadSuspended = false;
170
                                callActionCommandListeners(IncrementableEvent.RESUMED);
171
                                break;
172
                }
173
        }
174
}