KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > upgrade > UIUtil


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
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 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.upgrade;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.GraphicsConfiguration JavaDoc;
25 import java.awt.GraphicsDevice JavaDoc;
26 import java.awt.GridBagConstraints JavaDoc;
27 import java.awt.GridBagLayout JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.Toolkit JavaDoc;
30
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.SwingConstants JavaDoc;
33
34 /**
35  * Useful UI utilies.
36  *
37  * @author $Author: lee $
38  */

39 public class UIUtil implements SwingConstants JavaDoc {
40
41     
42
43     /**
44      * Add a component to a container that is using a <code>GridBagLayout</code>,
45      * together with its constraints and the
46      * <code>GridBagConstraints.gridwidth</code> value.
47      *
48      * @param parent parent container
49      * @param componentToAdd component to add
50      * @param constraints contraints
51      * @param pos grid width position
52      *
53      * @throws IllegalArgumentException
54      */

55     public static void jGridBagAdd(JComponent JavaDoc parent, Component JavaDoc componentToAdd, GridBagConstraints JavaDoc constraints, int pos) {
56         if (!(parent.getLayout() instanceof GridBagLayout JavaDoc)) {
57             throw new IllegalArgumentException JavaDoc("parent must have a GridBagLayout");
58         }
59
60         //
61
GridBagLayout JavaDoc layout = (GridBagLayout JavaDoc) parent.getLayout();
62
63         //
64
constraints.gridwidth = pos;
65         layout.setConstraints(componentToAdd, constraints);
66         parent.add(componentToAdd);
67     }
68
69     /**
70      * Position a component on the screen (must be a
71      * <code>java.awt.Window</code> to be useful)
72      *
73      * @param p postion from <code>SwingConstants</code>
74      * @param c component
75      */

76     public static void positionComponent(int p, Component JavaDoc c) {
77
78         positionComponent(p, c, c);
79
80     }
81
82     public static void positionComponent(int p, Component JavaDoc c, Component JavaDoc o) {
83         Rectangle JavaDoc d = null;
84         /*
85          * TODO This is very lame doesnt require the component to position
86          * around, just assuming its a window.
87          */

88         try {
89
90             // #ifdef JAVA1
91
/*
92              * throw new Exception();
93              */

94
95             // #else
96
GraphicsConfiguration JavaDoc config = o.getGraphicsConfiguration();
97             GraphicsDevice JavaDoc dev = config.getDevice();
98             d = config.getBounds();
99
100             // #endif JAVA1
101
} catch (Throwable JavaDoc t) {
102         }
103         positionComponent(p, c, d);
104         
105     }
106
107     public static void positionComponent(int p, Component JavaDoc c, Rectangle JavaDoc d) {
108         if (d == null) {
109             Dimension JavaDoc s = Toolkit.getDefaultToolkit().getScreenSize();
110             d = new Rectangle JavaDoc(0, 0, s != null ? s.width : 800, s != null ? s.height : 600);
111             System.out.println("Could not get metrics from graphics config, using default " + d);
112         }
113
114         switch (p) {
115             case NORTH_WEST:
116                 c.setLocation(d.x, d.y);
117                 break;
118             case NORTH:
119                 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y);
120                 break;
121             case NORTH_EAST:
122                 c.setLocation(d.x + (d.width - c.getSize().width), d.y);
123                 break;
124             case WEST:
125                 c.setLocation(d.x, d.y + (d.height - c.getSize().height) / 2);
126                 break;
127             case SOUTH_WEST:
128                 c.setLocation(d.x, d.y + (d.height - c.getSize().height));
129                 break;
130             case EAST:
131                 c.setLocation(d.x + d.width - c.getSize().width, d.y + (d.height - c.getSize().height) / 2);
132                 break;
133             case SOUTH_EAST:
134                 c.setLocation(d.x + (d.width - c.getSize().width), d.y + (d.height - c.getSize().height) - 30);
135                 break;
136             case CENTER:
137                 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y + (d.height - c.getSize().height) / 2);
138                 break;
139         }
140     }
141 }
142
Popular Tags