468f83213196971672db3ec085d22acdfbc89e33
[privoxy.git] / src / java / org / privoxy / activityconsole / ActivityConsoleGuiUtil.java
1 /*********************************************************************
2  *
3  * File        :  $Source$
4  *
5  * Purpose     :  Utility functions for GridBag layout and centering
6  *                frames.
7  *
8  * Copyright   :  Written by and Copyright (C) 2003 the SourceForge
9  *                Privoxy team. http://www.privoxy.org/
10  *
11  *                Based on the Internet Junkbuster originally written
12  *                by and Copyright (C) 1997 Anonymous Coders and
13  *                Junkbusters Corporation.  http://www.junkbusters.com
14  *
15  *                This program is free software; you can redistribute it
16  *                and/or modify it under the terms of the GNU General
17  *                Public License as published by the Free Software
18  *                Foundation; either version 2 of the License, or (at
19  *                your option) any later version.
20  *
21  *                This program is distributed in the hope that it will
22  *                be useful, but WITHOUT ANY WARRANTY; without even the
23  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
24  *                PARTICULAR PURPOSE.  See the GNU General Public
25  *                License for more details.
26  *
27  *                The GNU General Public License should be included with
28  *                this file.  If not, you can view it at
29  *                http://www.gnu.org/copyleft/gpl.html
30  *                or write to the Free Software Foundation, Inc., 59
31  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  *
33  * Revisions   :
34  *    $Log$
35  *********************************************************************/
36
37 package org.privoxy.activityconsole;
38
39 import java.awt.*;
40
41 /**
42  * The ActivityConsoleGuiUtil class: Helper routines for the GridBag layout.
43  * @author Last Modified By: $Author$
44  * @version $Rev$-$Date$$State$
45  */
46 public class ActivityConsoleGuiUtil 
47 {
48   private static final String
49     COPYRIGHT = org.privoxy.activityconsole.Copyright.COPYRIGHT;
50
51   /** constrain - Helper method for setting a componets constraints in a gridbag layout;
52     * takes all of the possible parameters for grid constraints.
53     * @param container  conatiner to add the component to
54     * @param component  component that will be added
55     * @param grid_x x value
56     * @param grid_y y value
57     * @param grid_width grid width for the component
58     * @param grid_height grid height for the component
59     * @param fill fill value
60     * @param anchor anchor value
61     * @param weight_x weight x
62     * @param weight_y weight y
63     * @param top top inset
64     * @param left left inset
65     * @param bottom bottom inset
66     * @param right right inset
67     */
68
69   public static void constrain(Container container,
70                                Component component, 
71                                int grid_x,
72                                int grid_y,
73                                int grid_width,
74                                int grid_height,
75                                int fill,
76                                int anchor,
77                                double weight_x,
78                                double weight_y,
79                                int top,
80                                int left,
81                                int bottom,
82                                int right)
83   {
84     GridBagConstraints c = new GridBagConstraints();
85     c.gridx = grid_x;
86     c.gridy = grid_y;
87     c.gridwidth = grid_width;
88     c.gridheight = grid_height;
89     c.fill = fill;
90     c.anchor = anchor;
91     c.weightx = weight_x;
92     c.weighty = weight_y;
93     if (top+bottom+left+right > 0)
94       c.insets = new Insets(top, left, bottom, right);
95
96     ((GridBagLayout)container.getLayout()).setConstraints(component, c);
97     container.add(component);
98   }
99
100   public static void constrain(Container container,
101                                Component component, 
102                                int grid_x,
103                                int grid_y,
104                                int grid_width,
105                                int grid_height)
106   {
107     constrain(container,
108               component,
109               grid_x,
110               grid_y,
111               grid_width,
112               grid_height,
113               GridBagConstraints.NONE, 
114               GridBagConstraints.NORTHWEST,
115               0.0, 0.0, 0, 0, 0, 0);
116   }
117
118   public static void constrain(Container container,
119                                Component component, 
120                                int grid_x,
121                                int grid_y,
122                                int grid_width,
123                                int grid_height,
124                                int top,
125                                int left,
126                                int bottom,
127                                int right)
128   {
129     constrain(container,
130               component,
131               grid_x,
132               grid_y, 
133               grid_width,
134               grid_height,
135               GridBagConstraints.NONE, 
136               GridBagConstraints.NORTHWEST, 
137               0.0, 0.0, top, left, bottom, right);
138   }
139
140   /** constrainLast - Helper method for setting a componets constraints in a gridbag layout;
141     * takes all of the possible parameters for grid constraints.
142     * @param container  conatiner to add the component to
143     * @param component  component that will be added
144     * @param grid_x x value
145     * @param grid_y y value
146     * @param top top inset
147     * @param left left inset
148     * @param bottom bottom inset
149     * @param right right inset
150     */
151
152   public static void constrainLast(Container container,
153                                    Component component,
154                                    int grid_x,
155                                    int grid_y,
156                                    int top,
157                                    int left,
158                                    int bottom,
159                                    int right)
160   {
161     constrain(container,
162               component,
163               grid_x,
164               grid_y, 
165               GridBagConstraints.REMAINDER,1,
166               GridBagConstraints.HORIZONTAL, 
167               GridBagConstraints.NORTHWEST, 
168               0.0, 0.0, top, left, bottom, right);
169   }
170
171   /** constrain - Helper method for setting a componets constraints in a gridbag layout;
172     * takes all of the possible parameters for grid constraints.
173     * @param container  conatiner to add the component to
174     * @param component  component that will be added
175     * @param int        x value
176     * @param int        y value
177     * @param int        top inset
178     * @param int        left inset
179     * @param int        bottom inset
180     * @param int        right inset
181     */
182
183   public static void constrain(Container container, Component component, 
184                                int grid_x, int grid_y,int top, int left, int bottom, int right)
185   {
186     constrain(container,
187               component,
188               grid_x,
189               grid_y, 
190               1, 1,
191               GridBagConstraints.NONE, 
192               GridBagConstraints.NORTHWEST, 
193               0.0, 0.0, top, left, bottom, right);
194   }
195
196   public static Rectangle center(Dimension dim)
197   {
198     final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
199
200     final Rectangle centeredRect =
201     new Rectangle( (screenSize.width  - dim.width)  /2,
202                    (screenSize.height - dim.height) /2,
203                    dim.width,
204                    dim.height);
205     return centeredRect;
206   }
207 }