Example usage for com.vaadin.ui GridLayout addComponent

List of usage examples for com.vaadin.ui GridLayout addComponent

Introduction

In this page you can find the example usage for com.vaadin.ui GridLayout addComponent.

Prototype

public void addComponent(Component component, int column, int row)
        throws OverlapsException, OutOfBoundsException 

Source Link

Document

Adds the component to the grid in cells column1,row1 (NortWest corner of the area.) End coordinates (SouthEast corner of the area) are the same as column1,row1.

Usage

From source file:org.activiti.administrator.AdminApp.java

License:Apache License

/**
 * Switch view//from  w  w  w .j a  v  a  2  s.  c om
 * 
 * @param name
 *          the name of the view class
 * @param view
 *          the view to switch to
 */
public void switchView(String name, Layout view) {

    // Add view to main layout
    mainLayout.addComponent(view, Consts.CONTENT);

    // Add logout button if user is authenticated
    if ((getUser() != null) && !getUser().toString().isEmpty()) {

        // Create logout grid with user icon, user id and logout button
        GridLayout logoutGrid = new GridLayout(3, 1);
        logoutGrid.setStyleName("logout");

        // Add user icon
        Embedded userIcon = new Embedded(null, new ThemeResource("img/user-icon.png"));
        userIcon.setType(Embedded.TYPE_IMAGE);
        userIcon.addStyleName("icon");

        // Add user id
        Label userLabel = new Label(getUser().toString());
        userLabel.addStyleName("user");

        // Add logout button
        logout.setStyleName(Reindeer.BUTTON_LINK);
        logout.addStyleName("logout");
        logout.setIcon(new ThemeResource("img/divider-white.png"));

        // Add to logout grid
        logoutGrid.addComponent(userIcon, 0, 0);
        logoutGrid.addComponent(userLabel, 1, 0);
        logoutGrid.addComponent(logout, 2, 0);

        // Add logout grid to header
        mainLayout.addComponent(logoutGrid, Consts.LOGOUT);

    } else {

        // Remove logout button
        mainLayout.removeComponent(Consts.LOGOUT);

    }

    // Switch to new view
    viewManager.switchScreen(name, mainLayout);

}

From source file:org.activiti.administrator.ui.UserEditForm.java

License:Apache License

public UserEditForm(AdminApp application) {

    // Set application reference
    this.app = application;

    // Enable buffering so that commit() must be called for the form.
    setWriteThrough(false);//from   ww w .j a va2s . c o  m

    // Set the form to act immediately on user input.
    setImmediate(true);

    // Set form size
    setSizeFull();

    // Setup footer layout
    HorizontalLayout footer = new HorizontalLayout();
    footer.setSpacing(true);
    footer.setWidth("100%");
    footer.setVisible(true);

    // Add footer
    setFooter(footer);

    // Init buttons
    save = new Button(app.getMessage(Messages.Save), (ClickListener) this);
    close = new Button(app.getMessage(Messages.Close), (ClickListener) this);
    reset = new Button(app.getMessage(Messages.Reset), this, "discard");

    // Set button grid
    GridLayout grid = new GridLayout(3, 1);
    grid.addComponent(save, 0, 0);
    grid.addComponent(reset, 1, 0);
    grid.addComponent(close, 2, 0);
    grid.setSpacing(true);

    // Add grid to footer
    footer.addComponent(grid);

    // Right align buttons in footer
    footer.setComponentAlignment(grid, Alignment.BOTTOM_RIGHT);

    // Get all available groups
    groups = new TwinColSelect(app.getMessage(Messages.Groups), app.getAdminService().getGroups());

    // Set column headers
    groups.setLeftColumnCaption(app.getMessage(Messages.AvailableGroups));
    groups.setRightColumnCaption(app.getMessage(Messages.MemberOfGroups));

    // Propagate changes directly
    groups.setImmediate(true);

    // Max width
    groups.setWidth("100%");

    // Field factory for over riding how fields are created
    setFormFieldFactory(new DefaultFieldFactory() {

        private static final long serialVersionUID = 1L;

        @Override
        public Field createField(Item item, Object propertyId, Component uiContext) {

            Field field = super.createField(item, propertyId, uiContext);

            if (propertyId.equals("id")) {
                TextField tf = (TextField) field;

                // Do not display "null" to the user when the field is empty
                tf.setNullRepresentation("");

                // Set as read-only. Changing the id will create a new user.
                tf.setReadOnly(true);

                // Set as required field
                // tf.setRequired(true);

                // Set validator example, should not be restricted in the
                // admin ui
                // tf.addValidator(new
                // RegexpValidator("^[a-zA-Z0-9_-]{4,20}",
                // app.getMessage(Messages.InvalidUsername)));

                // Set error message
                tf.setRequiredError(app.getMessage(Messages.UsernameIsMissing));

            } else if (propertyId.equals("password")) {
                TextField tf = (TextField) field;

                // Do not display "null" to the user when the field is empty
                tf.setNullRepresentation("");

                // Set as required field
                tf.setRequired(true);

                // Set as secret (todo: use password field instead of text
                // field)
                tf.setSecret(true);

                // Set error message
                tf.setRequiredError(app.getMessage(Messages.PasswordIsMissing));

            } else if (propertyId.equals("email")) {
                TextField tf = (TextField) field;

                // Do not display "null" to the user when the field is empty
                tf.setNullRepresentation("");

                // Set as required field, should not be required by default
                // in the admin ui
                // tf.setRequired(true);

                // Set error message
                // tf.setRequiredError(application.getMessage(Messages.EmailIsMissing));

                /* Add a validator for email and make it required */
                field.addValidator(new EmailValidator(app.getMessage(Messages.EmailFormatError)));

            } else if (propertyId.equals("firstName")) {
                TextField tf = (TextField) field;

                // Do not display "null" to the user when the field is empty
                tf.setNullRepresentation("");

            } else if (propertyId.equals("lastName")) {
                TextField tf = (TextField) field;

                // Do not display "null" to the user when the field is empty
                tf.setNullRepresentation("");

            }

            field.setWidth("100%");
            return field;
        }
    });

}

From source file:org.activiti.explorer.ui.reports.ChartGenerator.java

License:Apache License

protected static Component createChart(JsonNode dataNode, String[] names, Number[] values) {
    String type = dataNode.get("type").textValue();

    JsonNode xAxisNode = dataNode.get("xaxis");
    String xAxis = null;/*ww w  .  j  ava  2s.c  om*/
    if (xAxisNode != null) {
        xAxis = xAxisNode.textValue();
    }

    JsonNode yAxisNode = dataNode.get("yaxis");
    String yAxis = null;
    if (yAxisNode != null) {
        yAxis = yAxisNode.textValue();
    }

    Component chart = null;
    if (CHART_TYPE_BAR_CHART.equals(type)) {

        DataSeries dataSeries = new DataSeries().add((Object[]) values);
        SeriesDefaults seriesDefaults = new SeriesDefaults().setRenderer(SeriesRenderers.BAR);
        Axes axes = new Axes().addAxis(
                new XYaxis().setRenderer(AxisRenderers.CATEGORY).setTicks(new Ticks().add((Object[]) names)));
        Highlighter highlighter = new Highlighter().setShow(false);

        Options options = new Options().setSeriesDefaults(seriesDefaults).setAxes(axes)
                .setHighlighter(highlighter);
        options.setAnimate(true);
        options.setAnimateReplot(true);

        chart = new DCharts().setDataSeries(dataSeries).setOptions(options);

    } else if (CHART_TYPE_PIE_CHART.equals(type)) {

        DataSeries dataSeries = new DataSeries().newSeries();
        for (int i = 0; i < names.length; i++) {
            dataSeries.add(names[i], values[i]);
        }
        SeriesDefaults seriesDefaults = new SeriesDefaults().setRenderer(SeriesRenderers.PIE);

        Options options = new Options().setSeriesDefaults(seriesDefaults);
        options.setAnimate(true);
        options.setAnimateReplot(true);

        Legend legend = new Legend().setShow(true).setPlacement(LegendPlacements.INSIDE);
        options.setLegend(legend);

        Highlighter highlighter = new Highlighter().setShow(true);
        options.setHighlighter(highlighter);

        chart = new DCharts().setDataSeries(dataSeries).setOptions(options);

    } else if (CHART_TYPE_LINE_CHART.equals(type)) {

        AxesDefaults axesDefaults = new AxesDefaults().setLabelRenderer(LabelRenderers.CANVAS);
        Axes axes = new Axes()
                .addAxis(new XYaxis().setLabel(xAxis != null ? xAxis : "").setMin(names[0])
                        .setMax(names[values.length - 1]).setDrawMajorTickMarks(true))
                .addAxis(new XYaxis(XYaxes.Y).setLabel(yAxis != null ? yAxis : "").setDrawMajorTickMarks(true));
        Options options = new Options().setAxesDefaults(axesDefaults).setAxes(axes);
        DataSeries dataSeries = new DataSeries().newSeries();
        for (int i = 0; i < names.length; i++) {

            //        if (parseLong(names[i]) != null) {
            //          dataSeries.add(parseLong(names[i]), values[i]);
            //        } else if (parseDouble(names[i]) != null) {
            //          dataSeries.add(parseDouble(names[i]), values[i]);
            //        } else {
            //          dataSeries.add(names[i], values[i]);
            //        }

            dataSeries.add(names[i], values[i]);

        }

        Series series = new Series().addSeries(new XYseries().setShowLine(true).setMarkerOptions(
                new MarkerRenderer().setShadow(true).setSize(7).setStyle(MarkerStyles.CIRCLE)));
        options.setSeries(series);

        options.setAnimate(true);
        options.setAnimateReplot(true);

        Highlighter highlighter = new Highlighter().setShow(true);
        options.setHighlighter(highlighter);

        chart = new DCharts().setDataSeries(dataSeries).setOptions(options);

    } else if (CHART_TYPE_LIST.equals(type)) {

        GridLayout grid = new GridLayout(2, names.length);
        grid.setSpacing(true);

        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            Label nameLabel = new Label(name);
            nameLabel.addStyleName(ExplorerLayout.STYLE_LABEL_BOLD);
            grid.addComponent(nameLabel, 0, i);

            Number value = values[i];
            Label valueLabel = new Label(value + "");
            grid.addComponent(valueLabel, 1, i);
        }

        chart = grid;

    }

    if (chart instanceof DCharts) {
        // Needed, otherwise the chart will not be shown
        ((DCharts) chart).show();
    }

    return chart;
}

From source file:org.agocontrol.site.viewlet.bus.BusesFlowlet.java

License:Apache License

@Override
public void initialize() {
    final List<FieldDescriptor> fieldDescriptors = AgoControlSiteFields.getFieldDescriptors(Bus.class);

    final List<FilterDescriptor> filterDefinitions = new ArrayList<FilterDescriptor>();

    final EntityManager entityManager = getSite().getSiteContext().getObject(EntityManager.class);
    container = new EntityContainer<Bus>(entityManager, true, true, false, Bus.class, 1000,
            new String[] { "name" }, new boolean[] { true }, "busId");

    ContainerUtil.addContainerProperties(container, fieldDescriptors);

    final GridLayout gridLayout = new GridLayout(1, 2);
    gridLayout.setSizeFull();//from  w w  w . ja va  2  s .  c  om
    gridLayout.setMargin(false);
    gridLayout.setSpacing(true);
    gridLayout.setRowExpandRatio(1, 1f);
    setViewContent(gridLayout);

    final HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    buttonLayout.setSizeUndefined();
    gridLayout.addComponent(buttonLayout, 0, 0);

    final Table table = new FormattingTable();
    grid = new Grid(table, container);
    grid.setFields(fieldDescriptors);
    grid.setFilters(filterDefinitions);

    table.setColumnCollapsed("created", true);
    table.setColumnCollapsed("modified", true);
    gridLayout.addComponent(grid, 0, 1);

    final Button addButton = getSite().getButton("add");
    buttonLayout.addComponent(addButton);
    addButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            final Bus bus = new Bus();
            bus.setCreated(new Date());
            bus.setModified(bus.getCreated());
            bus.setInventorySynchronized(new Date(0L));
            bus.setConnectionStatus(BusConnectionStatus.Disconnected);
            bus.setOwner((Company) getSite().getSiteContext().getObject(Company.class));
            final BusFlowlet busView = getViewSheet().forward(BusFlowlet.class);
            busView.edit(bus, true);
        }
    });

    final Button editButton = getSite().getButton("edit");
    buttonLayout.addComponent(editButton);
    editButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            final Bus entity = container.getEntity(grid.getSelectedItemId());
            final BusFlowlet busView = getViewSheet().forward(BusFlowlet.class);
            busView.edit(entity, false);
        }
    });

    final Button removeButton = getSite().getButton("remove");
    buttonLayout.addComponent(removeButton);
    removeButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            container.removeItem(grid.getSelectedItemId());
            container.commit();
        }
    });

    final Company company = getSite().getSiteContext().getObject(Company.class);
    container.removeDefaultFilters();
    container.addDefaultFilter(new Compare.Equal("owner.companyId", company.getCompanyId()));
    grid.refresh();
}

From source file:org.agocontrol.site.viewlet.bus.BusFlowlet.java

License:Apache License

@Override
public void initialize() {
    entityManager = getSite().getSiteContext().getObject(EntityManager.class);

    final GridLayout gridLayout = new GridLayout(1, 2);
    gridLayout.setSizeFull();/*from  ww w  .  java  2s.  co  m*/
    gridLayout.setMargin(false);
    gridLayout.setSpacing(true);
    gridLayout.setRowExpandRatio(1, 1f);
    setViewContent(gridLayout);

    busEditor = new ValidatingEditor(AgoControlSiteFields.getFieldDescriptors(Bus.class));
    busEditor.setCaption("Bus");
    busEditor.addListener((ValidatingEditorStateListener) this);
    gridLayout.addComponent(busEditor, 0, 0);

    final HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    gridLayout.addComponent(buttonLayout, 0, 1);

    saveButton = new Button("Save");
    saveButton.setImmediate(true);
    buttonLayout.addComponent(saveButton);
    saveButton.addListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            busEditor.commit();
            entityManager.getTransaction().begin();
            try {
                entity = entityManager.merge(entity);
                entityManager.persist(entity);
                entityManager.getTransaction().commit();
                entityManager.detach(entity);
            } catch (final Throwable t) {
                if (entityManager.getTransaction().isActive()) {
                    entityManager.getTransaction().rollback();
                }
                throw new RuntimeException("Failed to save entity: " + entity, t);
            }
        }
    });

    discardButton = new Button("Discard");
    discardButton.setImmediate(true);
    buttonLayout.addComponent(discardButton);
    discardButton.addListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            busEditor.discard();
        }
    });

}

From source file:org.agocontrol.site.viewlet.dashboard.BuildingControlPanel.java

License:Apache License

/**
 * Default constructor.//from w  ww  . j a  v  a 2s  .  c  om
 */
public BuildingControlPanel() {
    site = ((AgoControlSiteUI) UI.getCurrent()).getSite();
    siteContext = site.getSiteContext();
    entityManager = siteContext.getObject(EntityManager.class);

    layout = new VerticalLayout();
    layout.setSpacing(true);
    layout.setMargin(true);
    layout.setSizeFull();
    layout.setStyleName(Reindeer.LAYOUT_WHITE);

    final Label title = new Label("Control Panel");
    title.setIcon(getSite().getIcon("inventory"));
    title.setStyleName(Reindeer.LABEL_H2);
    layout.addComponent(title);
    layout.setExpandRatio(title, 0);

    elementLayout = new VerticalLayout();
    elementLayout.setSpacing(true);
    elementLayout.setMargin(false);
    layout.addComponent(elementLayout);
    layout.setExpandRatio(elementLayout, 1);

    roomIcon = site.getIcon("room");
    deviceIcon = site.getIcon("device");
    temperatureIcon = site.getIcon("temperature");
    brightnessIcon = site.getIcon("brightness");
    humidityIcon = site.getIcon("humidity");
    eventIcon = site.getIcon("event");

    setCompositionRoot(layout);

    // the Refresher polls automatically
    final Refresher refresher = new Refresher();
    refresher.setRefreshInterval(200);
    refresher.addListener(new Refresher.RefreshListener() {
        @Override
        public void refresh(final Refresher refresher) {
            while (!recordsQueue.isEmpty()) {
                final List<Record> records = recordsQueue.remove();
                if (records.size() > 0) {
                    final Record record = records.get(0);
                    final RecordSet recordSet = record.getRecordSet();
                    final Element element = recordSet.getElement();

                    final GridLayout recordsLayout = recordsLayouts.get(element.getElementId());
                    if (recordsLayout == null) {
                        continue;
                    }

                    final int columnIndex = recordSet.getType().ordinal();
                    final int rowIndex = 0;
                    if (recordsLayout.getComponent(columnIndex, rowIndex) != null) {
                        continue;
                    }

                    final VerticalLayout recordLayout = new VerticalLayout();
                    recordLayout.setSpacing(true);
                    final Resource recordIcon;
                    switch (recordSet.getType()) {
                    case TEMPERATURE:
                        recordIcon = temperatureIcon;
                        break;
                    case BRIGHTNESS:
                        recordIcon = brightnessIcon;
                        break;
                    case HUMIDITY:
                        recordIcon = humidityIcon;
                        break;
                    default:
                        recordIcon = eventIcon;
                        break;
                    }

                    final Embedded embedded = new Embedded(null, recordIcon);
                    recordLayout.addComponent(embedded);
                    recordLayout.setExpandRatio(embedded, 0.1f);
                    embedded.setWidth(32, Unit.PIXELS);
                    embedded.setHeight(32, Unit.PIXELS);

                    final Label label = new Label();
                    recordLayout.addComponent(label);
                    recordLayout.setComponentAlignment(label, Alignment.MIDDLE_LEFT);

                    final String recordUnit = recordSet.getUnit();
                    final String displayUnit = DisplayValueConversionUtil.getDisplayUnit(recordSet.getType(),
                            recordUnit);

                    final double displayValue = DisplayValueConversionUtil.convertValue(recordSet.getType(),
                            recordUnit, displayUnit, record.getValue());

                    final String displayValueString = DisplayValueConversionUtil.formatDouble(displayValue);

                    label.setValue(displayValueString + " " + displayUnit);
                    label.setDescription(record.getCreated().toString());

                    recordsLayout.addComponent(recordLayout, columnIndex, rowIndex);
                }
            }
        }
    });
    addExtension(refresher);

}

From source file:org.agocontrol.site.viewlet.element.ElementFlowlet.java

License:Apache License

@Override
public void initialize() {
    entityManager = getSite().getSiteContext().getObject(EntityManager.class);

    final GridLayout gridLayout = new GridLayout(1, 2);
    gridLayout.setSizeFull();/*from  w  w w .  j  a  va 2  s .  com*/
    gridLayout.setMargin(false);
    gridLayout.setSpacing(true);
    gridLayout.setRowExpandRatio(1, 1f);
    setViewContent(gridLayout);

    elementEditor = new ValidatingEditor(AgoControlSiteFields.getFieldDescriptors(Element.class));
    elementEditor.setCaption("Element");
    elementEditor.addListener((ValidatingEditorStateListener) this);
    gridLayout.addComponent(elementEditor, 0, 0);

    final HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    gridLayout.addComponent(buttonLayout, 0, 1);

    saveButton = new Button("Save");
    saveButton.setImmediate(true);
    buttonLayout.addComponent(saveButton);
    saveButton.addListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            elementEditor.commit();
            entityManager.getTransaction().begin();
            try {
                entity = entityManager.merge(entity);
                entityManager.persist(entity);
                entityManager.getTransaction().commit();
                entityManager.detach(entity);
            } catch (final Throwable t) {
                if (entityManager.getTransaction().isActive()) {
                    entityManager.getTransaction().rollback();
                }
                throw new RuntimeException("Failed to save entity: " + entity, t);
            }
            final BusClient busClient = ((AgoControlSiteUI) UI.getCurrent()).getBusClient(entity.getBus());
            if (busClient != null) {
                if (busClient.saveElement(entity) && busClient.synchronizeInventory()) {
                    Notification.show("Element save sent to bus.", Notification.Type.HUMANIZED_MESSAGE);
                } else {
                    Notification.show("Element save bus error.", Notification.Type.ERROR_MESSAGE);
                }
            }
        }
    });

    discardButton = new Button("Discard");
    discardButton.setImmediate(true);
    buttonLayout.addComponent(discardButton);
    discardButton.addListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            elementEditor.discard();
        }
    });

}

From source file:org.agocontrol.site.viewlet.element.ElementsFlowlet.java

License:Apache License

@Override
public void initialize() {
    final List<FieldDescriptor> fieldDescriptors = AgoControlSiteFields.getFieldDescriptors(Element.class);

    final List<FilterDescriptor> filterDefinitions = new ArrayList<FilterDescriptor>();

    final EntityManager entityManager = getSite().getSiteContext().getObject(EntityManager.class);
    container = new EntityContainer<Element>(entityManager, true, true, false, Element.class, 1000,
            new String[] { "treeIndex" }, new boolean[] { true }, "elementId");

    ContainerUtil.addContainerProperties(container, fieldDescriptors);

    final GridLayout gridLayout = new GridLayout(1, 2);
    gridLayout.setSizeFull();/*from  w  w  w . j av a 2 s  .c  o  m*/
    gridLayout.setMargin(false);
    gridLayout.setSpacing(true);
    gridLayout.setRowExpandRatio(1, 1f);
    setViewContent(gridLayout);

    final HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    buttonLayout.setSizeUndefined();
    gridLayout.addComponent(buttonLayout, 0, 0);

    final Table table = new FormattingTable();
    grid = new Grid(table, container);
    grid.setFields(fieldDescriptors);
    grid.setFilters(filterDefinitions);

    table.setColumnCollapsed("elementId", true);
    table.setColumnCollapsed("bus", true);
    table.setColumnCollapsed("created", true);
    table.setColumnCollapsed("modified", true);
    gridLayout.addComponent(grid, 0, 1);

    final Button addButton = getSite().getButton("add");
    buttonLayout.addComponent(addButton);
    addButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            final Element element = new Element();
            element.setCreated(new Date());
            element.setModified(element.getCreated());
            element.setOwner((Company) getSite().getSiteContext().getObject(Company.class));
            final ElementFlowlet elementView = getViewSheet().forward(ElementFlowlet.class);
            elementView.edit(element, true);
        }
    });

    final Button editButton = getSite().getButton("edit");
    buttonLayout.addComponent(editButton);
    editButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            final Element entity = container.getEntity(grid.getSelectedItemId());
            final ElementFlowlet elementView = getViewSheet().forward(ElementFlowlet.class);
            elementView.edit(entity, false);
        }
    });

    final Button removeButton = getSite().getButton("remove");
    buttonLayout.addComponent(removeButton);
    removeButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            final Element element = container.getEntity(grid.getSelectedItemId());

            container.removeItem(grid.getSelectedItemId());
            container.commit();

            final BusClient busClient = ((AgoControlSiteUI) UI.getCurrent()).getBusClient(element.getBus());
            if (busClient != null) {
                if (busClient.removeElement(element)) {
                    Notification.show("Element removal sent to bus.", Notification.Type.HUMANIZED_MESSAGE);
                } else {
                    Notification.show("Element removal bus error.", Notification.Type.ERROR_MESSAGE);
                }
            }
        }
    });

    final Company company = getSite().getSiteContext().getObject(Company.class);
    container.removeDefaultFilters();
    container.addDefaultFilter(new Compare.Equal("owner.companyId", company.getCompanyId()));
    grid.refresh();
}

From source file:org.agocontrol.site.viewlet.record.RecordFlowlet.java

License:Apache License

@Override
public void initialize() {
    entityManager = getSite().getSiteContext().getObject(EntityManager.class);

    final GridLayout gridLayout = new GridLayout(1, 2);
    gridLayout.setSizeFull();//  ww  w  .  j a v a  2  s .c  o  m
    gridLayout.setMargin(false);
    gridLayout.setSpacing(true);
    gridLayout.setRowExpandRatio(1, 1f);
    setViewContent(gridLayout);

    recordEditor = new ValidatingEditor(AgoControlSiteFields.getFieldDescriptors(Record.class));
    recordEditor.setCaption("Record");
    recordEditor.addListener((ValidatingEditorStateListener) this);
    gridLayout.addComponent(recordEditor, 0, 0);

    final HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    gridLayout.addComponent(buttonLayout, 0, 1);

    saveButton = new Button("Save");
    saveButton.setImmediate(true);
    buttonLayout.addComponent(saveButton);
    saveButton.addListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            recordEditor.commit();
            entityManager.getTransaction().begin();
            try {
                entity = entityManager.merge(entity);
                entityManager.persist(entity);
                entityManager.getTransaction().commit();
                entityManager.detach(entity);
            } catch (final Throwable t) {
                if (entityManager.getTransaction().isActive()) {
                    entityManager.getTransaction().rollback();
                }
                throw new RuntimeException("Failed to save entity: " + entity, t);
            }
        }
    });

    discardButton = new Button("Discard");
    discardButton.setImmediate(true);
    buttonLayout.addComponent(discardButton);
    discardButton.addListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            recordEditor.discard();
        }
    });

}

From source file:org.agocontrol.site.viewlet.record.RecordsFlowlet.java

License:Apache License

@Override
public void initialize() {
    final List<FieldDescriptor> fieldDescriptors = AgoControlSiteFields.getFieldDescriptors(Record.class);

    final List<FilterDescriptor> filterDefinitions = new ArrayList<FilterDescriptor>();

    final EntityManager entityManager = getSite().getSiteContext().getObject(EntityManager.class);
    container = new LazyEntityContainer<Record>(entityManager, true, false, false, Record.class, 50,
            new String[] { "created" }, new boolean[] { false }, "recordId");

    ContainerUtil.addContainerProperties(container, fieldDescriptors);

    final GridLayout gridLayout = new GridLayout(1, 2);
    gridLayout.setSizeFull();//from ww w . ja  v  a 2  s . co m
    gridLayout.setMargin(false);
    gridLayout.setSpacing(true);
    gridLayout.setRowExpandRatio(1, 1f);
    setViewContent(gridLayout);

    final HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    buttonLayout.setSizeUndefined();
    gridLayout.addComponent(buttonLayout, 0, 0);

    final Table table = new FormattingTable();
    grid = new Grid(table, container);
    grid.setFields(fieldDescriptors);
    grid.setFilters(filterDefinitions);

    table.setColumnCollapsed("recordId", true);
    table.setColumnCollapsed("modified", true);
    gridLayout.addComponent(grid, 0, 1);

    final Button addButton = getSite().getButton("add");
    buttonLayout.addComponent(addButton);
    addButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            final Record record = new Record();
            record.setCreated(new Date());
            record.setModified(record.getCreated());
            record.setOwner((Company) getSite().getSiteContext().getObject(Company.class));
            final RecordFlowlet recordView = getViewSheet().forward(RecordFlowlet.class);
            recordView.edit(record, true);
        }
    });

    final Button editButton = getSite().getButton("edit");
    buttonLayout.addComponent(editButton);
    editButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            final Record entity = container.getEntity(grid.getSelectedItemId());
            final RecordFlowlet recordView = getViewSheet().forward(RecordFlowlet.class);
            recordView.edit(entity, false);
        }
    });

    final Button removeButton = getSite().getButton("remove");
    buttonLayout.addComponent(removeButton);
    removeButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            container.removeItem(grid.getSelectedItemId());
            container.commit();
        }
    });

    final Company company = getSite().getSiteContext().getObject(Company.class);
    container.removeDefaultFilters();
    container.addDefaultFilter(new Compare.Equal("owner.companyId", company.getCompanyId()));

}