Revision 10962 trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/incrementabletask/IncrementableTask.java
IncrementableTask.java | ||
---|---|---|
8 | 8 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
9 | 9 |
import org.gvsig.gui.beans.progresspanel.ProgressPanel; |
10 | 10 |
|
11 |
public class IncrementableTask extends Thread implements ActionListener {
|
|
11 |
public class IncrementableTask implements Runnable, ActionListener {
|
|
12 | 12 |
IIncrementable iIncrementable; |
13 |
ProgressPanel progressPanel = null; |
|
14 |
|
|
13 |
private volatile ProgressPanel progressPanel = getProgressPanel(); |
|
14 |
private volatile Thread blinker; |
|
15 |
private boolean threadSuspended = false; |
|
16 |
|
|
15 | 17 |
public IncrementableTask(IIncrementable incrementable) { |
16 | 18 |
iIncrementable = incrementable; |
17 | 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 |
} |
|
18 | 30 |
|
19 | 31 |
/** |
20 | 32 |
* Este thread va leyendo el porcentaje hasta que se completa el histograma. |
21 | 33 |
*/ |
22 | 34 |
public synchronized void run(){ |
23 |
while(true){ |
|
24 |
showWindow(); |
|
25 |
while (iIncrementable.getPercent() < 100){ |
|
35 |
Thread thisThread = Thread.currentThread(); |
|
36 |
while ((blinker == thisThread) && (iIncrementable.getPercent() < 100)) { |
|
37 |
try { |
|
38 |
Thread.sleep(100); |
|
26 | 39 |
getProgressPanel().setLabel(iIncrementable.getLabel()); |
27 | 40 |
getProgressPanel().setPercent(iIncrementable.getPercent()); |
28 | 41 |
getProgressPanel().setTitle(iIncrementable.getTitle()); |
29 | 42 |
getProgressPanel().setLog(iIncrementable.getLog()); |
30 |
try { |
|
31 |
sleep(100); |
|
32 |
} catch (InterruptedException e) { |
|
33 |
e.printStackTrace(); |
|
43 |
synchronized(this) { |
|
44 |
while (threadSuspended && blinker==thisThread) |
|
45 |
wait(500); |
|
34 | 46 |
} |
47 |
} catch (InterruptedException e) { |
|
35 | 48 |
} |
36 |
//Cerramos la ventana |
|
37 |
|
|
38 |
handleClose(); |
|
39 |
|
|
40 |
this.suspend(); |
|
41 | 49 |
} |
50 |
//Cerramos la ventana |
|
51 |
handleClose(); |
|
42 | 52 |
} |
43 | 53 |
|
44 | 54 |
private void handleClose() { |
45 | 55 |
getProgressPanel().setVisible(false); |
46 | 56 |
getProgressPanel().hide(); |
47 | 57 |
progressPanel = null; |
48 |
System.out.println("Cerrado"); |
|
58 |
if (iIncrementable.getPercent() == 100) |
|
59 |
iIncrementable.closed(); |
|
60 |
else |
|
61 |
iIncrementable.canceled(); |
|
49 | 62 |
} |
50 | 63 |
/** |
51 | 64 |
* Muestra la ventana de incremento con el porcentaje de la construcci?n del |
... | ... | |
56 | 69 |
getProgressPanel().showLog(false); |
57 | 70 |
getProgressPanel().show(); |
58 | 71 |
|
59 |
if(this.isAlive()) |
|
60 |
this.resume(); |
|
61 |
else |
|
62 |
this.start(); |
|
72 |
this.start(); |
|
63 | 73 |
} |
64 | 74 |
|
65 | 75 |
private ProgressPanel getProgressPanel() { |
... | ... | |
69 | 79 |
progressPanel.addWindowListener( new WindowAdapter() { |
70 | 80 |
public void windowClosing(WindowEvent e) |
71 | 81 |
{ |
72 |
handleClose();
|
|
82 |
blinker = null;
|
|
73 | 83 |
} |
74 | 84 |
}); |
75 | 85 |
} |
... | ... | |
78 | 88 |
|
79 | 89 |
public void actionPerformed(ActionEvent e) { |
80 | 90 |
if (e.getActionCommand().compareTo(ButtonsPanel.BUTTON_CANCEL + "") == 0) { |
81 |
handleClose();
|
|
91 |
blinker = null;
|
|
82 | 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 |
} |
|
83 | 101 |
} |
84 | 102 |
} |
Also available in: Unified diff