Statistics
| Revision:

root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / ui / mdiFrame / NewStatusBar.java @ 38512

History | View | Annotate | Download (18 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.andami.ui.mdiFrame;
23

    
24
import java.awt.BorderLayout;
25
import java.awt.Component;
26
import java.awt.Dimension;
27
import java.awt.FlowLayout;
28
import java.awt.LayoutManager;
29
import java.util.HashMap;
30
import java.util.Map;
31

    
32
import javax.swing.BorderFactory;
33
import javax.swing.ImageIcon;
34
import javax.swing.JLabel;
35
import javax.swing.JPanel;
36
import javax.swing.JProgressBar;
37
import javax.swing.SwingConstants;
38
import javax.swing.border.BevelBorder;
39

    
40
import org.apache.log4j.Logger;
41

    
42
import org.gvsig.andami.PluginServices;
43
import org.gvsig.andami.messages.Messages;
44
import org.gvsig.andami.plugins.config.generate.Label;
45
import org.gvsig.gui.beans.controls.IControl;
46
import org.gvsig.tools.swing.api.ToolsSwingLocator;
47
import org.gvsig.tools.swing.api.task.JTasksStatus;
48

    
49
/**
50
 * <p>
51
 * This class contains the status bar. It contains the graphical component, and
52
 * the methods to manage it.
53
 * </p>
54
 * 
55
 * <p>
56
 * The status bar is divided in several areas. At the very left, there is an
57
 * icon and the main status text. There are three icons to show: Info, Warning
58
 * and Error icons. They can be set together with the main status text using the
59
 * methods <code>setInfoText()</code>, <code>setWarningText()</code> and
60
 * <code>setErrorText()</code> (and also with <code>setInfoTextTemporal()</code>
61
 * , etc). Then, there is a right area which contains labels and other controls.
62
 * Labels are set in the config.xml files and are visible or not depending on
63
 * the currently selected Andami window. Controls are associated to extensions,
64
 * and are enabled/disabled and visible/hidden depending on the associated
65
 * extension.
66
 * </p>
67
 * 
68
 */
69
public class NewStatusBar extends JPanel {
70

    
71
    private static final long serialVersionUID = 5575549032728844632L;
72
    private static Logger LOG = Logger.getLogger(NewStatusBar.class);
73
    private static final int INFO = 0;
74
    private static final int WARNING = 1;
75
    private static final int ERROR = 2;
76
    private JLabel lblIcon = null;
77
    private JLabel lblTexto = null;
78
    private boolean contenidoTemporal;
79
    private String textoAnterior;
80
    private int estadoAnterior;
81
    private ImageIcon infoIcon;
82
    private ImageIcon warningIcon;
83
    private ImageIcon errorIcon;
84
    private int estado;
85
    private JProgressBar progressBar = null;
86
    private Map<String, JLabel> idLabel = new HashMap<String, JLabel>();
87
    private int[] widthlabels = null;
88
    private Map<String, Component> controls = new HashMap<String, Component>();
89
    private JPanel controlContainer;
90
    private JTasksStatus tasksStatus = null;
91

    
92
    /**
93
     * This is the default constructor
94
     */
95
    public NewStatusBar() {
96
        super();
97
        initialize();
98
        infoIcon = PluginServices.getIconTheme().get("info-icon");
99
        warningIcon = PluginServices.getIconTheme().get("warning-icon");
100
        errorIcon = PluginServices.getIconTheme().get("error-icon");
101
    }
102

    
103
    /**
104
     * This method initializes the status bar. It creates the required
105
     * containers and sets the layout.
106
     */
107
    private void initialize() {
108
        LayoutManager mainLayout = new BorderLayout();
109
        this.setLayout(mainLayout);
110

    
111
        JPanel panelRight = new JPanel(new FlowLayout(FlowLayout.RIGHT, 1, 0));
112

    
113
        controlContainer = new JPanel(new FlowLayout(FlowLayout.RIGHT, 1, 0));
114
        panelRight.add(controlContainer);
115
        panelRight.add(getTasksStatus());
116

    
117
        lblIcon = new JLabel();
118
        lblTexto = new JLabel();
119
        lblTexto.setAlignmentX(JLabel.LEFT_ALIGNMENT);
120
        lblTexto.setHorizontalAlignment(SwingConstants.LEFT);
121
        lblTexto.setHorizontalTextPosition(SwingConstants.LEFT);
122
        lblTexto.setVerticalAlignment(SwingConstants.CENTER);
123
        lblTexto.setVerticalTextPosition(SwingConstants.CENTER);
124
        lblIcon.setText("");
125
        lblTexto.setText(Messages.getString("StatusBar.Aplicacion_iniciada"));
126

    
127
        JPanel panelLeft = new JPanel();
128
        panelLeft.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 0));
129
        panelLeft.add(lblTexto);
130
        panelLeft.add(lblIcon);
131
        panelLeft.add(getProgressBar());
132

    
133
        this.add(panelLeft, BorderLayout.CENTER);
134
        this.add(panelRight, BorderLayout.EAST);
135
    }
136

    
137
    /**
138
     * Gets the status bar main text.
139
     * 
140
     * @return The status bar main text.
141
     * @see #setInfoText(String)
142
     * @see #setWarningText(String)
143
     * @see #setErrorText(String)
144
     * @see #setInfoTextTemporal(String)
145
     * @see #setWarningTextTemporal(String)
146
     * @see #setErrorTextTemporal(String)
147
     */
148
    public String getStatusText() {
149
        return lblTexto.getText();
150
    }
151

    
152
    /**
153
     * Restores the previous contents in the status bar main text,
154
     * after the {@link #setInfoTextTemporal(String)},
155
     * {@link #setWarningTextTemporal(String)} or
156
     * {@link #setErrorTextTemporal(String)} have been called.
157
     * 
158
     * @see #setInfoTextTemporal(String)
159
     * @see #setWarningTextTemporal(String)
160
     * @see #setErrorTextTemporal(String)
161
     */
162
    public void restaurarTexto() {
163
        contenidoTemporal = false;
164

    
165
        if (estadoAnterior == -1) {
166
            return;
167
        }
168

    
169
        switch (estadoAnterior) {
170
        case INFO:
171
            setInfoText(textoAnterior);
172

    
173
            break;
174

    
175
        case WARNING:
176
            setWarningText(textoAnterior);
177

    
178
            break;
179

    
180
        case ERROR:
181
            setErrorText(textoAnterior);
182

    
183
            break;
184
        }
185

    
186
        estadoAnterior = -1;
187
        textoAnterior = null;
188
    }
189

    
190
    /**
191
     * Sets a temporary information message in the status bar, and changes the
192
     * icon to an Info icon. The previous text and icon can be restored using
193
     * the {@link #restaurarTexto()} method.
194
     * 
195
     * @param texto
196
     *            The text to set
197
     * @see #restaurarTexto()
198
     */
199
    public void setInfoTextTemporal(String texto) {
200
        if (!contenidoTemporal) {
201
            textoAnterior = getStatusText();
202
            estadoAnterior = this.estado;
203
            contenidoTemporal = true;
204
        }
205

    
206
        this.estado = INFO;
207
        lblIcon.setIcon(infoIcon);
208
        lblTexto.setText(texto);
209
    }
210

    
211
    /**
212
     * Sets a temporary warning message in the status bar, and changes the
213
     * icon to a Warning icon. The previous text and icon can be restored using
214
     * the {@link #restaurarTexto()} method.
215
     * 
216
     * @param texto
217
     *            The text to set
218
     * @see #restaurarTexto()
219
     */
220
    public void setWarningTextTemporal(String texto) {
221
        if (!contenidoTemporal) {
222
            estadoAnterior = this.estado;
223
            textoAnterior = getStatusText();
224
            contenidoTemporal = true;
225
        }
226
        this.estado = WARNING;
227
        lblIcon.setIcon(warningIcon);
228
        lblTexto.setText(texto);
229
    }
230

    
231
    /**
232
     * Sets a temporary error message in the status bar, and changes the
233
     * icon to an Error icon. The previous text and icon can be restored using
234
     * the {@link #restaurarTexto()} method.
235
     * 
236
     * @param texto
237
     *            The text to set
238
     * @see #restaurarTexto()
239
     */
240
    public void setErrorTextTemporal(String texto) {
241
        if (!contenidoTemporal) {
242
            estadoAnterior = this.estado;
243
            textoAnterior = getStatusText();
244
            contenidoTemporal = true;
245
        }
246

    
247
        this.estado = ERROR;
248
        lblIcon.setIcon(errorIcon);
249
        lblTexto.setText(texto);
250
    }
251

    
252
    /**
253
     * Sets a permanent info message in the status bar, and changes the
254
     * permanent icon to an Info icon. If there is a temporary message showing
255
     * at the moment, the message set now is not shown until
256
     * the {@link #restaurarTexto()} method is called.
257
     * 
258
     * @param texto
259
     *            The permanent info message to set
260
     * @see #restaurarTexto()
261
     */
262
    public void setInfoText(String texto) {
263
        if (contenidoTemporal) {
264
            textoAnterior = texto;
265
            estadoAnterior = INFO;
266
        } else {
267
            lblTexto.setText(texto);
268
            lblIcon.setIcon(infoIcon);
269
            estado = INFO;
270
        }
271
    }
272

    
273
    /**
274
     * Sets a permanent warning message in the status bar, and changes the
275
     * permanent icon to a Warning icon. If there is a temporary message showing
276
     * at the moment, the message set now is not shown until
277
     * the {@link #restaurarTexto()} method is called.
278
     * 
279
     * @param texto
280
     *            The permanent warning message to set
281
     * @see #restaurarTexto()
282
     */
283
    public void setWarningText(String texto) {
284
        if (contenidoTemporal) {
285
            textoAnterior = texto;
286
            estadoAnterior = WARNING;
287
        } else {
288
            lblTexto.setText(texto);
289
            lblIcon.setIcon(warningIcon);
290
            estado = WARNING;
291
        }
292
    }
293

    
294
    /**
295
     * Sets a permanent error message in the status bar, and changes the
296
     * permanent icon to an Error icon. If there is a temporary message showing
297
     * at the moment, the message set now is not shown until
298
     * the {@link #restaurarTexto()} method is called.
299
     * 
300
     * @param texto
301
     *            The permanent info message to set
302
     * @see #restaurarTexto()
303
     */
304
    public void setErrorText(String texto) {
305
        if (contenidoTemporal) {
306
            textoAnterior = texto;
307
            estadoAnterior = ERROR;
308
        } else {
309
            lblTexto.setText(texto);
310
            lblIcon.setIcon(errorIcon);
311
            estado = ERROR;
312
        }
313
    }
314

    
315
    /**
316
     * If <code>p</code> is a value between 0 and 99, it shows a progress bar
317
     * in the left area of the status bar, and sets the specified progress.
318
     * If <code>p</code> is bigger than 99, it hides the progress bar.
319
     * 
320
     * @param p
321
     *            The progress to set in the progress bar. If it is bigger
322
     *            than 99, the task will be considered to be finished, and the
323
     *            progress bar will be hidden.
324
     * 
325
     * @deprecated use instead TaskStatus and TaskStatusManager
326
     */
327
    public void setProgress(int p) {
328
        if (p < 100) {
329
            getProgressBar().setValue(p);
330
            getProgressBar().setVisible(true);
331
        } else {
332
            getProgressBar().setVisible(false);
333
        }
334

    
335
        getProgressBar().repaint();
336
    }
337

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

    
353
        for (int i = 0; i < labels.length; i++) {
354
            // Set an initial text so the getPreferredSize works as expected
355
            JLabel lbl = new JLabel();
356
            lbl.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
357
            lbl.setName(labels[i].getId());
358
            controlContainer.add(lbl);
359

    
360
            /*
361
             * if (i != labels.length - 1){
362
             * this.add(new JSeparator(JSeparator.VERTICAL));
363
             * }
364
             */
365
            idLabel.put(labels[i].getId(), lbl);
366
        }
367

    
368
        JLabel[] configlabels =
369
            (JLabel[]) idLabel.values().toArray(new JLabel[0]);
370
        widthlabels = new int[configlabels.length];
371

    
372
        for (int i = 0; i < labels.length; i++) {
373
            widthlabels[i] = configlabels[i].getWidth();
374
        }
375

    
376
        this.validate();
377
    }
378

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

    
387
        JLabel[] labels = (JLabel[]) idLabel.values().toArray(new JLabel[0]);
388

    
389
        /*
390
         * double total = 1;
391
         * for (int i = 0; i < widthlabels.length; i++) {
392
         * if (labels[i].getText().compareTo("") != 0) {
393
         * total += widthlabels[i];
394
         * }
395
         * }
396
         * double p = (ws - lblTexto.getWidth() - 20) / total;
397
         */
398
        for (int i = 0; i < labels.length; i++) {
399
            JLabel label = (JLabel) labels[i];
400

    
401
            if (label.getText().compareTo("") != 0) {
402
                label.setVisible(true);
403
            } else {
404
                label.setVisible(false);
405
            }
406
        }
407
        validate();
408
    }
409

    
410
    /**
411
     * Removes all the labels from the status bar. It does not remove the
412
     * controls.
413
     */
414
    private void removeAllLabels() {
415
        Component[] controlArray = controlContainer.getComponents();
416

    
417
        for (int i = 0; i < controlArray.length; i++) {
418
            if ((controlArray[i] != lblIcon) && (controlArray[i] != lblTexto)
419
                && !(controlArray[i] instanceof IControl)
420
                && (controlArray[i] instanceof JLabel)) {
421
                controlContainer.remove(controlArray[i]);
422
            }
423
        }
424
    }
425

    
426
    /**
427
     * Sets the text of the provided label.
428
     * 
429
     * @param id
430
     *            The ID of the label to modify. It is defined in the
431
     *            config.xml file
432
     * @param msg
433
     *            The message to show in the label
434
     */
435
    public void setMessage(String id, String msg) {
436
        JLabel lbl = (JLabel) idLabel.get(id);
437

    
438
        if (lbl != null) {
439
                Dimension originalPreferredSize = lbl.getPreferredSize();
440
            lbl.setText(msg);
441
            // Set preferred size to null just in case it has been set
442
            // previously, as we want it to be calculated from the text size.
443
            lbl.setPreferredSize(null);
444
            // Allow only to increase label width, to avoid too much ugly 
445
            // width changes in the status bar components.
446
            if (lbl.getPreferredSize().width < originalPreferredSize.width) {
447
                    lbl.setPreferredSize(originalPreferredSize);
448
            }
449
        } else {
450
            // try with controls
451
            try {
452
                IControl control = (IControl) controls.get(id);
453
                if (control != null)
454
                    control.setValue(msg);
455
            } catch (ClassCastException ex) {
456
                LOG.debug("no label called " + id);
457
            }
458
        }
459

    
460
        ajustar();
461
    }
462

    
463
    /**
464
     * Sets the control identified by 'id' with the provided value.
465
     * 
466
     * @param id
467
     *            The ID of the control to modify
468
     * @param value
469
     *            The value to set in the control
470
     */
471
    public void setControlValue(String id, String value) {
472
        IControl control = (IControl) controls.get(id);
473
        if (control != null) {
474
            control.setValue(value);
475
        } else {
476
            LOG.debug("NewStatusBar -- no control called " + id);
477
        }
478
    }
479

    
480
    /**
481
     * This method initializes the progressBar and gets it.
482
     * 
483
     * @return javax.swing.JProgressBar
484
     */
485
    private JProgressBar getProgressBar() {
486
        if (progressBar == null) {
487
            progressBar = new JProgressBar();
488
            // progressBar.setPreferredSize(new java.awt.Dimension(100, 14));
489
            progressBar.setVisible(false);
490
            progressBar.setMinimum(0);
491
            progressBar.setMaximum(100);
492
            progressBar.setValue(50);
493
        }
494

    
495
        return progressBar;
496
    }
497

    
498
    private JTasksStatus getTasksStatus() {
499
        if (tasksStatus == null) {
500
            tasksStatus =
501
                ToolsSwingLocator.getTaskStatusSwingManager()
502
                    .createJTasksStatus();
503
            tasksStatus.setVisible(true);
504
        }
505
        return tasksStatus;
506
    }
507

    
508
    /**
509
     * Sets the width of the main message label.
510
     * 
511
     * @param d
512
     *            The width ob the main label
513
     * @deprecated
514
     */
515
    public void setFixedLabelWidth(double d) {
516
        lblTexto.setPreferredSize(new Dimension((int) d, lblTexto.getHeight()));
517
    }
518

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

    
539
    /**
540
     * Remove a control from the status bar
541
     * 
542
     * @param id
543
     *            The ID of the control to get
544
     */
545
    public Component removeControl(String id) {
546
        try {
547
            Component component = (Component) controls.get(id);
548
            controlContainer.remove(component);
549
            controls.remove(id);
550
            return component;
551
        } catch (ClassCastException ex) {
552
            LOG.debug("NewStatusBar.removeControl -- control " + id
553
                + "doesn't exist");
554
        }
555
        return null;
556
    }
557

    
558
    /**
559
     * Gets a control from the status bar
560
     * 
561
     * @param id
562
     *            The ID of the control to get
563
     */
564
    public Component getControl(String id) {
565
        return (Component) controls.get(id);
566
    }
567
} // @jve:decl-index=0:visual-constraint="10,10"