1 package com.sshtools.ui.swing; 2 3 import java.awt.Component ; 4 import java.awt.Container ; 5 import java.awt.Dimension ; 6 import java.awt.Insets ; 7 import java.awt.LayoutManager2 ; 8 import java.io.Serializable ; 9 import java.util.Vector ; 10 11 18 public class ListLayout 19 extends AbstractLayout 20 implements LayoutManager2 , 21 Serializable { 22 public static final int CENTER = 0; 23 public static final int LEFT = 1; 24 public static final int RIGHT = 2; 25 public static final int BOTH = 3; 26 protected Vector tabs = new Vector (); 27 protected Vector panels = new Vector (); 28 protected Component center; 29 protected int index = 1; 30 protected int alignment = BOTH; 31 32 35 public ListLayout() { 36 super(); 37 } 38 39 44 public ListLayout(int alignment) { 45 super(); 46 this.alignment = alignment; 47 } 48 49 54 public ListLayout(int hgap, int vgap) { 55 super(hgap, vgap); 56 } 57 58 64 public ListLayout(int hgap, int vgap, int alignment) { 65 super(hgap, vgap); 66 this.alignment = alignment; 67 } 68 69 74 public Dimension minimumLayoutSize(Container target) { 75 Insets insets = target.getInsets(); 76 int w = 0; 77 int h = insets.top; 78 Dimension size; 79 int ncomponents = target.getComponentCount(); 80 for (int i = 0; i < ncomponents; i++) { 81 size = target.getComponent(i).getMinimumSize(); 82 if (size.width > w) { 83 w = size.width; 84 } 85 h += size.height + vgap; 86 } 87 h += insets.bottom; 88 return new Dimension (w + (hgap * 2), h); 89 } 90 91 96 public Dimension preferredLayoutSize(Container target) { 97 Insets insets = target.getInsets(); 98 int w = 0; 99 int h = insets.top; 100 Dimension size; 101 int ncomponents = target.getComponentCount(); 102 for (int i = 0; i < ncomponents; i++) { 103 size = target.getComponent(i).getPreferredSize(); 104 if (size.width > w) { 105 w = size.width; 106 } 107 h += size.height + vgap; 108 } 109 h += insets.bottom; 110 return new Dimension (w + (hgap * 2), h); 111 } 112 113 119 public void layoutContainer(Container parent) { 120 Insets insets = parent.getInsets(); 121 int w = parent.getSize().width; 122 Component comp; 123 Dimension size; 124 int position = insets.top; 125 int ncomponents = parent.getComponentCount(); 126 127 for (int i = 0; i < ncomponents; i++) { 128 comp = parent.getComponent(i); 129 size = comp.getPreferredSize(); 130 131 int h = size.height - insets.top - insets.bottom; 132 133 switch (alignment) { 134 case CENTER: { 135 int l = (w - size.width) / 2; 136 comp.setBounds(insets.left + hgap + l, position, 137 size.width - insets.left - insets.right - 138 (hgap * 2), h); 139 140 break; 141 } 142 case LEFT: { 143 comp.setBounds(insets.left + hgap, position, 144 size.width - insets.left - insets.right - 145 (hgap * 2), h); 146 147 break; 148 } 149 case RIGHT: { 150 int l = w - size.width; 151 comp.setBounds(insets.left + hgap + l, position, 152 size.width - insets.left - insets.right - 153 (hgap * 2), h); 154 155 break; 156 } 157 default: { 158 comp.setBounds(insets.left + hgap, position, 159 w - insets.left - insets.right - (hgap * 2), 160 h); 161 162 break; 163 } 164 } 165 166 position += (h + vgap); 167 } 168 } 169 } 170 | Popular Tags |