Statistics
| Revision:

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

History | View | Annotate | Download (2.75 KB)

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

    
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5
import java.awt.event.WindowAdapter;
6
import java.awt.event.WindowEvent;
7

    
8
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
9
import org.gvsig.gui.beans.progresspanel.ProgressPanel;
10

    
11
public class IncrementableTask implements Runnable, ActionListener {
12
        IIncrementable iIncrementable;
13
        private volatile ProgressPanel progressPanel = getProgressPanel();
14
        private volatile Thread blinker;
15
        private boolean threadSuspended = false;
16

    
17
        public IncrementableTask(IIncrementable incrementable) {
18
                iIncrementable = incrementable;
19
        }
20
        
21
        public void start() {
22
                blinker = new Thread(this);
23
                blinker.start();
24
        }
25
        
26
        public synchronized void stop() {
27
                blinker = null;
28
                notify();
29
        }        
30

    
31
        /**
32
         * Este thread va leyendo el porcentaje hasta que se completa el histograma.
33
         */
34
        public synchronized void run(){
35
                Thread thisThread = Thread.currentThread();
36
                while ((blinker == thisThread) && (iIncrementable.getPercent() < 100)) {
37
                        try {
38
                                Thread.sleep(100);
39
                                getProgressPanel().setLabel(iIncrementable.getLabel());
40
                                getProgressPanel().setPercent(iIncrementable.getPercent());
41
                                getProgressPanel().setTitle(iIncrementable.getTitle());
42
                                getProgressPanel().setLog(iIncrementable.getLog());
43
                                synchronized(this) {
44
                                        while (threadSuspended && blinker==thisThread)
45
                                                wait(500);
46
                                }
47
                        } catch (InterruptedException e) {
48
                        }
49
                }
50
                //Cerramos la ventana
51
                handleClose();
52
        }
53

    
54
        private void handleClose() {
55
                getProgressPanel().setVisible(false);
56
                getProgressPanel().hide();
57
                progressPanel = null;
58
                if (iIncrementable.getPercent() == 100)
59
                        iIncrementable.closed();
60
                else
61
                        iIncrementable.canceled();
62
        }
63
        /**
64
         * Muestra la ventana de incremento con el porcentaje de la construcci?n del
65
         * histograma.
66
         */
67
        public void showWindow(){
68
                getProgressPanel().setTitle(iIncrementable.getTitle());
69
                getProgressPanel().showLog(false);
70
                getProgressPanel().show();
71
                
72
                this.start();
73
        }
74
        
75
        private ProgressPanel getProgressPanel() {
76
                if (progressPanel == null) {
77
                        progressPanel = new ProgressPanel();
78
                        progressPanel.getButtonsPanel().addActionListener(this);
79
                        progressPanel.addWindowListener( new WindowAdapter() {
80
        public void windowClosing(WindowEvent e)
81
        {
82
                blinker = null;
83
        }
84
      });
85
                }
86
                return progressPanel;
87
        }
88

    
89
        public void actionPerformed(ActionEvent e) {
90
                if (e.getActionCommand().compareTo(ButtonsPanel.BUTTON_CANCEL + "") == 0) {
91
            blinker = null;
92
                }
93
                if (e.getActionCommand().compareTo(ButtonsPanel.BUTTON_PAUSE + "") == 0) {
94
                        iIncrementable.suspended();
95
                        threadSuspended = true;
96
                }
97
                if (e.getActionCommand().compareTo(ButtonsPanel.BUTTON_RESTART + "") == 0) {
98
                        iIncrementable.resumed();
99
                        threadSuspended = false;
100
                }
101
        }
102
}