Search in sources :

Example 11 with Actions

use of com.codename1.rad.ui.Actions in project CodenameOne by codenameone.

the class RADEntityListAddRemoveInvalidateSample method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    // An entity for the rows our our list view.
    class Person extends Entity {
    }
    entityTypeBuilder(Person.class).string(Thing.name).factory(cls -> {
        return new Person();
    }).build();
    // A remove row action that will be added to each row
    ActionNode removeRow = UI.action(UI.icon(FontImage.MATERIAL_DELETE));
    // An internal list we will use to store the rows of the entitylist
    ArrayList internalList = new ArrayList();
    EntityList profileList = new EntityList() {

        /**
         * Override createInternalList() so that we can use our own data structure
         * for storing this list.  This is contrived to allow us to test the
         * invalidate() method.
         */
        @Override
        protected List createInternalList() {
            return internalList;
        }
    };
    // A list node to wrap our action and pass it to our view
    ListNode node = new ListNode(// of the list.
    UI.actions(ProfileListView.ACCOUNT_LIST_ROW_ACTIONS, removeRow));
    // A ProfileListView to render the list.
    // See https://shannah.github.io/CodeRAD/javadoc/com/codename1/rad/ui/entityviews/ProfileListView.html
    ProfileListView listView = new ProfileListView(profileList, node, 10);
    listView.setScrollableY(true);
    // Create a controller for the ProfileListView so that we can handle actions fired by the view.
    // Normally we'd do this in the FormController but since this sample doesn't have one
    // we create a ViewController for the ProfileListView directly.
    ViewController controller = new ViewController(null);
    controller.setView(listView);
    // Button to add rows to the list
    Button addRow = new Button(FontImage.MATERIAL_ADD);
    // Button to clear the list
    Button clear = new Button("Clear");
    addRow.addActionListener(evt -> {
        // "Add" button clicked.
        // Create new person and add to the list
        Person p = new Person();
        p.set(Thing.name, "Row " + profileList.size());
        profileList.add(p);
    // This will trigger an EntityAddedEvent which will allow
    // the ProfileListView to synchronize
    });
    clear.addActionListener(evt -> {
        // "Clear" button clicked
        // We could have called profileList.clear()
        // but this would send EntityRemoved events for each row removed
        // which is inefficient.  Instead we'll clear the elements in
        // the internal list directly, and then call invalidate()
        // so that the ProfileListView knows to resynchronize its state.
        internalList.clear();
        profileList.invalidate();
    });
    controller.addActionListener(removeRow, evt -> {
        // The "Remove" button was clicked on a row.
        profileList.remove(evt.getEntity());
    });
    Form hi = new Form("Hi World", new BorderLayout());
    hi.add(NORTH, GridLayout.encloseIn(2, clear, addRow));
    hi.add(CENTER, listView);
    hi.show();
}
Also used : Toolbar(com.codename1.ui.Toolbar) BoxLayout(com.codename1.ui.layouts.BoxLayout) EntityTypeBuilder.entityTypeBuilder(com.codename1.rad.models.EntityTypeBuilder.entityTypeBuilder) Form(com.codename1.ui.Form) NetworkEvent(com.codename1.io.NetworkEvent) ArrayList(java.util.ArrayList) ListNode(com.codename1.rad.nodes.ListNode) GridLayout(com.codename1.ui.layouts.GridLayout) Display(com.codename1.ui.Display) FontImage(com.codename1.ui.FontImage) Label(com.codename1.ui.Label) Button(com.codename1.ui.Button) CN(com.codename1.ui.CN) ViewController(com.codename1.rad.controllers.ViewController) UI(com.codename1.rad.ui.UI) Entity(com.codename1.rad.models.Entity) Resources(com.codename1.ui.util.Resources) EntityList(com.codename1.rad.models.EntityList) IOException(java.io.IOException) ActionNode(com.codename1.rad.nodes.ActionNode) Log(com.codename1.io.Log) BorderLayout(com.codename1.ui.layouts.BorderLayout) ProfileListView(com.codename1.rad.ui.entityviews.ProfileListView) UIManager(com.codename1.ui.plaf.UIManager) List(java.util.List) Dialog(com.codename1.ui.Dialog) Thing(com.codename1.rad.schemas.Thing) Entity(com.codename1.rad.models.Entity) BorderLayout(com.codename1.ui.layouts.BorderLayout) ViewController(com.codename1.rad.controllers.ViewController) Button(com.codename1.ui.Button) Form(com.codename1.ui.Form) ActionNode(com.codename1.rad.nodes.ActionNode) ArrayList(java.util.ArrayList) EntityList(com.codename1.rad.models.EntityList) ProfileListView(com.codename1.rad.ui.entityviews.ProfileListView) ListNode(com.codename1.rad.nodes.ListNode)

Example 12 with Actions

use of com.codename1.rad.ui.Actions in project CodenameOne by codenameone.

the class AndroidImplementation method getInstalledPushActionCategories.

/**
 * Retrieves the app's available push action categories from the XML file in which they
 * should have been installed on the first load.
 * @param context
 * @return
 * @throws IOException
 */
private static PushActionCategory[] getInstalledPushActionCategories(Context context) throws IOException {
    // NOTE:  This method may be called from the PushReceiver when the app isn't running so we can't access
    // the main activity context, display properties, or any CN1 stuff.  Just native android
    File categoriesFile = new File(context.getFilesDir().getAbsolutePath() + "/" + FILE_NAME_NOTIFICATION_CATEGORIES);
    if (!categoriesFile.exists()) {
        return new PushActionCategory[0];
    }
    javax.xml.parsers.DocumentBuilderFactory docFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    javax.xml.parsers.DocumentBuilder docBuilder;
    try {
        docBuilder = docFactory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        throw new IOException("Faield to create document builder for creating notification categories XML document", ex);
    }
    org.w3c.dom.Document doc;
    try {
        doc = docBuilder.parse(context.openFileInput(FILE_NAME_NOTIFICATION_CATEGORIES));
    } catch (SAXException ex) {
        Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        throw new IOException("Failed to parse instaled push action categories", ex);
    }
    org.w3c.dom.Element root = doc.getDocumentElement();
    java.util.List<PushActionCategory> out = new ArrayList<PushActionCategory>();
    org.w3c.dom.NodeList l = root.getElementsByTagName("category");
    int len = l.getLength();
    for (int i = 0; i < len; i++) {
        org.w3c.dom.Element el = (org.w3c.dom.Element) l.item(i);
        java.util.List<PushAction> actions = new ArrayList<PushAction>();
        org.w3c.dom.NodeList al = el.getElementsByTagName("action");
        int alen = al.getLength();
        for (int j = 0; j < alen; j++) {
            org.w3c.dom.Element actionEl = (org.w3c.dom.Element) al.item(j);
            String textInputPlaceholder = actionEl.hasAttribute("textInputPlaceholder") ? actionEl.getAttribute("textInputPlaceholder") : null;
            String textInputButtonText = actionEl.hasAttribute("textInputButtonText") ? actionEl.getAttribute("textInputButtonText") : null;
            PushAction action = new PushAction(actionEl.getAttribute("id"), actionEl.getAttribute("title"), actionEl.getAttribute("icon"), textInputPlaceholder, textInputButtonText);
            actions.add(action);
        }
        PushActionCategory cat = new PushActionCategory((String) el.getAttribute("id"), actions.toArray(new PushAction[actions.size()]));
        out.add(cat);
    }
    return out.toArray(new PushActionCategory[out.size()]);
}
Also used : Element(android.renderscript.Element) ArrayList(java.util.ArrayList) PushActionCategory(com.codename1.push.PushActionCategory) IOException(java.io.IOException) PushAction(com.codename1.push.PushAction) Paint(android.graphics.Paint) SAXException(org.xml.sax.SAXException) java.util(java.util) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 13 with Actions

use of com.codename1.rad.ui.Actions in project CodeRAD by shannah.

the class Controller method extendAction.

/**
 * Extends an existing action from one of the parent controllers, and registers it as an action
 * on this controller.
 * @param category The category to register the action to.
 * @param overwriteAttributes Whether to overwrite existing attributes of the action.  If false, attributes
 *                            provided will be ignored when extending actions that already have those attributes
 *                            defined.
 * @param attributes Attributes to add to the action.
 * @return The action that was added.
 * @since 2.0
 */
public ActionNode extendAction(ActionNode.Category category, boolean overwriteAttributes, Attribute... attributes) {
    ActionNode action = getInheritedAction(category);
    if (action == null) {
        action = UI.action(attributes);
    } else {
        action = (ActionNode) action.createProxy(action.getParent());
        action.setAttributes(overwriteAttributes, attributes);
    }
    addActions(category, action);
    return action;
}
Also used : ActionNode(com.codename1.rad.nodes.ActionNode)

Example 14 with Actions

use of com.codename1.rad.ui.Actions in project CodeRAD by shannah.

the class DefaultEntityListCellRenderer method makeSwipeable.

private EntityView makeSwipeable(Entity entity, ViewNode node, Component view) {
    // Check for swipeable container
    SwipeContainer swipe = (SwipeContainer) node.findAttribute(SwipeContainer.class);
    if (swipe != null) {
        EntityView leftCnt = null;
        EntityView rightCnt = null;
        ViewNode leftNode = swipe.getLeft();
        if (leftNode != null) {
            leftCnt = leftNode.createView(entity);
        }
        ViewNode rightNode = swipe.getRight();
        if (rightNode != null) {
            rightCnt = rightNode.createView(entity);
        }
        SwipeableContainer swipeWrapper = new SwipeableContainer((Component) leftCnt, (Component) rightCnt, view);
        return new WrapperEntityView(swipeWrapper, entity, node);
    }
    ActionNode deleteAction = node.getInheritedAction(ActionCategories.LIST_REMOVE_ACTION);
    Actions leftSwipeActions = node.getActions(ActionCategories.LEFT_SWIPE_MENU);
    if (deleteAction != null) {
        leftSwipeActions.add(deleteAction);
    }
    Actions rightSwipeActions = node.getActions(ActionCategories.RIGHT_SWIPE_MENU);
    if (!leftSwipeActions.isEmpty() || !rightSwipeActions.isEmpty()) {
        Container leftCnt = null;
        Container rightCnt = null;
        if (!leftSwipeActions.isEmpty()) {
            leftCnt = new Container(new GridLayout(leftSwipeActions.size()));
            for (ActionNode action : leftSwipeActions) {
                leftCnt.add(action.getViewFactory().createActionView(entity, action));
            }
        }
        if (!rightSwipeActions.isEmpty()) {
            rightCnt = new Container(new GridLayout(rightSwipeActions.size()));
            for (ActionNode action : rightSwipeActions) {
                rightCnt.add(action.getViewFactory().createActionView(entity, action));
            }
        }
        SwipeableContainer swipeWrapper = new SwipeableContainer((Component) leftCnt, (Component) rightCnt, view);
        return new WrapperEntityView(swipeWrapper, entity, node);
    } else {
    // System.out.println("Swipe actions not present");
    }
    return (EntityView) view;
}
Also used : WrapperEntityView(com.codename1.rad.ui.entityviews.WrapperEntityView) SwipeContainer(com.codename1.rad.nodes.SwipeContainer) SwipeableContainer(com.codename1.ui.SwipeableContainer) Container(com.codename1.ui.Container) GridLayout(com.codename1.ui.layouts.GridLayout) WrapperEntityView(com.codename1.rad.ui.entityviews.WrapperEntityView) MultiButtonEntityView(com.codename1.rad.ui.entityviews.MultiButtonEntityView) ActionNode(com.codename1.rad.nodes.ActionNode) ViewNode(com.codename1.rad.nodes.ViewNode) SwipeableContainer(com.codename1.ui.SwipeableContainer) SwipeContainer(com.codename1.rad.nodes.SwipeContainer)

Example 15 with Actions

use of com.codename1.rad.ui.Actions in project CodeRAD by shannah.

the class DefaultEntityViewFactory method makeSwipeable.

private EntityView makeSwipeable(Entity entity, ViewNode node, Component view) {
    // Check for swipeable container
    SwipeContainer swipe = (SwipeContainer) node.findAttribute(SwipeContainer.class);
    if (swipe != null) {
        EntityView leftCnt = null;
        EntityView rightCnt = null;
        ViewNode leftNode = swipe.getLeft();
        if (leftNode != null) {
            leftCnt = leftNode.createView(entity, this);
        }
        ViewNode rightNode = swipe.getRight();
        if (rightNode != null) {
            rightCnt = rightNode.createView(entity, this);
        }
        SwipeableContainer swipeWrapper = new SwipeableContainer((Component) leftCnt, (Component) rightCnt, view);
        return new WrapperEntityView(swipeWrapper, entity, node);
    }
    Actions leftSwipeActions = node.getActions(ActionCategories.LEFT_SWIPE_MENU);
    Actions rightSwipeActions = node.getActions(ActionCategories.RIGHT_SWIPE_MENU);
    if (!leftSwipeActions.isEmpty() || !rightSwipeActions.isEmpty()) {
        Container leftCnt = null;
        Container rightCnt = null;
        if (!leftSwipeActions.isEmpty()) {
            leftCnt = new Container(BoxLayout.y());
            NodeUtilFunctions.buildActionsBar(node, leftCnt, entity, null, leftSwipeActions, null);
        }
        if (!rightSwipeActions.isEmpty()) {
            rightCnt = new Container(BoxLayout.y());
            NodeUtilFunctions.buildActionsBar(node, leftCnt, entity, rightSwipeActions, null, null);
        }
        SwipeableContainer swipeWrapper = new SwipeableContainer((Component) leftCnt, (Component) rightCnt, view);
        return new WrapperEntityView(swipeWrapper, entity, node);
    }
    return (EntityView) view;
}
Also used : WrapperEntityView(com.codename1.rad.ui.entityviews.WrapperEntityView) Container(com.codename1.ui.Container) SwipeContainer(com.codename1.rad.nodes.SwipeContainer) SwipeableContainer(com.codename1.ui.SwipeableContainer) WrapperEntityView(com.codename1.rad.ui.entityviews.WrapperEntityView) MultiButtonEntityView(com.codename1.rad.ui.entityviews.MultiButtonEntityView) ViewNode(com.codename1.rad.nodes.ViewNode) SwipeableContainer(com.codename1.ui.SwipeableContainer) SwipeContainer(com.codename1.rad.nodes.SwipeContainer)

Aggregations

ActionNode (com.codename1.rad.nodes.ActionNode)6 IOException (java.io.IOException)5 PushAction (com.codename1.push.PushAction)4 PushActionCategory (com.codename1.push.PushActionCategory)4 ViewNode (com.codename1.rad.nodes.ViewNode)4 Container (com.codename1.ui.Container)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 GridLayout (com.codename1.ui.layouts.GridLayout)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ParseException (java.text.ParseException)3 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)2 Paint (android.graphics.Paint)2 RemoteException (android.os.RemoteException)2 Element (android.renderscript.Element)2 Log (com.codename1.io.Log)2 NetworkEvent (com.codename1.io.NetworkEvent)2 MediaException (com.codename1.media.AsyncMedia.MediaException)2 ViewController (com.codename1.rad.controllers.ViewController)2 Entity (com.codename1.rad.models.Entity)2 EntityList (com.codename1.rad.models.EntityList)2