Java JComponent Size getValidatedSize(Component c, int sizeflag)

Here you can find the source of getValidatedSize(Component c, int sizeflag)

Description

get Validated Size

License

Open Source License

Declaration

public static Dimension getValidatedSize(Component c, int sizeflag) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w  ww  .  j av  a 2s  . c o m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.awt.Component;

import java.awt.Dimension;

import javax.swing.JLabel;

import javax.swing.text.View;

public class Main {
    public static final int MIN = 0;
    public static final int PREF = 1;
    public static final int MAX = 2;

    public static Dimension getValidatedSize(Component c, int sizeflag) {
        return getValidatedSizes(c)[sizeflag];
    }

    public static Dimension[] getValidatedSizes(Component c) {
        Dimension[] d = new Dimension[] { getSize(c, MIN), getSize(c, PREF), getSize(c, MAX) };
        // validation
        if (d[MIN].width > d[PREF].width) {
            d[MIN].width = d[PREF].width;
        }
        if (d[MIN].height > d[PREF].height) {
            d[MIN].height = d[PREF].height;
        }
        if (d[MAX].width < d[PREF].width) {
            d[MAX].width = d[PREF].width;
        }
        if (d[MAX].height < d[PREF].height) {
            d[MAX].height = d[PREF].height;
        }
        return d;
    }

    public static Dimension getSize(Component c, int sizeflag) {
        if (c == null) {
            return new Dimension(0, 0);
        }
        //special case due to swing bug: html labels need to know the current parent layout's size
        if (c instanceof JLabel) {
            View v = (View) ((JLabel) c).getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
            if (v != null) {
                switch (sizeflag) {
                case MIN: {
                    Dimension d = new Dimension(c.getPreferredSize());
                    d.width = 1;
                    return d;
                }
                case PREF: {
                    Dimension d = new Dimension(c.getPreferredSize());
                    return d;
                }
                case MAX: {
                    Dimension d = new Dimension(10240, 10240);
                    d.width = 1;
                    return d;
                }
                }
            }
        }
        //
        switch (sizeflag) {
        case MIN: {
            return new Dimension(c.getMinimumSize());
        }
        case PREF: {
            return new Dimension(c.getPreferredSize());
        }
        case MAX: {
            return new Dimension(c.getMaximumSize());
        }
        }
        return new Dimension(c.getPreferredSize());
    }
}

Related

  1. getPreferredSizeComponent(JComponent component)
  2. getScreenSize()
  3. getScreenSize(Component invoker)
  4. getSize(byte[] data)
  5. getSize(Component c, int sizeflag)
  6. getViewableScreenSize()
  7. getVisibleSizeInViewport(Component c)
  8. getWindowSize(JComponent start)
  9. inset(Component c, int insetSizeHor, int insetSizeVert)