Example usage for javax.swing.text View getPreferredSpan

List of usage examples for javax.swing.text View getPreferredSpan

Introduction

In this page you can find the example usage for javax.swing.text View getPreferredSpan.

Prototype

public abstract float getPreferredSpan(int axis);

Source Link

Document

Determines the preferred span for this view along an axis.

Usage

From source file:org.nuclos.client.ui.resplan.header.JHeaderGrid.java

private void calculateGroupingExtents() {
    final Orientation groupOrient = orientation.opposite();
    int preferredGroupingsExtent = 0;
    for (CategoryView categoryView : groupings) {
        int preferredCategorySize = 0;
        for (HeaderCell cell : categoryView.cells) {
            JComponent renderer = setupCellRenderer(cell.levelValue, false);
            Dimension preferredSize = renderer.getPreferredSize();
            int preferredExtent = groupOrient.extentFrom(preferredSize);
            if (renderer instanceof JLabel) {
                // This is a some kind of hack for determing the preferred size of an HTML JLabel
                // using a given width or height. See also:
                // http://blog.nobel-joergensen.com/2009/01/18/changing-preferred-size-of-a-html-jlabel/
                // NOTE: This seems to work for horizontal orientations (i.e. calculating the height for a
                // given width) while for vertical orientations often the dummy value 0 is returned.
                View view = (View) renderer.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
                if (view != null) {
                    float w = (orientation == Orientation.HORIZONTAL) ? cellExtent : 0;
                    float h = (orientation == Orientation.VERTICAL) ? cellExtent : 0;
                    view.setSize(w, h);// w  w w  . ja  v a  2s  .  com
                    float span = (int) Math.ceil(view.getPreferredSpan(groupOrient.swingConstant()));
                    preferredExtent = Math.max(preferredExtent, (int) Math.ceil(span));
                }
            }
            preferredCategorySize = Math.max(preferredCategorySize, preferredExtent + 2);
        }
        categoryView.preferredSize = preferredCategorySize;
        categoryView.size = preferredCategorySize;
        preferredGroupingsExtent += preferredCategorySize + GRID_SIZE;
    }

    // Real size and preferred size may vary
    int realGroupingsExtent = groupOrient.extentFrom(getSize());
    int delta = realGroupingsExtent - preferredGroupingsExtent;
    if (delta > 0) {
        int gap = delta / groupings.length;
        for (CategoryView v : groupings) {
            v.size += gap;
        }
        int rem = delta % groupings.length;
        if (rem > 0 && groupings.length > 0) {
            groupings[groupings.length - 1].size += rem;
        }
    }
}