alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.netbeans.modules.form.util;

import java.awt.*;
import java.util.*;

// PENDING reordering components in the Form Editor does not immediately
// refresh the design form, though source is updated, and with FlowLayout both
// are refreshed immediately

/**
 * Layout manager with which components are laid out in this manner
 * 
 *     xxxx xxxxxx
 *       xx xxxxxx
 *      xxx xxxxxx
 * 
* or *
 *     xxxx xxxx
 *       xx xxxxxx
 *      xxx xxxxx
 * 
* * The alignment of each component is set individually by its constraint. */ public class FormLayout implements LayoutManager { public static final String LEFT = "Left"; // NOI18N public static final String RIGHT = "Right"; // NOI18N public static final String CENTER = "Center"; // NOI18N public static final String FILL = "Fill"; // NOI18N private int _hgap, _vgap; private Hashtable _constraints = new Hashtable(); public FormLayout() { this(0, 0); } public FormLayout(int hgap, int vgap) { _hgap = hgap; _vgap = vgap; } public void setHgap(int hgap) { _hgap = hgap; } public int getHgap() { return _hgap; } public void setVgap(int vgap) { _vgap = vgap; } public int getVgap() { return _vgap; } public void addLayoutComponent(String name, Component comp) { if (name == null) { name = LEFT; } _constraints.put(comp, name); } public void removeLayoutComponent(Component comp) { _constraints.remove(comp); } public Dimension preferredLayoutSize(Container parent) { Dimension size = new Dimension(); synchronized(parent.getTreeLock()) { Component[] comps = parent.getComponents(); Insets insets = parent.getInsets(); int maxleft = 0, maxright = 0; Dimension empty = new Dimension(); int count = comps.length; for (int i = 0; i < count; i += 2) { Dimension d = comps[i].getPreferredSize(); Dimension d2 =(i >= count - 1) ? empty : comps[i+1].getPreferredSize(); size.height += Math.max(d.height, d2.height); maxleft = Math.max(maxleft, d.width); maxright = Math.max(maxright, d2.width); // if not the last line if (i < count - 2) size.height += _vgap; } size.width += insets.left + insets.right + _hgap + maxleft + maxright; size.height += insets.top + insets.bottom; } return size; } public Dimension minimumLayoutSize(Container parent) { return preferredLayoutSize(parent); } public void layoutContainer(Container parent) { synchronized(parent.getTreeLock()) { Component[] comps = parent.getComponents(); int count = comps.length; Insets insets = parent.getInsets(); Dimension size = parent.getSize(); Dimension empty = new Dimension(); int xalign = 0; for (int i = 0; i < count; i += 2) xalign = Math.max(xalign, comps[i].getPreferredSize().width); xalign += insets.left; int y = insets.top; for (int i = 0; i < count; i += 2) { Component c, c2; Dimension d, d2; c = comps[i]; d = c.getPreferredSize(); if (i < comps.length - 1) { c2 = comps[i+1]; d2 = c2.getPreferredSize(); } else { c2 = null; d2 = empty; } int h = Math.max(d.height, d2.height); setComponentBounds(c, insets.left, y, xalign - insets.left, h); if (c2 != null) { setComponentBounds(c2, xalign + _hgap, y, size.width -(xalign + _hgap + insets.right), h); } y += h + _vgap; } } } private void setComponentBounds(Component c, int x, int y, int w, int h) { String constraint =(String) _constraints.get(c); Dimension sz = new Dimension(c.getPreferredSize()); sz.width = Math.min(sz.width, w); sz.height = Math.min(sz.height, h); if (LEFT.equals(constraint)) { w = sz.width; } else if (RIGHT.equals(constraint)) { x = w - sz.width; w = sz.width; } else if (CENTER.equals(constraint)) { x = x +(w - sz.width) / 2; w = sz.width; } else { // if (FILL.equals(constraint)) { // no-op } c.setBounds(x, y +(h - sz.height) / 2, w, sz.height); } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.