Search in sources :

Example 6 with Editable

use of com.codename1.rad.models.Property.Editable in project CodenameOne by codenameone.

the class Table method createCell.

/**
 * Creates a cell based on the given value
 *
 * @param value the new value object
 * @param row row number, -1 for the header rows
 * @param column column number
 * @param editable true if the cell is editable
 * @return cell component instance
 */
protected Component createCell(Object value, int row, final int column, boolean editable) {
    if (row == -1) {
        Button header = new Button((String) value, getUIID() + "Header");
        header.getAllStyles().setAlignment(titleAlignment);
        header.setTextPosition(LEFT);
        if (isSortSupported()) {
            header.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    Comparator cmp = createColumnSortComparator(column);
                    if (cmp == null) {
                        return;
                    }
                    if (column == sortedColumn) {
                        ascending = !ascending;
                    } else {
                        sortedColumn = column;
                        ascending = false;
                    }
                    if (model instanceof SortableTableModel) {
                        model = ((SortableTableModel) model).getUnderlying();
                    }
                    setModel(new SortableTableModel(sortedColumn, ascending, model, cmp));
                }
            });
            if (sortedColumn == column) {
                if (ascending) {
                    FontImage.setMaterialIcon(header, FontImage.MATERIAL_ARROW_DROP_UP);
                } else {
                    FontImage.setMaterialIcon(header, FontImage.MATERIAL_ARROW_DROP_DOWN);
                }
            }
        }
        return header;
    }
    int constraint = TextArea.ANY;
    Constraint validation = null;
    if (isAbstractTableModel()) {
        Class type = ((AbstractTableModel) model).getCellType(row, column);
        if (type == Boolean.class) {
            CheckBox cell = new CheckBox();
            cell.setSelected(Util.toBooleanValue(value));
            cell.setUIID(getUIID() + "Cell");
            cell.setEnabled(editable);
            return cell;
        }
        if (editable && (type == null || type == String.class)) {
            String[] multiChoice = ((AbstractTableModel) model).getMultipleChoiceOptions(row, column);
            if (multiChoice != null) {
                Picker cell = new Picker();
                cell.setStrings(multiChoice);
                if (value != null) {
                    cell.setSelectedString((String) value);
                }
                cell.setUIID(getUIID() + "Cell");
                return cell;
            }
        }
        if (editable && type == Date.class) {
            Picker cell = new Picker();
            cell.setType(Display.PICKER_TYPE_DATE);
            if (value != null) {
                cell.setDate((Date) value);
            }
            cell.setUIID(getUIID() + "Cell");
            return cell;
        }
        if (type == Integer.class || type == Long.class || type == Short.class || type == Byte.class) {
            constraint = TextArea.NUMERIC;
        } else {
            if (type == Float.class || type == Double.class) {
                constraint = TextArea.DECIMAL;
            }
        }
        if (((AbstractTableModel) model).getValidator() != null) {
            validation = ((AbstractTableModel) model).getValidationConstraint(row, column);
        }
    }
    if (editable) {
        TextField cell = new TextField(value == null ? "" : "" + value, -1);
        cell.setConstraint(constraint);
        cell.setLeftAndRightEditingTrigger(false);
        cell.setUIID(getUIID() + "Cell");
        if (validation != null) {
            Validator v = ((AbstractTableModel) model).getValidator();
            v.addConstraint(cell, validation);
        }
        return cell;
    }
    Label cell = new Label(value == null ? "" : "" + value);
    cell.setUIID(getUIID() + "Cell");
    cell.getUnselectedStyle().setAlignment(cellAlignment);
    cell.getSelectedStyle().setAlignment(cellAlignment);
    cell.setFocusable(true);
    return cell;
}
Also used : Constraint(com.codename1.ui.validation.Constraint) ActionEvent(com.codename1.ui.events.ActionEvent) Label(com.codename1.ui.Label) Constraint(com.codename1.ui.validation.Constraint) Date(java.util.Date) Comparator(java.util.Comparator) ActionListener(com.codename1.ui.events.ActionListener) Button(com.codename1.ui.Button) CheckBox(com.codename1.ui.CheckBox) Picker(com.codename1.ui.spinner.Picker) TextField(com.codename1.ui.TextField) Validator(com.codename1.ui.validation.Validator)

Example 7 with Editable

use of com.codename1.rad.models.Property.Editable in project CodenameOne by codenameone.

the class CodenameOneInputConnection method getEditable.

public Editable getEditable() {
    if (Display.isInitialized() && Display.getInstance().getCurrent() != null) {
        Component txtCmp = Display.getInstance().getCurrent().getFocused();
        if (txtCmp != null && txtCmp instanceof TextField) {
            TextField t = (TextField) txtCmp;
            String textFieldText = t.getText();
            edit.clear();
            edit.append(textFieldText);
            return edit;
        }
    }
    return super.getEditable();
}
Also used : TextField(com.codename1.ui.TextField) Component(com.codename1.ui.Component)

Example 8 with Editable

use of com.codename1.rad.models.Property.Editable in project CodeRAD by shannah.

the class ButtonListPropertyView method updateMultiSelectionModel.

private void updateMultiSelectionModel() {
    MultipleSelectionListModel model = getComponent().getMultiListModel();
    Entity e = getPropertySelector().getLeafEntity();
    Property p = getPropertySelector().getLeafProperty();
    Object val = p.getValue(e.getEntity());
    int len = model.getSize();
    if (getPropertySelector().isEmpty()) {
        if (model.getSelectedIndices().length > 0) {
            model.setSelectedIndices(new int[0]);
        }
    } else {
        List<Integer> selectedIndices = new ArrayList<>();
        if (p.getContentType().isEntityList()) {
            // Property contains an entity list
            List selectedObjects = new ArrayList();
            List<String> selectedIds = new ArrayList<>();
            boolean useIds = true;
            EntityList el = e.getEntity().getEntityList(p);
            for (Object obj : el) {
                selectedObjects.add(obj);
                if (obj instanceof Entity) {
                    String id = ((Entity) obj).getEntity().getText(Thing.identifier);
                    if (id == null) {
                        useIds = false;
                        break;
                    }
                    selectedIds.add(id);
                } else {
                    useIds = false;
                }
            }
            if (useIds) {
                // We will useIds to match rows in options list with rows in entity list
                for (int i = 0; i < len; i++) {
                    Object rowVal = model.getItemAt(i);
                    if (rowVal instanceof Entity) {
                        Entity rowEnt = (Entity) rowVal;
                        String rowId = rowEnt.getEntity().getText(Thing.identifier);
                        if (rowId == null) {
                            throw new IllegalStateException("Attempt to use identifiers for matching items in ButtonListPropertyView, but row item " + rowEnt + " has no identifier.  Property: " + p + " in entity " + e);
                        }
                        if (selectedIds.contains(rowId)) {
                            selectedIndices.add(i);
                        }
                    } else {
                        throw new IllegalStateException("Options for field should all be entities. Property " + p + " entity " + e);
                    }
                }
            } else {
                // Not using IDS.  We will use direct matching.
                for (int i = 0; i < len; i++) {
                    if (el.contains(model.getItemAt(i))) {
                        selectedIndices.add(i);
                    }
                }
            }
        } else if (Collection.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
            // It's a collection
            for (int i = 0; i < len; i++) {
                if (((Collection) val).contains(model.getItemAt(i))) {
                    selectedIndices.add(i);
                }
            }
        } else {
            throw new IllegalStateException("Property " + p + " must contain either EntityList or Collection in order to be editable by ButtonListPropertyView with a multi-selection options model.");
        }
        java.util.Collections.sort(selectedIndices, (i1, i2) -> {
            return i1 - i2;
        });
        List<Integer> existingSelectedIndices = new ArrayList<>();
        for (int index : model.getSelectedIndices()) {
            existingSelectedIndices.add(index);
        }
        java.util.Collections.sort(existingSelectedIndices, (i1, i2) -> {
            return i1 - i2;
        });
        if (!Objects.deepEquals(selectedIndices, existingSelectedIndices)) {
            int size0 = selectedIndices.size();
            int[] selectedIndicesArr = new int[size0];
            for (int i = 0; i < size0; i++) {
                selectedIndicesArr[i] = selectedIndices.get(i);
            }
            model.setSelectedIndices(selectedIndicesArr);
        }
    }
}
Also used : Entity(com.codename1.rad.models.Entity) EntityList(com.codename1.rad.models.EntityList) MultipleSelectionListModel(com.codename1.ui.list.MultipleSelectionListModel) ButtonList(com.codename1.components.ButtonList) EntityList(com.codename1.rad.models.EntityList) Property(com.codename1.rad.models.Property)

Example 9 with Editable

use of com.codename1.rad.models.Property.Editable in project CodeRAD by shannah.

the class EntityListTableCellRenderer method getTableCellRendererComponent.

@Override
public Component getTableCellRendererComponent(Table table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    EntityListTableModel model = (EntityListTableModel) table.getModel();
    FieldNode field = model.getColumnField(column);
    FieldNode fieldCopy = field.copy();
    fieldCopy.setAttributes(new Editable(false));
    Entity entity = model.getEntity(row);
    if (entity == null) {
        if (parent != null) {
            return parent.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        } else {
            return new com.codename1.ui.Label();
        }
    }
    return viewFactory.createPropertyView(entity, fieldCopy);
}
Also used : Entity(com.codename1.rad.models.Entity) FieldNode(com.codename1.rad.nodes.FieldNode) Editable(com.codename1.rad.models.Property.Editable)

Aggregations

Component (com.codename1.ui.Component)4 Button (com.codename1.ui.Button)3 Entity (com.codename1.rad.models.Entity)2 TextField (com.codename1.ui.TextField)2 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 TableLayout (com.codename1.ui.table.TableLayout)2 ButtonList (com.codename1.components.ButtonList)1 EntityList (com.codename1.rad.models.EntityList)1 Property (com.codename1.rad.models.Property)1 Editable (com.codename1.rad.models.Property.Editable)1 Label (com.codename1.rad.models.Property.Label)1 FieldNode (com.codename1.rad.nodes.FieldNode)1 EntityEditor (com.codename1.rad.ui.EntityEditor)1 DescriptionStyleAttribute (com.codename1.rad.ui.EntityEditor.DescriptionStyleAttribute)1 LabelStyleAttribute (com.codename1.rad.ui.EntityEditor.LabelStyleAttribute)1 UI (com.codename1.rad.ui.UI)1 CheckBox (com.codename1.ui.CheckBox)1 Container (com.codename1.ui.Container)1 Form (com.codename1.ui.Form)1 Label (com.codename1.ui.Label)1