Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / incrementabletask / IncrementableTask.java @ 11004

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

    
28
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
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, ActionListener {
39
        IIncrementable iIncrementable;
40
        private volatile ProgressPanel progressPanel = getProgressPanel();
41
        private volatile Thread blinker;
42
        private boolean threadSuspended = false;
43
        
44
        private ArrayList actionCommandListeners = new ArrayList();
45
        private boolean bDoCallListeners = true;
46
        static private int eventId = Integer.MIN_VALUE;
47

    
48
        public IncrementableTask(IIncrementable incrementable) {
49
                iIncrementable = incrementable;
50
        }
51
        
52
        public void start() {
53
                blinker = new Thread(this);
54
                blinker.start();
55
        }
56
        
57
        public synchronized void stop() {
58
                blinker = null;
59
                notify();
60
        }        
61

    
62
        /**
63
         * Este thread va leyendo el porcentaje hasta que se completa el histograma.
64
         */
65
        public synchronized void run(){
66
                Thread thisThread = Thread.currentThread();
67
                while ((blinker == thisThread) && (iIncrementable.getPercent() < 100)) {
68
                        try {
69
                                Thread.sleep(100);
70
                                getProgressPanel().setLabel(iIncrementable.getLabel());
71
                                getProgressPanel().setPercent(iIncrementable.getPercent());
72
                                getProgressPanel().setTitle(iIncrementable.getTitle());
73
                                getProgressPanel().setLog(iIncrementable.getLog());
74
                                synchronized(this) {
75
                                        while (threadSuspended && blinker==thisThread)
76
                                                wait(500);
77
                                }
78
                        } catch (InterruptedException e) {
79
                        }
80
                }
81
                //Cerramos la ventana
82
                handleClose();
83
        }
84

    
85
        private void handleClose() {
86
                getProgressPanel().setVisible(false);
87
                getProgressPanel().hide();
88
                progressPanel = null;
89
                if (iIncrementable.getPercent() == 100)
90
                        callActionCommandListeners(IncrementableEvent.CLOSED);
91
                else
92
                        callActionCommandListeners(IncrementableEvent.CANCELED);
93
        }
94
        /**
95
         * Muestra la ventana de incremento con el porcentaje de la construcci?n del
96
         * histograma.
97
         */
98
        public void showWindow(){
99
                getProgressPanel().setTitle(iIncrementable.getTitle());
100
                getProgressPanel().showLog(false);
101
                getProgressPanel().show();
102
                
103
                this.start();
104
        }
105
        
106
        private ProgressPanel getProgressPanel() {
107
                if (progressPanel == null) {
108
                        progressPanel = new ProgressPanel();
109
                        progressPanel.getButtonsPanel().addActionListener(this);
110
                        progressPanel.addWindowListener( new WindowAdapter() {
111
        public void windowClosing(WindowEvent e)
112
        {
113
                blinker = null;
114
        }
115
      });
116
                }
117
                return progressPanel;
118
        }
119

    
120
        public void actionPerformed(ActionEvent e) {
121
                if (e.getActionCommand().compareTo(ButtonsPanel.BUTTON_CANCEL + "") == 0) {
122
            blinker = null;
123
                }
124
                if (e.getActionCommand().compareTo(ButtonsPanel.BUTTON_PAUSE + "") == 0) {
125
                        callActionCommandListeners(IncrementableEvent.SUSPENDED);
126
                        threadSuspended = true;
127
                }
128
                if (e.getActionCommand().compareTo(ButtonsPanel.BUTTON_RESTART + "") == 0) {
129
                        callActionCommandListeners(IncrementableEvent.RESUMED);
130
                        threadSuspended = false;
131
                }
132
        }
133
        
134
        private void callActionCommandListeners(int actions) {
135
                if (!bDoCallListeners)
136
                        return;
137
                Iterator acIterator = actionCommandListeners.iterator();
138
                while (acIterator.hasNext()) {
139
                        IncrementableListener listener = (IncrementableListener) acIterator.next();
140
                        switch (actions) {
141
                                case IncrementableEvent.CLOSED:
142
                                        listener.actionClosed(new IncrementableEvent(this));
143
                                        break;
144
                                case IncrementableEvent.RESUMED:
145
                                        listener.actionResumed(new IncrementableEvent(this));
146
                                        break;
147
                                case IncrementableEvent.SUSPENDED:
148
                                        listener.actionSuspended(new IncrementableEvent(this));
149
                                        break;
150
                                case IncrementableEvent.CANCELED:
151
                                        listener.actionCanceled(new IncrementableEvent(this));
152
                                        break;
153
                        }
154
                }
155
                eventId++;
156
        }
157

    
158
        public void addIncrementableListener(IncrementableListener listener) {
159
                if (!actionCommandListeners.contains(listener))
160
                        actionCommandListeners.add(listener);
161
        }
162

    
163
        public void removeIncrementableListener(IncrementableListener listener) {
164
                actionCommandListeners.remove(listener);
165
        }
166
}