KickJava   Java API By Example, From Geeks To Geeks.

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


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.view.ui.toolbars;
21
22 import org.openide.awt.Toolbar;
23 import org.openide.awt.ToolbarPool;
24
25 import java.awt.*;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 /**
32  * This class represents one row of toolbars.
33  *
34  * Toolbar row is part of toolbar configuration and contains list of toolbars,
35  * it is possible to add, remove and switch constraints.
36  * There is cached row's neighbournhood, so when there is some row motion
37  * those cached values are recomputed.
38  *
39  * @author Libor Kramolis
40  */

41 public class ToolbarRow {
42     /** ToolbarConfiguration */
43     ToolbarConfiguration toolbarConfig;
44     /** Previous row of toolbars. */
45     ToolbarRow prevRow;
46     /** Next row of toolbars. */
47     ToolbarRow nextRow;
48
49     /** List of toolbars in row. */
50     private Vector JavaDoc<ToolbarConstraints> toolbars;
51     /** listener for changes of constraints of contained toolbars */
52     private PropertyChangeListener JavaDoc constraintsL;
53     /** cached preferred height of this row */
54     private int prefHeight;
55     
56     /** Create new ToolbarRow.
57      * @param own ToolbarConfiguration
58      */

59     ToolbarRow (ToolbarConfiguration config) {
60         toolbarConfig = config;
61         toolbars = new Vector JavaDoc<ToolbarConstraints>();
62         prevRow = nextRow = null;
63         // invoke revalidation of toolbar rows below if height changes
64
constraintsL = new PropertyChangeListener JavaDoc () {
65             public void propertyChange (PropertyChangeEvent JavaDoc evt) {
66                 if (ToolbarConstraints.PREFERRED_SIZE.equals(evt.getPropertyName())) {
67                     Dimension oldTCSize = (Dimension)evt.getOldValue();
68                     Dimension newTCSize = (Dimension)evt.getNewValue();
69                     if (oldTCSize.height != newTCSize.height) {
70                         updateRowsBelow();
71                     }
72                 }
73             }
74         };
75     }
76
77     /** Add toolbar to end of row.
78      * @param tc ToolbarConstraints
79      */

80     void addToolbar (ToolbarConstraints tc) {
81         addToolbar2 (tc, toolbars.size());
82     }
83
84     /** Add toolbar to specific position
85      * @param newTC ToolbarConstraints
86      * @param pos specified position of new toolbar
87      */

88     void addToolbar (ToolbarConstraints newTC, int pos) {
89         int index = newTC.checkInitialIndexInRow();
90         if( index >= 0 ) {
91             //the toolbar is being added for the first time so get its index
92
//from the order of declarations in layers xml
93
index = Math.min( index, toolbars.size() );
94         } else {
95             index = 0;
96             Iterator JavaDoc it = toolbars.iterator();
97             ToolbarConstraints tc;
98             while (it.hasNext()) {
99                 tc = (ToolbarConstraints)it.next();
100                 if (pos <= tc.getPosition())
101                     break;
102                 index++;
103             }
104         }
105         addToolbar2 (newTC, index);
106     }
107
108     /** Add toolbar to specific index int row
109      * @param tc ToolbarConstraints
110      * @param index specified index of new toolbar
111      */

112     private void addToolbar2 (ToolbarConstraints tc, int index) {
113         if (toolbars.contains (tc))
114             return;
115         ToolbarConstraints prev = null;
116         ToolbarConstraints next = null;
117         if (index != 0) {
118             prev = (ToolbarConstraints)toolbars.elementAt (index - 1);
119             prev.addNextBar (tc);
120             tc.addPrevBar (prev);
121         }
122         if (index < toolbars.size()) {
123             next = (ToolbarConstraints)toolbars.elementAt (index);
124             tc.addNextBar (next);
125             next.addPrevBar (tc);
126         }
127         if ((prev != null) && (next != null)) {
128             prev.removeNextBar (next);
129             next.removePrevBar (prev);
130         }
131         
132         int oldHeight = getPreferredHeight();
133
134         tc.addOwnRow (this);
135         toolbars.insertElementAt (tc, index);
136
137         tc.updatePosition();
138
139         tc.addPropertyChangeListener(constraintsL);
140     }
141
142     /** Remove toolbar from row.
143      * @param tc toolbar for remove
144      */

145     void removeToolbar (ToolbarConstraints tc) {
146         int index = toolbars.indexOf (tc);
147
148         ToolbarConstraints prev = null;
149         ToolbarConstraints next = null;
150         try {
151             prev = (ToolbarConstraints)toolbars.elementAt (index - 1);
152             prev.removeNextBar (tc);
153         } catch (ArrayIndexOutOfBoundsException JavaDoc e) { }
154         try {
155             next = (ToolbarConstraints)toolbars.elementAt (index + 1);
156             next.removePrevBar (tc);
157         } catch (ArrayIndexOutOfBoundsException JavaDoc e) { }
158         if ((prev != null) && (next != null)) {
159             prev.addNextBar (next);
160             next.addPrevBar (prev);
161         }
162
163         toolbars.removeElement (tc);
164
165         if (prev != null) {
166             prev.updatePosition();
167         } else {
168             if (next != null) {
169                 next.updatePosition();
170             }
171         }
172         tc.removePropertyChangeListener(constraintsL);
173     }
174
175     /** @return Iterator of toolbars int row. */
176     Iterator JavaDoc<ToolbarConstraints> iterator () {
177         return toolbars.iterator();
178     }
179
180     /** Set a previous row.
181      * @param prev new previous row.
182      */

183     void setPrevRow (ToolbarRow prev) {
184         prevRow = prev;
185     }
186
187     /** @return previous row. */
188     ToolbarRow getPrevRow () {
189         return prevRow;
190     }
191
192     /** Set a next row.
193      * @param next new next row.
194      */

195     void setNextRow (ToolbarRow next) {
196         nextRow = next;
197     }
198
199     /** @return next row. */
200     ToolbarRow getNextRow () {
201         return nextRow;
202     }
203
204     /** @return preferred width of row. */
205     int getPrefWidth () {
206         if (toolbars.isEmpty())
207             return -1;
208         return ((ToolbarConstraints)toolbars.lastElement()).getPrefWidth();
209     }
210
211     /** @return true if row is empty */
212     boolean isEmpty () {
213         return toolbars.isEmpty();
214     }
215
216     /** @return number of toolbars int row. */
217     int toolbarCount () {
218         return toolbars.size();
219     }
220
221     /** Update bounds of all row toolbars. */
222     void updateBounds () {
223         Iterator JavaDoc it = toolbars.iterator();
224         ToolbarConstraints tc;
225         while (it.hasNext()) {
226             tc = (ToolbarConstraints)it.next();
227             tc.updateBounds();
228         }
229     }
230
231     /** Update position of rows below this one. Called when height of this row
232      * has changed.
233      */

234     private void updateRowsBelow () {
235         for (int i = toolbarConfig.rowIndex(this) + 1; i < toolbarConfig.getRowCount(); i++) {
236             toolbarConfig.getRow(i).updateBounds();
237         }
238     }
239
240     /** Switch two toolbars.
241      * @param left ToolbarConstraints
242      * @param right ToolbarConstraints
243      */

244     void switchBars (ToolbarConstraints left, ToolbarConstraints right) {
245         int leftIndex = toolbars.indexOf (left);
246         int rightIndex = toolbars.indexOf (right);
247         ToolbarConstraints leftPrev = null;
248         ToolbarConstraints rightNext = null;
249
250         try {
251             leftPrev = (ToolbarConstraints)toolbars.elementAt (leftIndex - 1);
252         } catch (ArrayIndexOutOfBoundsException JavaDoc e) { }
253         try {
254             rightNext = (ToolbarConstraints)toolbars.elementAt (rightIndex + 1);
255         } catch (ArrayIndexOutOfBoundsException JavaDoc e) { }
256
257         if (leftPrev != null)
258             leftPrev.removeNextBar (left);
259         left.removePrevBar (leftPrev);
260         left.removeNextBar (right);
261
262         right.removePrevBar (left);
263         right.removeNextBar (rightNext);
264         if (rightNext != null)
265             rightNext.removePrevBar (right);
266
267         if (leftPrev != null)
268             leftPrev.addNextBar (right);
269         left.addPrevBar (right);
270         left.addNextBar (rightNext);
271
272         right.addPrevBar (leftPrev);
273         right.addNextBar (left);
274         if (rightNext != null)
275             rightNext.addPrevBar (left);
276
277         toolbars.setElementAt (left, rightIndex);
278         toolbars.setElementAt (right, leftIndex);
279     }
280
281     /** Let's try switch toolbar left.
282      * @param ToolbarConstraints
283      */

284     void trySwitchLeft (ToolbarConstraints tc) {
285         int index = toolbars.indexOf (tc);
286         if (index == 0)
287             return;
288
289         try {
290             ToolbarConstraints prev = (ToolbarConstraints)toolbars.elementAt (index - 1);
291             if (ToolbarConstraints.canSwitchLeft (tc.getPosition(), tc.getWidth(), prev.getPosition(), prev.getWidth())) {
292                 switchBars (prev, tc);
293             }
294         } catch (ArrayIndexOutOfBoundsException JavaDoc e) { /* No left toolbar - it means tc is toolbar like Palette (:-)) */ }
295     }
296
297     /** Let's try switch toolbar right.
298      * @param ToolbarConstraints
299      */

300     void trySwitchRight (ToolbarConstraints tc) {
301         int index = toolbars.indexOf (tc);
302
303         try {
304             ToolbarConstraints next = (ToolbarConstraints)toolbars.elementAt (index + 1);
305             if (ToolbarConstraints.canSwitchRight (tc.getPosition(), tc.getWidth(), next.getPosition(), next.getWidth())) {
306                 switchBars (tc, next);
307                 next.setPosition (tc.getPosition() - next.getWidth() - ToolbarLayout.HGAP);
308             }
309         } catch (ArrayIndexOutOfBoundsException JavaDoc e) { /* No right toolbar - it means tc is toolbar like Palette (:-)) */ }
310     }
311     
312     /** @return preferred height of this row. Computed as max from preferred
313      * heights of individual toolbars, but not bigger then BASIC_HEIGHT
314      */

315     int getPreferredHeight () {
316         ToolbarConstraints curConstr = null;
317         ToolbarPool pool = ToolbarPool.getDefault();
318         prefHeight = 0;
319         int curHeight = 0;
320         for (Iterator JavaDoc iter = toolbars.iterator(); iter.hasNext(); ) {
321             curConstr = (ToolbarConstraints)iter.next();
322             // compute only from one-row toolbars
323
if (curConstr.getRowCount() == 1) {
324                 Toolbar curToolbar = pool.findToolbar(curConstr.getName());
325                 // data may be out of sync, see ToolbarConfiguration.updateConfiguration
326
// for explanation
327
if (curToolbar != null) {
328                     curHeight = curToolbar.getPreferredSize().height;
329                     if (prefHeight < curHeight) {
330                         prefHeight = curHeight;
331                     }
332                 }
333             }
334         }
335         prefHeight = prefHeight <= 0 ? Toolbar.getBasicHeight() : Math.min(Toolbar.getBasicHeight(), prefHeight);
336         return prefHeight;
337     }
338
339     /** Class to store row in xml format. */
340     static class WritableToolbarRow {
341         /** List of toolbars. */
342         Vector JavaDoc<ToolbarConstraints.WritableToolbar> toolbars;
343
344         /** Create new WritableToolbarRow.
345          */

346         public WritableToolbarRow () {
347             toolbars = new Vector JavaDoc<ToolbarConstraints.WritableToolbar>();
348         }
349
350         /** Create new WritableToolbarRow.
351          * @param row ToolbarRow
352          */

353         public WritableToolbarRow (ToolbarRow row) {
354             this();
355             initToolbars (row);
356         }
357
358         /** Init list of writable toolbars. */
359         void initToolbars (ToolbarRow r) {
360             Iterator JavaDoc<ToolbarConstraints> it = r.toolbars.iterator();
361             while (it.hasNext()) {
362                 toolbars.addElement (new ToolbarConstraints.WritableToolbar (it.next()));
363             }
364         }
365
366         /** Add toolbar to list of writable toolbars.
367          * @param newTC new tested ToolbarConstraints
368          */

369         void addToolbar (ToolbarConstraints newTC) {
370             int index = 0;
371             Iterator JavaDoc it = toolbars.iterator();
372             ToolbarConstraints.WritableToolbar tc;
373             while (it.hasNext()) {
374                 tc = (ToolbarConstraints.WritableToolbar)it.next();
375                 if (newTC.getPosition() < tc.position)
376                     break;
377                 index++;
378             }
379
380             toolbars.insertElementAt (new ToolbarConstraints.WritableToolbar (newTC), index);
381         }
382
383         /** @return true if row is empty */
384         boolean isEmpty () {
385             return toolbars.isEmpty();
386         }
387
388         /** @return ToolbarRow in xml format. */
389         public String JavaDoc toString () {
390             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
391
392             sb.append (" <").append (ToolbarConfiguration.TAG_ROW).append (">\n"); // NOI18N
393
Iterator JavaDoc it = toolbars.iterator();
394             while (it.hasNext()) {
395                 sb.append (it.next().toString());
396             }
397             sb.append (" </").append (ToolbarConfiguration.TAG_ROW).append (">\n"); // NOI18N
398

399             return sb.toString();
400         }
401     } // end of class WritableToolbarRow
402
} // end of class ToolbarRow
403

404
Popular Tags