Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: a0087abeb5b551fd72805073d3f591f3650bba7a (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xsd.ui.internal.graph.figures;

import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.AbstractLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;


/**
 * Figures using the StackLayout as their layout manager have
 * their children placed on top of one another. Order of 
 * placement is determined by the order in which the children
 * were added, first child added placed on the bottom.
 */
public class CenterLayout
	extends AbstractLayout
{
                             
protected int spacing;

public CenterLayout(){}
public CenterLayout(int spacing){ this.spacing = spacing; }

/**
 * Calculates and returns the preferred size of the input container.
 * This is the size of the largest child of the container, as all
 * other children fit into this size.
 *
 * @param figure  Container figure for which preferred size is required.
 * @return  The preferred size of the input figure.
 */
protected Dimension calculatePreferredSize(IFigure figure, int w, int h){
	Dimension d = calculatePreferredClientAreaSize(figure);
	/*d.expand(figure.getInsets().getWidth(),
	         figure.getInsets().getHeight()); */
        d.expand(w,
	         h);
	d.union(getBorderPreferredSize(figure));
	return d;
}

protected Dimension calculatePreferredClientAreaSize(IFigure figure){
	Dimension d = new Dimension();
	List children = figure.getChildren();
	for (Iterator i = children.iterator(); i.hasNext(); )
  {
		IFigure child = (IFigure)i.next();
    Dimension childSize = child.getPreferredSize();
    d.height += childSize.height;
    d.width = Math.max(childSize.width, d.width);
	}	                    
  int childrenSize = children.size();       
  if (childrenSize > 0)
  {
    d.height += spacing * children.size() - 1;
  }
	return d;
}

/*
 * Returns the minimum size required by the input container.
 * This is the size of the largest child of the container, as all
 * other children fit into this size.
 */
public Dimension getMinimumSize(IFigure figure, int wHint, int hHint){
	Dimension d = new Dimension();
	List children = figure.getChildren();
	IFigure child;
	for (int i=0; i < children.size(); i++){
		child = (IFigure)children.get(i);
		d.union(child.getMinimumSize());
	}
	d.expand(figure.getInsets().getWidth(),
	         figure.getInsets().getHeight());
	return d;
}

public Dimension getPreferredSize(IFigure figure, int wHint, int hHint){
	return calculatePreferredSize(figure, wHint, hHint);
}


/*
 * Lays out the children on top of each other with
 * their sizes equal to that of the available
 * paintable area of the input container figure.
 */
public void layout(IFigure figure){
	Rectangle r = figure.getClientArea();
	List children = figure.getChildren();
  
  Dimension preferredClientAreaSize = calculatePreferredClientAreaSize(figure);
  int x = r.x + (r.width - preferredClientAreaSize.width) / 2;
  int y = r.y + (r.height - preferredClientAreaSize.height) / 2;

	for (Iterator i = children.iterator(); i.hasNext(); )
  {
		IFigure child = (IFigure)i.next();
    Dimension childSize = child.getPreferredSize();
		child.setBounds(new Rectangle(x, y, childSize.width, childSize.height));
    y += childSize.height + spacing;
	}
}
}

Back to the top