File: Utility.java

package info (click to toggle)
geotranz 3.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 242,380 kB
  • sloc: cpp: 37,618; java: 13,594; javascript: 3,068; makefile: 955; xml: 67; sh: 38; csh: 21
file content (110 lines) | stat: -rwxr-xr-x 3,314 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// CLASSIFICATION: UNCLASSIFIED
package mspccs_spreadsheet_tester.utility;



public class Utility
{
    /** 
     * Center a component over its parent.
     */
    public static void center(java.awt.Container parent, java.awt.Component comp) 
    {
        int x, y; 
        java.awt.Rectangle boundingRect; 
        java.awt.Dimension compSize = comp.getSize();
        
        // If Container is null then our bounding rectangle is the whole screen 
        if (parent == null)  
        { 
            boundingRect = new java.awt.Rectangle(comp.getToolkit().getScreenSize()); 
            boundingRect.setLocation(0,0); 
        } 
        // Else bounding rectangle is the Container 
        else 
            boundingRect = parent.getBounds(); 
        
        // Place the component so its center is the same 
        // as the center of the bounding rectangle 
        x = boundingRect.x + ((boundingRect.width/2) - (compSize.width/2)); 
        y = boundingRect.y + ((boundingRect.height/2) - (compSize.height/2)); 
        
        comp.setLocation(x, y);
    } 

    /** 
     * Change title bar icon.
     */
    public static void setIcon(javax.swing.JFrame jFrame, String icon)
    {
        java.net.URL url = jFrame.getClass().getResource(icon);
        try
        {
            java.awt.Image img = jFrame.createImage((java.awt.image.ImageProducer)url.getContent());
            jFrame.setIconImage(img);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    /** 
     * Extract a code from a string of the form code: name.
     */
    public static String parseString(String str)
    {
        String result = "";
        java.util.StringTokenizer strTokenizer = new java.util.StringTokenizer(str, ":");
        if (strTokenizer.hasMoreTokens())
            result = strTokenizer.nextToken();

        return result;
    }

    /** 
     * Return the text field value as a double.
     */
    public static double getDoubleTextField(javax.swing.JTextField textField)
    {
        return Double.parseDouble(textField.getText().trim());
    }
    
    /** 
     * Convert a string to double.
     */
    public static double toDouble(String str)
    {
        return Double.parseDouble(str);
    }
    
    /** 
     * Set a text field with the given value using a precision of 10.
     */
    public static void setTextField(javax.swing.JTextField textField, double value)
    {
        int precision = 10;

        java.text.NumberFormat nf = java.text.NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(precision);
        nf.setMinimumFractionDigits(precision);
        nf.setGroupingUsed(false);

        textField.setText(nf.format(value));
        textField.setCaretPosition(0);
    }

    /** 
     * Set a text field with the given value using the given precision.
     */
    public static void setTextField(javax.swing.JTextField textField, double value, int precision)
    {
        java.text.NumberFormat nf = java.text.NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(precision);
        nf.setMinimumFractionDigits(precision);
        nf.setGroupingUsed(false);

        textField.setText(nf.format(value));
        textField.setCaretPosition(0);
    }
}// CLASSIFICATION: UNCLASSIFIED