Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.image.extension / src / main / java / org / gvsig / arcims / image / gui / panels / utils / MultilineToolTipUI.java @ 32321

History | View | Annotate | Download (4.11 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

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 Prodevelop S.L. main development
26
 * http://www.prodevelop.es
27
 */
28

    
29
package org.gvsig.arcims.image.gui.panels.utils;
30

    
31
import java.awt.Dimension;
32
import java.awt.Font;
33
import java.awt.Graphics;
34
import java.awt.Insets;
35

    
36
import javax.swing.CellRendererPane;
37
import javax.swing.JComponent;
38
import javax.swing.JTextArea;
39
import javax.swing.JToolTip;
40
import javax.swing.plaf.ComponentUI;
41
import javax.swing.plaf.basic.BasicToolTipUI;
42

    
43

    
44
/**
45
 * This class allows nice multiline tool tips.
46
 *
47
 * @author Zafir Anjum
48
 * @author jldominguez
49
 */
50
public class MultilineToolTipUI extends BasicToolTipUI {
51
    static MultilineToolTipUI sharedInstance = new MultilineToolTipUI();
52

    
53
    // Dialog, SansSerif - similar to Arial
54
    // DialogInput, Monospaced - similar to Courier
55
    // Serif - similar to Times New Roman 
56
    static JToolTip tip;
57
    private static JTextArea textArea;
58
    Font smallFont = new Font("Dialog", Font.PLAIN, 12);
59
    protected CellRendererPane rendererPane;
60

    
61
    public MultilineToolTipUI() {
62
        super();
63
    }
64

    
65
    public static ComponentUI createUI(JComponent c) {
66
        return sharedInstance;
67
    }
68

    
69
    public void installUI(JComponent c) {
70
        super.installUI(c);
71
        tip = (JToolTip) c;
72
        rendererPane = new CellRendererPane();
73
        c.add(rendererPane);
74
    }
75

    
76
    public void uninstallUI(JComponent c) {
77
        super.uninstallUI(c);
78

    
79
        c.remove(rendererPane);
80
        rendererPane = null;
81
    }
82

    
83
    public void paint(Graphics g, JComponent c) {
84
        Dimension size = c.getSize();
85
        rendererPane.paintComponent(g, textArea, c, 1, 1, size.width - 1,
86
            size.height - 1, true);
87
    }
88

    
89
    /**
90
     * Overrides a method that is called by the system and,
91
     * at the same time, creates a nice textarea and displays the tool tip in it.
92
     */
93
    public Dimension getPreferredSize(JComponent c) {
94
        String tipText = ((JToolTip) c).getTipText();
95

    
96
        if (tipText == null) {
97
            return new Dimension(0, 0);
98
        }
99

    
100
        textArea = new JTextArea(tipText);
101
        textArea.setMargin(new Insets(10, 10, 10, 10));
102
        textArea.setBackground(tip.getBackground());
103
        rendererPane.removeAll();
104
        rendererPane.add(textArea);
105
        textArea.setWrapStyleWord(true);
106
        textArea.setFont(smallFont);
107

    
108
        int width = ((JMultilineToolTip) c).getFixedWidth();
109
        int columns = ((JMultilineToolTip) c).getColumns();
110

    
111
        if (columns > 0) {
112
            textArea.setColumns(columns);
113
            textArea.setSize(0, 0);
114
            textArea.setLineWrap(true);
115
            textArea.setSize(textArea.getPreferredSize());
116
        }
117
        else if (width > 0) {
118
            textArea.setLineWrap(true);
119

    
120
            Dimension d = textArea.getPreferredSize();
121
            d.width = width;
122
            d.height++;
123
            textArea.setSize(d);
124
        }
125
        else
126
        {
127
            textArea.setLineWrap(false);
128
        }
129

    
130
        Dimension dim = textArea.getPreferredSize();
131

    
132
        dim.height += 1;
133
        dim.width += 1;
134

    
135
        return dim;
136
    }
137

    
138
    public Dimension getMinimumSize(JComponent c) {
139
        return getPreferredSize(c);
140
    }
141

    
142
    public Dimension getMaximumSize(JComponent c) {
143
        return getPreferredSize(c);
144
    }
145
}