001    /*
002     * @(#)PaletteLayout.java
003     *
004     * Project:             JHotdraw - a GUI framework for technical drawings
005     *                              http://www.jhotdraw.org
006     *                              http://jhotdraw.sourceforge.net
007     * Copyright:   © by the original author(s) and all contributors
008     * License:             Lesser GNU Public License (LGPL)
009     *                              http://www.opensource.org/licenses/lgpl-license.html
010     */
011    
012    package org.objectweb.jac.ide.diagrams;
013    
014    import java.awt.Component;
015    import java.awt.Container;
016    import java.awt.Dimension;
017    import java.awt.Insets;
018    import java.awt.LayoutManager;
019    import java.awt.Point;
020    
021    /**
022     * A custom layout manager for the palette. Copied from
023     * CH.ifa.draw.util.PaletteLayout and enhanced to provide centered
024     * alignment.
025     */
026    
027    public class PaletteLayout implements LayoutManager {
028    
029       private int         fGap;
030       private Point       fBorder;
031       private boolean     fVerticalLayout;
032    
033       /**
034        * Initializes the palette layout.
035        * @param gap the gap between palette entries.
036        */
037       public PaletteLayout(int gap) {
038          this(gap, new Point(0,0), true);
039       }
040    
041       public PaletteLayout(int gap, Point border) {
042          this(gap, border, true);
043       }
044    
045       public PaletteLayout(int gap, Point border, boolean vertical) {
046          fGap = gap;
047          fBorder = border;
048          fVerticalLayout = vertical;
049       }
050    
051       public void addLayoutComponent(String name, Component comp) {
052       }
053    
054       public void removeLayoutComponent(Component comp) {
055       }
056    
057       public Dimension preferredLayoutSize(Container target) {
058          return minimumLayoutSize(target);
059       }
060    
061       public Dimension minimumLayoutSize(Container target) {
062          Dimension dim = new Dimension(0, 0);
063          int nmembers = target.getComponentCount();
064    
065          for (int i = 0 ; i < nmembers ; i++) {
066             Component m = target.getComponent(i);
067             if (m.isVisible()) {
068                Dimension d = m.getMinimumSize();
069                if (fVerticalLayout) {
070                   dim.width = Math.max(dim.width, d.width);
071                   if (i > 0) {
072                      dim.height += fGap;
073                   }
074                   dim.height += d.height;
075                }
076                else {
077                   dim.height = Math.max(dim.height, d.height);
078                   if (i > 0) {
079                      dim.width += fGap;
080                   }
081                   dim.width += d.width;
082                }
083             }
084          }
085    
086          Insets insets = target.getInsets();
087          dim.width += insets.left + insets.right;
088          dim.width += 2 * fBorder.x;
089          dim.height += insets.top + insets.bottom;
090          dim.height += 2 * fBorder.y;
091          return dim;
092       }
093    
094       public void layoutContainer(Container target) {
095          Insets insets = target.getInsets();
096          int nmembers = target.getComponentCount();
097          int x = insets.left + fBorder.x;
098          int y = insets.top + fBorder.y;
099          Dimension dim = target.getMinimumSize();
100    
101          for (int i=0; i<nmembers; i++) {
102             Component m = target.getComponent(i);
103             if (m.isVisible()) {
104                Dimension 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    }