Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / centerComponent / CenterComponent.java @ 40561

History | View | Annotate | Download (4.51 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils.centerComponent;
25

    
26
import java.awt.Component;
27
import java.awt.Point;
28
import java.awt.Rectangle;
29

    
30
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
31
 *
32
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
33
 *
34
 * This program is free software; you can redistribute it and/or
35
 * modify it under the terms of the GNU General Public License
36
 * as published by the Free Software Foundation; either version 2
37
 * of the License, or (at your option) any later version.
38
 *
39
 * This program is distributed in the hope that it will be useful,
40
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
 * GNU General Public License for more details.
43
 *
44
 * You should have received a copy of the GNU General Public License
45
 * along with this program; if not, write to the Free Software
46
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
47
 *
48
 * For more information, contact:
49
 *
50
 *  Generalitat Valenciana
51
 *   Conselleria d'Infraestructures i Transport
52
 *   Av. Blasco Ib??ez, 50
53
 *   46010 VALENCIA
54
 *   SPAIN
55
 *
56
 *      +34 963862235
57
 *   gvsig@gva.es
58
 *      www.gvsig.gva.es
59
 *
60
 *    or
61
 *
62
 *   IVER T.I. S.A
63
 *   Salamanca 50
64
 *   46005 Valencia
65
 *   Spain
66
 *
67
 *   +34 963163400
68
 *   dac@iver.es
69
 */
70

    
71
/**
72
 * Allows center a component (Ex. JInternalFrame, JFrame, etc.) respect its parent component
73
 * 
74
 * Exceptions:<br>
75
 * <ul>
76
 *  <li>If the component is a JDialog -> better use:<br>
77
 *      jDialog.setLocationRelativeTo(parentComponent);</li>
78
 *  <li>If the component is a JFrame or a JWindow -> better use:<br>
79
 *      jFrame.setSize(width, height);<br>
80
 *      jFrame.setLocationRelativeTo(null);<br>
81
 *    or<br>
82
 *      jWindow.setSize(width, height);<br>
83
 *      jWindow.setLocationRelativeTo(null);</li> 
84
 * </ul>
85
 * 
86
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
87
 */
88
public class CenterComponent {
89

    
90
        /**
91
         * Centers a component (Ex. JInternalFrame, JFrame, etc.) according its size and the bounds of its parent<br>
92
         * 
93
         * Exceptions:<br>
94
         * <ul>
95
         *  <li>If the component is a JDialog -> better use:<br>
96
         *      jDialog.setLocationRelativeTo(parentComponent);</li>
97
         *  <li>If the component is a JFrame or a JWindow -> better use:<br>
98
         *      jFrame.setSize(width, height);<br>
99
         *      jFrame.setLocationRelativeTo(null);<br>
100
         *    or<br>
101
         *      jWindow.setSize(width, height);<br>
102
         *      jWindow.setLocationRelativeTo(null);</li>
103
         * </ul>
104
         *
105
           * @param component The component to center 
106
         * @param parentBounds Bounds of the parent of the component.
107
         */
108
        public static void centerComponent(Component component, Rectangle parentBounds) {
109
                final int DefaultXMargin = 20;
110
                final int DefaultYMargin = 20;
111
                final int MinimumXMargin = 10;
112
                final int MinimumYMargin = 10;
113
                
114
                // The top-left square of JComponent reference
115
                Point newReferencePoint = new Point();
116
                newReferencePoint.x = (parentBounds.x + (parentBounds.width - component.getWidth()) / 2);
117
                newReferencePoint.y = (parentBounds.y + (parentBounds.height - component.getHeight()) / 2);
118
                
119
                // Calculate the new point reference (Assure that the top-left corner is showed)
120
                if (newReferencePoint.x < 0)
121
                {
122
                        if (newReferencePoint.x > MinimumXMargin)
123
                                newReferencePoint.x = DefaultXMargin;
124
                        else
125
                                newReferencePoint.x = 0;
126
                }
127
                        
128
                        
129
                if (newReferencePoint.y < 0)
130
                {
131
                        if (newReferencePoint.y > MinimumYMargin)
132
                                newReferencePoint.y = DefaultYMargin;
133
                        else
134
                                newReferencePoint.y = 0;
135
                }                        
136

    
137
                // Set the new location for this JComponent object
138
                component.setLocation(newReferencePoint);
139
        }        
140
}