Statistics
| Revision:

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

History | View | Annotate | Download (12.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.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
        /**
87
         * This is the default constructor
88
         */
89
        public NewStatusBar() {
90
                super();
91
                initialize();
92
                infoIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
93
                                                                                                   .getResource("images/info.gif"));
94
                warningIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
95
                                                                                                          .getResource("images/warning.gif"));
96
                errorIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
97
                                                                                                        .getResource("images/error.gif"));
98
        }
99

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

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

    
136
        }
137

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

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

    
153
                if (estadoAnterior == -1) {
154
                        return;
155
                }
156

    
157
                switch (estadoAnterior) {
158
                        case INFO:
159
                                setInfoText(textoAnterior);
160

    
161
                                break;
162

    
163
                        case WARNING:
164
                                setWarningText(textoAnterior);
165

    
166
                                break;
167

    
168
                        case ERROR:
169
                                setErrorText(textoAnterior);
170

    
171
                                break;
172
                }
173

    
174
                estadoAnterior = -1;
175
                textoAnterior = null;
176
        }
177

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

    
187
                estadoAnterior = this.estado;
188
                this.estado = INFO;
189
                lblIcon.setIcon(infoIcon);
190

    
191
                textoAnterior = getStatusText();
192
                lblTexto.setText(texto);
193
        }
194

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

    
204
                estadoAnterior = this.estado;
205
                this.estado = WARNING;
206
                lblIcon.setIcon(warningIcon);
207

    
208
                textoAnterior = getStatusText();
209
                lblTexto.setText(texto);
210
        }
211

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

    
221
                estadoAnterior = this.estado;
222
                this.estado = ERROR;
223
                lblIcon.setIcon(errorIcon);
224

    
225
                textoAnterior = getStatusText();
226
                lblTexto.setText(texto);
227
        }
228

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

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

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

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

    
294
                getProgressBar().repaint();
295
        }
296

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

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

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

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

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

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

    
332
                this.repaint();
333
        }
334

    
335
        /*public void setControls(ArrayList controlList) {
336

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

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

    
356
                int ws = this.getWidth();
357

    
358
                JLabel[] labels = (JLabel[]) idLabel.values().toArray(new JLabel[0]);
359

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

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

    
379
                                //label.setPreferredSize(new Dimension(0,this.getHeight()));
380
                        }
381

    
382
                        //}
383
                }
384
        }
385

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

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

    
399

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

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

    
413
        /**
414
         * Establece el texto de una de las etiquetas
415
         *
416
         * @param id Identificador de las etiquetas
417
         * @param msg Mensaje que se quiere poner en la etiqueta
418
         */
419
        public void setMessage(String id, String msg) {
420
                JLabel lbl = (JLabel) idLabel.get(id);
421

    
422
                if (lbl == null) {
423
//                        logger.debug("no label called " + id);
424
                        // try with controls
425
                        try {
426
                                IControl control = (IControl) controls.get(id);
427
                                if (control!=null) control.setValue(msg);
428
                        }
429
                        catch (ClassCastException ex) {}
430
                } else {
431
                        lbl.setText(msg);
432
                }
433
                ajustar();
434
        }
435

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

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

    
467
                return progressBar;
468
        }
469

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

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

    
490
        /**
491
         * Gets a control from the status bar
492
         */
493
        public Component getControl(String id, Component control) {
494
                return (Component) controls.get(id);
495
        }
496
} //  @jve:decl-index=0:visual-constraint="10,10"