Search in sources :

Example 1 with Accordion

use of org.apache.pivot.wtk.Accordion in project pivot by apache.

the class TerraAccordionSkin method keyPressed.

/**
 * Key presses have no effect if the event has already been consumed.<p>
 * CommandModifier + {@link KeyCode#KEYPAD_1 KEYPAD_1} to
 * {@link KeyCode#KEYPAD_9 KEYPAD_9}<br>or CommandModifier +
 * {@link KeyCode#N1 1} to {@link KeyCode#N9 9} Select the (enabled) pane at
 * index 0 to 8 respectively<p> {@link Modifier#ALT ALT} +
 * {@link KeyCode#UP UP} Select the next enabled panel.<br>
 * {@link Modifier#ALT ALT} + {@link KeyCode#DOWN DOWN} Select the previous
 * enabled panel.
 *
 * @see Platform#getCommandModifier()
 */
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = super.keyPressed(component, keyCode, keyLocation);
    if (!consumed) {
        Accordion accordion = (Accordion) getComponent();
        Accordion.PanelSequence panels = accordion.getPanels();
        Keyboard.Modifier commandModifier = Platform.getCommandModifier();
        if (Keyboard.isPressed(commandModifier)) {
            int selectedIndex = -1;
            switch(keyCode) {
                case Keyboard.KeyCode.KEYPAD_1:
                case Keyboard.KeyCode.N1:
                    {
                        selectedIndex = 0;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_2:
                case Keyboard.KeyCode.N2:
                    {
                        selectedIndex = 1;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_3:
                case Keyboard.KeyCode.N3:
                    {
                        selectedIndex = 2;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_4:
                case Keyboard.KeyCode.N4:
                    {
                        selectedIndex = 3;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_5:
                case Keyboard.KeyCode.N5:
                    {
                        selectedIndex = 4;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_6:
                case Keyboard.KeyCode.N6:
                    {
                        selectedIndex = 5;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_7:
                case Keyboard.KeyCode.N7:
                    {
                        selectedIndex = 6;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_8:
                case Keyboard.KeyCode.N8:
                    {
                        selectedIndex = 7;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_9:
                case Keyboard.KeyCode.N9:
                    {
                        selectedIndex = 8;
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            if (selectedIndex >= 0 && selectedIndex < panels.getLength() && panels.get(selectedIndex).isEnabled()) {
                accordion.setSelectedIndex(selectedIndex);
                consumed = true;
            }
        } else if (Keyboard.isPressed(Keyboard.Modifier.ALT)) {
            int n = panels.getLength();
            int selectedIndex = accordion.getSelectedIndex();
            switch(keyCode) {
                case Keyboard.KeyCode.UP:
                    {
                        do {
                            selectedIndex--;
                        } while (selectedIndex >= 0 && !panels.get(selectedIndex).isEnabled());
                        break;
                    }
                case Keyboard.KeyCode.DOWN:
                    {
                        do {
                            selectedIndex++;
                        } while (selectedIndex < n && !panels.get(selectedIndex).isEnabled());
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            if (selectedIndex >= 0 && selectedIndex < n && panels.get(selectedIndex).isEnabled()) {
                accordion.setSelectedIndex(selectedIndex);
                consumed = true;
            }
        }
    }
    return consumed;
}
Also used : Accordion(org.apache.pivot.wtk.Accordion) Modifier(org.apache.pivot.wtk.Keyboard.Modifier) Keyboard(org.apache.pivot.wtk.Keyboard) GradientPaint(java.awt.GradientPaint)

Example 2 with Accordion

use of org.apache.pivot.wtk.Accordion in project pivot by apache.

the class TerraAccordionSkin method getPreferredSize.

@Override
public Dimensions getPreferredSize() {
    Accordion accordion = (Accordion) getComponent();
    int preferredHeight = 0;
    int maxPanelHeaderWidth = 0;
    for (PanelHeader panelHeader : panelHeaders) {
        Dimensions preferredSize = panelHeader.getPreferredSize();
        maxPanelHeaderWidth = Math.max(preferredSize.width, maxPanelHeaderWidth);
        preferredHeight += preferredSize.height - 1;
    }
    int maxPanelWidth = 0;
    int maxPanelHeight = 0;
    for (Component panel : accordion.getPanels()) {
        Dimensions preferredSize = panel.getPreferredSize();
        maxPanelWidth = Math.max(preferredSize.width, maxPanelWidth);
        maxPanelHeight = Math.max(maxPanelHeight, preferredSize.height);
    }
    int preferredWidth = Math.max(maxPanelHeaderWidth, maxPanelWidth + (padding.getWidth() + 2));
    preferredHeight += (maxPanelHeight + padding.getHeight() + 2);
    return new Dimensions(preferredWidth, preferredHeight);
}
Also used : Accordion(org.apache.pivot.wtk.Accordion) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component) GradientPaint(java.awt.GradientPaint)

Example 3 with Accordion

use of org.apache.pivot.wtk.Accordion in project pivot by apache.

the class TerraAccordionSkin method layout.

@Override
public void layout() {
    Accordion accordion = (Accordion) getComponent();
    int width = getWidth();
    int height = getHeight();
    int contentWidth = Math.max(width - (padding.getWidth() + 2), 0);
    // Determine the content height
    int panelHeight = 0;
    int contentHeight = 0;
    if (selectionChangeTransition == null) {
        panelHeight = height;
        for (PanelHeader panelHeader : panelHeaders) {
            panelHeader.setSize(width, panelHeader.getPreferredHeight(width));
            panelHeight -= (panelHeader.getHeight() - 1);
        }
        panelHeight = Math.max(panelHeight - 2, 0);
        contentHeight = Math.max(panelHeight - padding.getHeight(), 0);
    } else {
        panelHeight = selectionChangeTransition.toPanel.getHeight() + padding.getHeight();
    }
    // Lay out the components
    Accordion.PanelSequence panels = accordion.getPanels();
    int panelY = 0;
    for (int i = 0, n = panels.getLength(); i < n; i++) {
        Component panel = panels.get(i);
        PanelHeader panelHeader = panelHeaders.get(i);
        panelHeader.setLocation(0, panelY);
        panelY += (panelHeader.getHeight() - 1);
        if (selectionChangeTransition == null) {
            Component toPanel = accordion.getSelectedPanel();
            if (panel == toPanel) {
                panel.setVisible(true);
                panel.setSize(contentWidth, contentHeight);
                panel.setLocation(padding.left + 1, panelY + padding.top + 1);
                panelY += panelHeight + 1;
            } else {
                panel.setVisible(false);
            }
        } else {
            if (selectionChangeTransition.isRunning()) {
                if (panel == selectionChangeTransition.fromPanel) {
                    panel.setLocation(padding.left + 1, panelY + padding.top + 1);
                    int previousSelectedPanelHeight = Math.round(panelHeight * (1.0f - selectionChangeTransition.getEasedPercentComplete()));
                    previousSelectedPanelClipDecorator.setWidth(contentWidth);
                    previousSelectedPanelClipDecorator.setHeight(previousSelectedPanelHeight);
                    panelY += previousSelectedPanelHeight + 1;
                }
                if (panel == selectionChangeTransition.toPanel) {
                    panel.setLocation(padding.left + 1, panelY + padding.top + 1);
                    int selectedPanelHeight = Math.round(panelHeight * selectionChangeTransition.getEasedPercentComplete());
                    selectedPanelClipDecorator.setWidth(contentWidth);
                    selectedPanelClipDecorator.setHeight(selectedPanelHeight);
                    panelY += selectedPanelHeight;
                }
            } else {
                selectionChangeTransition.toPanel.setSize(selectionChangeTransition.fromPanel.getSize());
                selectionChangeTransition.toPanel.setVisible(true);
            }
        }
    }
}
Also used : Accordion(org.apache.pivot.wtk.Accordion) Component(org.apache.pivot.wtk.Component) GradientPaint(java.awt.GradientPaint)

Example 4 with Accordion

use of org.apache.pivot.wtk.Accordion in project pivot by apache.

the class Accordions method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    accordion = (Accordion) namespace.get("accordion");
    accordion.getAccordionSelectionListeners().add(new AccordionSelectionListener() {

        private int selectedIndex = -1;

        @Override
        public Vote previewSelectedIndexChange(Accordion accordionArgument, int selectedIndexArgument) {
            this.selectedIndex = selectedIndexArgument;
            // transition looks smoother
            if (selectedIndexArgument != -1) {
                int previousSelectedIndex = accordionArgument.getSelectedIndex();
                if (selectedIndexArgument > previousSelectedIndex) {
                    accordionArgument.getPanels().get(selectedIndexArgument).setEnabled(true);
                } else {
                    accordionArgument.getPanels().get(previousSelectedIndex).setEnabled(false);
                }
            }
            return Vote.APPROVE;
        }

        @Override
        public void selectedIndexChangeVetoed(Accordion accordionArgument, Vote reason) {
            if (reason == Vote.DENY && selectedIndex != -1) {
                Component panel = accordionArgument.getPanels().get(selectedIndex);
                panel.setEnabled(!panel.isEnabled());
            }
        }

        @Override
        public void selectedIndexChanged(Accordion accordionArgument, int previousSelection) {
            updateAccordion();
        }
    });
    updateAccordion();
}
Also used : Vote(org.apache.pivot.util.Vote) Accordion(org.apache.pivot.wtk.Accordion) AccordionSelectionListener(org.apache.pivot.wtk.AccordionSelectionListener) Component(org.apache.pivot.wtk.Component)

Example 5 with Accordion

use of org.apache.pivot.wtk.Accordion in project pivot by apache.

the class TerraAccordionSkin method getPreferredWidth.

@Override
public int getPreferredWidth(int height) {
    Accordion accordion = (Accordion) getComponent();
    // The preferred width is the maximum unconstrained preferred width of
    // the headers and the panels, plus border
    int maxPanelHeaderWidth = 0;
    for (PanelHeader panelHeader : panelHeaders) {
        maxPanelHeaderWidth = Math.max(panelHeader.getPreferredWidth(), maxPanelHeaderWidth);
    }
    int maxPanelWidth = 0;
    for (Component panel : accordion.getPanels()) {
        maxPanelWidth = Math.max(panel.getPreferredWidth(), maxPanelWidth);
    }
    int preferredWidth = Math.max(maxPanelHeaderWidth, maxPanelWidth + (padding.getWidth() + 2));
    return preferredWidth;
}
Also used : Accordion(org.apache.pivot.wtk.Accordion) Component(org.apache.pivot.wtk.Component) GradientPaint(java.awt.GradientPaint)

Aggregations

Accordion (org.apache.pivot.wtk.Accordion)7 GradientPaint (java.awt.GradientPaint)5 Component (org.apache.pivot.wtk.Component)5 Vote (org.apache.pivot.util.Vote)1 AccordionSelectionListener (org.apache.pivot.wtk.AccordionSelectionListener)1 Dimensions (org.apache.pivot.wtk.Dimensions)1 Keyboard (org.apache.pivot.wtk.Keyboard)1 Modifier (org.apache.pivot.wtk.Keyboard.Modifier)1