KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > diagrams > PaletteLayout


1 /*
2  * @(#)PaletteLayout.java
3  *
4  * Project: JHotdraw - a GUI framework for technical drawings
5  * http://www.jhotdraw.org
6  * http://jhotdraw.sourceforge.net
7  * Copyright: © by the original author(s) and all contributors
8  * License: Lesser GNU Public License (LGPL)
9  * http://www.opensource.org/licenses/lgpl-license.html
10  */

11
12 package org.objectweb.jac.ide.diagrams;
13
14 import java.awt.Component JavaDoc;
15 import java.awt.Container JavaDoc;
16 import java.awt.Dimension JavaDoc;
17 import java.awt.Insets JavaDoc;
18 import java.awt.LayoutManager JavaDoc;
19 import java.awt.Point JavaDoc;
20
21 /**
22  * A custom layout manager for the palette. Copied from
23  * CH.ifa.draw.util.PaletteLayout and enhanced to provide centered
24  * alignment.
25  */

26
27 public class PaletteLayout implements LayoutManager JavaDoc {
28
29    private int fGap;
30    private Point JavaDoc fBorder;
31    private boolean fVerticalLayout;
32
33    /**
34     * Initializes the palette layout.
35     * @param gap the gap between palette entries.
36     */

37    public PaletteLayout(int gap) {
38       this(gap, new Point JavaDoc(0,0), true);
39    }
40
41    public PaletteLayout(int gap, Point JavaDoc border) {
42       this(gap, border, true);
43    }
44
45    public PaletteLayout(int gap, Point JavaDoc border, boolean vertical) {
46       fGap = gap;
47       fBorder = border;
48       fVerticalLayout = vertical;
49    }
50
51    public void addLayoutComponent(String JavaDoc name, Component JavaDoc comp) {
52    }
53
54    public void removeLayoutComponent(Component JavaDoc comp) {
55    }
56
57    public Dimension JavaDoc preferredLayoutSize(Container JavaDoc target) {
58       return minimumLayoutSize(target);
59    }
60
61    public Dimension JavaDoc minimumLayoutSize(Container JavaDoc target) {
62       Dimension JavaDoc dim = new Dimension JavaDoc(0, 0);
63       int nmembers = target.getComponentCount();
64
65       for (int i = 0 ; i < nmembers ; i++) {
66          Component JavaDoc m = target.getComponent(i);
67          if (m.isVisible()) {
68             Dimension JavaDoc d = m.getMinimumSize();
69             if (fVerticalLayout) {
70                dim.width = Math.max(dim.width, d.width);
71                if (i > 0) {
72                   dim.height += fGap;
73                }
74                dim.height += d.height;
75             }
76             else {
77                dim.height = Math.max(dim.height, d.height);
78                if (i > 0) {
79                   dim.width += fGap;
80                }
81                dim.width += d.width;
82             }
83          }
84       }
85
86       Insets JavaDoc insets = target.getInsets();
87       dim.width += insets.left + insets.right;
88       dim.width += 2 * fBorder.x;
89       dim.height += insets.top + insets.bottom;
90       dim.height += 2 * fBorder.y;
91       return dim;
92    }
93
94    public void layoutContainer(Container JavaDoc target) {
95       Insets JavaDoc insets = target.getInsets();
96       int nmembers = target.getComponentCount();
97       int x = insets.left + fBorder.x;
98       int y = insets.top + fBorder.y;
99       Dimension JavaDoc dim = target.getMinimumSize();
100
101       for (int i=0; i<nmembers; i++) {
102          Component JavaDoc m = target.getComponent(i);
103          if (m.isVisible()) {
104             Dimension JavaDoc d = m.getMinimumSize();
105             if (fVerticalLayout) {
106                m.setBounds(x+(dim.width-d.width)/2-fBorder.x, y, d.width, d.height);
107                y += d.height;
108                y += fGap;
109             } else {
110                m.setBounds(x, y+(dim.height-d.height)/2-fBorder.y, d.width, d.height);
111                x += d.width;
112                x += fGap;
113             }
114          }
115       }
116    }
117 }
118
Popular Tags