Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.api / src / main / java / org / gvsig / tools / swing / api / ToolsSwingUtils.java @ 2514

History | View | Annotate | Download (2.61 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.tools.swing.api;
7

    
8
import java.awt.Dimension;
9
import javax.swing.JComponent;
10
import javax.swing.JLabel;
11

    
12
/**
13
 *
14
 * @author gvSIG Team
15
 */
16
public class ToolsSwingUtils {
17
    
18
    private ToolsSwingUtils() {
19
        
20
    }
21
    
22
    private static double pixelsXrow = -1;
23
    private static double pixelsXcolumn = -1;
24
    
25
    private static void calculateRowsCols2Pixels() {
26
        String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ[]()!?|_/@";
27
        JLabel label = new JLabel(s);
28
        Dimension dim = label.getPreferredSize();
29
        pixelsXrow = dim.height;
30
        pixelsXcolumn = dim.width / s.length();
31
    }
32
    
33
    public static int rows2px(int rows) {
34
        if( pixelsXrow == -1 ) {
35
            calculateRowsCols2Pixels();
36
        }
37
        return (int) (rows * pixelsXrow);
38
    }
39
    
40
    public static int cols2px(int cols) {
41
        if( pixelsXcolumn == -1 ) {
42
            calculateRowsCols2Pixels();
43
        }
44
        return (int) (cols * pixelsXcolumn);
45
    }
46
    
47
    public static Dimension rowscols2dimension(int rows, int cols) {
48
        return new Dimension(cols2px(cols), rows2px(rows));
49
    }
50
    
51
    public static Dimension ensureRowsCols(JComponent comp, int rows, int cols) {
52
        Dimension dim = comp.getPreferredSize();
53
        return ensureRowsCols(dim, rows, cols);
54
    }
55
    
56
    public static Dimension ensureRowsCols(Dimension dim, int rows, int cols) {
57
        int h = rows2px(rows);
58
        if( dim.height < h ) {
59
            dim.height = h;
60
        }
61
        int w = cols2px(cols);
62
        if( dim.width < w ) {
63
            dim.width = w;
64
        }
65
        return dim;
66
    }
67
    public static Dimension ensureMaxRows(JComponent comp, int rows) {
68
        Dimension dim = comp.getPreferredSize();
69
        int h = rows2px(rows);
70
        if( dim.height > h ) {
71
            dim.height = h;
72
        }
73
        return dim;
74
    }
75
    
76
    public static Dimension ensureMaxRows(Dimension dim, int rows) {
77
        int h = rows2px(rows);
78
        if( dim.height > h ) {
79
            dim.height = h;
80
        }
81
        return dim;
82
    }
83
       
84
    public static Dimension ensureRows(Dimension dim, int rows) {
85
        int h = rows2px(rows);
86
        if( dim.height < h ) {
87
            dim.height = h;
88
        }
89
        return dim;
90
    }
91

    
92
    public static Dimension ensureCols(Dimension dim, int cols) {
93
        int w = cols2px(cols);
94
        if( dim.width < w ) {
95
            dim.width = w;
96
        }
97
        return dim;
98
    }
99
}