Statistics
| Revision:

svn-gvsig-desktop / trunk / frameworks / _fwAndami / src / com / iver / andami / ui / mdiFrame / NewStatusBar.java @ 10542

History | View | Annotate | Download (12.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * 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
 *
10
 * 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
 *
15
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.andami.ui.mdiFrame;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.util.HashMap;
48

    
49
import javax.swing.BorderFactory;
50
import javax.swing.ImageIcon;
51
import javax.swing.JLabel;
52
import javax.swing.JPanel;
53
import javax.swing.JProgressBar;
54
import javax.swing.SwingConstants;
55
import javax.swing.border.BevelBorder;
56

    
57
import org.apache.log4j.Logger;
58
import org.gvsig.gui.beans.controls.IControl;
59

    
60
import com.iver.andami.messages.Messages;
61
import com.iver.andami.plugins.config.generate.Label;
62

    
63

    
64
/**
65
 */
66
public class NewStatusBar extends JPanel {
67
        private static Logger logger = Logger.getLogger(NewStatusBar.class.getName());
68
        private static final int INFO = 0;
69
        private static final int WARNING = 1;
70
        private static final int ERROR = 2;
71
        private JLabel lblIcon = null;
72
        private JLabel lblTexto = null;
73
        private boolean contenidoTemporal;
74
        private String textoAnterior;
75
        private int estadoAnterior;
76
        private ImageIcon infoIcon;
77
        private ImageIcon warningIcon;
78
        private ImageIcon errorIcon;
79
        private int estado;
80
        private JProgressBar progressBar = null;
81
        private HashMap idLabel = new HashMap();
82
        private int[] widthlabels = null;
83
        private HashMap controls = new HashMap();
84
        private JPanel controlContainer;
85
        /**
86
         * This is the default constructor
87
         */
88
        public NewStatusBar() {
89
                super();
90
                initialize();
91
                infoIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
92
                                                                                                   .getResource("images/info.gif"));
93
                warningIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
94
                                                                                                          .getResource("images/warning.gif"));
95
                errorIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
96
                                                                                                        .getResource("images/error.gif"));
97
        }
98

    
99
        /**
100
         * This method initializes this
101
         */
102
        private void initialize() {
103
                BorderLayout mainLayout = new BorderLayout();
104
                this.setLayout(mainLayout);
105
                
106
                JPanel container1 = new JPanel();
107
                this.add(container1, BorderLayout.CENTER);
108
                controlContainer = new JPanel();
109
                this.add(controlContainer, BorderLayout.EAST);
110
                
111
                this.setPreferredSize(new java.awt.Dimension(183,20));
112
                this.setSize(new java.awt.Dimension(183,20));
113
                lblIcon = new JLabel();
114
                lblTexto = new JLabel();
115
                lblTexto.setAlignmentX(JLabel.LEFT_ALIGNMENT);
116
                lblTexto.setHorizontalAlignment(SwingConstants.LEFT);
117
                lblTexto.setHorizontalTextPosition(SwingConstants.LEFT);
118

    
119
                FlowLayout rightLayout = new FlowLayout();
120
                controlContainer.setLayout(rightLayout);
121
                rightLayout.setHgap(1);
122
                rightLayout.setVgap(2);
123
                rightLayout.setAlignment(java.awt.FlowLayout.RIGHT);
124
                lblIcon.setText("");
125
                lblTexto.setText(Messages.getString("StatusBar.Aplicacion_iniciada"));
126
                
127
                FlowLayout leftLayout = new FlowLayout(FlowLayout.LEFT);
128
                leftLayout.setHgap(1);
129
                leftLayout.setVgap(2);
130
                container1.setLayout(leftLayout);
131
                container1.add(lblIcon, null);
132
                container1.add(getProgressBar(), null);
133
                container1.add(lblTexto, null);
134
        }
135

    
136
        /**
137
         * Obtiene el texto de la barra de estado
138
         *
139
         * @return texto
140
         */
141
        public String getStatusText() {
142
                return lblTexto.getText();
143
        }
144

    
145
        /**
146
         * Restaura el contenido permanente de la barra de estado
147
         */
148
        public void restaurarTexto() {
149
                contenidoTemporal = false;
150

    
151
                if (estadoAnterior == -1) {
152
                        return;
153
                }
154

    
155
                switch (estadoAnterior) {
156
                        case INFO:
157
                                setInfoText(textoAnterior);
158

    
159
                                break;
160

    
161
                        case WARNING:
162
                                setWarningText(textoAnterior);
163

    
164
                                break;
165

    
166
                        case ERROR:
167
                                setErrorText(textoAnterior);
168

    
169
                                break;
170
                }
171

    
172
                estadoAnterior = -1;
173
                textoAnterior = null;
174
        }
175

    
176
        /**
177
         * Establece el texto de la barra de estado de forma temporal. Se almacena
178
         * el texto que hab?a en la barra de estado.
179
         *
180
         * @param texto texto
181
         */
182
        public void setInfoTextTemporal(String texto) {
183
                contenidoTemporal = true;
184

    
185
                estadoAnterior = this.estado;
186
                this.estado = INFO;
187
                lblIcon.setIcon(infoIcon);
188

    
189
                textoAnterior = getStatusText();
190
                lblTexto.setText(texto);
191
        }
192

    
193
        /**
194
         * Establece el texto de la barra de estado de forma temporal. Se almacena
195
         * el texto que hab?a en la barra de estado.
196
         *
197
         * @param texto texto
198
         */
199
        public void setWarningTextTemporal(String texto) {
200
                contenidoTemporal = true;
201

    
202
                estadoAnterior = this.estado;
203
                this.estado = WARNING;
204
                lblIcon.setIcon(warningIcon);
205

    
206
                textoAnterior = getStatusText();
207
                lblTexto.setText(texto);
208
        }
209

    
210
        /**
211
         * Establece el texto de la barra de estado de forma temporal. Se almacena
212
         * el texto que hab?a en la barra de estado.
213
         *
214
         * @param texto texto
215
         */
216
        public void setErrorTextTemporal(String texto) {
217
                contenidoTemporal = true;
218

    
219
                estadoAnterior = this.estado;
220
                this.estado = ERROR;
221
                lblIcon.setIcon(errorIcon);
222

    
223
                textoAnterior = getStatusText();
224
                lblTexto.setText(texto);
225
        }
226

    
227
        /**
228
         * Establece el texto de la barar de manera permanente. Si se est?
229
         * mostrando texto de forma temporal no se actualiza la interfaz
230
         *
231
         * @param texto Texto
232
         */
233
        public void setInfoText(String texto) {
234
                if (contenidoTemporal) {
235
                        textoAnterior = texto;
236
                        estadoAnterior = INFO;
237
                } else {
238
                        lblTexto.setText(texto);
239
                        lblIcon.setIcon(infoIcon);
240
                        estado = INFO;
241
                }
242
        }
243

    
244
        /**
245
         * Establece el texto de la barar de manera permanente. Si se est?
246
         * mostrando texto de forma temporal no se actualiza la interfaz
247
         *
248
         * @param texto Texto
249
         */
250
        public void setWarningText(String texto) {
251
                if (contenidoTemporal) {
252
                        textoAnterior = texto;
253
                        estadoAnterior = WARNING;
254
                } else {
255
                        lblTexto.setText(texto);
256
                        lblIcon.setIcon(warningIcon);
257
                        estado = WARNING;
258
                }
259
        }
260

    
261
        /**
262
         * Establece el texto de la barar de manera permanente. Si se est?
263
         * mostrando texto de forma temporal no se actualiza la interfaz
264
         *
265
         * @param texto Texto
266
         */
267
        public void setErrorText(String texto) {
268
                if (contenidoTemporal) {
269
                        textoAnterior = texto;
270
                        estadoAnterior = ERROR;
271
                } else {
272
                        lblTexto.setText(texto);
273
                        lblIcon.setIcon(errorIcon);
274
                        estado = ERROR;
275
                }
276
        }
277

    
278
        /**
279
         * Establece el porcentaje en la barra de progreso. Si es 100 se oculta la
280
         * barra
281
         *
282
         * @param p DOCUMENT ME!
283
         */
284
        public void setProgress(int p) {
285
                if (p < 100) {
286
                        getProgressBar().setValue(p);
287
                        getProgressBar().setVisible(true);
288
                } else {
289
                        getProgressBar().setVisible(false);
290
                }
291

    
292
                getProgressBar().repaint();
293
        }
294

    
295
        /*public void setControls(Label[] labels, ArrayList controlArray) {
296
                removeAllLabels();
297
                if (labels!=null)
298
                        setLabelSet(labels);
299
                if (controlArray!=null)
300
                        setControls(controlArray);
301
                this.repaint();
302
        }*/
303

    
304
        public void setLabelSet(Label[] labels) {
305
                removeAllLabels();
306
                idLabel.clear();
307

    
308
                for (int i = 0; i < labels.length; i++) {
309
                        JLabel lbl = new JLabel();
310
                        lbl.setPreferredSize(new Dimension(labels[i].getSize(),
311
                                        this.getHeight() - 2));
312
                        lbl.setSize(new Dimension(labels[i].getSize(), this.getHeight() - 2));
313
                        lbl.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
314
                        controlContainer.add(lbl);
315

    
316
                        /*            if (i != labels.length - 1){
317
                           this.add(new JSeparator(JSeparator.VERTICAL));
318
                           }
319
                         */
320
                        idLabel.put(labels[i].getId(), lbl);
321
                }
322

    
323
                JLabel[] configlabels = (JLabel[]) idLabel.values().toArray(new JLabel[0]);
324
                widthlabels = new int[configlabels.length];
325

    
326
                for (int i = 0; i < labels.length; i++) {
327
                        widthlabels[i] = configlabels[i].getWidth();
328
                }
329

    
330
                this.repaint();
331
        }
332

    
333
        /*public void setControls(ArrayList controlList) {
334

335
                for (int i = 0; i < controlList.size(); i++) {
336
                        Component control = (Component) controlList.get(i);
337
                        control.setVisible(true);
338
                        control.setEnabled(true);
339
                        this.add(control);
340
                }
341
        }*/
342

    
343
        /**
344
         * Los Labels que estan sin texto no se a?aden a la barra y su espacio es
345
         * utilizado por otros labels. Si se quiere hacer en un futuro la
346
         * asignaci?n de espacio de los elementos que se a?adan a la barra se
347
         * puede hacer aqu?.
348
         */
349
        public void ajustar() {
350
                if (widthlabels == null) {
351
                        return;
352
                }
353

    
354
                int ws = this.getWidth();
355

    
356
                JLabel[] labels = (JLabel[]) idLabel.values().toArray(new JLabel[0]);
357

    
358
                /*        double total = 1;
359
                   for (int i = 0; i < widthlabels.length; i++) {
360
                           if (labels[i].getText().compareTo("") != 0) {
361
                                   total += widthlabels[i];
362
                           }
363
                   }
364
                   double p = (ws - lblTexto.getWidth() - 20) / total;
365
                 */
366
                for (int i = 0; i < labels.length; i++) {
367
                        //if (labels[i] instanceof JLabel){
368
                        JLabel label = (JLabel) labels[i];
369

    
370
                        if (label.getText().compareTo("") != 0) {
371
                                label.setVisible(true);
372
                                label.setPreferredSize(new Dimension((int) (widthlabels[i]),
373
                                                this.getHeight() - 2));
374
                        } else {
375
                                label.setVisible(false);
376

    
377
                                //label.setPreferredSize(new Dimension(0,this.getHeight()));
378
                        }
379

    
380
                        //}
381
                }
382
        }
383

    
384
        /**
385
         * Removes all the labels from the status bar.
386
         */
387
        private void removeAllLabels() {
388
                Component[] controlArray = controlContainer.getComponents();
389

    
390
                for (int i = 0; i < controlArray.length; i++) {
391
                        if ((controlArray[i] != lblIcon) && (controlArray[i] != lblTexto && controlArray[i] instanceof JLabel)) {
392
                                controlContainer.remove(controlArray[i]);
393
                        }
394
                }
395
        }
396

    
397

    
398
        /**
399
         * Removes all the controls (including labels) from the status bar.
400
         */
401
        private void removeAllControls() {
402
                Component[] controlArray = controlContainer.getComponents();
403

    
404
                for (int i = 0; i < controlArray.length; i++) {
405
                        if ((controlArray[i] != lblIcon) && (controlArray[i] != lblTexto)) {
406
                                controlContainer.remove(controlArray[i]);
407
                        }
408
                }
409
        }
410

    
411
        /**
412
         * Establece el texto de una de las etiquetas
413
         *
414
         * @param id Identificador de las etiquetas
415
         * @param msg Mensaje que se quiere poner en la etiqueta
416
         */
417
        public void setMessage(String id, String msg) {
418
                JLabel lbl = (JLabel) idLabel.get(id);
419
                
420
                if (lbl!=null) {
421
                        lbl.setText(msg);
422
                }
423
                else {
424
                        // try with controls
425
                        try {
426
                                IControl control = (IControl) controls.get(id);
427
                                if (control!=null)
428
                                        control.setValue(msg);
429
                        }
430
                        catch (ClassCastException ex) {
431
                                logger.debug("no label called " + id);
432
                        }
433
                }
434

    
435
                ajustar();
436
        }
437

    
438
        /**
439
         * Sets the control identified by 'id' with the provided value.
440
         *
441
         * @param id
442
         * @param value
443
         */
444
        public void setControlValue(String id, String value) {
445
                IControl control = (IControl) controls.get(id);
446
                if (control!=null) {
447
                        control.setValue(value);
448
                }
449
                else {
450
                        logger.debug("NewStatusBar -- no control called " + id);
451
                }
452
        }
453

    
454
        /**
455
         * This method initializes progressBar
456
         *
457
         * @return javax.swing.JProgressBar
458
         */
459
        private JProgressBar getProgressBar() {
460
                if (progressBar == null) {
461
                        progressBar = new JProgressBar();
462
                        progressBar.setPreferredSize(new java.awt.Dimension(100, 14));
463
                        progressBar.setVisible(false);
464
                        progressBar.setMinimum(0);
465
                        progressBar.setMaximum(100);
466
                        progressBar.setValue(50);
467
                }
468

    
469
                return progressBar;
470
        }
471

    
472
        /**
473
         * DOCUMENT ME!
474
         *
475
         * @param d
476
         */
477
        public void setFixedLabelWidth(double d) {
478
                lblTexto.setPreferredSize(new Dimension((int) d, lblTexto.getHeight()));
479
        }
480

    
481
        /**
482
         * Adds a control to the status bar
483
         */
484
        public void addControl(String id, Component control) {
485
                if (!controls.containsKey(control.getName())) {
486
                        controls.put(control.getName(), control);
487
                        controlContainer.add(control);
488
                }
489
                else {
490
                        logger.debug("NewStatusBar.addControl -- control 'id' already exists"+ id);
491
                }
492
        }
493

    
494
        /**
495
         * Remove a control from the status bar
496
         */
497
        public Component removeControl(String id) {
498
                try {
499
                        Component component = (Component) controls.get(id);
500
                        controlContainer.remove(component);
501
                        controls.remove(id);
502
                        return component;
503
                }
504
                catch (ClassCastException ex) {
505
                        logger.debug("NewStatusBar.removeControl -- control "+id+"doesn't exist");
506
                }
507
                return null;
508
        }
509
        
510
        /**
511
         * Gets a control from the status bar
512
         */
513
        public Component getControl(String id) {
514
                return (Component) controls.get(id);
515
        }
516
} //  @jve:decl-index=0:visual-constraint="10,10"