1 package org.apache.batik.ext.swing; 18 19 import java.awt.Component ; 20 import java.awt.GridBagConstraints ; 21 import java.awt.GridBagLayout ; 22 import java.awt.Insets ; 23 import java.awt.LayoutManager ; 24 25 import javax.swing.JPanel ; 26 27 33 34 public class JGridBagPanel extends JPanel implements GridBagConstants{ 35 38 public static interface InsetsManager{ 39 42 public Insets getInsets(int gridx, int gridy); 43 } 44 45 48 private static class ZeroInsetsManager implements InsetsManager{ 49 private Insets insets = new Insets (0, 0, 0, 0); 50 51 public Insets getInsets(int gridx, int gridy){ 52 return insets; 53 } 54 } 55 56 59 private static class DefaultInsetsManager implements InsetsManager{ 60 64 int leftInset=5; 65 66 70 int topInset=5; 71 72 public Insets positiveInsets = new Insets (topInset, leftInset, 0, 0); 73 public Insets leftInsets = new Insets (topInset, 0, 0, 0); 74 public Insets topInsets = new Insets (0, leftInset, 0, 0); 75 public Insets topLeftInsets = new Insets (0, 0, 0, 0); 76 77 public Insets getInsets(int gridx, int gridy){ 78 if(gridx > 0){ 79 if(gridy > 0) 80 return positiveInsets; 81 else 82 return topInsets; 83 } 84 else{ 85 if(gridy > 0) 86 return leftInsets; 87 else 88 return topLeftInsets; 89 } 90 } 91 } 92 93 96 public static final InsetsManager ZERO_INSETS = new ZeroInsetsManager(); 97 98 101 public static final InsetsManager DEFAULT_INSETS = new DefaultInsetsManager(); 102 103 104 107 public InsetsManager insetsManager; 108 109 112 public JGridBagPanel(){ 113 this(new DefaultInsetsManager()); 114 } 115 116 119 public JGridBagPanel(InsetsManager insetsManager){ 120 super(new GridBagLayout ()); 121 122 if(insetsManager != null) 123 this.insetsManager = insetsManager; 124 else 125 this.insetsManager = new DefaultInsetsManager(); 126 } 127 128 131 public void setLayout(LayoutManager layout){ 132 if(layout instanceof GridBagLayout ) 133 super.setLayout(layout); 134 } 135 136 153 public void add(Component cmp, int gridx, int gridy, 154 int gridwidth, int gridheight, int anchor, int fill, 155 double weightx, double weighty){ 156 Insets insets = insetsManager.getInsets(gridx, gridy); 157 GridBagConstraints constraints = new GridBagConstraints (); 158 constraints.gridx = gridx; 159 constraints.gridy = gridy; 160 constraints.gridwidth = gridwidth; 161 constraints.gridheight = gridheight; 162 constraints.anchor = anchor; 163 constraints.fill = fill; 164 constraints.weightx = weightx; 165 constraints.weighty = weighty; 166 constraints.insets = insets; 167 add(cmp, constraints); 168 } 169 170 } 171 | Popular Tags |