Statistics
| Revision:

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

History | View | Annotate | Download (5.03 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
        
45
        private ArrayList actionCommandListeners = new ArrayList();
46
        private boolean bDoCallListeners = true;
47
        static private int eventId = Integer.MIN_VALUE;
48

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

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

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

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

    
121
        private void callActionCommandListeners(int actions) {
122
                if (!bDoCallListeners)
123
                        return;
124
                Iterator acIterator = actionCommandListeners.iterator();
125
                while (acIterator.hasNext()) {
126
                        IncrementableListener listener = (IncrementableListener) acIterator.next();
127
                        switch (actions) {
128
                                case IncrementableEvent.RESUMED:
129
                                        listener.actionResumed(new IncrementableEvent(this));
130
                                        break;
131
                                case IncrementableEvent.SUSPENDED:
132
                                        listener.actionSuspended(new IncrementableEvent(this));
133
                                        break;
134
                                case IncrementableEvent.CANCELED:
135
                                        listener.actionCanceled(new IncrementableEvent(this));
136
                                        break;
137
                        }
138
                }
139
                eventId++;
140
        }
141

    
142
        public void addIncrementableListener(IncrementableListener listener) {
143
                if (!actionCommandListeners.contains(listener))
144
                        actionCommandListeners.add(listener);
145
        }
146

    
147
        public void removeIncrementableListener(IncrementableListener listener) {
148
                actionCommandListeners.remove(listener);
149
        }
150

    
151
        public void actionButtonPressed(ButtonsPanelEvent e) {
152
                switch (e.getButton()) {
153
                        case ButtonsPanel.BUTTON_CANCEL:
154
                                ended = true;
155
                                callActionCommandListeners(IncrementableEvent.CANCELED);
156
                                break;
157
                        case ButtonsPanel.BUTTON_PAUSE:
158
                                threadSuspended = true;
159
                                callActionCommandListeners(IncrementableEvent.SUSPENDED);
160
                                break;
161
                        case ButtonsPanel.BUTTON_RESTART:
162
                                threadSuspended = false;
163
                                callActionCommandListeners(IncrementableEvent.RESUMED);
164
                                break;
165
                }
166
        }
167
}