svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / incrementabletask / IncrementableTask.java @ 21577
History | View | Annotate | Download (7.02 KB)
1 | 20830 | bsanchez | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | 20563 | bsanchez | *
|
3 | 20830 | bsanchez | * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
4 | *
|
||
5 | 20563 | bsanchez | * 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 | 20830 | bsanchez | *
|
10 | 20563 | bsanchez | * 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 | 20830 | bsanchez | *
|
15 | 20563 | bsanchez | * 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 | 20830 | bsanchez | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 | 20563 | bsanchez | */
|
19 | 20830 | bsanchez | package org.gvsig.gui.beans.incrementabletask; |
20 | 20563 | bsanchez | |
21 | 10970 | bsanchez | import java.util.ArrayList; |
22 | import java.util.Iterator; |
||
23 | 10948 | nacho | |
24 | 11270 | bsanchez | import javax.swing.JOptionPane; |
25 | 20830 | bsanchez | import javax.swing.WindowConstants; |
26 | 11270 | bsanchez | |
27 | 20830 | bsanchez | import org.gvsig.gui.beans.Messages; |
28 | 10948 | nacho | import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
29 | 11042 | bsanchez | import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent; |
30 | import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener; |
||
31 | 10948 | nacho | import org.gvsig.gui.beans.progresspanel.ProgressPanel; |
32 | 10970 | bsanchez | /**
|
33 | * <code>IncrementableTask</code>. Es un dialogo que contiene un ProgressPanel.
|
||
34 | * Se ejecuta bajo un Thread y va consultando a un objeto de tipo IIncrementable
|
||
35 | * para modificar sus valores.
|
||
36 | *
|
||
37 | 20563 | bsanchez | * @version 23/04/2008
|
38 | 12368 | bsanchez | * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
39 | 10970 | bsanchez | */
|
40 | 11042 | bsanchez | public class IncrementableTask implements Runnable, ButtonsPanelListener { |
41 | 20830 | bsanchez | private ArrayList<IncrementableListener> actionCommandListeners = new ArrayList<IncrementableListener>(); |
42 | private IIncrementable iIncrementable = null; |
||
43 | private volatile ProgressPanel progressPanel = null; |
||
44 | private volatile Thread blinker = null; |
||
45 | private boolean threadSuspended = false; |
||
46 | private boolean ended = false; |
||
47 | private boolean askOnCancel = true; |
||
48 | private boolean bDoCallListeners = true; |
||
49 | static private int eventId = Integer.MIN_VALUE; |
||
50 | 12107 | bsanchez | |
51 | 11273 | bsanchez | /**
|
52 | * Constructor del IncrementableTask.
|
||
53 | * @param incrementable
|
||
54 | */
|
||
55 | 10948 | nacho | public IncrementableTask(IIncrementable incrementable) {
|
56 | iIncrementable = incrementable; |
||
57 | } |
||
58 | 11042 | bsanchez | |
59 | 11273 | bsanchez | /**
|
60 | * Inicio del thread para que la ventana vaya consultando por si sola al
|
||
61 | * iIncrementable
|
||
62 | */
|
||
63 | 10962 | bsanchez | public void start() { |
64 | blinker = new Thread(this); |
||
65 | blinker.start(); |
||
66 | } |
||
67 | 11273 | bsanchez | |
68 | /**
|
||
69 | 11312 | nacho | * Detiene el proceso de consulta de la ventana.
|
70 | 11273 | bsanchez | */
|
71 | 11312 | nacho | public void stop() { |
72 | 11042 | bsanchez | ended = true;
|
73 | 12107 | bsanchez | } |
74 | 10948 | nacho | |
75 | /**
|
||
76 | * Este thread va leyendo el porcentaje hasta que se completa el histograma.
|
||
77 | */
|
||
78 | 11042 | bsanchez | public synchronized void run() { |
79 | 11311 | nacho | while (!ended && (iIncrementable.getPercent() <= 100)) { |
80 | 10962 | bsanchez | try {
|
81 | 10948 | nacho | getProgressPanel().setLabel(iIncrementable.getLabel()); |
82 | getProgressPanel().setPercent(iIncrementable.getPercent()); |
||
83 | getProgressPanel().setTitle(iIncrementable.getTitle()); |
||
84 | getProgressPanel().setLog(iIncrementable.getLog()); |
||
85 | 11042 | bsanchez | Thread.sleep(100); |
86 | 11273 | bsanchez | synchronized (this) { |
87 | 11042 | bsanchez | while (threadSuspended && !ended)
|
88 | 10962 | bsanchez | wait(500);
|
89 | 10948 | nacho | } |
90 | 10962 | bsanchez | } catch (InterruptedException e) { |
91 | 10948 | nacho | } |
92 | } |
||
93 | } |
||
94 | 12107 | bsanchez | |
95 | 11273 | bsanchez | /**
|
96 | 12368 | bsanchez | * Termina el proceso de lectura de porcentajes y logs de la ventana y
|
97 | * cierra esta.
|
||
98 | 11312 | nacho | */
|
99 | public void processFinalize() { |
||
100 | stop(); |
||
101 | while (isAlive());
|
||
102 | hide(); |
||
103 | } |
||
104 | 12107 | bsanchez | |
105 | 11312 | nacho | /**
|
106 | 11273 | bsanchez | * Ocultar la ventana y parar el proceso
|
107 | */
|
||
108 | 11312 | nacho | private void hide() { |
109 | 17855 | nbrodin | hideWindow(); |
110 | 10948 | nacho | progressPanel = null;
|
111 | 12107 | bsanchez | blinker = null;
|
112 | 10948 | nacho | } |
113 | 17855 | nbrodin | |
114 | /**
|
||
115 | * Ocultar la ventana
|
||
116 | */
|
||
117 | public void hideWindow() { |
||
118 | getProgressPanel().setVisible(false);
|
||
119 | } |
||
120 | 12107 | bsanchez | |
121 | 11273 | bsanchez | /**
|
122 | * Devuelve un booleano indicando si esta activa la ventana.
|
||
123 | * @return boolean
|
||
124 | */
|
||
125 | 11042 | bsanchez | public boolean isAlive() { |
126 | 15679 | bsanchez | if (blinker == null) |
127 | return false; |
||
128 | 11042 | bsanchez | return blinker.isAlive();
|
129 | } |
||
130 | |||
131 | 10948 | nacho | /**
|
132 | * Muestra la ventana de incremento con el porcentaje de la construcci?n del
|
||
133 | * histograma.
|
||
134 | */
|
||
135 | 11273 | bsanchez | public void showWindow() { |
136 | 10948 | nacho | getProgressPanel().setTitle(iIncrementable.getTitle()); |
137 | getProgressPanel().showLog(false);
|
||
138 | 12107 | bsanchez | getProgressPanel().setVisible(true);
|
139 | 10948 | nacho | } |
140 | 11273 | bsanchez | |
141 | /**
|
||
142 | * Devuelve el componente ProgressPanel de la ventana incrementable.
|
||
143 | * @return ProgressPanel
|
||
144 | */
|
||
145 | 20563 | bsanchez | public ProgressPanel getProgressPanel() {
|
146 | 10948 | nacho | if (progressPanel == null) { |
147 | 11068 | bsanchez | progressPanel = new ProgressPanel(false); |
148 | 15928 | bsanchez | progressPanel.setAlwaysOnTop(true);
|
149 | 11042 | bsanchez | progressPanel.addButtonPressedListener(this);
|
150 | 20830 | bsanchez | progressPanel.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
|
151 | 10948 | nacho | } |
152 | return progressPanel;
|
||
153 | } |
||
154 | |||
155 | 10970 | bsanchez | private void callActionCommandListeners(int actions) { |
156 | if (!bDoCallListeners)
|
||
157 | return;
|
||
158 | 20830 | bsanchez | Iterator<IncrementableListener> acIterator = actionCommandListeners.iterator();
|
159 | 10970 | bsanchez | while (acIterator.hasNext()) {
|
160 | 20830 | bsanchez | IncrementableListener listener = acIterator.next(); |
161 | 10970 | bsanchez | switch (actions) {
|
162 | case IncrementableEvent.RESUMED:
|
||
163 | listener.actionResumed(new IncrementableEvent(this)); |
||
164 | break;
|
||
165 | case IncrementableEvent.SUSPENDED:
|
||
166 | listener.actionSuspended(new IncrementableEvent(this)); |
||
167 | break;
|
||
168 | case IncrementableEvent.CANCELED:
|
||
169 | listener.actionCanceled(new IncrementableEvent(this)); |
||
170 | break;
|
||
171 | } |
||
172 | } |
||
173 | eventId++; |
||
174 | } |
||
175 | |||
176 | 11273 | bsanchez | /**
|
177 | * A?adir el manejador de eventos para atender las peticiones de start,
|
||
178 | * stop...
|
||
179 | 12368 | bsanchez | *
|
180 | 11273 | bsanchez | * @param listener
|
181 | */
|
||
182 | 10970 | bsanchez | public void addIncrementableListener(IncrementableListener listener) { |
183 | if (!actionCommandListeners.contains(listener))
|
||
184 | actionCommandListeners.add(listener); |
||
185 | } |
||
186 | |||
187 | 11273 | bsanchez | /**
|
188 | * Borrar un manejador de eventos.
|
||
189 | * @param listener
|
||
190 | */
|
||
191 | 10970 | bsanchez | public void removeIncrementableListener(IncrementableListener listener) { |
192 | actionCommandListeners.remove(listener); |
||
193 | } |
||
194 | 11042 | bsanchez | |
195 | 11270 | bsanchez | /**
|
196 | 11273 | bsanchez | * Definir si queremos que confirme al usuario si realmente desea cancelar el
|
197 | * proceso
|
||
198 | 12368 | bsanchez | *
|
199 | 11270 | bsanchez | * @param value
|
200 | */
|
||
201 | public void setAskCancel(boolean value) { |
||
202 | askOnCancel = value; |
||
203 | } |
||
204 | 12107 | bsanchez | |
205 | 11273 | bsanchez | /**
|
206 | * Metodo para gestionar todos los eventos del objeto.
|
||
207 | */
|
||
208 | 11042 | bsanchez | public void actionButtonPressed(ButtonsPanelEvent e) { |
209 | switch (e.getButton()) {
|
||
210 | case ButtonsPanel.BUTTON_CANCEL:
|
||
211 | 11270 | bsanchez | boolean cancelled = true; |
212 | if (askOnCancel) {
|
||
213 | cancelled = false;
|
||
214 | String string1 = Messages.getText("si"); |
||
215 | String string2 = Messages.getText("no"); |
||
216 | Object[] options = { string1, string2 }; |
||
217 | 11707 | bsanchez | int answer = JOptionPane.showOptionDialog(getProgressPanel(), Messages |
218 | 11273 | bsanchez | .getText("msg_cancel_incrementable"), Messages
|
219 | .getText("title_cancel_incrementable"),
|
||
220 | 11270 | bsanchez | JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, |
221 | options, string1); |
||
222 | if (answer == JOptionPane.YES_OPTION) |
||
223 | cancelled = true;
|
||
224 | } |
||
225 | if (cancelled) {
|
||
226 | ended = true;
|
||
227 | callActionCommandListeners(IncrementableEvent.CANCELED); |
||
228 | } |
||
229 | 11042 | bsanchez | break;
|
230 | case ButtonsPanel.BUTTON_PAUSE:
|
||
231 | threadSuspended = true;
|
||
232 | callActionCommandListeners(IncrementableEvent.SUSPENDED); |
||
233 | break;
|
||
234 | case ButtonsPanel.BUTTON_RESTART:
|
||
235 | threadSuspended = false;
|
||
236 | callActionCommandListeners(IncrementableEvent.RESUMED); |
||
237 | break;
|
||
238 | } |
||
239 | } |
||
240 | 20563 | bsanchez | |
241 | /**
|
||
242 | * @see ProgressPanel#getButtonsPanel()
|
||
243 | */
|
||
244 | public ButtonsPanel getButtonsPanel() {
|
||
245 | return getProgressPanel().getButtonsPanel();
|
||
246 | } |
||
247 | 10948 | nacho | } |