Here is an early version of EasyLayout. I’m still changing a few things around.
Also, you need to have the code of Combo and EasyPanel to make it work. But those 2 class are pretty short.
public class EasyLayout implements LayoutManager{
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
private int type;
public EasyLayout(int type){
this.type = type;
}
@Override
public void addLayoutComponent(String name, Component comp) {
//Not used
}
@Override
public void layoutContainer(Container parent) {
if(parent instanceof EasyPanel){
EasyPanel panel = (EasyPanel)parent;
ArrayList<Combo> list = panel.getComboList();
if(type == HORIZONTAL){
horizontalLayout(list, panel);
}else if(type == VERTICAL){
verticalLayout(list, panel);
}
}
}
private void horizontalLayout(ArrayList<Combo> list, EasyPanel parent){
int currentPos = 0;
for(int i=0; i<list.size(); i++){
Combo combo = list.get(i);
if(combo.getType() == Combo.Normal){
Component comp = combo.getComponent();
int width = (int)comp.getPreferredSize().getWidth();
comp.setLocation(currentPos, 0);
comp.setSize(width, parent.getHeight());
currentPos += width;
}else if(combo.getType() == Combo.Space){
Component comp = combo.getComponent();
int width = (Integer)combo.getValue();
comp.setLocation(currentPos, 0);
comp.setSize(width, parent.getHeight());
currentPos += width;
}else if(combo.getType() == Combo.Fill){
Component comp = combo.getComponent();
double fillPercent = ((Double)combo.getValue())/getTotalFillValue(list);
int width = (int) (getTotalFillSize(list, parent)*fillPercent);
comp.setLocation(currentPos, 0);
comp.setSize(width, parent.getHeight());
currentPos += width;
}else{
//We have a problem
}
}
}
private void verticalLayout(ArrayList<Combo> list, EasyPanel parent){
int currentPos = 0;
for(int i=0; i<list.size(); i++){
Combo combo = list.get(i);
if(combo.getType() == Combo.Normal){
Component comp = combo.getComponent();
int height = (int)comp.getPreferredSize().getHeight();
comp.setLocation(0, currentPos);
comp.setSize(parent.getWidth(), height);
currentPos += height;
}else if(combo.getType() == Combo.Space){
Component comp = combo.getComponent();
int height = (Integer)combo.getValue();
comp.setLocation(0, currentPos);
comp.setSize(parent.getWidth(), height);
currentPos += height;
}else if(combo.getType() == Combo.Fill){
Component comp = combo.getComponent();
double fillPercent = ((Double)combo.getValue())/getTotalFillValue(list);
int height = (int) (getTotalFillSize(list, parent)*fillPercent);
comp.setLocation(0, currentPos);
comp.setSize(parent.getWidth(), height);
currentPos += height;
}else{
//We have a problem
}
}
}
private int getTotalFillSize(ArrayList<Combo> list, EasyPanel parent){
int result = 0;
if(type == HORIZONTAL){
result = parent.getWidth()-getTotalFixedSize(list);
}else if(type == VERTICAL){
result = parent.getHeight()-getTotalFixedSize(list);
}
if(result < 0){
result = 0;
}
return result;
}
private double getTotalFillValue(ArrayList<Combo> list){
double result = 0;
for(int i=0; i<list.size(); i++){
Combo combo = list.get(i);
if(combo.getType() == Combo.Fill){
result += (Double)combo.getValue();
}
}
return result;
}
public int getTotalFixedSize(ArrayList<Combo> list){
int result = 0;
for(int i=0; i<list.size(); i++){
Combo combo = list.get(i);
if(combo.getType() == Combo.Normal){
Component comp = combo.getComponent();
if(type == HORIZONTAL){
result += (int)comp.getPreferredSize().getWidth();
}else if(type == VERTICAL){
result += (int)comp.getPreferredSize().getHeight();
}
}else if(combo.getType() == Combo.Space){
result += (Integer)combo.getValue();
}
}
return result;
}
public int getTotalPrefSize(ArrayList<Combo> list){
int result = 0;
for(int i=0; i<list.size(); i++){
Combo combo = list.get(i);
if(combo.getType() == Combo.Normal){
Component comp = combo.getComponent();
if(type == HORIZONTAL){
result += (int)comp.getPreferredSize().getWidth();
}else if(type == VERTICAL){
result += (int)comp.getPreferredSize().getHeight();
}
}else if(combo.getType() == Combo.Space){
result += (Integer)combo.getValue();
}else if(combo.getType() == Combo.Fill){
Component comp = combo.getComponent();
if(type == HORIZONTAL){
result += (int)comp.getPreferredSize().getWidth();
}else if(type == VERTICAL){
result += (int)comp.getPreferredSize().getHeight();
}
}
}
return result;
}
@Override
public Dimension minimumLayoutSize(Container parent) {
//Not used
return new Dimension(10, 10);
}
@Override
public Dimension preferredLayoutSize(Container parent) {
if(type == HORIZONTAL){
EasyPanel panel = (EasyPanel) parent;
ArrayList<Combo> list = panel.getComboList();
int width = getTotalPrefSize(list);
int height = 0;
for(Combo combo : list){
Component comp = combo.getComponent();
int prefHeight = (int) comp.getPreferredSize().getHeight();
if(prefHeight > height){
height = prefHeight;
}
}
return new Dimension(width, height);
}
if(type == VERTICAL){
EasyPanel panel = (EasyPanel) parent;
ArrayList<Combo> list = panel.getComboList();
int width = 0;
int height = getTotalPrefSize(list);
for(Combo combo : list){
Component comp = combo.getComponent();
int prefWidth = (int) comp.getPreferredSize().getWidth();
if(prefWidth > width){
width = prefWidth;
}
}
return new Dimension(width, height);
}
//Not used
return new Dimension(10, 10);
}
@Override
public void removeLayoutComponent(Component comp) {
//Not used
}
}