Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / progresspanel / ProgressPanel.java @ 30541

History | View | Annotate | Download (10.8 KB)

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

    
3
/* gvSIG. Geographic Information System of the Valencian Government
4
 *
5
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
6
 * of the Valencian Government (CIT)
7
 * 
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 * 
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *  
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
21
 * MA  02110-1301, USA.
22
 * 
23
 */
24

    
25
import java.awt.Dialog;
26
import java.awt.Dimension;
27
import java.awt.FlowLayout;
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.gui.beans.Messages;
41
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
42
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
43
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
44
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
45

    
46
/**
47
 * <code>ProgressPanel</code>. Muestra una ventana de di?logo para representar
48
 * una barra de progreso con su ventana de registro.
49
 *
50
 * @version 15/07/2008
51
 *
52
 * @author BorSanZa - Borja Sanchez Zamorano (borja.sanchez@iver.es)
53
 */
54
public class ProgressPanel extends JDialog {
55
        private static final long serialVersionUID = 4321011219898234352L;
56

    
57
        private JPanel              jPanel              = null;
58
        private JLabel              jLabel              = null;
59
        private JLabel              jLabel1             = null;
60
        private JPanel              jPanel1             = null;
61
        private JPanel              njp                 = null;
62
        private DefaultButtonsPanel defaultButtonsPanel = null;
63
        private JProgressBar        jProgressBar        = null;
64
        private JScrollPane         jScrollPane         = null;
65
        private JTextPane           jTextPane           = null;
66
        private LogControl          text                = new LogControl();
67
        private long                startTime           = System.currentTimeMillis();
68
        private boolean             showPause           = true;
69

    
70
        /**
71
         * Constructor
72
         */
73
        public ProgressPanel() {
74
                super();
75
                initialize();
76
        }
77

    
78
        public ProgressPanel(boolean showPause) {
79
                super();
80
                this.showPause = showPause;
81
                initialize();
82
        }
83
        
84
        /**
85
         * @see JDialog#JDialog(Dialog, boolean)
86
         */
87
        public ProgressPanel(Dialog owner, boolean modal) throws HeadlessException {
88
                super(owner, modal);
89
                initialize();
90
        }
91

    
92
        /**
93
         * @see JDialog#JDialog(Dialog, String, boolean, GraphicsConfiguration)
94
         */
95
        public ProgressPanel(Dialog owner, String title, boolean modal,        GraphicsConfiguration gc) throws HeadlessException {
96
                super(owner, title, modal, gc);
97
                initialize();
98
        }
99

    
100
        /**
101
         * @see JDialog#JDialog(Dialog, String, boolean)
102
         */
103
        public ProgressPanel(Dialog owner, String title, boolean modal)
104
                        throws HeadlessException {
105
                super(owner, title, modal);
106
                initialize();
107
        }
108

    
109
        /**
110
         * @see JDialog#JDialog(Dialog, String)
111
         */
112
        public ProgressPanel(Dialog owner, String title) throws HeadlessException {
113
                super(owner, title);
114
                initialize();
115
        }
116

    
117
        /**
118
         * @see JDialog#JDialog(Dialog)
119
         */
120
        public ProgressPanel(Dialog owner) throws HeadlessException {
121
                super(owner);
122
                initialize();
123
        }
124

    
125
        /**
126
         * @see JDialog#JDialog(Frame, boolean)
127
         */
128
        public ProgressPanel(Frame owner, boolean modal) throws HeadlessException {
129
                super(owner, modal);
130
                initialize();
131
        }
132

    
133
        /**
134
         * @see JDialog#JDialog(Frame, String, boolean, GraphicsConfiguration)
135
         */
136
        public ProgressPanel(Frame owner, String title, boolean modal, GraphicsConfiguration gc) {
137
                super(owner, title, modal, gc);
138
                initialize();
139
        }
140

    
141
        /**
142
         * @see JDialog#JDialog(Frame, String, boolean)
143
         */
144
        public ProgressPanel(Frame owner, String title, boolean modal)
145
                        throws HeadlessException {
146
                super(owner, title, modal);
147
                initialize();
148
        }
149

    
150
        /**
151
         * @see JDialog#JDialog(Frame, String)
152
         */
153
        public ProgressPanel(Frame owner, String title) throws HeadlessException {
154
                super(owner, title);
155
                initialize();
156
        }
157

    
158
        /**
159
         * @see JDialog#JDialog(Frame)
160
         */
161
        public ProgressPanel(Frame owner) throws HeadlessException {
162
                super(owner);
163
                initialize();
164
        }        
165
        
166
        /**
167
         * This method initializes this
168
         */
169
        private void initialize() {
170
                njp = new JPanel();
171
                njp.setLayout(new java.awt.BorderLayout(5, 5));
172
                njp.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 15, 5, 15));
173
                this.setContentPane(njp);
174
                this.setResizable(false);
175
                njp.add(getJPanel(), java.awt.BorderLayout.NORTH);
176
                njp.add(getJPanel1(), java.awt.BorderLayout.CENTER);
177
                njp.add(getButtonsPanel(), java.awt.BorderLayout.SOUTH);
178
                showLog(false);
179
                setPercent(0);
180
                Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
181
                this.setLocation(
182
                                (int) (d.getWidth() - this.getWidth()) >> 1,
183
                                (int) (d.getHeight() - this.getHeight()) >> 1);
184
                this.setVisible(true);
185
        }
186

    
187
        /**
188
         * This method initializes jPanel
189
         *
190
         * @return javax.swing.JPanel
191
         */
192
        private JPanel getJPanel() {
193
                if (jPanel == null) {
194
                        jLabel1 = new JLabel();
195
                        jLabel = new JLabel();
196
                        jLabel.setText(Messages.getText("espere") + "...");
197
                        jPanel = new JPanel();
198
                        jPanel.setLayout(new java.awt.BorderLayout(5, 5));
199
                        jPanel.add(jLabel, java.awt.BorderLayout.WEST);
200
                        jPanel.add(jLabel1, java.awt.BorderLayout.EAST);
201
                }
202
                return jPanel;
203
        }
204

    
205
        /**
206
         * This method initializes jPanel1
207
         *
208
         * @return javax.swing.JPanel
209
         */
210
        private JPanel getJPanel1() {
211
                if (jPanel1 == null) {
212
                        jPanel1 = new JPanel();
213
                        jPanel1.setLayout(new java.awt.BorderLayout(5, 5));
214
                        jPanel1.add(getJProgressBar(), java.awt.BorderLayout.NORTH);
215
                        jPanel1.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
216
                }
217
                return jPanel1;
218
        }
219

    
220
        public void addButtonPressedListener(ButtonsPanelListener listener) {
221
                getDefaultButtonsPanel().addButtonPressedListener(listener);
222
        }
223

    
224
        public void removeButtonPressedListener(ButtonsPanelListener listener) {
225
                getDefaultButtonsPanel().removeButtonPressedListener(listener);
226
        }
227

    
228
        public void showLog(boolean visible) {
229
                getButtonsPanel().getButton(ButtonsPanel.BUTTON_SEEDETAILS).setVisible(!visible);
230
                getButtonsPanel().getButton(ButtonsPanel.BUTTON_HIDEDETAILS).setVisible(visible);
231
                jScrollPane.setVisible(visible);
232

    
233
                this.setIgnoreRepaint(true);
234

    
235
                if (visible)
236
                        this.setSize(this.getWidth(), 300);
237
                else
238
                        this.pack();
239

    
240
                this.setIgnoreRepaint(false);
241
                if (this.isVisible())
242
                        this.setVisible(true);
243
        }
244

    
245
        public void showPause(boolean visible) {
246
                if (showPause) {
247
                        getButtonsPanel().getButton(ButtonsPanel.BUTTON_RESTART).setVisible(!visible);
248
                        getButtonsPanel().getButton(ButtonsPanel.BUTTON_PAUSE).setVisible(visible);
249
                } else {
250
                        getButtonsPanel().getButton(ButtonsPanel.BUTTON_RESTART).setVisible(false);
251
                        getButtonsPanel().getButton(ButtonsPanel.BUTTON_PAUSE).setVisible(false);
252
                }
253
        }
254

    
255
        /**
256
         * @see DefaultButtonsPanel#getButtonsPanel()
257
         */
258
        public ButtonsPanel getButtonsPanel() {
259
                return getDefaultButtonsPanel().getButtonsPanel();
260
        }
261
        
262
        /**
263
         * This method initializes DefaultButtonsPanel
264
         *
265
         * @return DefaultButtonsPanel
266
         */
267
        public DefaultButtonsPanel getDefaultButtonsPanel() {
268
                if (defaultButtonsPanel == null) {
269
                        defaultButtonsPanel = new DefaultButtonsPanel(ButtonsPanel.BUTTONS_NONE);
270
                        getButtonsPanel().addSeeDetails();
271
                        getButtonsPanel().addHideDetails();
272
                        getButtonsPanel().addPause();
273
                        getButtonsPanel().addRestart();
274
                        showPause(true);
275
                        getButtonsPanel().addCancel();
276
                        getButtonsPanel().setLayout(new java.awt.FlowLayout(FlowLayout.CENTER));
277
                        getButtonsPanel().addButtonPressedListener(new ButtonsPanelListener() {
278
                                public void actionButtonPressed(ButtonsPanelEvent e) {
279
                                        switch (e.getButton()) {
280
                                                case ButtonsPanel.BUTTON_SEEDETAILS:
281
                                                        showLog(true);
282
                                                        break;
283
                                                case ButtonsPanel.BUTTON_HIDEDETAILS:
284
                                                        showLog(false);
285
                                                        break;
286
                                                case ButtonsPanel.BUTTON_PAUSE:
287
                                                        showPause(false);
288
                                                        break;
289
                                                case ButtonsPanel.BUTTON_RESTART:
290
                                                        showPause(true);
291
                                                        break;
292
                                        }
293
                                }
294
                        });
295
                }
296
                return defaultButtonsPanel;
297
        }
298

    
299
        /**
300
         * This method initializes jProgressBar
301
         *
302
         * @return javax.swing.JProgressBar
303
         */
304
        private JProgressBar getJProgressBar() {
305
                if (jProgressBar == null) {
306
                        jProgressBar = new JProgressBar();
307
                        jProgressBar.setValue(50);
308
                        jProgressBar.setPreferredSize(new Dimension(450, 18));
309
                }
310
                return jProgressBar;
311
        }
312

    
313
        /**
314
         * This method initializes jScrollPane
315
         *
316
         * @return javax.swing.JScrollPane
317
         */
318
        private JScrollPane getJScrollPane() {
319
                if (jScrollPane == null) {
320
                        jScrollPane = new JScrollPane();
321
                        jScrollPane.setViewportView(getJTextPane());
322
                        jScrollPane.setVisible(false);
323
                }
324
                return jScrollPane;
325
        }
326

    
327
        /**
328
         * This method initializes jTextPane
329
         *
330
         * @return javax.swing.JTextPane
331
         */
332
        private JTextPane getJTextPane() {
333
                if (jTextPane == null) {
334
                        jTextPane = new JTextPane();
335
                        jTextPane.setEditable(false);
336
                }
337
                return jTextPane;
338
        }
339

    
340
        private void updateLog() {
341
                jTextPane.setText(text.getText());
342
                jTextPane.setCaretPosition(jTextPane.getText().length());
343
        }
344

    
345
        public void addLineLog(String line) {
346
                text.addLine(line);
347
                updateLog();
348
        }
349

    
350
        public void replaceLastLineLog(String line) {
351
                text.replaceLastLine(line);
352
                updateLog();
353
        }
354

    
355
        public void setLog(String value) {
356
                long time = (System.currentTimeMillis() - startTime) / 1000;
357
                text.setText(value + "\n" + Messages.getText("tiempo_transcurrido") + ": " + time + "s");
358
                updateLog();
359
        }
360

    
361
        public int getPercent() {
362
                return jProgressBar.getValue();
363
        }
364

    
365
        public void setPercent(int value) {
366
                if ((value == 0) && (!jProgressBar.isIndeterminate())) {
367
                        jProgressBar.setIndeterminate(true);
368
                        jLabel1.setVisible(false);
369
                }
370
                if ((value != 0) && (jProgressBar.isIndeterminate())) {
371
                        jProgressBar.setIndeterminate(false);
372
                        jLabel1.setVisible(true);
373
                }
374
                jProgressBar.setValue(value);
375
                jLabel1.setText(value + "%");
376
        }
377

    
378
        public void setLabel(String value) {
379
                jLabel.setText(value);
380
        }
381

    
382
        /**
383
         * <p>Sets whether this panel will show a normal progress bar
384
         * or an indeterminate one.</p>
385
         *
386
         * @see JProgressBar.setIndeterminate(boolean)
387
         * @param indeterminate
388
         */
389
        public void setIndeterminate(boolean indeterminate) {
390
                getJProgressBar().setIndeterminate(indeterminate);
391
        }
392

    
393
        /**
394
         * <p>Gets whether this panel will show a normal progress bar
395
         * or an indeterminate one.</p>
396
         *
397
         * @see JProgressBar.setIndeterminate(boolean)
398
         * @param indeterminate
399
         */
400
        public boolean getIndeterminate() {
401
                return getJProgressBar().isIndeterminate();
402
        }
403
}