Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/swing/ResizeManager.java


1   /*
2    * ResizeManager.java
3    *
4    * Created on 16 gennaio 2002, 11.35
5       Javatools (modified version) - Some useful general classes.
6       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
7   
8       This program is free software; you can redistribute it and/or modify
9       it under the terms of the GNU General Public License as published by
10      the Free Software Foundation; either version 2 of the License, or
11      (at your option) any later version.
12  
13      This program is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16      GNU General Public License for more details.
17  
18      You should have received a copy of the GNU General Public License
19      along with this program; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  
22      Contact me at: brenmcguire@users.sourceforge.net
23   */
24  
25  package javatools.swing;
26  
27  import java.awt.*;
28  import javax.swing.JComponent;
29  import java.lang.*;
30  import java.util.LinkedList;
31  /**
32   * It's a class to manage resizing in form in which the <CODE>GridBagLayout</CODE>
33   * is used.
34   * Preferably do not use it, but if you want improve it...
35   * @author Antonio Petrelli
36   * @version 0.0.1
37   */
38  public class ResizeManager {
39  
40      /** Creates new ResizeManager */
41      public ResizeManager() {
42          componentList = new LinkedList();
43          resizeChoiceList = new LinkedList();
44          initialDimensions = new LinkedList();
45      }
46      
47      /** No resizing will be performed.
48       */    
49      public final int NONE       = -1;
50      /** Only horizontal resizing will be performed.
51       */    
52      public final int HORIZONTAL = 0;
53      /** Only vertical resizing will be performed.
54       */    
55      public final int VERTICAL   = 1;
56      /** Both horizontal and vertical resizing will be performed.
57       */    
58      public final int BOTH       = 2;
59  
60      /** Sets the container whose dimension will be checked.
61       * @param cont The referenced container.
62       * @param initialDims Initial dimension of the container.
63       * @param minDims Minimum dimension of the container.
64       */    
65      public void setContainer(java.awt.Container cont, Dimension initialDims, Dimension minDims) {
66          if (cont != null && initialDims != null && minDims != null) {
67              managedContainer = cont;
68              containerInitialDimension = new Dimension(initialDims);
69              containerMinDimension = new Dimension(minDims);
70          }
71      }
72      
73      /** Returns the monitored container.
74       * @return The referenced container.
75       */    
76      public java.awt.Container getContainer() {
77          return managedContainer;
78      }
79      
80      /** Returns initial dimension of the monitored container.
81       * @return The initial dimension of the referenced container.
82       */    
83      public Dimension getContainerInitialDimension() {
84          if (managedContainer != null)
85              return new Dimension(containerInitialDimension);
86          else
87              return null;
88      }
89      
90      /** Returns minimum dimension of the monitored container.
91       * @return The minimum dimension of the referenced container.
92       */    
93      public Dimension getContainerMinDimension() {
94          if (managedContainer != null)
95              return new Dimension(containerMinDimension);
96          else
97              return null;
98      }
99      
100     /** Adds a component to resize on resizing of the monitored container.
101      * @param comp The component to add.
102      * @param dims Initial dimension of the component.
103      * @param resizeOpt Integer value representing how resizing will be performed.
104      */    
105     public void addComponent(javax.swing.JComponent comp, Dimension dims, int resizeOpt) {
106         if (comp != null && dims != null && resizeOpt >= -1 && resizeOpt <=2) {
107             componentList.add(comp);
108             initialDimensions.add(new Dimension(dims));
109             resizeChoiceList.add(new Integer(resizeOpt));
110         }
111     }
112     
113     /** Returns the number of managed components.
114      * @return The number of managed components.
115      */    
116     public int getNumComponents() {
117         return componentList.size();
118     }
119     
120     /** Returns a referenced component.
121      * @param numComponent The position of the needed component.
122      * @throws IndexOutOfBoundsException If <CODE>numComponent</CODE> is not valid.
123      * @return The reference to managed component.
124      */    
125     public javax.swing.JComponent getComponent(int numComponent) throws IndexOutOfBoundsException {
126         if (numComponent >= 0 && numComponent < componentList.size())
127             return (javax.swing.JComponent) componentList.get(numComponent);
128         else
129             throw new IndexOutOfBoundsException("Component number not valid!");
130     }
131     
132     /** Removes a component from the list.
133      * @param numComponent The position of the component.
134      * @throws IndexOutOfBoundsException If <CODE>numComponent</CODE> is not valid.
135      */    
136     public void removeComponent(int numComponent) throws IndexOutOfBoundsException {
137         if (numComponent >= 0 && numComponent < componentList.size()) {
138             componentList.remove(numComponent);
139             initialDimensions.remove(numComponent);
140             resizeChoiceList.remove(numComponent);
141         }
142         else
143             throw new IndexOutOfBoundsException("Component number not valid!");
144     }
145     
146     /** Returns initial dimension of a component.
147      * @param numComponent The position of the component.
148      * @return The initial dimension of the component.
149      * @throws IndexOutOfBoundsException If <CODE>numComponent</CODE> is not valid.
150      */    
151     public Dimension getInitialDimension(int numComponent) throws IndexOutOfBoundsException {
152         if (numComponent >= 0 && numComponent < initialDimensions.size())
153             return new Dimension((Dimension) initialDimensions.get(numComponent));
154         else
155             throw new IndexOutOfBoundsException("Component number not valid!");
156     }
157     
158     /** Returns the resize option for a component.
159      * @param numComponent The position of the needed component.
160      * @return The value representing how resizing will be performed.
161      * @throws IndexOutOfBoundsException If <CODE>numComponent</CODE> is not valid.
162      */    
163     public int getResizeOption(int numComponent) throws IndexOutOfBoundsException {
164         if (numComponent >= 0 && numComponent < resizeChoiceList.size())
165             return ((Integer) resizeChoiceList.get(numComponent)).intValue();
166         else
167             throw new IndexOutOfBoundsException("Component number not valid!");
168     }
169     
170     /** Resizes the form by taking current dimension of the container.
171      */    
172     public void resize() {
173         Dimension containerActualDimension;
174         Dimension actualDims;
175         int diffWidth, diffHeight;
176         int newWidth, newHeight;
177         int i, numComponents;
178         int choice;
179 
180         if (managedContainer != null) {
181             containerActualDimension = managedContainer.getSize();
182             newWidth = containerActualDimension.width;
183             newHeight = containerActualDimension.height;
184             if (newWidth < containerMinDimension.width)
185                 newWidth = containerMinDimension.width;
186             if (newHeight < containerMinDimension.height)
187                 newHeight = containerMinDimension.height;
188             diffWidth = newWidth - containerInitialDimension.width;
189             diffHeight = newHeight - containerInitialDimension.height;
190             numComponents = componentList.size();
191             for (i=0; i<numComponents; i++) {
192                 actualDims = ((JComponent) componentList.get(i)).getSize();
193                 choice = ((Integer) resizeChoiceList.get(i)).intValue();
194                 if (choice == this.HORIZONTAL || choice == this.BOTH)
195                     actualDims.width = ((Dimension) initialDimensions.get(i)).width + diffWidth;
196                 if (choice == this.VERTICAL || choice == this.BOTH)
197                     actualDims.height = ((Dimension) initialDimensions.get(i)).height + diffHeight;
198                 if (choice != this.NONE) {
199                     ((JComponent) componentList.get(i)).setPreferredSize(actualDims);
200                     ((JComponent) componentList.get(i)).setSize(actualDims);
201                 }
202             }
203         }
204     }
205     
206     /** Initializes the monitored object with initials sizes.
207      */    
208     public void initSize() {
209         int i, numComponents;
210         
211         managedContainer.setSize(containerMinDimension);
212         numComponents = componentList.size();
213         for (i=0; i<numComponents; i++) {
214             ((JComponent) componentList.get(i)).setPreferredSize((Dimension) initialDimensions.get(i));
215             ((JComponent) componentList.get(i)).setSize((Dimension) initialDimensions.get(i));
216         }
217     }
218 
219     private java.awt.Container managedContainer;
220     private Dimension containerInitialDimension, containerMinDimension;
221     private LinkedList componentList;
222     private LinkedList initialDimensions;
223     private LinkedList minDimensions;
224     private LinkedList resizeChoiceList;
225 }