Class SwipeableContainer

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class SwipeableContainer extends Container

SwipeableContainer allows us to side swipe a component and expose underlying configuration within it. This is useful for editing, ranking of elements within a set of components e.g. in the sample code below we use a ranking widget and swiping to expose the elements:

public void showForm() {
  Form hi = new Form("Swipe", new BoxLayout(BoxLayout.Y_AXIS));
  hi.add(createRankWidget("A Game of Thrones", "1996")).
      add(createRankWidget("A Clash Of Kings", "1998")).
      add(createRankWidget("A Storm Of Swords", "2000")).
      add(createRankWidget("A Feast For Crows", "2005")).
      add(createRankWidget("A Dance With Dragons", "2011")).
      add(createRankWidget("The Winds of Winter", "TBD")).
      add(createRankWidget("A Dream of Spring", "TBD"));
  hi.show();
}

public SwipeableContainer createRankWidget(String title, String year) {
    MultiButton button = new MultiButton(title);
    button.setTextLine2(year);
    return new SwipeableContainer(FlowLayout.encloseCenterMiddle(createStarRankSlider()),
            button);
}

private void initStarRankStyle(Style s, Image star) {
    s.setBackgroundType(Style.BACKGROUND_IMAGE_TILE_BOTH);
    s.setBorder(Border.createEmpty());
    s.setBgImage(star);
    s.setBgTransparency(0);
}

private Slider createStarRankSlider() {
    Slider starRank = new Slider();
    starRank.setEditable(true);
    starRank.setMinValue(0);
    starRank.setMaxValue(10);
    Font fnt = Font.createTrueTypeFont("native:MainLight", "native:MainLight").
            derive(Display.getInstance().convertToPixels(5, true), Font.STYLE_PLAIN);
    Style s = new Style(0xffff33, 0, fnt, (byte)0);
    Image fullStar = FontImage.createMaterial(FontImage.MATERIAL_STAR, s).toImage();
    s.setOpacity(100);
    s.setFgColor(0);
    Image emptyStar = FontImage.createMaterial(FontImage.MATERIAL_STAR, s).toImage();
    initStarRankStyle(starRank.getSliderEmptySelectedStyle(), emptyStar);
    initStarRankStyle(starRank.getSliderEmptyUnselectedStyle(), emptyStar);
    initStarRankStyle(starRank.getSliderFullSelectedStyle(), fullStar);
    initStarRankStyle(starRank.getSliderFullUnselectedStyle(), fullStar);
    starRank.setPreferredSize(new Dimension(fullStar.getWidth() * 5, fullStar.getHeight()));
    return starRank;
}
  • Constructor Details

    • SwipeableContainer

      public SwipeableContainer(Component bottomLeft, Component top)

      Simple Constructor

      Parameters
      • bottomLeft: @param bottomLeft the Component below the top, this Component is exposed when dragging the top to the right

      • top: the component on top.

    • SwipeableContainer

      public SwipeableContainer(Component bottomLeft, Component bottomRight, Component top)

      Simple Constructor

      Parameters
      • bottomLeft: @param bottomLeft the Component below the top, this Component is exposed when dragging the top to the right

      • bottomRight: @param bottomRight the Component below the top, this Component is exposed when dragging the top to the Left

      • top: the component on top.

  • Method Details

    • deinitialize

      protected void deinitialize()
      Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
      Overrides:
      deinitialize in class Component
    • initComponent

      protected void initComponent()
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class Component
    • openToRight

      public void openToRight()
      This method will open the top Component to the right if there is a Component to expose on the left.
    • openToLeft

      public void openToLeft()
      This method will open the top Component to the left if there is a Component to expose on the right.
    • close

      public void close()
      Close the top component if it is currently opened.
    • getComponentAt

      public Component getComponentAt(int x, int y)
      Description copied from class: Container

      Returns a Component at coordinate (x, y).

      WARNING: This method may return components that are disabled, or invisible, or that do not respond to pointer events. If you are looking for the top-most component that responds to pointer events, you should use int) as it is guaranteed to return a component with Component#respondsToPointerEvents() true; or null if none is found at the coordinate.

      Parameters
      • x: absolute screen location

      • y: absolute screen location

      Returns

      a Component if found, null otherwise

      See also
      • Component#contains

      • #getResponderAt(int, int)

      Overrides:
      getComponentAt in class Container
    • animate

      public boolean animate()
      Description copied from class: Component

      Allows the animation to reduce "repaint" calls when it returns false. It is called once for every frame. Frames are defined by the com.codename1.ui.Display class.

      Returns

      true if a repaint is desired or false if no repaint is necessary

      Specified by:
      animate in interface Animation
      Overrides:
      animate in class Component
    • isSwipeActivated

      public boolean isSwipeActivated()
      Returns true if swipe is activated
    • setSwipeActivated

      public void setSwipeActivated(boolean swipeActivated)
      disable/enable dragging of the top Component
    • isOpen

      public boolean isOpen()
      Returns true if the top Component is currently opened
    • isOpenedToRight

      public boolean isOpenedToRight()
      Returns true if the top Component is opened to the right side
    • isOpenedToLeft

      public boolean isOpenedToLeft()
      Returns true if the top Component is opened to the left side
    • addSwipeOpenListener

      public void addSwipeOpenListener(ActionListener l)

      Adds a listener to the SwipeableContainer which will cause an event to dispatch once the SwipeableContainer is fully opened

      Parameters
      • l: implementation of the action listener interface
    • removeSwipeOpenListener

      public void removeSwipeOpenListener(ActionListener l)

      Removes the given listener from the SwipeableContainer

      Parameters
      • l: implementation of the action listener interface
    • getPreviouslyOpened

      public SwipeableContainer getPreviouslyOpened()

      returns a previously opened SwipeableContainer that should be automatically closed when starting to open this one. Called as soon as this Swipeable starts opening. One approach is to override this method to return a previously opened SwipeableContainer Can be overridden to return a SwipeableContainer stored outside this container.

      Returns
      Returns:
      an already open SwipeableContainer that will be closed when opening this one, or null if none
    • setPreviouslyOpened

      public void setPreviouslyOpened(SwipeableContainer previouslyOpened)

      set a previously open SwipeableContainer, it will be closed as soon as the user starts swiping this one. Be aware that with a long list of Swipeable containers it may be a better approach to store the previously opened outside the list and simply override getPreviouslyOpened to return it

      Parameters
      • previouslyOpened: @param previouslyOpened an already open SwipeableContainer that will be closed if this one is opened