Statistics
| Revision:

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

History | View | Annotate | Download (7.06 KB)

1
package org.gvsig.gui.beans.incrementabletask;
2

    
3
/* gvSIG. Geographic Information System of the Valencian Government
4
 *
5
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
6
 * of the Valencian Government (CIT)
7
 * 
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 * 
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *  
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
21
 * MA  02110-1301, USA.
22
 * 
23
 */
24

    
25
import java.awt.event.WindowAdapter;
26
import java.awt.event.WindowEvent;
27
import java.util.ArrayList;
28
import java.util.Iterator;
29

    
30
import javax.swing.JOptionPane;
31

    
32
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
33
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
34
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
35
import org.gvsig.gui.beans.Messages;
36
import org.gvsig.gui.beans.progresspanel.ProgressPanel;
37
/**
38
 * <code>IncrementableTask</code>. Es un dialogo que contiene un ProgressPanel.
39
 * Se ejecuta bajo un Thread y va consultando a un objeto de tipo IIncrementable
40
 * para modificar sus valores.
41
 *
42
 * @version 23/04/2008
43
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
44
 */
45
public class IncrementableTask implements Runnable, ButtonsPanelListener {
46
        IIncrementable iIncrementable = null;
47
        private volatile ProgressPanel progressPanel = null;
48
        private volatile Thread blinker = null;
49
        private boolean threadSuspended = false;
50
        private boolean ended = false;
51
        private boolean askOnCancel = true;
52

    
53
        private ArrayList actionCommandListeners = new ArrayList();
54
        private boolean bDoCallListeners = true;
55
        static private int eventId = Integer.MIN_VALUE;
56

    
57
        /**
58
         * Constructor del IncrementableTask.
59
         * @param incrementable
60
         */
61
        public IncrementableTask(IIncrementable incrementable) {
62
                iIncrementable = incrementable;
63
        }
64

    
65
        /**
66
         * Inicio del thread para que la ventana vaya consultando por si sola al
67
         * iIncrementable
68
         */
69
        public void start() {
70
                blinker = new Thread(this);
71
                blinker.start();
72
        }
73

    
74
        /**
75
         * Detiene el proceso de consulta de la ventana.
76
         */
77
        public void stop() {
78
                ended = true;
79
        }
80

    
81
        /**
82
         * Este thread va leyendo el porcentaje hasta que se completa el histograma.
83
         */
84
        public synchronized void run() {
85
                while (!ended && (iIncrementable.getPercent() <= 100)) {
86
                        try {
87
                                getProgressPanel().setLabel(iIncrementable.getLabel());
88
                                getProgressPanel().setPercent(iIncrementable.getPercent());
89
                                getProgressPanel().setTitle(iIncrementable.getTitle());
90
                                getProgressPanel().setLog(iIncrementable.getLog());
91
                                Thread.sleep(100);
92
                                synchronized (this) {
93
                                        while (threadSuspended && !ended)
94
                                                wait(500);
95
                                }
96
                        } catch (InterruptedException e) {
97
                        }
98
                }
99
        }
100

    
101
        /**
102
         * Termina el proceso de lectura de porcentajes y logs de la ventana y
103
         * cierra esta.
104
         */
105
        public void processFinalize() {
106
                stop();
107
                while (isAlive());
108
                hide();
109
        }
110

    
111
        /**
112
         * Ocultar la ventana y parar el proceso
113
         */
114
        private void hide() {
115
                hideWindow();
116
                progressPanel = null;
117
                blinker = null;
118
        }
119
        
120
        /**
121
         * Ocultar la ventana
122
         */
123
        public void hideWindow() {
124
                getProgressPanel().setVisible(false);
125
        }
126

    
127
        /**
128
         * Devuelve un booleano indicando si esta activa la ventana.
129
         * @return boolean
130
         */
131
        public boolean isAlive() {
132
                if (blinker == null)
133
                        return false;
134
                return blinker.isAlive();
135
        }
136

    
137
        /**
138
         * Muestra la ventana de incremento con el porcentaje de la construcci?n del
139
         * histograma.
140
         */
141
        public void showWindow() {
142
                getProgressPanel().setTitle(iIncrementable.getTitle());
143
                getProgressPanel().showLog(false);
144
                getProgressPanel().setVisible(true);
145
        }
146

    
147
        /**
148
         * Devuelve el componente ProgressPanel de la ventana incrementable.
149
         * @return ProgressPanel
150
         */
151
        public ProgressPanel getProgressPanel() {
152
                if (progressPanel == null) {
153
                        progressPanel = new ProgressPanel(false);
154
                        progressPanel.setAlwaysOnTop(true);
155
                        progressPanel.addButtonPressedListener(this);
156
                        progressPanel.addWindowListener(new WindowAdapter() {
157
                                public void windowClosing(WindowEvent e) {
158
                                        ended = true;
159
                                        callActionCommandListeners(IncrementableEvent.CANCELED);
160
                                }
161
                        });
162
                }
163
                return progressPanel;
164
        }
165

    
166
        private void callActionCommandListeners(int actions) {
167
                if (!bDoCallListeners)
168
                        return;
169
                Iterator acIterator = actionCommandListeners.iterator();
170
                while (acIterator.hasNext()) {
171
                        IncrementableListener listener = (IncrementableListener) acIterator.next();
172
                        switch (actions) {
173
                                case IncrementableEvent.RESUMED:
174
                                        listener.actionResumed(new IncrementableEvent(this));
175
                                        break;
176
                                case IncrementableEvent.SUSPENDED:
177
                                        listener.actionSuspended(new IncrementableEvent(this));
178
                                        break;
179
                                case IncrementableEvent.CANCELED:
180
                                        listener.actionCanceled(new IncrementableEvent(this));
181
                                        break;
182
                        }
183
                }
184
                eventId++;
185
        }
186

    
187
        /**
188
         * A?adir el manejador de eventos para atender las peticiones de start,
189
         * stop...
190
         *
191
         * @param listener
192
         */
193
        public void addIncrementableListener(IncrementableListener listener) {
194
                if (!actionCommandListeners.contains(listener))
195
                        actionCommandListeners.add(listener);
196
        }
197

    
198
        /**
199
         * Borrar un manejador de eventos.
200
         * @param listener
201
         */
202
        public void removeIncrementableListener(IncrementableListener listener) {
203
                actionCommandListeners.remove(listener);
204
        }
205

    
206
        /**
207
         * Definir si queremos que confirme al usuario si realmente desea cancelar el
208
         * proceso
209
         *
210
         * @param value
211
         */
212
        public void setAskCancel(boolean value) {
213
                askOnCancel = value;
214
        }
215

    
216
        /**
217
         * Metodo para gestionar todos los eventos del objeto.
218
         */
219
        public void actionButtonPressed(ButtonsPanelEvent e) {
220
                switch (e.getButton()) {
221
                        case ButtonsPanel.BUTTON_CANCEL:
222
                                boolean cancelled = true;
223
                                if (askOnCancel) {
224
                                        cancelled = false;
225
                                        String string1 = Messages.getText("si");
226
                                        String string2 = Messages.getText("no");
227
                                        Object[] options = { string1, string2 };
228
                                        int answer = JOptionPane.showOptionDialog(getProgressPanel(), Messages
229
                                                        .getText("msg_cancel_incrementable"), Messages
230
                                                        .getText("title_cancel_incrementable"),
231
                                                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
232
                                                        options, string1);
233
                                        if (answer == JOptionPane.YES_OPTION)
234
                                                cancelled = true;
235
                                }
236
                                if (cancelled) {
237
                                        ended = true;
238
                                        callActionCommandListeners(IncrementableEvent.CANCELED);
239
                                }
240
                                break;
241
                        case ButtonsPanel.BUTTON_PAUSE:
242
                                threadSuspended = true;
243
                                callActionCommandListeners(IncrementableEvent.SUSPENDED);
244
                                break;
245
                        case ButtonsPanel.BUTTON_RESTART:
246
                                threadSuspended = false;
247
                                callActionCommandListeners(IncrementableEvent.RESUMED);
248
                                break;
249
                }
250
        }
251

    
252
        /**
253
         * @see ProgressPanel#getButtonsPanel()
254
         */
255
        public ButtonsPanel getButtonsPanel() {
256
                return getProgressPanel().getButtonsPanel();
257
        }
258
}