gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.algorithm / src / main / java / org / gvsig / raster / algorithm / gui / ProgressPanel.java @ 2106
History | View | Annotate | Download (10.9 KB)
1 | 2106 | nbrodin | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | package org.gvsig.raster.algorithm.gui; |
||
25 | |||
26 | import java.awt.Dialog; |
||
27 | import java.awt.Dimension; |
||
28 | import java.awt.Frame; |
||
29 | import java.awt.GraphicsConfiguration; |
||
30 | import java.awt.HeadlessException; |
||
31 | import java.awt.Toolkit; |
||
32 | |||
33 | import javax.swing.JDialog; |
||
34 | import javax.swing.JLabel; |
||
35 | import javax.swing.JPanel; |
||
36 | import javax.swing.JProgressBar; |
||
37 | import javax.swing.JScrollPane; |
||
38 | import javax.swing.JTextPane; |
||
39 | |||
40 | import org.gvsig.i18n.Messages; |
||
41 | import org.gvsig.raster.swing.RasterSwingLocator; |
||
42 | import org.gvsig.raster.swing.basepanel.ButtonsPanelEvent; |
||
43 | import org.gvsig.raster.swing.basepanel.ButtonsPanelListener; |
||
44 | import org.gvsig.raster.swing.basepanel.IButtonsPanel; |
||
45 | import org.gvsig.raster.swing.impl.basepanel.ButtonsPanel; |
||
46 | |||
47 | |||
48 | /**
|
||
49 | * <code>ProgressPanel</code>. Muestra una ventana de di?logo para representar
|
||
50 | * una barra de progreso con su ventana de registro.
|
||
51 | *
|
||
52 | * @version 15/07/2008
|
||
53 | *
|
||
54 | * @author BorSanZa - Borja Sanchez Zamorano (borja.sanchez@iver.es)
|
||
55 | */
|
||
56 | public class ProgressPanel extends JDialog { |
||
57 | private static final long serialVersionUID = 4321011219898234352L; |
||
58 | |||
59 | private JPanel jPanel = null; |
||
60 | private JLabel jLabel = null; |
||
61 | private JLabel jLabel1 = null; |
||
62 | private JPanel jPanel1 = null; |
||
63 | private JPanel njp = null; |
||
64 | private IButtonsPanel defaultButtonsPanel = null; |
||
65 | private JProgressBar jProgressBar = null; |
||
66 | private JScrollPane jScrollPane = null; |
||
67 | private JTextPane jTextPane = null; |
||
68 | private LogControl text = new LogControl(); |
||
69 | private long startTime = System.currentTimeMillis(); |
||
70 | private boolean showPause = true; |
||
71 | |||
72 | /**
|
||
73 | * Constructor
|
||
74 | */
|
||
75 | public ProgressPanel() {
|
||
76 | super();
|
||
77 | initialize(); |
||
78 | } |
||
79 | |||
80 | public ProgressPanel(boolean showPause) { |
||
81 | super();
|
||
82 | this.showPause = showPause;
|
||
83 | initialize(); |
||
84 | } |
||
85 | |||
86 | /**
|
||
87 | * @see JDialog#JDialog(Dialog, boolean)
|
||
88 | */
|
||
89 | public ProgressPanel(Dialog owner, boolean modal) throws HeadlessException { |
||
90 | super(owner, modal);
|
||
91 | initialize(); |
||
92 | } |
||
93 | |||
94 | /**
|
||
95 | * @see JDialog#JDialog(Dialog, String, boolean, GraphicsConfiguration)
|
||
96 | */
|
||
97 | public ProgressPanel(Dialog owner, String title, boolean modal, GraphicsConfiguration gc) throws HeadlessException { |
||
98 | super(owner, title, modal, gc);
|
||
99 | initialize(); |
||
100 | } |
||
101 | |||
102 | /**
|
||
103 | * @see JDialog#JDialog(Dialog, String, boolean)
|
||
104 | */
|
||
105 | public ProgressPanel(Dialog owner, String title, boolean modal) |
||
106 | throws HeadlessException { |
||
107 | super(owner, title, modal);
|
||
108 | initialize(); |
||
109 | } |
||
110 | |||
111 | /**
|
||
112 | * @see JDialog#JDialog(Dialog, String)
|
||
113 | */
|
||
114 | public ProgressPanel(Dialog owner, String title) throws HeadlessException { |
||
115 | super(owner, title);
|
||
116 | initialize(); |
||
117 | } |
||
118 | |||
119 | /**
|
||
120 | * @see JDialog#JDialog(Dialog)
|
||
121 | */
|
||
122 | public ProgressPanel(Dialog owner) throws HeadlessException { |
||
123 | super(owner);
|
||
124 | initialize(); |
||
125 | } |
||
126 | |||
127 | /**
|
||
128 | * @see JDialog#JDialog(Frame, boolean)
|
||
129 | */
|
||
130 | public ProgressPanel(Frame owner, boolean modal) throws HeadlessException { |
||
131 | super(owner, modal);
|
||
132 | initialize(); |
||
133 | } |
||
134 | |||
135 | /**
|
||
136 | * @see JDialog#JDialog(Frame, String, boolean, GraphicsConfiguration)
|
||
137 | */
|
||
138 | public ProgressPanel(Frame owner, String title, boolean modal, GraphicsConfiguration gc) { |
||
139 | super(owner, title, modal, gc);
|
||
140 | initialize(); |
||
141 | } |
||
142 | |||
143 | /**
|
||
144 | * @see JDialog#JDialog(Frame, String, boolean)
|
||
145 | */
|
||
146 | public ProgressPanel(Frame owner, String title, boolean modal) |
||
147 | throws HeadlessException { |
||
148 | super(owner, title, modal);
|
||
149 | initialize(); |
||
150 | } |
||
151 | |||
152 | /**
|
||
153 | * @see JDialog#JDialog(Frame, String)
|
||
154 | */
|
||
155 | public ProgressPanel(Frame owner, String title) throws HeadlessException { |
||
156 | super(owner, title);
|
||
157 | initialize(); |
||
158 | } |
||
159 | |||
160 | /**
|
||
161 | * @see JDialog#JDialog(Frame)
|
||
162 | */
|
||
163 | public ProgressPanel(Frame owner) throws HeadlessException { |
||
164 | super(owner);
|
||
165 | initialize(); |
||
166 | } |
||
167 | |||
168 | /**
|
||
169 | * This method initializes this
|
||
170 | */
|
||
171 | private void initialize() { |
||
172 | njp = new JPanel(); |
||
173 | njp.setLayout(new java.awt.BorderLayout(5, 5)); |
||
174 | njp.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 15, 5, 15)); |
||
175 | this.setContentPane(njp);
|
||
176 | this.setResizable(false); |
||
177 | njp.add(getJPanel(), java.awt.BorderLayout.NORTH); |
||
178 | njp.add(getJPanel1(), java.awt.BorderLayout.CENTER); |
||
179 | njp.add(getButtonsPanel().getComponent(), java.awt.BorderLayout.SOUTH); |
||
180 | showLog(false);
|
||
181 | setPercent(0);
|
||
182 | Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); |
||
183 | this.setLocation(
|
||
184 | (int) (d.getWidth() - this.getWidth()) >> 1, |
||
185 | (int) (d.getHeight() - this.getHeight()) >> 1); |
||
186 | this.setVisible(true); |
||
187 | } |
||
188 | |||
189 | /**
|
||
190 | * This method initializes jPanel
|
||
191 | *
|
||
192 | * @return javax.swing.JPanel
|
||
193 | */
|
||
194 | private JPanel getJPanel() { |
||
195 | if (jPanel == null) { |
||
196 | jLabel1 = new JLabel(); |
||
197 | jLabel = new JLabel(); |
||
198 | jLabel.setText(Messages.getText("espere") + "..."); |
||
199 | jPanel = new JPanel(); |
||
200 | jPanel.setLayout(new java.awt.BorderLayout(5, 5)); |
||
201 | jPanel.add(jLabel, java.awt.BorderLayout.WEST); |
||
202 | jPanel.add(jLabel1, java.awt.BorderLayout.EAST); |
||
203 | } |
||
204 | return jPanel;
|
||
205 | } |
||
206 | |||
207 | /**
|
||
208 | * This method initializes jPanel1
|
||
209 | *
|
||
210 | * @return javax.swing.JPanel
|
||
211 | */
|
||
212 | private JPanel getJPanel1() { |
||
213 | if (jPanel1 == null) { |
||
214 | jPanel1 = new JPanel(); |
||
215 | jPanel1.setLayout(new java.awt.BorderLayout(5, 5)); |
||
216 | jPanel1.add(getJProgressBar(), java.awt.BorderLayout.NORTH); |
||
217 | jPanel1.add(getJScrollPane(), java.awt.BorderLayout.CENTER); |
||
218 | } |
||
219 | return jPanel1;
|
||
220 | } |
||
221 | |||
222 | public void addButtonPressedListener(ButtonsPanelListener listener) { |
||
223 | getDefaultButtonsPanel().addButtonPressedListener(listener); |
||
224 | } |
||
225 | |||
226 | public void removeButtonPressedListener(ButtonsPanelListener listener) { |
||
227 | getDefaultButtonsPanel().removeButtonPressedListener(listener); |
||
228 | } |
||
229 | |||
230 | public void showLog(boolean visible) { |
||
231 | getButtonsPanel().getButton(ButtonsPanel.BUTTON_SEEDETAILS).setVisible(!visible); |
||
232 | getButtonsPanel().getButton(ButtonsPanel.BUTTON_HIDEDETAILS).setVisible(visible); |
||
233 | jScrollPane.setVisible(visible); |
||
234 | |||
235 | this.setIgnoreRepaint(true); |
||
236 | |||
237 | if (visible)
|
||
238 | this.setSize(this.getWidth(), 300); |
||
239 | else
|
||
240 | this.pack();
|
||
241 | |||
242 | this.setIgnoreRepaint(false); |
||
243 | if (this.isVisible()) |
||
244 | this.setVisible(true); |
||
245 | } |
||
246 | |||
247 | public void showPause(boolean visible) { |
||
248 | if (showPause) {
|
||
249 | getButtonsPanel().getButton(ButtonsPanel.BUTTON_RESTART).setVisible(!visible); |
||
250 | getButtonsPanel().getButton(ButtonsPanel.BUTTON_PAUSE).setVisible(visible); |
||
251 | } else {
|
||
252 | getButtonsPanel().getButton(ButtonsPanel.BUTTON_RESTART).setVisible(false);
|
||
253 | getButtonsPanel().getButton(ButtonsPanel.BUTTON_PAUSE).setVisible(false);
|
||
254 | } |
||
255 | } |
||
256 | |||
257 | /**
|
||
258 | * @see DefaultButtonsPanel#getButtonsPanel()
|
||
259 | */
|
||
260 | public IButtonsPanel getButtonsPanel() {
|
||
261 | return getDefaultButtonsPanel();
|
||
262 | } |
||
263 | |||
264 | /**
|
||
265 | * This method initializes DefaultButtonsPanel
|
||
266 | *
|
||
267 | * @return DefaultButtonsPanel
|
||
268 | */
|
||
269 | public IButtonsPanel getDefaultButtonsPanel() {
|
||
270 | if (defaultButtonsPanel == null) { |
||
271 | defaultButtonsPanel = RasterSwingLocator.getSwingManager().createButtonsPanel(ButtonsPanel.BUTTONS_NONE, getJPanel1()); |
||
272 | getButtonsPanel().addSeeDetails(); |
||
273 | getButtonsPanel().addHideDetails(); |
||
274 | getButtonsPanel().addPause(); |
||
275 | getButtonsPanel().addRestart(); |
||
276 | showPause(true);
|
||
277 | getButtonsPanel().addCancel(); |
||
278 | //getButtonsPanel().setLayout(new java.awt.FlowLayout(FlowLayout.CENTER));
|
||
279 | getButtonsPanel().addButtonPressedListener(new ButtonsPanelListener() {
|
||
280 | public void actionButtonPressed(ButtonsPanelEvent e) { |
||
281 | switch (e.getButton()) {
|
||
282 | case ButtonsPanel.BUTTON_SEEDETAILS:
|
||
283 | showLog(true);
|
||
284 | break;
|
||
285 | case ButtonsPanel.BUTTON_HIDEDETAILS:
|
||
286 | showLog(false);
|
||
287 | break;
|
||
288 | case ButtonsPanel.BUTTON_PAUSE:
|
||
289 | showPause(false);
|
||
290 | break;
|
||
291 | case ButtonsPanel.BUTTON_RESTART:
|
||
292 | showPause(true);
|
||
293 | break;
|
||
294 | } |
||
295 | } |
||
296 | }); |
||
297 | } |
||
298 | return defaultButtonsPanel;
|
||
299 | } |
||
300 | |||
301 | /**
|
||
302 | * This method initializes jProgressBar
|
||
303 | *
|
||
304 | * @return javax.swing.JProgressBar
|
||
305 | */
|
||
306 | private JProgressBar getJProgressBar() { |
||
307 | if (jProgressBar == null) { |
||
308 | jProgressBar = new JProgressBar(); |
||
309 | jProgressBar.setValue(50);
|
||
310 | jProgressBar.setPreferredSize(new Dimension(450, 18)); |
||
311 | } |
||
312 | return jProgressBar;
|
||
313 | } |
||
314 | |||
315 | /**
|
||
316 | * This method initializes jScrollPane
|
||
317 | *
|
||
318 | * @return javax.swing.JScrollPane
|
||
319 | */
|
||
320 | private JScrollPane getJScrollPane() { |
||
321 | if (jScrollPane == null) { |
||
322 | jScrollPane = new JScrollPane(); |
||
323 | jScrollPane.setViewportView(getJTextPane()); |
||
324 | jScrollPane.setVisible(false);
|
||
325 | } |
||
326 | return jScrollPane;
|
||
327 | } |
||
328 | |||
329 | /**
|
||
330 | * This method initializes jTextPane
|
||
331 | *
|
||
332 | * @return javax.swing.JTextPane
|
||
333 | */
|
||
334 | private JTextPane getJTextPane() { |
||
335 | if (jTextPane == null) { |
||
336 | jTextPane = new JTextPane(); |
||
337 | jTextPane.setEditable(false);
|
||
338 | } |
||
339 | return jTextPane;
|
||
340 | } |
||
341 | |||
342 | private void updateLog() { |
||
343 | jTextPane.setText(text.getText()); |
||
344 | jTextPane.setCaretPosition(jTextPane.getText().length()); |
||
345 | } |
||
346 | |||
347 | public void addLineLog(String line) { |
||
348 | text.addLine(line); |
||
349 | updateLog(); |
||
350 | } |
||
351 | |||
352 | public void replaceLastLineLog(String line) { |
||
353 | text.replaceLastLine(line); |
||
354 | updateLog(); |
||
355 | } |
||
356 | |||
357 | public void setLog(String value) { |
||
358 | long time = (System.currentTimeMillis() - startTime) / 1000; |
||
359 | text.setText(value + "\n" + Messages.getText("tiempo_transcurrido") + ": " + time + "s"); |
||
360 | updateLog(); |
||
361 | } |
||
362 | |||
363 | public int getPercent() { |
||
364 | return jProgressBar.getValue();
|
||
365 | } |
||
366 | |||
367 | public void setPercent(int value) { |
||
368 | if ((value == 0) && (!jProgressBar.isIndeterminate())) { |
||
369 | jProgressBar.setIndeterminate(true);
|
||
370 | jLabel1.setVisible(false);
|
||
371 | } |
||
372 | if ((value != 0) && (jProgressBar.isIndeterminate())) { |
||
373 | jProgressBar.setIndeterminate(false);
|
||
374 | jLabel1.setVisible(true);
|
||
375 | } |
||
376 | jProgressBar.setValue(value); |
||
377 | jLabel1.setText(value + "%");
|
||
378 | } |
||
379 | |||
380 | public void setLabel(String value) { |
||
381 | jLabel.setText(value); |
||
382 | } |
||
383 | |||
384 | /**
|
||
385 | * <p>Sets whether this panel will show a normal progress bar
|
||
386 | * or an indeterminate one.</p>
|
||
387 | *
|
||
388 | * @see JProgressBar.setIndeterminate(boolean)
|
||
389 | * @param indeterminate
|
||
390 | */
|
||
391 | public void setIndeterminate(boolean indeterminate) { |
||
392 | getJProgressBar().setIndeterminate(indeterminate); |
||
393 | } |
||
394 | |||
395 | /**
|
||
396 | * <p>Gets whether this panel will show a normal progress bar
|
||
397 | * or an indeterminate one.</p>
|
||
398 | *
|
||
399 | * @see JProgressBar.setIndeterminate(boolean)
|
||
400 | * @param indeterminate
|
||
401 | */
|
||
402 | public boolean getIndeterminate() { |
||
403 | return getJProgressBar().isIndeterminate();
|
||
404 | } |
||
405 | } |