KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > toolbars > ToolbarLayout


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.view.ui.toolbars;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Container JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Frame JavaDoc;
26 import java.awt.Insets JavaDoc;
27 import java.awt.LayoutManager2 JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.openide.awt.ToolbarPool;
37 import org.openide.windows.WindowManager;
38
39
40 /**
41  * ToolbarLayout is a LayoutManager2 that should be used on a toolbar panel to
42  * allow placement of components in absolute positions.
43  *
44  * This is the place where components are setted it's bounds by actual
45  * toolbar configuration.
46  *
47  * @author Libor Kramolis
48  */

49 public class ToolbarLayout implements LayoutManager2 JavaDoc, java.io.Serializable JavaDoc {
50     /** Toolbar horizontal gap. */
51     public static final int HGAP = 1;
52     /** Toolbar vertical gap. */
53     public static final int VGAP = 1;
54
55     static final long serialVersionUID =7489472539255790677L;
56     
57     /** ToolbarConfiguration cached for getting preferred toolbar configuration width. */
58     ToolbarConfiguration toolbarConfig;
59     /** Map of components. */
60     HashMap JavaDoc<Component JavaDoc,Object JavaDoc> componentMap;
61
62     /**
63      * Creates a new ToolbarLayout.
64      */

65     public ToolbarLayout (ToolbarConfiguration conf) {
66         toolbarConfig = conf;
67         componentMap = new HashMap JavaDoc<Component JavaDoc, Object JavaDoc>();
68     }
69
70     /** Adds the specified component with the specified name to
71      * the layout. Everytime throws IllegalArgumentException.
72      * @param name the component name
73      * @param comp the component to be added
74      */

75     public void addLayoutComponent (String JavaDoc name, Component JavaDoc comp) {
76         throw new IllegalArgumentException JavaDoc();
77     }
78
79     /**
80      * Adds the specified component to the layout, using the specified
81      * constraint object.
82      * @param comp the component to be added
83      * @param constraints the where/how the component is added to the layout.
84      * @exception <code>ClassCastException</code> if the argument is not a
85      * <code>ToolbarConstraints</code>.
86      */

87     public void addLayoutComponent (Component JavaDoc comp, Object JavaDoc constr) {
88         if (!(constr instanceof ToolbarConstraints))
89             throw new IllegalArgumentException JavaDoc (ToolbarConfiguration.getBundleString("EXC_wrongConstraints"));
90
91         componentMap.put (comp, constr);
92         ToolbarConstraints tc = (ToolbarConstraints)constr;
93         tc.setPreferredSize (comp.getPreferredSize());
94         comp.setVisible (tc.isVisible());
95     }
96
97     /**
98      * Removes the specified component from this layout.
99      * @param comp the component to be removed
100      */

101     public void removeLayoutComponent (Component JavaDoc comp) {
102         componentMap.remove (comp);
103     }
104
105     /**
106      * Calculates the preferred dimension for the specified panel given the
107      * components in the specified parent container.
108      * @param parent the component to be laid out
109      *
110      * @see #minimumLayoutSize
111      */

112     public Dimension JavaDoc preferredLayoutSize (Container JavaDoc parent) {
113         Insets JavaDoc insets = parent.getInsets();
114         Dimension JavaDoc prefSize = new Dimension JavaDoc (insets.left + toolbarConfig.getPrefWidth() + insets.right,
115                                             insets.top + toolbarConfig.getPrefHeight() + insets.bottom);
116         return prefSize;
117     }
118
119     /**
120      * Calculates the minimum dimension for the specified
121      * panel given the components in the specified parent container.
122      * @param parent the component to be laid out
123      * @see #preferredLayoutSize
124      */

125     public Dimension JavaDoc minimumLayoutSize (Container JavaDoc parent) {
126         return preferredLayoutSize (parent);
127     }
128
129     /**
130      * Returns the maximum size of this component.
131      * @see java.awt.Component#getMinimumSize()
132      * @see java.awt.Component#getPreferredSize()
133      * @see LayoutManager
134      */

135     public Dimension JavaDoc maximumLayoutSize (Container JavaDoc parent) {
136         return new Dimension JavaDoc (Integer.MAX_VALUE, Integer.MAX_VALUE);
137     }
138
139     /**
140      * Returns the alignment along the x axis. This specifies how the
141      * component would like to be aligned relative to other components. The
142      * value should be a number between 0 and 1 where 0 represents alignment
143      * along the origin, 1 is aligned the furthest away from the origin, 0.5
144      * is centered, etc.
145      */

146     public float getLayoutAlignmentX (Container JavaDoc parent) {
147         return 0;
148     }
149
150     /**
151      * Returns the alignment along the y axis. This specifies how the
152      * component would like to be aligned relative to other components. The
153      * value should be a number between 0 and 1 where 0 represents alignment
154      * along the origin, 1 is aligned the furthest away from the origin, 0.5
155      * is centered, etc.
156      */

157     public float getLayoutAlignmentY (Container JavaDoc parent) {
158         return 0;
159     }
160
161     /**
162      * Invalidates the layout, indicating that if the layout manager has
163      * cached information it should be discarded.
164      */

165     public void invalidateLayout (Container JavaDoc parent) {
166     }
167
168     /**
169      * Lays out the container in the specified panel.
170      * @param parent the component which needs to be laid out
171      */

172     public void layoutContainer (Container JavaDoc parent) {
173         synchronized (parent.getTreeLock()) {
174             //Insets insets = parent.getInsets();
175
//int maxPosition = parent.getWidth() - (insets.left + insets.right) - HGAP;
176
Insets JavaDoc insets = WindowManager.getDefault().getMainWindow().getInsets();
177             int maxPosition;
178             Frame JavaDoc f = WindowManager.getDefault().getMainWindow();
179             if ((f.getExtendedState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH) {
180                 maxPosition = f.getWidth() - HGAP;
181             } else {
182                 maxPosition = f.getWidth() - (insets.left + insets.right) - HGAP;
183             }
184             Component JavaDoc comp;
185             ToolbarConstraints constr;
186
187         /* It is very important to update preferred sizes for each component
188            because when any component change it's size it can affect to other
189            components' size and position. */

190             Iterator JavaDoc it = componentMap.keySet().iterator();
191             while (it.hasNext()) {
192                 comp = (Component JavaDoc)it.next();
193                 constr = (ToolbarConstraints)componentMap.get (comp);
194                 constr.updatePreferredSize (comp.getPreferredSize());
195             }
196
197         /* Setting components' bounds. */
198             HashSet JavaDoc<ToolbarConstraints> completed = new HashSet JavaDoc<ToolbarConstraints>(componentMap.size()*2);
199             for (int i = 0; i < toolbarConfig.getRowCount(); i++) {
200                 ToolbarConstraints overflownTC = processRow(toolbarConfig.getRow(i), completed, maxPosition);
201                 // add row members to completed
202
for (Iterator JavaDoc<ToolbarConstraints> iter = toolbarConfig.getRow(i).iterator(); it.hasNext(); ) {
203                     completed.add(iter.next());
204                 }
205             }
206             parent.repaint();
207         }
208     }
209      
210     private ToolbarConstraints processRow (ToolbarRow row, Collection JavaDoc completed, int maxPosition) {
211         Rectangle JavaDoc bounds;
212         Component JavaDoc comp;
213         ToolbarConstraints constr;
214         List JavaDoc<ToolbarConstraints> moveDownCandidates = new ArrayList JavaDoc<ToolbarConstraints>(5);
215         for (Iterator JavaDoc it = row.iterator(); it.hasNext(); ) {
216             constr = (ToolbarConstraints)it.next();
217             // don't compute twice
218
if (completed.contains(constr)) {
219                 continue;
220             }
221             comp = ToolbarPool.getDefault().findToolbar(constr.getName());
222
223             /* ToolbarConstraints has component bounds prepared. */
224             bounds = constr.getBounds();
225             if ((bounds.x < maxPosition) && // If component starts on visible position ...
226
(bounds.x + bounds.width > maxPosition)) { // ... but with width it is over visible area ...
227
bounds.width = maxPosition - bounds.x; // ... so width is cropped to max possible.
228
comp.setBounds(bounds);
229             } else {
230                 if (constr.getPosition() > maxPosition + HGAP) {
231                     // mark toolbar as candidate for move to next row down
232
if (!constr.isAlone() && ((row.toolbarCount() - moveDownCandidates.size()) > 1)) {
233                         moveDownCandidates.add(constr);
234                     }
235                 } else {
236                     comp.setBounds(bounds);
237                 }
238             }
239         }
240         // move chosen toolbars to next row
241
for (Iterator JavaDoc iter = moveDownCandidates.iterator(); iter.hasNext(); ) {
242             moveToolbarDown((ToolbarConstraints)iter.next());
243         }
244         
245         return null;
246     }
247     
248
249     /** Move toolbar specified by given constraints to the next row */
250     private void moveToolbarDown (ToolbarConstraints constr) {
251         int pos = constr.rowIndex() + 1;
252         constr.destroy();
253         for (int i = pos; i < pos + constr.getRowCount(); i++) {
254             toolbarConfig.getRow(i).addToolbar(constr, 0);
255         }
256         constr.setPosition(0);
257         constr.updatePosition();
258     }
259     
260     
261 }
262
263
Popular Tags