Revision 11042 trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/incrementabletask/IncrementableTask.java

View differences:

IncrementableTask.java
18 18
*/
19 19
package org.gvsig.gui.beans.incrementabletask;
20 20

  
21
import java.awt.event.ActionEvent;
22
import java.awt.event.ActionListener;
23 21
import java.awt.event.WindowAdapter;
24 22
import java.awt.event.WindowEvent;
25 23
import java.util.ArrayList;
26 24
import java.util.Iterator;
27 25

  
28 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 29
import org.gvsig.gui.beans.progresspanel.ProgressPanel;
30 30
/**
31 31
 * <code>IncrementableTask</code>. Es un dialogo que contiene un ProgressPanel.
......
35 35
 * @version 29/03/2007
36 36
 * @author Borja S?nchez Zamorano (borja.sanchez@iver.es)
37 37
 */
38
public class IncrementableTask implements Runnable, ActionListener {
39
	IIncrementable iIncrementable;
40
	private volatile ProgressPanel progressPanel = getProgressPanel();
41
	private volatile Thread blinker;
38
public class IncrementableTask implements Runnable, ButtonsPanelListener {
39
	IIncrementable iIncrementable = null;
40
	private volatile ProgressPanel progressPanel = null;
41
	private volatile Thread blinker = null;
42 42
	private boolean threadSuspended = false;
43
	private boolean ended = false;
44
	private boolean started = false;
43 45
	
44 46
	private ArrayList actionCommandListeners = new ArrayList();
45 47
	private boolean bDoCallListeners = true;
46 48
	static private int eventId = Integer.MIN_VALUE;
47 49

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

  
52 55
	public void start() {
53 56
		blinker = new Thread(this);
54 57
		blinker.start();
55 58
	}
56 59
	
57 60
	public synchronized void stop() {
61
		ended = true;
58 62
		blinker = null;
59 63
		notify();
60 64
	}	
......
62 66
	/**
63 67
	 * Este thread va leyendo el porcentaje hasta que se completa el histograma.
64 68
	 */
65
	public synchronized void run(){
66
		Thread thisThread = Thread.currentThread();
67
		while ((blinker == thisThread) && (iIncrementable.getPercent() < 100)) {
69
	public synchronized void run() {
70
		getProgressPanel().setPercent(0);
71
		while (!ended && (iIncrementable.getPercent() < 100)) {
68 72
			try {
69
				Thread.sleep(100);
70 73
				getProgressPanel().setLabel(iIncrementable.getLabel());
71 74
				getProgressPanel().setPercent(iIncrementable.getPercent());
72 75
				getProgressPanel().setTitle(iIncrementable.getTitle());
73 76
				getProgressPanel().setLog(iIncrementable.getLog());
77
				started = true;
78
				Thread.sleep(100);
74 79
				synchronized(this) {
75
					while (threadSuspended && blinker==thisThread)
80
					while (threadSuspended && !ended)
76 81
						wait(500);
77 82
				}
78 83
			} catch (InterruptedException e) {
79 84
			}
80 85
		}
81
		//Cerramos la ventana
82
		handleClose();
83 86
	}
87
	
88
	public boolean getStarted() {
89
		return started;
90
	}
84 91

  
85
	private void handleClose() {
92
	public void Hide() {
86 93
		getProgressPanel().setVisible(false);
87 94
		getProgressPanel().hide();
88 95
		progressPanel = null;
89
		if (iIncrementable.getPercent() == 100)
90
			callActionCommandListeners(IncrementableEvent.CLOSED);
91
		else
92
			callActionCommandListeners(IncrementableEvent.CANCELED);
96
		this.stop();
93 97
	}
98
	
99
	public boolean isAlive() {
100
		return blinker.isAlive();
101
	}
102

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

  
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 128
	private void callActionCommandListeners(int actions) {
135 129
		if (!bDoCallListeners)
136 130
			return;
......
138 132
		while (acIterator.hasNext()) {
139 133
			IncrementableListener listener = (IncrementableListener) acIterator.next();
140 134
			switch (actions) {
141
				case IncrementableEvent.CLOSED:
142
					listener.actionClosed(new IncrementableEvent(this));
143
					break;
144 135
				case IncrementableEvent.RESUMED:
145 136
					listener.actionResumed(new IncrementableEvent(this));
146 137
					break;
......
163 154
	public void removeIncrementableListener(IncrementableListener listener) {
164 155
		actionCommandListeners.remove(listener);
165 156
	}
157

  
158
	public void actionButtonPressed(ButtonsPanelEvent e) {
159
		switch (e.getButton()) {
160
			case ButtonsPanel.BUTTON_CANCEL:
161
				ended = true;
162
				callActionCommandListeners(IncrementableEvent.CANCELED);
163
				break;
164
			case ButtonsPanel.BUTTON_PAUSE:
165
				threadSuspended = true;
166
				callActionCommandListeners(IncrementableEvent.SUSPENDED);
167
				break;
168
			case ButtonsPanel.BUTTON_RESTART:
169
				threadSuspended = false;
170
				callActionCommandListeners(IncrementableEvent.RESUMED);
171
				break;
172
		}
173
	}
166 174
}

Also available in: Unified diff