001: package snow.utils.gui;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import javax.swing.event.*;
007: import javax.swing.border.*;
008: import java.util.*;
009:
010: /** A grid layout using gridbag layout.
011: It has better resizes features than the GridLayout2
012: */
013: public class GridLayout3 extends GridBagLayout {
014: int cols;
015: JPanel target;
016: final public GridBagConstraints constr = new GridBagConstraints();
017:
018: final Hashtable<Integer, Integer> columnAligmnents = new Hashtable<Integer, Integer>();
019:
020: /** USAGE: add the component to this layout with
021: add(comp, true/false), not with the panel add !
022: */
023: public GridLayout3(int cols, JPanel target) {
024: super ();
025:
026: this .cols = cols;
027: this .target = target;
028:
029: constr.gridwidth = 1;
030: constr.anchor = GridBagConstraints.WEST;
031: constr.insets = new Insets(1, 5, 1, 1);
032:
033: target.setLayout(this );
034: }
035:
036: /** @param alignment is one of GridBagConstraints.WEST, ...
037: if not specified, default is WEST
038: */
039: public void setColumnAlignment(int col, int alignment) {
040: columnAligmnents.put(new Integer(col), new Integer(alignment));
041: }
042:
043: // used as a counter
044: private int actualCol = 0;
045: private double[] weights = null;
046:
047: /** Example: give {0,10,0} to cause only the second column to be resized
048: */
049: public void setColumnWeights(double[] w) {
050: this .weights = w;
051: }
052:
053: public JLabel add(String a) {
054: JLabel jl = new JLabel(a);
055: jl.setOpaque(false);
056: jl.setAlignmentX(JComponent.LEFT_ALIGNMENT);
057: add(jl, false);
058:
059: Dimension ps = jl.getPreferredSize();
060: jl.setMinimumSize(ps); // MUST be set, otherwise, resize is NOT possible
061: return jl;
062: }
063:
064: /** Uses a textfield.
065: */
066: public JTextField addTF(String a) {
067: JTextField jl = new JTextField(a);
068: jl.setOpaque(false);
069: jl.setEditable(false);
070: jl.setAlignmentX(JComponent.LEFT_ALIGNMENT);
071: jl.setBorder(null);
072: add(jl, false);
073: Dimension ps = jl.getPreferredSize();
074: jl.setMinimumSize(ps); // MUST be set, otherwise, resize is NOT possible
075: return jl;
076: }
077:
078: /** Don't fills.
079: */
080: public JComponent add(JComponent comp) {
081: add(comp, false);
082: return comp;
083: }
084:
085: public JTextPane addExplanationArea(String expl) {
086: JTextPane tp = new JTextPane();
087: tp.setAlignmentX(JComponent.LEFT_ALIGNMENT);
088: tp.setEditable(false);
089: tp.setText(expl);
090: tp.setBackground(GUIUtils.giveYellowTouch(target
091: .getBackground()));
092:
093: constr.gridwidth = constr.REMAINDER;
094: constr.fill = constr.HORIZONTAL;
095: this .setConstraints(tp, constr);
096: target.add(tp);
097: actualCol = 0;
098:
099: return tp;
100: }
101:
102: public JComponent addTitleSeparator(String title) {
103: // Color.darkGray
104: return addTitleSeparator(title,
105: TitledBorder.DEFAULT_JUSTIFICATION);
106: }
107:
108: public JComponent addTitleSeparator(String title,
109: int TitledBorderjustification) {
110: // Color.darkGray
111: return addTitleSeparator(title, UIManager
112: .getColor("TitledBorder.titleColor"),
113: TitledBorderjustification,
114: TitledBorder.DEFAULT_POSITION, UIManager.getFont(
115: "Label.font").getSize(), 0);
116: }
117:
118: public JComponent addSeparator() {
119: constr.gridwidth = constr.REMAINDER;
120: constr.fill = constr.HORIZONTAL;
121: JPanel pan = new JPanel(new BorderLayout(0, 0));
122: pan.setOpaque(false);
123: pan.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
124: this .setConstraints(pan, constr);
125: target.add(pan);
126: actualCol = 0;
127: return pan;
128: }
129:
130: /** you have to import javax.swing.border.*;
131: @param TitledBorderjustification TitledBorder.CENTER, ...
132: @param TitledBorderposition TitledBorder.DEFAULT_POSITION, ...
133: the color should be UIManager.getColor("TitledBorder.titleColor");
134: */
135: public JComponent addTitleSeparator(String title, Color lineColor,
136: int TitledBorderjustification, int TitledBorderposition,
137: int spaceAbove, int spaceBelow) {
138: constr.gridwidth = constr.REMAINDER;
139: constr.fill = constr.HORIZONTAL;
140: JPanel pan = new JPanel(new BorderLayout(0, 0)); //new BorderLayout());
141: pan.setOpaque(false);
142: pan.setBorder(BorderFactory.createCompoundBorder(BorderFactory
143: .createEmptyBorder(spaceAbove, 0, spaceBelow, 0),
144: BorderFactory.createTitledBorder(BorderFactory
145: .createMatteBorder(1, 0, 0, 0, lineColor),
146: title, TitledBorderjustification,
147: TitledBorderposition)));
148:
149: this .setConstraints(pan, constr);
150: target.add(pan);
151: actualCol = 0;
152: return pan;
153: }
154:
155: public JComponent add(JComponent comp, boolean fillHorizontally) {
156: constr.fill = (fillHorizontally ? constr.HORIZONTAL
157: : constr.NONE);
158:
159: if (actualCol % cols == cols - 1) {
160: // this is for the last column of each row
161: constr.gridwidth = constr.REMAINDER;
162: } else if (enforceLineBreakAfter) {
163: constr.gridwidth = constr.REMAINDER;
164: } else {
165: // all the other columns
166: constr.gridwidth = 1;
167: }
168:
169: if (this .columnAligmnents.containsKey(actualCol)) {
170: constr.anchor = this .columnAligmnents.get(actualCol);
171: } else {
172: // default
173: constr.anchor = constr.WEST;
174: }
175:
176: if (weights != null && actualCol < weights.length) {
177: constr.weightx = weights[actualCol];
178: } else {
179: // default behaviour, resize all but the first
180: if (actualCol == 0) {
181: constr.weightx = 0;
182: } else {
183: constr.weightx = 1000;
184: }
185: }
186:
187: this .setConstraints(comp, constr);
188: target.add(comp);
189:
190: actualCol++;
191: if (actualCol == cols) {
192: actualCol = 0;
193: }
194:
195: // always reset, use only once when set to true
196: if (enforceLineBreakAfter) {
197: // always reset, use only once when set to true
198: enforceLineBreakAfter = false;
199: actualCol = 0;
200: }
201: return comp;
202:
203: }
204:
205: boolean enforceLineBreakAfter = false;
206:
207: /** force next added component to terminate the line
208: */
209: public void insertLineBreakAfterNextComponent() {
210: enforceLineBreakAfter = true;
211: // constr.gridwidth = constr.REMAINDER;
212: // actualCol = 0;
213:
214: }
215:
216: /**
217: * Usage demos
218: public static void main( String[] arguments )
219: {
220: test1();
221: }
222:
223:
224: public static void test1()
225: {
226: JPanel panel = new JPanel();
227:
228: GridLayout3 gl = new GridLayout3(3, panel);
229: gl.setColumnAlignment(0, GridBagConstraints.EAST);
230: gl.setColumnWeights(new double[]{0,1000,0});
231:
232: JFrame jf = new JFrame("test");
233:
234: jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
235:
236: jf.setContentPane(panel);
237:
238: gl.add( "a");
239: gl.add( new JTextField(8), true);
240: gl.add( new JLabel("mm"), false);
241:
242: gl.add( "b");
243: gl.add( new JTextField(6), true);
244: gl.add( new JLabel("cm"), false);
245:
246: gl.addTitleSeparator("Hello");
247:
248: gl.add( "Hello");
249: gl.add( new JTextField(), true);
250: gl.add( new JLabel("km"), false);
251:
252: gl.addTitleSeparator("Hello", Color.yellow, TitledBorder.CENTER, TitledBorder.ABOVE_TOP, 100, 50);
253:
254: jf.pack();
255: jf.setLocationRelativeTo(null);
256: jf.setVisible(true);
257:
258: } // main
259:
260:
261: public static void test2()
262: {
263: JFrame frame = new JFrame("GridLayout3 line break test");
264: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
265:
266: JPanel content = new JPanel();
267: frame.add(content, BorderLayout.CENTER);
268: GridLayout3 gl3 = new GridLayout3(5,content);
269:
270: for(int i=0; i<20; i++)
271: {
272: JButton jb = new JButton(getRandomString());
273:
274: if(i==2)
275: {
276: gl3.insertLineBreakAfterNextComponent();
277: jb.setBackground(Color.red);
278: }
279:
280: gl3.add(jb);
281:
282: }
283:
284: frame.pack();
285: frame.setLocationRelativeTo(null);
286: frame.setVisible(true);
287: }
288:
289: public static String getRandomString()
290: {
291: int len = (int) (Math.random()*30) + 1;
292: StringBuilder sb = new StringBuilder();
293: for(int i=0; i<len; i++)
294: {
295: sb.append(""+(char)('a'+i));
296: }
297: return sb.toString();
298: }*/
299: }
|