1 11 12 package org.objectweb.jac.ide.diagrams; 13 14 import java.awt.Component ; 15 import java.awt.Container ; 16 import java.awt.Dimension ; 17 import java.awt.Insets ; 18 import java.awt.LayoutManager ; 19 import java.awt.Point ; 20 21 26 27 public class PaletteLayout implements LayoutManager { 28 29 private int fGap; 30 private Point fBorder; 31 private boolean fVerticalLayout; 32 33 37 public PaletteLayout(int gap) { 38 this(gap, new Point (0,0), true); 39 } 40 41 public PaletteLayout(int gap, Point border) { 42 this(gap, border, true); 43 } 44 45 public PaletteLayout(int gap, Point border, boolean vertical) { 46 fGap = gap; 47 fBorder = border; 48 fVerticalLayout = vertical; 49 } 50 51 public void addLayoutComponent(String name, Component comp) { 52 } 53 54 public void removeLayoutComponent(Component comp) { 55 } 56 57 public Dimension preferredLayoutSize(Container target) { 58 return minimumLayoutSize(target); 59 } 60 61 public Dimension minimumLayoutSize(Container target) { 62 Dimension dim = new Dimension (0, 0); 63 int nmembers = target.getComponentCount(); 64 65 for (int i = 0 ; i < nmembers ; i++) { 66 Component m = target.getComponent(i); 67 if (m.isVisible()) { 68 Dimension 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 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 target) { 95 Insets insets = target.getInsets(); 96 int nmembers = target.getComponentCount(); 97 int x = insets.left + fBorder.x; 98 int y = insets.top + fBorder.y; 99 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 } 118 | Popular Tags |