Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extArcims / src / es / prodevelop / cit / gvsig / arcims / gui / panels / utils / MultilineToolTipUI.java @ 8110

History | View | Annotate | Download (2.69 KB)

1
package es.prodevelop.cit.gvsig.arcims.gui.panels.utils;
2

    
3
import java.awt.Dimension;
4
import java.awt.Font;
5
import java.awt.Graphics;
6
import java.awt.Insets;
7

    
8
import javax.swing.CellRendererPane;
9
import javax.swing.JComponent;
10
import javax.swing.JTextArea;
11
import javax.swing.JToolTip;
12
import javax.swing.plaf.ComponentUI;
13
import javax.swing.plaf.basic.*;
14

    
15
/**
16
 * This class allows nice multiline tool tips.
17
 * 
18
 * @author Zafir Anjum
19
 * @author jldominguez
20
 */
21
public class MultilineToolTipUI extends BasicToolTipUI {
22
        static MultilineToolTipUI sharedInstance = new MultilineToolTipUI();
23

    
24
        Font smallFont = new Font("Dialog", Font.PLAIN, 12);
25
        // Dialog, SansSerif - similar to Arial
26
        // DialogInput, Monospaced - similar to Courier
27
        // Serif - similar to Times New Roman 
28

    
29
        static JToolTip tip;
30
        protected CellRendererPane rendererPane;
31
        private static JTextArea textArea;
32

    
33
        public static ComponentUI createUI(JComponent c) {
34
                return sharedInstance;
35
        }
36

    
37
        public MultilineToolTipUI() {
38
                super();
39
        }
40

    
41
        public void installUI(JComponent c) {
42
                super.installUI(c);
43
                tip = (JToolTip) c;
44
                rendererPane = new CellRendererPane();
45
                c.add(rendererPane);
46
        }
47

    
48
        public void uninstallUI(JComponent c) {
49
                super.uninstallUI(c);
50

    
51
                c.remove(rendererPane);
52
                rendererPane = null;
53
        }
54

    
55
        public void paint(Graphics g, JComponent c) {
56
                Dimension size = c.getSize();
57
                rendererPane.paintComponent(g, textArea, c, 1, 1, size.width - 1,
58
                                size.height - 1, true);
59
        }
60

    
61
        /**
62
         * Overrides a method that is called by the system and,
63
         * at the same time, creates a nice textarea and displays the tool tip in it.
64
         */
65
        public Dimension getPreferredSize(JComponent c) {
66
                String tipText = ((JToolTip) c).getTipText();
67
                if (tipText == null)
68
                        return new Dimension(0, 0);
69
                textArea = new JTextArea(tipText);
70
                textArea.setMargin(new Insets(10, 10, 10, 10));
71
                textArea.setBackground(tip.getBackground());
72
                rendererPane.removeAll();
73
                rendererPane.add(textArea);
74
                textArea.setWrapStyleWord(true);
75
                textArea.setFont(smallFont);
76
                int width = ((JMultilineToolTip) c).getFixedWidth();
77
                int columns = ((JMultilineToolTip) c).getColumns();
78

    
79
                if (columns > 0) {
80
                        textArea.setColumns(columns);
81
                        textArea.setSize(0, 0);
82
                        textArea.setLineWrap(true);
83
                        textArea.setSize(textArea.getPreferredSize());
84
                } else if (width > 0) {
85
                        textArea.setLineWrap(true);
86
                        Dimension d = textArea.getPreferredSize();
87
                        d.width = width;
88
                        d.height++;
89
                        textArea.setSize(d);
90
                } else
91
                        textArea.setLineWrap(false);
92

    
93
                Dimension dim = textArea.getPreferredSize();
94

    
95
                dim.height += 1; 
96
                dim.width += 1; 
97
                return dim;
98
        }
99

    
100
        public Dimension getMinimumSize(JComponent c) {
101
                return getPreferredSize(c);
102
        }
103

    
104
        public Dimension getMaximumSize(JComponent c) {
105
                return getPreferredSize(c);
106
        }
107
}