1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| public class ColumnLayout implements LayoutManager, LayoutManager2 {
private final List<Component> components = new ArrayList<>();
private final int gap;
// cache
private Dimension preferredLayoutSize;
private Dimension minimumLayoutSize;
private final List<Rectangle> bounds = new ArrayList<>();
public ColumnLayout() {
this(0);
}
public ColumnLayout(int gap) {
if ( gap<0 ) throw new IllegalArgumentException("Gap must be >=0");
this.gap=gap;
}
@Override
public void addLayoutComponent(Component comp, Object constraints) {
components.add(comp);
reset();
}
@Override
public void addLayoutComponent(String name, Component comp) {
components.add(comp);
reset();
}
private void reset() {
preferredLayoutSize = null;
minimumLayoutSize = null;
bounds.clear();
}
@Override
public Dimension maximumLayoutSize(Container target) {
return preferredLayoutSize(target);
}
@Override
public float getLayoutAlignmentX(Container target) {
return 0.5f;
}
@Override
public float getLayoutAlignmentY(Container target) {
return 0.5f;
}
@Override
public void invalidateLayout(Container target) {
reset();
}
@Override
public void removeLayoutComponent(Component comp) {
components.remove(comp);
reset();
}
@Override
public Dimension preferredLayoutSize(Container parent) {
if ( preferredLayoutSize==null ) {
int width=0;
int height=gap * (components.size()-1);
for(final Component component : components) {
final Dimension size = component.getMaximumSize();
height += size.height;
width = Math.max(width, size.width);
}
final Insets insets = parent.getInsets();
preferredLayoutSize = new Dimension(width+insets.left+insets.right, height+insets.top+insets.bottom);
}
return preferredLayoutSize;
}
@Override
public Dimension minimumLayoutSize(Container parent) {
if ( minimumLayoutSize==null ) {
int width=Integer.MAX_VALUE;
int height=gap * (components.size()-1);
for(Component component : components) {
final Dimension size = component.getMinimumSize();
height += size.height;
width = Math.min(width, size.width);
}
final Insets insets = parent.getInsets();
minimumLayoutSize = new Dimension(width+insets.left+insets.right, height+insets.top+insets.bottom);
}
return minimumLayoutSize;
}
@Override
public void layoutContainer(Container parent) {
if ( bounds.size()!=components.size() ) {
bounds.clear();
Dimension size = preferredLayoutSize(parent);
final Insets insets = parent.getInsets();
size.width-=(insets.left+insets.right);
size.height-=(insets.left+insets.right);
final Dimension parentSize = parent.getSize();
parentSize.width-=(insets.left+insets.right);
parentSize.height-=(insets.top+insets.bottom);
if( parentSize.width<size.width || parentSize.height<size.height ) {
final int width = Math.min(parentSize.width, size.width);
final int totalGap = gap * (components.size()-1);
final int totalHeight = size.height - totalGap;
if ( totalHeight>parentSize.height ) {
int x = insets.left + (parentSize.width-width)/2;
int y = insets.top;
float coef = (float)parentSize.height/totalHeight;
for(Component component : components) {
final int height = (int)(component.getPreferredSize().height*coef);
component.setBounds(x,y,width,height);
bounds.add(component.getBounds());
y+=height;
}
}
else {
float gap = (float)(parentSize.height-totalHeight)/(components.size()-1);
int x = insets.left + (parentSize.width-size.width)/2;
float y = insets.top + (parentSize.height-totalHeight)/2f;
for(Component component : components) {
final int height = component.getPreferredSize().height;
component.setBounds(x, (int)y, size.width, height);
bounds.add(component.getBounds());
y += height;
y += gap;
}
}
}
else {
int x = insets.left + (parentSize.width-size.width)/2;
int y = insets.top + (parentSize.height-size.height)/2;
for(Component component : components) {
final int height = component.getPreferredSize().height;
component.setBounds(x, y, size.width, height);
bounds.add(component.getBounds());
y += height;
y += gap;
}
}
}
else {
for(int i=0; i<components.size(); i++ ) {
components.get(i).setBounds(bounds.get(i));
}
}
}
} |
Partager