svn-gvsig-desktop / tags / v1_0_2_Build_904 / libraries / libIverUtiles / src / com / iver / utiles / centerComponent / CenterComponent.java @ 10724
History | View | Annotate | Download (3.46 KB)
1 | 7431 | ppiqueras | package com.iver.utiles.centerComponent; |
---|---|---|---|
2 | |||
3 | import java.awt.Component; |
||
4 | import java.awt.Point; |
||
5 | import java.awt.Rectangle; |
||
6 | |||
7 | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
8 | *
|
||
9 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
10 | *
|
||
11 | * This program is free software; you can redistribute it and/or
|
||
12 | * modify it under the terms of the GNU General Public License
|
||
13 | * as published by the Free Software Foundation; either version 2
|
||
14 | * of the License, or (at your option) any later version.
|
||
15 | *
|
||
16 | * This program is distributed in the hope that it will be useful,
|
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
19 | * GNU General Public License for more details.
|
||
20 | *
|
||
21 | * You should have received a copy of the GNU General Public License
|
||
22 | * along with this program; if not, write to the Free Software
|
||
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
24 | *
|
||
25 | * For more information, contact:
|
||
26 | *
|
||
27 | * Generalitat Valenciana
|
||
28 | * Conselleria d'Infraestructures i Transport
|
||
29 | * Av. Blasco Ib??ez, 50
|
||
30 | * 46010 VALENCIA
|
||
31 | * SPAIN
|
||
32 | *
|
||
33 | * +34 963862235
|
||
34 | * gvsig@gva.es
|
||
35 | * www.gvsig.gva.es
|
||
36 | *
|
||
37 | * or
|
||
38 | *
|
||
39 | * IVER T.I. S.A
|
||
40 | * Salamanca 50
|
||
41 | * 46005 Valencia
|
||
42 | * Spain
|
||
43 | *
|
||
44 | * +34 963163400
|
||
45 | * dac@iver.es
|
||
46 | */
|
||
47 | |||
48 | /**
|
||
49 | * Allows center a component (Ex. JInternalFrame, JFrame, etc.) respect its parent component
|
||
50 | *
|
||
51 | * Exceptions:
|
||
52 | * - If the component is a JDialog -> better use:
|
||
53 | * jDialog.setLocationRelativeTo(parentComponent);
|
||
54 | *
|
||
55 | * - If the component is a JFrame or a JWindow -> better use:
|
||
56 | * jFrame.setSize(width, height);
|
||
57 | * jFrame.setLocationRelativeTo(null);
|
||
58 | * or
|
||
59 | * jWindow.setSize(width, height);
|
||
60 | * jWindow.setLocationRelativeTo(null);
|
||
61 | *
|
||
62 | * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
||
63 | */
|
||
64 | public class CenterComponent { |
||
65 | |||
66 | /**
|
||
67 | * Centers a component (Ex. JInternalFrame, JFrame, etc.) according its size and the bounds of its parent
|
||
68 | *
|
||
69 | * Exceptions:
|
||
70 | * - If the component is a JDialog -> better use:
|
||
71 | * jDialog.setLocationRelativeTo(parentComponent);
|
||
72 | *
|
||
73 | * - If the component is a JFrame or a JWindow -> better use:
|
||
74 | * jFrame.setSize(width, height);
|
||
75 | * jFrame.setLocationRelativeTo(null);
|
||
76 | * or
|
||
77 | * jWindow.setSize(width, height);
|
||
78 | * jWindow.setLocationRelativeTo(null);
|
||
79 | *
|
||
80 | * @param component The component to center
|
||
81 | * @param parentBounds Bounds of the parent of the component.
|
||
82 | */
|
||
83 | public static void centerComponent(Component component, Rectangle parentBounds) { |
||
84 | final int DefaultXMargin = 20; |
||
85 | final int DefaultYMargin = 20; |
||
86 | final int MinimumXMargin = 10; |
||
87 | final int MinimumYMargin = 10; |
||
88 | |||
89 | // The top-left square of JComponent reference
|
||
90 | Point newReferencePoint = new Point(); |
||
91 | newReferencePoint.x = (parentBounds.x + (parentBounds.width - component.getWidth()) / 2);
|
||
92 | newReferencePoint.y = (parentBounds.y + (parentBounds.height - component.getHeight()) / 2);
|
||
93 | |||
94 | // Calculate the new point reference (Assure that the top-left corner is showed)
|
||
95 | if (newReferencePoint.x < 0) |
||
96 | { |
||
97 | if (newReferencePoint.x > MinimumXMargin)
|
||
98 | newReferencePoint.x = DefaultXMargin; |
||
99 | else
|
||
100 | newReferencePoint.x = 0;
|
||
101 | } |
||
102 | |||
103 | |||
104 | if (newReferencePoint.y < 0) |
||
105 | { |
||
106 | if (newReferencePoint.y > MinimumYMargin)
|
||
107 | newReferencePoint.y = DefaultYMargin; |
||
108 | else
|
||
109 | newReferencePoint.y = 0;
|
||
110 | } |
||
111 | |||
112 | // Set the new location for this JComponent object
|
||
113 | component.setLocation(newReferencePoint); |
||
114 | } |
||
115 | } |