/* Copyright (C) 2000, 2001, Geert Vernaeve. All Rights Reserved.	*/
package gve.calc.formula;

import java.awt.*;

public class HorizontalLayoutMgr implements LayoutManager,BaselineLayout {
	private static boolean debug = false;
	private static boolean debugexc = false;
	private static int gap = 1;

	public HorizontalLayoutMgr() {}

	public void addLayoutComponent(String name,Component comp) {}

	public void removeLayoutComponent(Component comp) {}

	public void setGap(int gap) { this.gap = gap; }
	public int getGap() { return gap; }

	public Dimension preferredLayoutSize(Container target) {
if (debug && target instanceof InfixBinaryOpView) {
 InfixBinaryOp model = (InfixBinaryOp)((InfixBinaryOpView)target).getModel();
 Part p = model;
 while (p!=null) { p=p.parent; System.out.print(" ");}
 System.out.println("calculating preferred size "+model.getName()+" "+model.hashCode());
 if (debugexc)try { throw new Error("calculating preferred size "+target); } catch (Error err) { err.printStackTrace(System.out); }
}
		int mybase = 0;
		int myheight = 0;
		int comps = target.getComponentCount();
		int mywidth = 0;
		for (int i = 0; i < comps; i++) {
			Component child = target.getComponent(i);
			Dimension siz = getPreferredSize(i,child);	//child.getPreferredSize();
//System.out.println("hlayout child "+child+" size "+siz);
			int childbase;
			if (child instanceof HasBaseline)
				childbase = ((HasBaseline)child).getBaseline();
			else childbase = siz.height / 2;
			if (i == 0) {
				mybase = childbase;
				mywidth = siz.width;
				myheight = siz.height;
				continue;
			}
			if (childbase > mybase) {	// dit kind steekt hoger uit dan die links ervan
				myheight += childbase-mybase;
				mybase = childbase;
				if (siz.height > myheight)	// kind steekt lager uit dan die links ervan
					myheight = siz.height;
			} else {
				int lowesty = mybase + siz.height - childbase;
				if (lowesty > myheight)	// kind steekt lager uit dan die links ervan
					myheight = lowesty;
			}
			mywidth += siz.width + gap;
		}
		return new Dimension(mywidth,myheight);
	}

	public Dimension minimumLayoutSize(Container target) {
		return preferredLayoutSize(target);
	}

	
Internal buffer for components and their preferred sizes
public Component [] compBuff; public Dimension [] dimBuff; public void layoutContainer(Container target) { if (debug && target instanceof InfixBinaryOpView) { InfixBinaryOp model = (InfixBinaryOp)((InfixBinaryOpView)target).getModel(); Part p = model; while (p!=null) { p=p.parent; System.out.print(" ");} System.out.println("layouting "+model.getName()+" "+model.hashCode()+" "+target); if (debugexc) try { throw new Error("Layouting"); } catch (Error err) { err.printStackTrace(System.out); } } int comps = target.getComponentCount(); if (compBuff == null) { dimBuff = new Dimension[comps]; compBuff = new Component[comps]; } if (compBuff.length != comps) { // PENDING: copy (some of) the old buffered data dimBuff = new Dimension[comps]; compBuff = new Component[comps]; } for (int i = 0; i < comps; i++) { Component child = target.getComponent(i); if (child.isValid() && compBuff[i] == child) continue; child.validate(); compBuff[i] = child; dimBuff[i] = child.getPreferredSize(); } int mybase = getBaseline(target); int x = 0; //System.out.println("horizontal layout:"); for (int i = 0; i < comps; i++) { Component child = compBuff[i]; //target.getComponent(i); Dimension siz = dimBuff[i]; //child.getPreferredSize(); int childbase; if (child instanceof HasBaseline) childbase = ((HasBaseline)child).getBaseline(); else childbase = siz.height / 2; child.setBounds(x,mybase - childbase,siz.width,siz.height); //System.out.println("comp "+i+" x=["+x+","+(x+siz.width)+"["); x += siz.width + gap; } //System.out.println("x is "+x+", width = "+target.getSize().width); if (debug && target instanceof InfixBinaryOpView) { InfixBinaryOp model = (InfixBinaryOp)((InfixBinaryOpView)target).getModel(); Part p = model; while (p!=null) { p=p.parent; System.out.print(" ");} System.out.println("layouting done"+model.getName()+" "+model.hashCode()); if (debugexc)try { throw new Error("Layouting done"); } catch (Error err) { err.printStackTrace(System.out); } } }
Return comp.getPreferredSize(), buffering if possible
private Dimension getPreferredSize(int count,Component comp) { if (dimBuff==null || count>=dimBuff.length) return comp.getPreferredSize(); if (compBuff[count]==comp && comp.isValid()) return dimBuff[count]; compBuff[count] = comp; dimBuff[count] = comp.getPreferredSize(); return dimBuff[count]; } public int getBaseline(Container target) { int mybase = 0; int comps = target.getComponentCount(); for (int i = 0; i < comps; i++) { Component child = target.getComponent(i); Dimension siz = getPreferredSize(i,child); //child.getPreferredSize(); //System.out.println("hlayout child "+child+" size "+siz); int childbase; if (child instanceof HasBaseline) childbase = ((HasBaseline)child).getBaseline(); else childbase = siz.height / 2; if (childbase > mybase) mybase = childbase; } return mybase; } }