1 3 package com.sshtools.ui.swing; 4 5 import java.awt.Component ; 6 import java.awt.Container ; 7 import java.awt.Dimension ; 8 import java.awt.Insets ; 9 import java.awt.LayoutManager ; 10 import java.awt.Rectangle ; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 14 public class ActionToolBarLayout implements LayoutManager { 15 16 private boolean wrap; 17 private int overunIndex; 18 private int height; 19 private Component expandComponent; 20 21 public ActionToolBarLayout(Component expandComponent) { 22 overunIndex = -1; 23 setExpandComponent(expandComponent); 24 } 25 26 29 public ActionToolBarLayout() { 30 this(null); 31 } 32 33 public void setExpandComponent(Component expandComponent) { 34 this.expandComponent = expandComponent; 35 } 36 37 public Component getExpandComponent() { 38 return expandComponent; 39 } 40 41 public void addLayoutComponent(String con, Component c) { 42 } 43 44 public void removeLayoutComponent(Component c) { 45 } 46 47 public void layoutContainer(Container target) { 48 synchronized (target.getTreeLock()) { 49 Insets insets = target.getInsets(); 50 Dimension 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 c = null; 57 Rectangle b = null; 58 Dimension z = null; 59 overunIndex = -1; 60 Dimension 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 r = new Rectangle (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 minimumLayoutSize(Container target) { 107 synchronized (target.getTreeLock()) { 108 Insets insets = target.getInsets(); 109 int count = target.getComponentCount(); 110 Dimension d = new Dimension (insets.left, 0); 111 Component c = null; 112 Dimension 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 preferredLayoutSize(Container target) { 124 synchronized (target.getTreeLock()) { 125 Insets insets = target.getInsets(); 126 int count = target.getComponentCount(); 127 Dimension s = target.getSize(); 128 Component c = null; 129 Dimension t = new Dimension (0 , 0); 130 Dimension z = null; 131 Dimension 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 (width, height); 163 } 164 } 165 166 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 |