Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / frameworks / _fwAndami / src / com / iver / andami / ui / mdiFrame / NewStatusBar.java @ 7340

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,17));
102
                lblIcon = new JLabel();
103
                lblTexto = new JLabel();
104

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

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

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

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

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

    
140
                                break;
141

    
142
                        case WARNING:
143
                                setWarningText(textoAnterior);
144

    
145
                                break;
146

    
147
                        case ERROR:
148
                                setErrorText(textoAnterior);
149

    
150
                                break;
151
                }
152

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
311
                this.repaint();
312
        }
313

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

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

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

    
335
                int ws = this.getWidth();
336

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

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

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

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

    
361
                        //}
362
                }
363
        }
364

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

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

    
378

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

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

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

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

    
407
                ajustar();
408
        }
409

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

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

    
441
                return progressBar;
442
        }
443

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

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

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