Java Code Examples for com.google.gwt.view.client.SingleSelectionModel#addSelectionChangeHandler()

The following examples show how to use com.google.gwt.view.client.SingleSelectionModel#addSelectionChangeHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TemplateUploadWizard.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the scrollable list of cells each of which serves as a link to a template.
 *
 * @param list an ArrayList of TemplateInfo
 * @return A CellList widget
 */
public CellList<TemplateInfo> makeTemplateSelector(ArrayList<TemplateInfo> list) {
  TemplateCell templateCell = new TemplateCell(list.get(0), templateHostUrl);

  CellList<TemplateInfo> templateCellList = new CellList<TemplateInfo>(templateCell,TemplateInfo.KEY_PROVIDER);
  templateCellList.setPageSize(list.size() + 10);
  templateCellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
  templateCellList.setWidth("250px");
  templateCellList.setHeight("355px");
  templateCellList.setVisible(true);
  templateCellList.getElement().getStyle().setOverflowY(Style.Overflow.SCROLL);

  // Add a selection model to handle user selection.
  final SingleSelectionModel<TemplateInfo> selectionModel =
    new SingleSelectionModel<TemplateInfo>(TemplateInfo.KEY_PROVIDER);
  templateCellList.setSelectionModel(selectionModel);
  selectionModel.setSelected(list.get(0), true);
  final TemplateUploadWizard wizard = this;
  selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
      public void onSelectionChange(SelectionChangeEvent event) {
        TemplateInfo selected = selectionModel.getSelectedObject();
        if (selected != null) {
          selectedTemplateNAME = selected.name;
          TemplateWidget.setTemplate(selected, wizard.getTemplateUrlHost());
        }
      }
    });

  // Set the total row count. This isn't strictly necessary, but it affects
  // paging calculations, so its good habit to keep the row count up to date.
  templateCellList.setRowCount(list.size(), true);

  // Push the data into the widget.
  templateCellList.setRowData(0, list);
  return templateCellList;
}
 
Example 2
Source File: PropertyEditor.java    From core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@SuppressWarnings("unchecked")
public Widget asWidget() {
    VerticalPanel panel = new VerticalPanel();
    panel.addStyleName("fill-layout-width");

    // table
    table = new DefaultCellTable<>(numRows, keyProvider);
    dataProvider = new ListDataProvider<>(keyProvider);
    dataProvider.addDataDisplay(table);
    final SingleSelectionModel<Property> selectionModel = new SingleSelectionModel<>(keyProvider);
    table.setSelectionModel(selectionModel);
    selectionModel.addSelectionChangeHandler(event -> {
        Property selection = selectionModel.getSelectedObject();
        if (selection == null) {
            propertyManager.onDeselect();
        } else {
            propertyManager.onSelect(selection);
        }
    });

    // columns
    Column<Property, String> keyColumn = new TextColumn<Property>() {
        @Override
        public String getValue(Property property) {
            return property.getName();
        }
    };
    keyColumn.setSortable(true);
    Column<Property, String> valueColumn;
    if (inlineEditing) {
        valueColumn = new Column<Property, String>(new DefaultEditTextCell()) { {
                setFieldUpdater((index, property, value) -> getPropertyValue(property).set(value));
            }

            @Override
            public String getValue(Property property) {
                return property.getValue().get(VALUE).asString();
            }
        };
    } else {
        valueColumn = new TextColumn<Property>() {
            @Override
            public String getValue(Property property) {
                return getPropertyValue(property).asString();
            }
        };
    }
    ColumnSortEvent.ListHandler<Property> sortHandler = new ColumnSortEvent.ListHandler<>(dataProvider.getList());
    sortHandler.setComparator(keyColumn, new Comparator<Property>() {
        @Override
        public int compare(Property o1, Property o2) {
            return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
        }
    });
    table.addColumn(keyColumn, Console.CONSTANTS.common_label_key());
    table.addColumn(valueColumn, Console.CONSTANTS.common_label_value());
    table.setColumnWidth(keyColumn, 40, Style.Unit.PCT);
    table.setColumnWidth(valueColumn, 60, Style.Unit.PCT);
    table.addColumnSortHandler(sortHandler);
    table.getColumnSortList().push(keyColumn);

    // tools
    if (!hideTools) {
        ToolStrip tools = new ToolStrip();
        addButton = new ToolButton(addLabel, event -> {
            addDialog.clearValues();
            propertyManager.openAddDialog(addDialog);
        });
        removeButton = new ToolButton(removeLabel, event -> {
            final Property selection = selectionModel.getSelectedObject();
            if (selection != null) {
                Feedback.confirm(Console.MESSAGES.removeProperty(),
                        Console.MESSAGES.removePropertyConfirm(selection.getName()),
                        new Feedback.ConfirmationHandler() {
                            @Override
                            public void onConfirmation(boolean isConfirmed) {
                                if (isConfirmed) {
                                    propertyManager.onRemove(selection);
                                }
                            }
                        });
            }
        });
        AddressTemplate effectiveAddress = operationAddress != null ? operationAddress : propertyManager
                .getAddress();
        if (effectiveAddress != null) {
            addButton.setOperationAddress(effectiveAddress.getTemplate(), propertyManager.getAddOperationName());
            removeButton
                    .setOperationAddress(effectiveAddress.getTemplate(), propertyManager.getRemoveOperationName());
        }
        tools.addToolButtonRight(addButton);
        tools.addToolButtonRight(removeButton);
        panel.add(tools);
    }

    panel.add(table);
    DefaultPager pager = new DefaultPager();
    pager.setDisplay(table);
    panel.add(pager);
    return panel;
}
 
Example 3
Source File: ComboPicker.java    From core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ComboPicker(String cssSuffix) {

        cellList = new CellList<String>(new TextCell()
        {
            @Override
            public void render(Context context, String data, SafeHtmlBuilder sb) {
                String cssName = (context.getIndex() %2 > 0) ? "combobox-item combobox-item-odd" : "combobox-item";

                if(data.equals(displayed.getActual()))
                    cssName+=" combobox-item-selected";

                sb.append(TEMPLATE.item(cssName, data));
            }

        });

        cellList.setPageSize(100);

        final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
        cellList.setSelectionModel(selectionModel);

        selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
            public void onSelectionChange(SelectionChangeEvent event) {
                String selectedValue = selectionModel.getSelectedObject();

                setDisplayedValue(selectedValue);
                popup.hide();

                onSelection(selectedValue);
            }
        });

        final String panelId = "combopicker_popup";
        popup = new PopupPanel(true, true) {

            @Override
            protected void onPreviewNativeEvent(Event.NativePreviewEvent event) {
                if (Event.ONKEYUP == event.getTypeInt()) {
                    if (event.getNativeEvent().getKeyCode() == ESCAPE) {
                        // Dismiss when escape is pressed
                        popup.hide();
                    }
                }
            }

            public void onBrowserEvent(Event event) {
                super.onBrowserEvent(event);
            }
        };

        popup.getElement().setId(panelId);

        popup.setStyleName("default-popup");

        popup.addStyleName("triangle-border");
        popup.addStyleName("top-left");

        popup.setWidget(new ScrollPanel(cellList));

        displayed = new Display("");

        header = new HorizontalPanel();
        header.setStyleName("combobox"+cssSuffix);
        header.add(displayed);

        HTML icon = new HTML("<span style='font-size:12px;cursor:pointer'><i class='icon-chevron-down'></i></span>");
        header.add(icon);

        displayed.getElement().getParentElement().setAttribute("width", "100%");

        icon.getParent().getElement().setAttribute("width", "18");

        //header.getElement().setAttribute("width", "95%");
        header.getElement().setAttribute("cellspacing", "0");
        header.getElement().setAttribute("cellpadding", "0");
        header.getElement().setAttribute("border", "0");


        ClickHandler clickHandler = new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                openPanel();
            }
        };

        displayed.addClickHandler(clickHandler);
        icon.addClickHandler(clickHandler);

    }