Как создать вертикальную javax.swing.ComponentOrientation для BoxLayout?

1

Я пишу альтернативу BoxLayout, поэтому мне нужно проверить PAGE_AXIS и LINE_AXIS. Для этого требуется создать значение ComponentOrientation с выключенным HORIZ_BIT. Кажется, нет такого животного.

Как создать компонентную ориентацию для вертикальной компоновки вместо горизонтальной? (И вспомогательный вопрос: был ли BoxLayout тщательно протестирован?)

Теги:
swing
vertical-alignment
internationalization
boxlayout

1 ответ

0

Моя первая попытка решить проблему с помощью сериализации. Это был огромный перебор. Задача может быть выполнена с помощью рефлексии, которая позволяет звонить частным конструкторам.

// from ComponentOrientation.java
private static final int HORIZ_BIT = 2;
private static final int LTR_BIT = 4;

/**
 * Generate the full gamut of ComponentOrientation values.
 *
 * ComponentOrientation mentions various languages that write
 * vertically, but offers no way to construct vertical orientations.
 * The only un-deprecated way to get a ComponentOrientation
 * value is by referring to the Locale, but ComponentOrientation
 * understands only a few locales, none of them with vertical text.
 *
 * See also http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4418738
 * which describes the rationale behind lobotomizing Locale handling
 * in ComponentOrientation.
 *
 * An inability to generate vertical orientations stymies Blox
 * which relies on the orientation to resolve PAGE_AXIS and LINE_AXIS.
 *
 * @param ltr Should text lines be left-to-right? Otherwise right to left.
 * 
 * @param hor Should text lines be horizontal? Otherwise vertical.
 * 
 * @return A ComponentOrientation value with the specified values for
 *      left-to-right-ness and verticality. If an exception occurs,
 *      a default value of LEFT_TO_RIGHT is returned.
 */
public static ComponentOrientation componentOrientationFactory(
            boolean ltr, boolean hor) {
    try {
        Class<ComponentOrientation> coClass = ComponentOrientation.class;
        Constructor<ComponentOrientation> conint
                = coClass.getDeclaredConstructor(int.class);
        conint.setAccessible(true);
        int dir = (ltr ? LTR_BIT : 0) + (hor ? HORIZ_BIT : 0);
        return conint.newInstance(dir);
    } catch (NoSuchMethodException | InstantiationException 
            | IllegalAccessException | IllegalArgumentException 
            | InvocationTargetException | NullPointerException ex) {
        return ComponentOrientation.LEFT_TO_RIGHT;
    }
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню