KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > ActionToolBarLayout


1 /*
2  */

3 package com.sshtools.ui.swing;
4
5 import java.awt.Component JavaDoc;
6 import java.awt.Container JavaDoc;
7 import java.awt.Dimension JavaDoc;
8 import java.awt.Insets JavaDoc;
9 import java.awt.LayoutManager JavaDoc;
10 import java.awt.Rectangle JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 public class ActionToolBarLayout implements LayoutManager JavaDoc {
15   
16   private boolean wrap;
17   private int overunIndex;
18   private int height;
19   private Component JavaDoc expandComponent;
20
21   public ActionToolBarLayout(Component JavaDoc expandComponent) {
22     overunIndex = -1;
23     setExpandComponent(expandComponent);
24   }
25   
26   /**
27    *
28    */

29   public ActionToolBarLayout() {
30     this(null);
31   }
32
33   public void setExpandComponent(Component JavaDoc expandComponent) {
34     this.expandComponent = expandComponent;
35   }
36   
37   public Component JavaDoc getExpandComponent() {
38     return expandComponent;
39   }
40
41   public void addLayoutComponent(String JavaDoc con, Component JavaDoc c) {
42   }
43
44   public void removeLayoutComponent(Component JavaDoc c) {
45   }
46
47   public void layoutContainer(Container JavaDoc target) {
48     synchronized (target.getTreeLock()) {
49       Insets JavaDoc insets = target.getInsets();
50       Dimension JavaDoc s = target.getSize();
51       int x = insets.left;
52       int y = insets.top;
53       int count = target.getComponentCount();
54       int rowHeight = 0;
55       int maxRowHeight = -1;
56       Component JavaDoc c = null;
57       Rectangle JavaDoc b = null;
58       Dimension JavaDoc z = null;
59       overunIndex = -1;
60       Dimension JavaDoc e = expandComponent != null ? expandComponent.getPreferredSize() : null;
61       for(int i = 0 ; i < count && overunIndex == -1 ; i++) {
62         c = target.getComponent(i);
63         if(c != expandComponent) {
64           z = c.getPreferredSize();
65           rowHeight = Math.max(rowHeight, z.height);
66           if(maxRowHeight == -1) {
67             maxRowHeight = rowHeight;
68           }
69           if(z.width + x >= ( ( s.width - insets.left ) - ( e != null ? e.width : 0 ) ) ) {
70             if(wrap) {
71               y += rowHeight;
72               x = insets.left;
73               rowHeight = 0;
74               c.setBounds(x, y, z.width, maxRowHeight);
75             }
76             else {
77               overunIndex = i;
78             }
79           }
80           else {
81             c.setBounds(x, y, z.width, maxRowHeight);
82           }
83           x += z.width;
84         }
85       }
86       if(overunIndex != -1) {
87         for(int i = overunIndex; i < count ; i++) {
88           c = target.getComponent(i);
89           if(c != expandComponent) {
90             c.setBounds(0, 0, 0, 0);
91           }
92         }
93       }
94       if(e != null) {
95         if(overunIndex != -1) {
96           Rectangle JavaDoc r = new Rectangle JavaDoc(s.width - insets.right - e.width, insets.top, e.width, maxRowHeight);
97           expandComponent.setBounds(r);
98         }
99         else {
100           expandComponent.setBounds(0, 0, 0, 0);
101         }
102       }
103     }
104   }
105
106   public Dimension JavaDoc minimumLayoutSize(Container JavaDoc target) {
107     synchronized (target.getTreeLock()) {
108       Insets JavaDoc insets = target.getInsets();
109       int count = target.getComponentCount();
110       Dimension JavaDoc d = new Dimension JavaDoc(insets.left, 0);
111       Component JavaDoc c = null;
112       Dimension JavaDoc s = null;
113       for(int i = 0 ; i < count; i++) {
114         c = target.getComponent(i);
115         s = c.getMinimumSize();
116         d.width += s.width;
117         d.height = Math.max(d.height, insets.top + insets.bottom + s.height);
118       }
119       return d;
120     }
121   }
122
123   public Dimension JavaDoc preferredLayoutSize(Container JavaDoc target) {
124     synchronized (target.getTreeLock()) {
125       Insets JavaDoc insets = target.getInsets();
126       int count = target.getComponentCount();
127       Dimension JavaDoc s = target.getSize();
128       Component JavaDoc c = null;
129       Dimension JavaDoc t = new Dimension JavaDoc(0 , 0);
130       Dimension JavaDoc z = null;
131       Dimension JavaDoc e = expandComponent != null ? expandComponent.getPreferredSize() : null;
132       int rowHeight = -1;
133       int x = insets.left;
134       int y = insets.top;
135       int width = insets.left;
136       int height = insets.top + insets.bottom;
137       for(int i = 0 ; i < count; i++) {
138         c = target.getComponent(i);
139         if(c != expandComponent) {
140           z = c.getPreferredSize();
141           rowHeight = Math.max(rowHeight, z.height);
142           if( z.width + x >= ( ( s.width - insets.left ) - ( e != null ? e.width : 0 ) ) ) {
143             overunIndex = i;
144             if(wrap) {
145               y += rowHeight;
146               x = insets.left;
147               height = Math.max(height, y + rowHeight + insets.bottom);
148               rowHeight = 0;
149             }
150             else {
151               height = Math.max(height, y + rowHeight + insets.bottom);
152             }
153           }
154           else {
155             height = Math.max(height, y + rowHeight + insets.bottom);
156           }
157           x += z.width;
158           width = Math.max(width, x);
159         }
160       }
161       width += insets.right;
162       return new Dimension JavaDoc(width, height);
163     }
164   }
165
166   /**
167    * @param wrap wrap
168    */

169   public void setWrap(boolean wrap) {
170     if(!this.wrap == wrap) {
171       this.wrap = wrap;
172       if(wrap) {
173         overunIndex = -1;
174       }
175     }
176   }
177
178   public boolean isWrap() {
179     return wrap;
180   }
181
182   public int getOverunIndex() {
183     return overunIndex;
184   }
185 }
Popular Tags