Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / MessageBarControllerImpl.java @ 2206

History | View | Annotate | Download (2.44 KB)

1
package org.gvsig.tools.swing.impl;
2

    
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5
import javax.swing.ImageIcon;
6
import javax.swing.JLabel;
7
import javax.swing.JOptionPane;
8
import javax.swing.SwingUtilities;
9
import javax.swing.Timer;
10
import javax.swing.text.JTextComponent;
11
import org.apache.commons.lang3.StringUtils;
12
import org.gvsig.tools.swing.api.MessageBarController;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15

    
16
/**
17
 *
18
 * @author omartinez
19
 */
20
public class MessageBarControllerImpl implements MessageBarController {
21

    
22
    private JLabel label;
23
    private int time;
24
    private final Timer timer;
25
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageBarControllerImpl.class);
26

    
27
    public MessageBarControllerImpl(JLabel label, int time) {
28
        this.label = label;
29
        this.time = time;
30
        this.timer = this.buildTimer();
31
    }
32
    
33
    private Timer buildTimer() {
34
        Timer newTimer;
35
        newTimer = new Timer(this.time, new ActionListener() {
36
            public void actionPerformed(ActionEvent e) {
37
                try {
38
                    clear();
39
                } catch (Throwable ex) {
40
                    LOGGER.info("Can't clear message", ex);
41
                }
42
            }
43
        });
44
        return newTimer;
45
    }
46

    
47
    private String toHTML(String s) {
48
        s = StringUtils.replace(s, "\n", "\n<br>");
49
        s = StringUtils.replace(s, "<html>", "");
50
        s = StringUtils.replace(s, "</html>", "");
51
        s = "<html>" + s + "</html>";
52
        return s;
53
    }
54

    
55
    @Override
56
    public void setTime(int time) {
57
        this.time = time;
58
        this.timer.setDelay(this.time);
59
    }
60

    
61
    @Override
62
    public void setText(String msg, int messageType) {
63

    
64
        if (!SwingUtilities.isEventDispatchThread()) {
65
            SwingUtilities.invokeLater(new Runnable() {
66
                public void run() {
67
                    setText(msg, messageType);
68
                }
69
            });
70
            return;
71
        }
72
        timer.stop();
73
        if (StringUtils.isBlank(msg)) {
74
            this.clear();
75
            return;
76
        }
77
        label.setText(toHTML(msg));
78
        label.paintImmediately(label.getVisibleRect());
79
        label.paintImmediately(label.getVisibleRect());
80
        timer.start();
81
    }
82

    
83
    @Override
84
    public void setText(String text) {
85
        this.setText(text, -1);
86
        
87
    }
88

    
89
    @Override
90
    public void clear() {
91
        this.label.setText("");
92
    }
93
    
94
}