Statistics
| Revision:

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

History | View | Annotate | Download (12.1 KB)

1 1104 fjp
/* 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 598 fernando
package com.iver.andami.ui.mdiFrame;
42
43 10404 cesar
import java.awt.BorderLayout;
44 663 fernando
import java.awt.Component;
45 598 fernando
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 10404 cesar
import javax.swing.SwingConstants;
55 663 fernando
import javax.swing.border.BevelBorder;
56 598 fernando
57 5275 jaume
import org.apache.log4j.Logger;
58 6623 cesar
import org.gvsig.gui.beans.controls.IControl;
59 598 fernando
60 5275 jaume
import com.iver.andami.messages.Messages;
61
import com.iver.andami.plugins.config.generate.Label;
62
63
64 598 fernando
/**
65
 */
66
public class NewStatusBar extends JPanel {
67 1268 vcaballero
        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 6623 cesar
        private HashMap controls = new HashMap();
84 10404 cesar
        private JPanel controlContainer;
85 598 fernando
86 1268 vcaballero
        /**
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 598 fernando
100 1268 vcaballero
        /**
101
         * This method initializes this
102
         */
103
        private void initialize() {
104 10404 cesar
                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 7346 maquerol
                this.setPreferredSize(new java.awt.Dimension(183,20));
113
                this.setSize(new java.awt.Dimension(183,20));
114 1268 vcaballero
                lblIcon = new JLabel();
115
                lblTexto = new JLabel();
116 10404 cesar
                lblTexto.setAlignmentX(JLabel.LEFT_ALIGNMENT);
117
                lblTexto.setHorizontalAlignment(SwingConstants.LEFT);
118
                lblTexto.setHorizontalTextPosition(SwingConstants.LEFT);
119 2938 fjp
120 10404 cesar
                FlowLayout rightLayout = new FlowLayout();
121
                controlContainer.setLayout(rightLayout);
122
                rightLayout.setHgap(1);
123
                rightLayout.setVgap(2);
124
                rightLayout.setAlignment(java.awt.FlowLayout.RIGHT);
125 1268 vcaballero
                lblIcon.setText("");
126 2938 fjp
                lblTexto.setText(Messages.getString("StatusBar.Aplicacion_iniciada"));
127 10404 cesar
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 1268 vcaballero
        }
137 598 fernando
138 1268 vcaballero
        /**
139
         * Obtiene el texto de la barra de estado
140
         *
141
         * @return texto
142
         */
143
        public String getStatusText() {
144
                return lblTexto.getText();
145
        }
146 598 fernando
147 1268 vcaballero
        /**
148
         * Restaura el contenido permanente de la barra de estado
149
         */
150
        public void restaurarTexto() {
151
                contenidoTemporal = false;
152 598 fernando
153 1268 vcaballero
                if (estadoAnterior == -1) {
154
                        return;
155
                }
156 598 fernando
157 1268 vcaballero
                switch (estadoAnterior) {
158
                        case INFO:
159
                                setInfoText(textoAnterior);
160 598 fernando
161 1268 vcaballero
                                break;
162 598 fernando
163 1268 vcaballero
                        case WARNING:
164
                                setWarningText(textoAnterior);
165 598 fernando
166 1268 vcaballero
                                break;
167 598 fernando
168 1268 vcaballero
                        case ERROR:
169
                                setErrorText(textoAnterior);
170 598 fernando
171 1268 vcaballero
                                break;
172
                }
173 598 fernando
174 1268 vcaballero
                estadoAnterior = -1;
175
                textoAnterior = null;
176
        }
177 598 fernando
178 1268 vcaballero
        /**
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 598 fernando
187 1268 vcaballero
                estadoAnterior = this.estado;
188
                this.estado = INFO;
189
                lblIcon.setIcon(infoIcon);
190 598 fernando
191 1268 vcaballero
                textoAnterior = getStatusText();
192
                lblTexto.setText(texto);
193
        }
194 598 fernando
195 1268 vcaballero
        /**
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 598 fernando
204 1268 vcaballero
                estadoAnterior = this.estado;
205
                this.estado = WARNING;
206
                lblIcon.setIcon(warningIcon);
207 598 fernando
208 1268 vcaballero
                textoAnterior = getStatusText();
209
                lblTexto.setText(texto);
210
        }
211 598 fernando
212 1268 vcaballero
        /**
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 598 fernando
221 1268 vcaballero
                estadoAnterior = this.estado;
222
                this.estado = ERROR;
223
                lblIcon.setIcon(errorIcon);
224 598 fernando
225 1268 vcaballero
                textoAnterior = getStatusText();
226
                lblTexto.setText(texto);
227
        }
228 598 fernando
229 1268 vcaballero
        /**
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 2938 fjp
246 1268 vcaballero
        /**
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 598 fernando
263 1268 vcaballero
        /**
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 598 fernando
280 1268 vcaballero
        /**
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 598 fernando
294 1268 vcaballero
                getProgressBar().repaint();
295
        }
296 6860 jaume
297 6614 cesar
        /*public void setControls(Label[] labels, ArrayList controlArray) {
298
                removeAllLabels();
299 6588 cesar
                if (labels!=null)
300
                        setLabelSet(labels);
301
                if (controlArray!=null)
302
                        setControls(controlArray);
303
                this.repaint();
304 6614 cesar
        }*/
305 6860 jaume
306 1268 vcaballero
        public void setLabelSet(Label[] labels) {
307 6614 cesar
                removeAllLabels();
308 1268 vcaballero
                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 7346 maquerol
                                        this.getHeight() - 2));
314
                        lbl.setSize(new Dimension(labels[i].getSize(), this.getHeight() - 2));
315 1268 vcaballero
                        lbl.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
316 10404 cesar
                        controlContainer.add(lbl);
317 1268 vcaballero
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 6614 cesar
332
                this.repaint();
333 1268 vcaballero
        }
334 6860 jaume
335 6614 cesar
        /*public void setControls(ArrayList controlList) {
336 6860 jaume

337 6588 cesar
                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 6614 cesar
        }*/
344 1268 vcaballero
345
        /**
346
         * Los Labels que estan sin texto no se a?aden a la barra y su espacio es
347 6588 cesar
         * utilizado por otros labels. Si se quiere hacer en un futuro la
348 1268 vcaballero
         * 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 7346 maquerol
                                                this.getHeight() - 2));
376 1268 vcaballero
                        } else {
377
                                label.setVisible(false);
378
379
                                //label.setPreferredSize(new Dimension(0,this.getHeight()));
380
                        }
381
382
                        //}
383
                }
384
        }
385
386
        /**
387 6614 cesar
         * Removes all the labels from the status bar.
388 1268 vcaballero
         */
389 6614 cesar
        private void removeAllLabels() {
390 10404 cesar
                Component[] controlArray = controlContainer.getComponents();
391 6614 cesar
392
                for (int i = 0; i < controlArray.length; i++) {
393
                        if ((controlArray[i] != lblIcon) && (controlArray[i] != lblTexto && controlArray[i] instanceof JLabel)) {
394 10404 cesar
                                controlContainer.remove(controlArray[i]);
395 6614 cesar
                        }
396
                }
397
        }
398 6860 jaume
399
400 6614 cesar
        /**
401
         * Removes all the controls (including labels) from the status bar.
402
         */
403 6588 cesar
        private void removeAllControls() {
404 10404 cesar
                Component[] controlArray = controlContainer.getComponents();
405 1268 vcaballero
406 6588 cesar
                for (int i = 0; i < controlArray.length; i++) {
407
                        if ((controlArray[i] != lblIcon) && (controlArray[i] != lblTexto)) {
408 10404 cesar
                                controlContainer.remove(controlArray[i]);
409 663 fernando
                        }
410
                }
411
        }
412
413
        /**
414 1268 vcaballero
         * 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 598 fernando
422 1268 vcaballero
                if (lbl == null) {
423 9490 fjp
//                        logger.debug("no label called " + id);
424 1268 vcaballero
                } else {
425
                        lbl.setText(msg);
426
                }
427 598 fernando
428 1268 vcaballero
                ajustar();
429
        }
430 6860 jaume
431 6623 cesar
        /**
432
         * Sets the control identified by 'id' with the provided value.
433 6860 jaume
         *
434 6623 cesar
         * @param id
435
         * @param value
436
         */
437
        public void setControlValue(String id, String value) {
438
                IControl control = (IControl) controls.get(id);
439
                if (control!=null) {
440
                        control.setValue(value);
441
                }
442
                else {
443
                        logger.debug("NewStatusBar -- no control called " + id);
444
                }
445
        }
446 598 fernando
447 1268 vcaballero
        /**
448
         * This method initializes progressBar
449
         *
450
         * @return javax.swing.JProgressBar
451
         */
452
        private JProgressBar getProgressBar() {
453
                if (progressBar == null) {
454
                        progressBar = new JProgressBar();
455 2112 fernando
                        progressBar.setPreferredSize(new java.awt.Dimension(100, 14));
456 1268 vcaballero
                        progressBar.setVisible(false);
457
                        progressBar.setMinimum(0);
458
                        progressBar.setMaximum(100);
459 2112 fernando
                        progressBar.setValue(50);
460 1268 vcaballero
                }
461 598 fernando
462 1268 vcaballero
                return progressBar;
463
        }
464
465 598 fernando
        /**
466 1268 vcaballero
         * DOCUMENT ME!
467
         *
468 598 fernando
         * @param d
469
         */
470
        public void setFixedLabelWidth(double d) {
471 10404 cesar
        //        lblTexto.setPreferredSize(new Dimension((int) d, lblTexto.getHeight()));
472 598 fernando
        }
473 6860 jaume
474 6623 cesar
        /**
475
         * Adds a control to the status bar
476
         */
477
        public void addControl(String id, Component control) {
478 10404 cesar
                controlContainer.add(control);
479 6623 cesar
                if (!controls.containsKey(control.getName()))
480
                                controls.put(control.getName(), control);
481
                else
482
                        logger.debug("NewStatusBar.addControl -- control 'id' already exists"+ id);
483
        }
484 6860 jaume
485 6623 cesar
        /**
486
         * Gets a control from the status bar
487
         */
488
        public Component getControl(String id, Component control) {
489
                return (Component) controls.get(id);
490
        }
491 598 fernando
} //  @jve:decl-index=0:visual-constraint="10,10"