Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_RELEASE / frameworks / _fwAndami / src / com / iver / andami / ui / mdiFrame / NewStatusBar.java @ 9167

History | View | Annotate | Download (11.3 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.Component;
44
import java.awt.Dimension;
45
import java.awt.FlowLayout;
46
import java.util.HashMap;
47

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

    
55
import org.apache.log4j.Logger;
56
import org.gvsig.gui.beans.controls.IControl;
57

    
58
import com.iver.andami.messages.Messages;
59
import com.iver.andami.plugins.config.generate.Label;
60

    
61

    
62
/**
63
 */
64
public class NewStatusBar extends JPanel {
65
        private static Logger logger = Logger.getLogger(NewStatusBar.class.getName());
66
        private static final int INFO = 0;
67
        private static final int WARNING = 1;
68
        private static final int ERROR = 2;
69
        private JLabel lblIcon = null;
70
        private JLabel lblTexto = null;
71
        private boolean contenidoTemporal;
72
        private String textoAnterior;
73
        private int estadoAnterior;
74
        private ImageIcon infoIcon;
75
        private ImageIcon warningIcon;
76
        private ImageIcon errorIcon;
77
        private int estado;
78
        private JProgressBar progressBar = null;
79
        private HashMap idLabel = new HashMap();
80
        private int[] widthlabels = null;
81
        private HashMap controls = new HashMap();
82

    
83
        /**
84
         * This is the default constructor
85
         */
86
        public NewStatusBar() {
87
                super();
88
                initialize();
89
                infoIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
90
                                                                                                   .getResource("images/info.gif"));
91
                warningIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
92
                                                                                                          .getResource("images/warning.gif"));
93
                errorIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
94
                                                                                                        .getResource("images/error.gif"));
95
        }
96

    
97
        /**
98
         * This method initializes this
99
         */
100
        private void initialize() {
101
                this.setPreferredSize(new java.awt.Dimension(183,20));
102
                this.setSize(new java.awt.Dimension(183,20));
103
                lblIcon = new JLabel();
104
                lblTexto = new JLabel();
105

    
106
                FlowLayout flowLayout2 = new FlowLayout();
107
                this.setLayout(flowLayout2);
108
                flowLayout2.setHgap(1);
109
                flowLayout2.setVgap(2);
110
                flowLayout2.setAlignment(java.awt.FlowLayout.LEFT);
111
                lblIcon.setText("");
112
                lblTexto.setText(Messages.getString("StatusBar.Aplicacion_iniciada"));
113
                this.add(lblIcon, null);
114
                this.add(getProgressBar(), null);
115
                this.add(lblTexto, null);
116
        }
117

    
118
        /**
119
         * Obtiene el texto de la barra de estado
120
         *
121
         * @return texto
122
         */
123
        public String getStatusText() {
124
                return lblTexto.getText();
125
        }
126

    
127
        /**
128
         * Restaura el contenido permanente de la barra de estado
129
         */
130
        public void restaurarTexto() {
131
                contenidoTemporal = false;
132

    
133
                if (estadoAnterior == -1) {
134
                        return;
135
                }
136

    
137
                switch (estadoAnterior) {
138
                        case INFO:
139
                                setInfoText(textoAnterior);
140

    
141
                                break;
142

    
143
                        case WARNING:
144
                                setWarningText(textoAnterior);
145

    
146
                                break;
147

    
148
                        case ERROR:
149
                                setErrorText(textoAnterior);
150

    
151
                                break;
152
                }
153

    
154
                estadoAnterior = -1;
155
                textoAnterior = null;
156
        }
157

    
158
        /**
159
         * Establece el texto de la barra de estado de forma temporal. Se almacena
160
         * el texto que hab?a en la barra de estado.
161
         *
162
         * @param texto texto
163
         */
164
        public void setInfoTextTemporal(String texto) {
165
                contenidoTemporal = true;
166

    
167
                estadoAnterior = this.estado;
168
                this.estado = INFO;
169
                lblIcon.setIcon(infoIcon);
170

    
171
                textoAnterior = getStatusText();
172
                lblTexto.setText(texto);
173
        }
174

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

    
184
                estadoAnterior = this.estado;
185
                this.estado = WARNING;
186
                lblIcon.setIcon(warningIcon);
187

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

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

    
201
                estadoAnterior = this.estado;
202
                this.estado = ERROR;
203
                lblIcon.setIcon(errorIcon);
204

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

    
209
        /**
210
         * Establece el texto de la barar de manera permanente. Si se est?
211
         * mostrando texto de forma temporal no se actualiza la interfaz
212
         *
213
         * @param texto Texto
214
         */
215
        public void setInfoText(String texto) {
216
                if (contenidoTemporal) {
217
                        textoAnterior = texto;
218
                        estadoAnterior = INFO;
219
                } else {
220
                        lblTexto.setText(texto);
221
                        lblIcon.setIcon(infoIcon);
222
                        estado = INFO;
223
                }
224
        }
225

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

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

    
260
        /**
261
         * Establece el porcentaje en la barra de progreso. Si es 100 se oculta la
262
         * barra
263
         *
264
         * @param p DOCUMENT ME!
265
         */
266
        public void setProgress(int p) {
267
                if (p < 100) {
268
                        getProgressBar().setValue(p);
269
                        getProgressBar().setVisible(true);
270
                } else {
271
                        getProgressBar().setVisible(false);
272
                }
273

    
274
                getProgressBar().repaint();
275
        }
276

    
277
        /*public void setControls(Label[] labels, ArrayList controlArray) {
278
                removeAllLabels();
279
                if (labels!=null)
280
                        setLabelSet(labels);
281
                if (controlArray!=null)
282
                        setControls(controlArray);
283
                this.repaint();
284
        }*/
285

    
286
        public void setLabelSet(Label[] labels) {
287
                removeAllLabels();
288
                idLabel.clear();
289

    
290
                for (int i = 0; i < labels.length; i++) {
291
                        JLabel lbl = new JLabel();
292
                        lbl.setPreferredSize(new Dimension(labels[i].getSize(),
293
                                        this.getHeight() - 2));
294
                        lbl.setSize(new Dimension(labels[i].getSize(), this.getHeight() - 2));
295
                        lbl.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
296
                        this.add(lbl);
297

    
298
                        /*            if (i != labels.length - 1){
299
                           this.add(new JSeparator(JSeparator.VERTICAL));
300
                           }
301
                         */
302
                        idLabel.put(labels[i].getId(), lbl);
303
                }
304

    
305
                JLabel[] configlabels = (JLabel[]) idLabel.values().toArray(new JLabel[0]);
306
                widthlabels = new int[configlabels.length];
307

    
308
                for (int i = 0; i < labels.length; i++) {
309
                        widthlabels[i] = configlabels[i].getWidth();
310
                }
311

    
312
                this.repaint();
313
        }
314

    
315
        /*public void setControls(ArrayList controlList) {
316

317
                for (int i = 0; i < controlList.size(); i++) {
318
                        Component control = (Component) controlList.get(i);
319
                        control.setVisible(true);
320
                        control.setEnabled(true);
321
                        this.add(control);
322
                }
323
        }*/
324

    
325
        /**
326
         * Los Labels que estan sin texto no se a?aden a la barra y su espacio es
327
         * utilizado por otros labels. Si se quiere hacer en un futuro la
328
         * asignaci?n de espacio de los elementos que se a?adan a la barra se
329
         * puede hacer aqu?.
330
         */
331
        public void ajustar() {
332
                if (widthlabels == null) {
333
                        return;
334
                }
335

    
336
                int ws = this.getWidth();
337

    
338
                JLabel[] labels = (JLabel[]) idLabel.values().toArray(new JLabel[0]);
339

    
340
                /*        double total = 1;
341
                   for (int i = 0; i < widthlabels.length; i++) {
342
                           if (labels[i].getText().compareTo("") != 0) {
343
                                   total += widthlabels[i];
344
                           }
345
                   }
346
                   double p = (ws - lblTexto.getWidth() - 20) / total;
347
                 */
348
                for (int i = 0; i < labels.length; i++) {
349
                        //if (labels[i] instanceof JLabel){
350
                        JLabel label = (JLabel) labels[i];
351

    
352
                        if (label.getText().compareTo("") != 0) {
353
                                label.setVisible(true);
354
                                label.setPreferredSize(new Dimension((int) (widthlabels[i]),
355
                                                this.getHeight() - 2));
356
                        } else {
357
                                label.setVisible(false);
358

    
359
                                //label.setPreferredSize(new Dimension(0,this.getHeight()));
360
                        }
361

    
362
                        //}
363
                }
364
        }
365

    
366
        /**
367
         * Removes all the labels from the status bar.
368
         */
369
        private void removeAllLabels() {
370
                Component[] controlArray = this.getComponents();
371

    
372
                for (int i = 0; i < controlArray.length; i++) {
373
                        if ((controlArray[i] != lblIcon) && (controlArray[i] != lblTexto && controlArray[i] instanceof JLabel)) {
374
                                remove(controlArray[i]);
375
                        }
376
                }
377
        }
378

    
379

    
380
        /**
381
         * Removes all the controls (including labels) from the status bar.
382
         */
383
        private void removeAllControls() {
384
                Component[] controlArray = this.getComponents();
385

    
386
                for (int i = 0; i < controlArray.length; i++) {
387
                        if ((controlArray[i] != lblIcon) && (controlArray[i] != lblTexto)) {
388
                                remove(controlArray[i]);
389
                        }
390
                }
391
        }
392

    
393
        /**
394
         * Establece el texto de una de las etiquetas
395
         *
396
         * @param id Identificador de las etiquetas
397
         * @param msg Mensaje que se quiere poner en la etiqueta
398
         */
399
        public void setMessage(String id, String msg) {
400
                JLabel lbl = (JLabel) idLabel.get(id);
401

    
402
                if (lbl == null) {
403
                        logger.debug("no label called " + id);
404
                } else {
405
                        lbl.setText(msg);
406
                }
407

    
408
                ajustar();
409
        }
410

    
411
        /**
412
         * Sets the control identified by 'id' with the provided value.
413
         *
414
         * @param id
415
         * @param value
416
         */
417
        public void setControlValue(String id, String value) {
418
                IControl control = (IControl) controls.get(id);
419
                if (control!=null) {
420
                        control.setValue(value);
421
                }
422
                else {
423
                        logger.debug("NewStatusBar -- no control called " + id);
424
                }
425
        }
426

    
427
        /**
428
         * This method initializes progressBar
429
         *
430
         * @return javax.swing.JProgressBar
431
         */
432
        private JProgressBar getProgressBar() {
433
                if (progressBar == null) {
434
                        progressBar = new JProgressBar();
435
                        progressBar.setPreferredSize(new java.awt.Dimension(100, 14));
436
                        progressBar.setVisible(false);
437
                        progressBar.setMinimum(0);
438
                        progressBar.setMaximum(100);
439
                        progressBar.setValue(50);
440
                }
441

    
442
                return progressBar;
443
        }
444

    
445
        /**
446
         * DOCUMENT ME!
447
         *
448
         * @param d
449
         */
450
        public void setFixedLabelWidth(double d) {
451
                lblTexto.setPreferredSize(new Dimension((int) d, lblTexto.getHeight()));
452
        }
453

    
454
        /**
455
         * Adds a control to the status bar
456
         */
457
        public void addControl(String id, Component control) {
458
                this.add(control);
459
                if (!controls.containsKey(control.getName()))
460
                                controls.put(control.getName(), control);
461
                else
462
                        logger.debug("NewStatusBar.addControl -- control 'id' already exists"+ id);
463
        }
464

    
465
        /**
466
         * Gets a control from the status bar
467
         */
468
        public Component getControl(String id, Component control) {
469
                return (Component) controls.get(id);
470
        }
471
} //  @jve:decl-index=0:visual-constraint="10,10"