Statistics
| Revision:

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

History | View | Annotate | Download (14.9 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
 * <p>This class contains the status bar. It contains the graphical component,
66
 * and the methods to manage it.
67
 * </p>
68
 * 
69
 * <p>The status bar is divided in several areas. At the very left, there is
70
 * an icon and the main status text. There are three icons to show:
71
 * Info, Warning and Error icons. They can be set together with the main
72
 * status text using the methods <code>setInfoText()</code>,
73
 * <code>setWarningText()</code> and <code>setErrorText()</code> (and also with
74
 * <code>setInfoTextTemporal()</code>, etc). Then, there is a right area which
75
 * contains labels and other controls. Labels are set in the config.xml files
76
 * and are visible or not depending on the currently selected Andami window.
77
 * Controls are associated to extensions, and are enabled/disabled and
78
 * visible/hidden depending on the associated extension.
79
 * </p>
80
 * 
81
 */
82
public class NewStatusBar extends JPanel {
83
        private static Logger logger = Logger.getLogger(NewStatusBar.class.getName());
84
        private static final int INFO = 0;
85
        private static final int WARNING = 1;
86
        private static final int ERROR = 2;
87
        private JLabel lblIcon = null;
88
        private JLabel lblTexto = null;
89
        private boolean contenidoTemporal;
90
        private String textoAnterior;
91
        private int estadoAnterior;
92
        private ImageIcon infoIcon;
93
        private ImageIcon warningIcon;
94
        private ImageIcon errorIcon;
95
        private int estado;
96
        private JProgressBar progressBar = null;
97
        private HashMap idLabel = new HashMap();
98
        private int[] widthlabels = null;
99
        private HashMap controls = new HashMap();
100
        private JPanel controlContainer;
101

    
102
        /**
103
         * This is the default constructor
104
         */
105
        public NewStatusBar() {
106
                super();
107
                initialize();
108
                infoIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
109
                                                                                                   .getResource("images/info.gif"));
110
                warningIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
111
                                                                                                          .getResource("images/warning.gif"));
112
                errorIcon = new ImageIcon(NewStatusBar.class.getClassLoader()
113
                                                                                                        .getResource("images/error.gif"));
114
        }
115

    
116
        /**
117
         * This method initializes the status bar. It creates the required
118
         * containers and sets the layout.
119
         */
120
        private void initialize() {
121
                BorderLayout mainLayout = new BorderLayout();
122
                this.setLayout(mainLayout);
123
                
124
                JPanel container1 = new JPanel();
125
                this.add(container1, BorderLayout.CENTER);
126
                controlContainer = new JPanel();
127
                this.add(controlContainer, BorderLayout.EAST);
128
                
129
                this.setPreferredSize(new java.awt.Dimension(183,20));
130
                this.setSize(new java.awt.Dimension(183,20));
131
                lblIcon = new JLabel();
132
                lblTexto = new JLabel();
133
                lblTexto.setAlignmentX(JLabel.LEFT_ALIGNMENT);
134
                lblTexto.setHorizontalAlignment(SwingConstants.LEFT);
135
                lblTexto.setHorizontalTextPosition(SwingConstants.LEFT);
136

    
137
                FlowLayout rightLayout = new FlowLayout();
138
                controlContainer.setLayout(rightLayout);
139
                rightLayout.setHgap(1);
140
                rightLayout.setVgap(2);
141
                rightLayout.setAlignment(java.awt.FlowLayout.RIGHT);
142
                lblIcon.setText("");
143
                lblTexto.setText(Messages.getString("StatusBar.Aplicacion_iniciada"));
144
                
145
                FlowLayout leftLayout = new FlowLayout(FlowLayout.LEFT);
146
                leftLayout.setHgap(1);
147
                leftLayout.setVgap(2);
148
                container1.setLayout(leftLayout);
149
                container1.add(lblIcon, null);
150
                container1.add(getProgressBar(), null);
151
                container1.add(lblTexto, null);
152

    
153
        }
154

    
155
        /**
156
         * Gets the status bar main text.
157
         *
158
         * @return The status bar main text.
159
         * @see #setInfoText(String)
160
         * @see #setWarningText(String)
161
         * @see #setErrorText(String)
162
         * @see #setInfoTextTemporal(String)
163
         * @see #setWarningTextTemporal(String)
164
         * @see #setErrorTextTemporal(String)
165
         */
166
        public String getStatusText() {
167
                return lblTexto.getText();
168
        }
169

    
170
        /**
171
         * Restores the previous contents in the status bar main text,
172
         * after the {@link #setInfoTextTemporal(String)}, {@link #setWarningTextTemporal(String)}
173
         * or {@link #setErrorTextTemporal(String)} have been called.
174
         * 
175
         * @see #setInfoTextTemporal(String)
176
         * @see #setWarningTextTemporal(String)
177
         * @see #setErrorTextTemporal(String)
178
         */
179
        public void restaurarTexto() {
180
                contenidoTemporal = false;
181

    
182
                if (estadoAnterior == -1) {
183
                        return;
184
                }
185

    
186
                switch (estadoAnterior) {
187
                        case INFO:
188
                                setInfoText(textoAnterior);
189

    
190
                                break;
191

    
192
                        case WARNING:
193
                                setWarningText(textoAnterior);
194

    
195
                                break;
196

    
197
                        case ERROR:
198
                                setErrorText(textoAnterior);
199

    
200
                                break;
201
                }
202

    
203
                estadoAnterior = -1;
204
                textoAnterior = null;
205
        }
206

    
207
        /**
208
         * Sets a temporary information message in the status bar, and changes the
209
         * icon to an Info icon. The previous text and icon can be restored using
210
         * the {@link #restaurarTexto()} method.
211
         *
212
         * @param texto The text to set
213
         * @see #restaurarTexto()
214
         */
215
        public void setInfoTextTemporal(String texto) {
216
                contenidoTemporal = true;
217

    
218
                estadoAnterior = this.estado;
219
                this.estado = INFO;
220
                lblIcon.setIcon(infoIcon);
221

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

    
226
        /**
227
         * Sets a temporary warning message in the status bar, and changes the
228
         * icon to a Warning icon. The previous text and icon can be restored using
229
         * the {@link #restaurarTexto()} method.
230
         *
231
         * @param texto The text to set
232
         * @see #restaurarTexto()
233
         */
234
        public void setWarningTextTemporal(String texto) {
235
                contenidoTemporal = true;
236

    
237
                estadoAnterior = this.estado;
238
                this.estado = WARNING;
239
                lblIcon.setIcon(warningIcon);
240

    
241
                textoAnterior = getStatusText();
242
                lblTexto.setText(texto);
243
        }
244

    
245
        /**
246
         * Sets a temporary error message in the status bar, and changes the
247
         * icon to an Error icon. The previous text and icon can be restored using
248
         * the {@link #restaurarTexto()} method.
249
         *
250
         * @param texto The text to set
251
         * @see #restaurarTexto()
252
         */
253
        public void setErrorTextTemporal(String texto) {
254
                contenidoTemporal = true;
255

    
256
                estadoAnterior = this.estado;
257
                this.estado = ERROR;
258
                lblIcon.setIcon(errorIcon);
259

    
260
                textoAnterior = getStatusText();
261
                lblTexto.setText(texto);
262
        }
263

    
264
        /**
265
         * Sets a permanent info message in the status bar, and changes the
266
         * permanent icon to an Info icon. If there is a temporary message showing
267
         * at the moment, the message set now is not shown until
268
         * the {@link #restaurarTexto()} method is called.
269
         *
270
         * @param texto The permanent info message to set
271
         * @see #restaurarTexto()
272
         */
273
        public void setInfoText(String texto) {
274
                if (contenidoTemporal) {
275
                        textoAnterior = texto;
276
                        estadoAnterior = INFO;
277
                } else {
278
                        lblTexto.setText(texto);
279
                        lblIcon.setIcon(infoIcon);
280
                        estado = INFO;
281
                }
282
        }
283

    
284
        /**
285
         * Sets a permanent warning message in the status bar, and changes the
286
         * permanent icon to a Warning icon. If there is a temporary message showing
287
         * at the moment, the message set now is not shown until
288
         * the {@link #restaurarTexto()} method is called.
289
         *
290
         * @param texto The permanent warning message to set
291
         * @see #restaurarTexto()
292
         */
293
        public void setWarningText(String texto) {
294
                if (contenidoTemporal) {
295
                        textoAnterior = texto;
296
                        estadoAnterior = WARNING;
297
                } else {
298
                        lblTexto.setText(texto);
299
                        lblIcon.setIcon(warningIcon);
300
                        estado = WARNING;
301
                }
302
        }
303

    
304
        /**
305
         * Sets a permanent error message in the status bar, and changes the
306
         * permanent icon to an Error icon. If there is a temporary message showing
307
         * at the moment, the message set now is not shown until
308
         * the {@link #restaurarTexto()} method is called.
309
         *
310
         * @param texto The permanent info message to set
311
         * @see #restaurarTexto()
312
         */
313
        public void setErrorText(String texto) {
314
                if (contenidoTemporal) {
315
                        textoAnterior = texto;
316
                        estadoAnterior = ERROR;
317
                } else {
318
                        lblTexto.setText(texto);
319
                        lblIcon.setIcon(errorIcon);
320
                        estado = ERROR;
321
                }
322
        }
323

    
324
        /**
325
         * If <code>p</code> is a value between 0 and 99, it shows a progress bar
326
         * in the left area of the status bar, and sets the specified progress.
327
         * If <code>p</code> is bigger than 99, it hides the progress bar.
328
         *
329
         * @param p The progress to set in the progress bar. If it is bigger
330
         * than 99, the task will be considered to be finished, and the
331
         * progress bar will be hidden.
332
         */
333
        public void setProgress(int p) {
334
                if (p < 100) {
335
                        getProgressBar().setValue(p);
336
                        getProgressBar().setVisible(true);
337
                } else {
338
                        getProgressBar().setVisible(false);
339
                }
340

    
341
                getProgressBar().repaint();
342
        }
343

    
344
        /**
345
         * Sets a label-set to be shown in the status bar. This method it is not
346
         * intended to be used directly, because the set will be overwritten when the selected
347
         * window changes. Use {@link MainFrame#setStatusBarLabels(Class, Label[])}
348
         * to permanently associate a label set with a window.
349
         * 
350
         * @param labels The labels to set.
351
         * @see MainFrame#setStatusBarLabels(Class, Label[])
352
         */
353
        public void setLabelSet(Label[] labels) {
354
                removeAllLabels();
355
                idLabel.clear();
356

    
357
                for (int i = 0; i < labels.length; i++) {
358
                        JLabel lbl = new JLabel();
359
                        lbl.setPreferredSize(new Dimension(labels[i].getSize(),
360
                                        this.getHeight() - 2));
361
                        lbl.setSize(new Dimension(labels[i].getSize(), this.getHeight() - 2));
362
                        lbl.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
363
                        controlContainer.add(lbl);
364

    
365
                        /*            if (i != labels.length - 1){
366
                           this.add(new JSeparator(JSeparator.VERTICAL));
367
                           }
368
                         */
369
                        idLabel.put(labels[i].getId(), lbl);
370
                }
371

    
372
                JLabel[] configlabels = (JLabel[]) idLabel.values().toArray(new JLabel[0]);
373
                widthlabels = new int[configlabels.length];
374

    
375
                for (int i = 0; i < labels.length; i++) {
376
                        widthlabels[i] = configlabels[i].getWidth();
377
                }
378

    
379
                this.repaint();
380
        }
381

    
382
        /**
383
         * Hides the empty labels and adjust the space in the bar.
384
         */
385
        public void ajustar() {
386
                if (widthlabels == null) {
387
                        return;
388
                }
389

    
390
                int ws = this.getWidth();
391

    
392
                JLabel[] labels = (JLabel[]) idLabel.values().toArray(new JLabel[0]);
393

    
394
                /*        double total = 1;
395
                   for (int i = 0; i < widthlabels.length; i++) {
396
                           if (labels[i].getText().compareTo("") != 0) {
397
                                   total += widthlabels[i];
398
                           }
399
                   }
400
                   double p = (ws - lblTexto.getWidth() - 20) / total;
401
                 */
402
                for (int i = 0; i < labels.length; i++) {
403
                        //if (labels[i] instanceof JLabel){
404
                        JLabel label = (JLabel) labels[i];
405

    
406
                        if (label.getText().compareTo("") != 0) {
407
                                label.setVisible(true);
408
                                label.setPreferredSize(new Dimension((int) (widthlabels[i]),
409
                                                this.getHeight() - 2));
410
                        } else {
411
                                label.setVisible(false);
412

    
413
                                //label.setPreferredSize(new Dimension(0,this.getHeight()));
414
                        }
415

    
416
                        //}
417
                }
418
        }
419

    
420
        /**
421
         * Removes all the labels from the status bar. It does not remove the
422
         * controls.
423
         */
424
        private void removeAllLabels() {
425
                Component[] controlArray = controlContainer.getComponents();
426

    
427
                for (int i = 0; i < controlArray.length; i++) {
428
                        if ((controlArray[i] != lblIcon) && (controlArray[i] != lblTexto) && !(controlArray[i] instanceof IControl) && (controlArray[i] instanceof JLabel)) {
429
                                controlContainer.remove(controlArray[i]);
430
                        }
431
                }
432
        }
433

    
434

    
435
        /**
436
         * Removes all the controls (including labels) from the status bar.
437
         */
438
        private void removeAllControls() {
439
                Component[] controlArray = controlContainer.getComponents();
440

    
441
                for (int i = 0; i < controlArray.length; i++) {
442
                        if ((controlArray[i] != lblIcon) && (controlArray[i] != lblTexto)) {
443
                                controlContainer.remove(controlArray[i]);
444
                        }
445
                }
446
        }
447

    
448
        /**
449
         * Sets the text of the provided label.
450
         *
451
         * @param id The ID of the label to modify. It is defined in the
452
         * config.xml file
453
         * @param msg The message to show in the label
454
         */
455
        public void setMessage(String id, String msg) {
456
                JLabel lbl = (JLabel) idLabel.get(id);
457

    
458
                if (lbl == null) {
459
//                        logger.debug("no label called " + id);
460
                        // try with controls
461
                        try {
462
                                IControl control = (IControl) controls.get(id);
463
                                if (control!=null) control.setValue(msg);
464
                        }
465
                        catch (ClassCastException ex) {}
466
                } else {
467
                        lbl.setText(msg);
468
                }
469
                ajustar();
470
        }
471

    
472
        /**
473
         * Sets the control identified by 'id' with the provided value.
474
         *
475
         * @param id The ID of the control to modify
476
         * @param value The value to set in the control
477
         */
478
        public void setControlValue(String id, String value) {
479
                IControl control = (IControl) controls.get(id);
480
                if (control!=null) {
481
                        control.setValue(value);
482
                }
483
                else {
484
                        logger.debug("NewStatusBar -- no control called " + id);
485
                }
486
        }
487

    
488
        /**
489
         * This method initializes the progressBar and gets it.
490
         *
491
         * @return javax.swing.JProgressBar
492
         */
493
        private JProgressBar getProgressBar() {
494
                if (progressBar == null) {
495
                        progressBar = new JProgressBar();
496
                        progressBar.setPreferredSize(new java.awt.Dimension(100, 14));
497
                        progressBar.setVisible(false);
498
                        progressBar.setMinimum(0);
499
                        progressBar.setMaximum(100);
500
                        progressBar.setValue(50);
501
                }
502

    
503
                return progressBar;
504
        }
505

    
506
        /**
507
         * DOCUMENT ME!
508
         *
509
         * @param d
510
         * @deprecated
511
         */
512
        public void setFixedLabelWidth(double d) {
513
        //        lblTexto.setPreferredSize(new Dimension((int) d, lblTexto.getHeight()));
514
        }
515

    
516
        /**
517
         * Adds a control to the status bar
518
         * 
519
         * @param id The ID of the control, useful to later retrive it or set its value
520
         * @param control The control to add
521
         */
522
        public void addControl(String id, Component control) {
523
                controlContainer.add(control);
524
                if (!controls.containsKey(control.getName()))
525
                                controls.put(control.getName(), control);
526
                else
527
                        logger.debug("NewStatusBar.addControl -- control 'id' already exists"+ id);
528
        }
529

    
530
        /**
531
         * Gets a control from the status bar
532
         * 
533
         * @param id The ID of the control to get
534
         */
535
        public Component getControl(String id, Component control) {
536
                return (Component) controls.get(id);
537
        }
538
} //  @jve:decl-index=0:visual-constraint="10,10"