Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / ui / mdiFrame / NewStatusBar.java @ 37848

History | View | Annotate | Download (17.7 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.Component;
25
import java.awt.Dimension;
26
import java.awt.FlowLayout;
27
import java.awt.LayoutManager;
28
import java.util.HashMap;
29
import java.util.Map;
30

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

    
39
import org.apache.log4j.Logger;
40

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
174
            break;
175

    
176
        case WARNING:
177
            setWarningText(textoAnterior);
178

    
179
            break;
180

    
181
        case ERROR:
182
            setErrorText(textoAnterior);
183

    
184
            break;
185
        }
186

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

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

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

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

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

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

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

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

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

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

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

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

    
354
        for (int i = 0; i < labels.length; i++) {
355
            // Set an initial text so the getPreferredSize works as expected
356
            JLabel lbl = new JLabel();
357
            // lbl.setPreferredSize(new Dimension(labels[i].getSize(), lbl
358
            // .getPreferredSize().height + 2));
359
            // Remove the initial text
360
            // lbl.setText(" ");
361
            lbl.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
362
            lbl.setName(labels[i].getId());
363
            controlContainer.add(lbl);
364

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

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

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

    
381
        this.validate();
382
    }
383

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

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

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

    
406
            if (label.getText().compareTo("") != 0) {
407
                label.setVisible(true);
408
            } else {
409
                label.setVisible(false);
410
            }
411
        }
412
        validate();
413
    }
414

    
415
    /**
416
     * Removes all the labels from the status bar. It does not remove the
417
     * controls.
418
     */
419
    private void removeAllLabels() {
420
        Component[] controlArray = controlContainer.getComponents();
421

    
422
        for (int i = 0; i < controlArray.length; i++) {
423
            if ((controlArray[i] != lblIcon) && (controlArray[i] != lblTexto)
424
                && !(controlArray[i] instanceof IControl)
425
                && (controlArray[i] instanceof JLabel)) {
426
                controlContainer.remove(controlArray[i]);
427
            }
428
        }
429
    }
430

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

    
443
        if (lbl != null) {
444
            lbl.setText(msg);
445
        } else {
446
            // try with controls
447
            try {
448
                IControl control = (IControl) controls.get(id);
449
                if (control != null)
450
                    control.setValue(msg);
451
            } catch (ClassCastException ex) {
452
                LOG.debug("no label called " + id);
453
            }
454
        }
455

    
456
        ajustar();
457
    }
458

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

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

    
491
        return progressBar;
492
    }
493

    
494
    private JTasksStatus getTasksStatus() {
495
        if (tasksStatus == null) {
496
            tasksStatus =
497
                ToolsSwingLocator.getTaskStatusSwingManager()
498
                    .createJTasksStatus();
499
            tasksStatus.setVisible(true);
500
        }
501
        return tasksStatus;
502
    }
503

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

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

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

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