Example usage for com.vaadin.ui GridLayout setRowExpandRatio

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

Introduction

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

Prototype

public void setRowExpandRatio(int rowIndex, float ratio) 

Source Link

Document

Sets the expand ratio of given row.

Usage

From source file:com.mymita.vaadlets.VaadletsBuilder.java

License:Apache License

private static void setLayoutAttributes(final com.vaadin.ui.Component vaadinComponent,
        final com.mymita.vaadlets.core.Component vaadletsComponent) {
    if (vaadletsComponent instanceof com.mymita.vaadlets.layout.AbstractLayout) {
        final Boolean margin = ((com.mymita.vaadlets.layout.AbstractLayout) vaadletsComponent).isMargin();
        if (margin != null) {
            ((com.vaadin.ui.AbstractLayout) vaadinComponent).setMargin(margin);
        }//from w  ww .  j a  v a 2 s. c  om
        final Boolean marginTop = ((com.mymita.vaadlets.layout.AbstractLayout) vaadletsComponent).isMarginTop();
        final Boolean marginRight = ((com.mymita.vaadlets.layout.AbstractLayout) vaadletsComponent)
                .isMarginRight();
        final Boolean marginBottom = ((com.mymita.vaadlets.layout.AbstractLayout) vaadletsComponent)
                .isMarginBottom();
        final Boolean marginLeft = ((com.mymita.vaadlets.layout.AbstractLayout) vaadletsComponent)
                .isMarginLeft();
        if (marginTop != null || marginRight != null || marginBottom != null || marginLeft != null) {
            ((com.vaadin.ui.AbstractLayout) vaadinComponent).setMargin(
                    firstNonNull(marginTop, firstNonNull(margin, FALSE)),
                    firstNonNull(marginRight, firstNonNull(margin, FALSE)),
                    firstNonNull(marginBottom, firstNonNull(margin, FALSE)),
                    firstNonNull(marginLeft, firstNonNull(margin, FALSE)));
        }
    }
    if (vaadletsComponent instanceof com.mymita.vaadlets.layout.OrderedLayout) {
        final Boolean spacing = ((com.mymita.vaadlets.layout.OrderedLayout) vaadletsComponent).isSpacing();
        if (spacing != null) {
            ((com.vaadin.ui.AbstractOrderedLayout) vaadinComponent).setSpacing(spacing);
        }
    }
    if (vaadletsComponent instanceof com.mymita.vaadlets.layout.GridLayout) {
        final com.vaadin.ui.GridLayout vaadinGridLayout = (com.vaadin.ui.GridLayout) vaadinComponent;
        final com.mymita.vaadlets.layout.GridLayout vaadletsGridLayout = (com.mymita.vaadlets.layout.GridLayout) vaadletsComponent;
        vaadinGridLayout.setColumns(vaadletsGridLayout.getColumns());
        vaadinGridLayout.setRows(vaadletsGridLayout.getRows());
        for (final Object object : vaadletsGridLayout.getColumnExpandRatioOrRowExpandRatio()) {
            if (object instanceof GridLayoutColumExpandRatio) {
                final GridLayoutColumExpandRatio cr = (GridLayoutColumExpandRatio) object;
                vaadinGridLayout.setColumnExpandRatio(cr.getColumnIndex(), cr.getRatio());
            }
            if (object instanceof GridLayoutRowExpandRatio) {
                final GridLayoutRowExpandRatio rr = (GridLayoutRowExpandRatio) object;
                vaadinGridLayout.setRowExpandRatio(rr.getRowIndex(), rr.getRatio());
            }
        }
        final Boolean spacing = ((com.mymita.vaadlets.layout.GridLayout) vaadletsComponent).isSpacing();
        if (spacing != null) {
            ((com.vaadin.ui.GridLayout) vaadinComponent).setSpacing(spacing);
        }
    }
}

From source file:com.scsb.crpro.MessageBox.java

License:Apache License

/**
 * Similar to {@link #MessageBox(Window, String, Icon, String, Alignment, ButtonConfig...)},
 * but the message component is defined explicitly. The component can be even a composite
 * of a layout manager and manager further Vaadin components.
 * /*w w w . j a v  a 2 s.c  om*/
 * @param messageComponent a Vaadin component
 */
public MessageBox(String dialogWidth, String dialogHeight, String dialogCaption, Icon dialogIcon,
        Component messageComponent, Alignment buttonsAlignment, ButtonConfig... buttonConfigs) {
    super();
    setResizable(false);
    setClosable(false);
    setSizeUndefined();
    setWidth(dialogWidth);
    //setHeight(dialogHeight);
    setCaption(dialogCaption);

    GridLayout mainLayout = new GridLayout(2, 2);
    mainLayout.setMargin(true);
    mainLayout.setSpacing(true);
    mainLayout.setSizeUndefined();
    mainLayout.setWidth(GRID_DEFAULT_WIDTH1);

    // Add Content
    messageComponent.setSizeUndefined();
    messageComponent.setWidth(GRID_DEFAULT_WIDTH2);
    if (dialogIcon == null || Icon.NONE.equals(dialogIcon)) {
        mainLayout.addComponent(messageComponent, 0, 0, 1, 0);
        mainLayout.setRowExpandRatio(0, 1.0f);
        mainLayout.setColumnExpandRatio(0, 1.0f);
    } else {
        mainLayout.addComponent(messageComponent, 1, 0);
        mainLayout.setRowExpandRatio(0, 1.0f);
        mainLayout.setColumnExpandRatio(1, 1.0f);

        Embedded icon = null;
        switch (dialogIcon) {
        case QUESTION:
            icon = new Embedded(null, new ThemeResource("images/question.png"));
            break;
        case INFO:
            icon = new Embedded(null, new ThemeResource("images/info.png"));
            break;
        case WARN:
            icon = new Embedded(null, new ThemeResource("images/warn.png"));
            break;
        case ERROR:
            icon = new Embedded(null, new ThemeResource("images/error.png"));
            break;
        }
        mainLayout.addComponent(icon, 0, 0);
        icon.setWidth(ICON_DEFAULT_SIZE);
        icon.setHeight(ICON_DEFAULT_SIZE);
    }

    // Add Buttons
    HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    mainLayout.addComponent(buttonLayout, 0, 1, 1, 1);
    mainLayout.setComponentAlignment(buttonLayout, buttonsAlignment);
    for (ButtonConfig buttonConfig : buttonConfigs) {
        Button button = new Button(buttonConfig.getCaption(),
                new ButtonClickListener(buttonConfig.getButtonType()));
        if (buttonConfig.getWidth() != null) {
            button.setWidth(buttonConfig.getWidth());
        }
        if (buttonConfig.getOptionalResource() != null) {
            button.setIcon(buttonConfig.getOptionalResource());
        } else {
            Resource icon = null;
            switch (buttonConfig.getButtonType()) {
            case ABORT:
                icon = new ThemeResource("images/famfamfam/cross.png");
                break;
            case CANCEL:
                icon = new ThemeResource("images/famfamfam/cross.png");
                break;
            case CLOSE:
                icon = new ThemeResource("images/famfamfam/door.png");
                break;
            case HELP:
                icon = new ThemeResource("images/famfamfam/lightbulb.png");
                break;
            case OK:
                icon = new ThemeResource("images/famfamfam/tick.png");
                break;
            case YES:
                icon = new ThemeResource("images/famfamfam/tick.png");
                break;
            case NO:
                icon = new ThemeResource("images/famfamfam/cross.png");
                break;
            case SAVE:
                icon = new ThemeResource("images/famfamfam/disk.png");
                break;
            case RETRY:
                icon = new ThemeResource("images/famfamfam/arrow_refresh.png");
                break;
            case IGNORE:
                icon = new ThemeResource("images/famfamfam/lightning_go.png");
                break;
            }
            button.setIcon(icon);
        }
        buttonLayout.addComponent(button);
    }

    setContent(mainLayout);
}

From source file:com.scsb.vaadin.composite.MessageBox.java

License:Apache License

/**
 * Similar to {@link #MessageBox(Window, String, Icon, String, Alignment, ButtonConfig...)},
 * but the message component is defined explicitly. The component can be even a composite
 * of a layout manager and manager further Vaadin components.
 * //from  w  ww. j a va 2s .  c o m
 * @param messageComponent a Vaadin component
 */
public MessageBox(String dialogWidth, String dialogHeight, String dialogCaption, Icon dialogIcon,
        Component messageComponent, Alignment buttonsAlignment, ButtonConfig... buttonConfigs) {
    super();
    setResizable(false);
    setClosable(false);
    setSizeUndefined();
    setWidth(dialogWidth);
    //setHeight(dialogHeight);
    setCaption(dialogCaption);

    GridLayout mainLayout = new GridLayout(2, 2);
    mainLayout.setMargin(true);
    mainLayout.setSpacing(true);
    mainLayout.setSizeUndefined();
    mainLayout.setWidth(GRID_DEFAULT_WIDTH1);

    // Add Content
    messageComponent.setSizeUndefined();
    messageComponent.setWidth(GRID_DEFAULT_WIDTH2);
    if (dialogIcon == null || Icon.NONE.equals(dialogIcon)) {
        mainLayout.addComponent(messageComponent, 0, 0, 1, 0);
        mainLayout.setRowExpandRatio(0, 1.0f);
        mainLayout.setColumnExpandRatio(0, 1.0f);
    } else {
        mainLayout.addComponent(messageComponent, 1, 0);
        mainLayout.setRowExpandRatio(0, 1.0f);
        mainLayout.setColumnExpandRatio(1, 1.0f);

        Embedded icon = null;
        switch (dialogIcon) {
        case QUESTION:
            icon = new Embedded(null, new ThemeResource("./images/question.png"));
            break;
        case INFO:
            icon = new Embedded(null, new ThemeResource("./images/info.png"));
            break;
        case WARN:
            icon = new Embedded(null, new ThemeResource("./images/warn.png"));
            break;
        case ERROR:
            icon = new Embedded(null, new ThemeResource("./images/error.png"));
            break;
        }
        mainLayout.addComponent(icon, 0, 0);
        icon.setWidth(ICON_DEFAULT_SIZE);
        icon.setHeight(ICON_DEFAULT_SIZE);
    }

    // Add Buttons
    HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    mainLayout.addComponent(buttonLayout, 0, 1, 1, 1);
    mainLayout.setComponentAlignment(buttonLayout, buttonsAlignment);
    for (ButtonConfig buttonConfig : buttonConfigs) {
        Button button = new Button(buttonConfig.getCaption(),
                new ButtonClickListener(buttonConfig.getButtonType()));
        if (buttonConfig.getWidth() != null) {
            button.setWidth(buttonConfig.getWidth());
        }
        if (buttonConfig.getOptionalResource() != null) {
            button.setIcon(buttonConfig.getOptionalResource());
        } else {
            Resource icon = null;
            switch (buttonConfig.getButtonType()) {
            case ABORT:
                icon = new ThemeResource("images/famfamfam/cross.png");
                break;
            case CANCEL:
                icon = new ThemeResource("images/famfamfam/cross.png");
                break;
            case CLOSE:
                icon = new ThemeResource("images/famfamfam/door.png");
                break;
            case HELP:
                icon = new ThemeResource("images/famfamfam/lightbulb.png");
                break;
            case OK:
                icon = new ThemeResource("images/famfamfam/tick.png");
                break;
            case YES:
                icon = new ThemeResource("images/famfamfam/tick.png");
                break;
            case NO:
                icon = new ThemeResource("images/famfamfam/cross.png");
                break;
            case SAVE:
                icon = new ThemeResource("images/famfamfam/disk.png");
                break;
            case RETRY:
                icon = new ThemeResource("images/famfamfam/arrow_refresh.png");
                break;
            case IGNORE:
                icon = new ThemeResource("images/famfamfam/lightning_go.png");
                break;
            }
            button.setIcon(icon);
        }
        buttonLayout.addComponent(button);
    }

    setContent(mainLayout);
}

From source file:com.tripoin.util.ui.calendar.CalendarActionsUI.java

License:Apache License

@SuppressWarnings("deprecation")
@Override/*from  w w  w . j  a  v a 2 s. c  om*/
protected void init(VaadinRequest request) {
    GridLayout content = new GridLayout(1, 2);
    content.setSizeFull();
    setContent(content);

    final Calendar calendar = new Calendar();
    calendar.setLocale(new Locale("fi", "FI"));

    calendar.setSizeFull();
    calendar.setStartDate(new Date(100, 1, 1));
    calendar.setEndDate(new Date(100, 2, 1));

    calendar.addActionHandler(new Action.Handler() {

        /**
        * 
        */
        private static final long serialVersionUID = -9176166330213062162L;
        public final Action NEW_EVENT = new Action("Add event");
        @SuppressWarnings("unused")
        public final Action EDIT_EVENT = new Action("Edit event");
        public final Action REMOVE_EVENT = new Action("Remove event");

        /*
         * (non-Javadoc)
         * 
         * @see
         * com.vaadin.event.Action.Handler#handleAction(com.vaadin.event
         * .Action, java.lang.Object, java.lang.Object)
         */
        @Override
        public void handleAction(Action action, Object sender, Object target) {
            Date date = (Date) target;
            if (action == NEW_EVENT) {
                BasicEvent event = new BasicEvent("New event", "Hello world", date, date);
                calendar.addEvent(event);
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see com.vaadin.event.Action.Handler#getActions(java.lang.Object,
         * java.lang.Object)
         */
        @Override
        public Action[] getActions(Object target, Object sender) {
            CalendarDateRange date = (CalendarDateRange) target;

            java.util.Calendar cal = java.util.Calendar.getInstance();
            cal.set(2000, 1, 1, 12, 0, 0);

            if (date.inRange(cal.getTime())) {
                return new Action[] { NEW_EVENT, };
            }

            cal.add(java.util.Calendar.DAY_OF_WEEK, 1);

            if (date.inRange(cal.getTime())) {
                return new Action[] { REMOVE_EVENT };
            }

            return null;
        }
    });

    content.addComponent(calendar);

    content.addComponent(new Button("Set week view", new Button.ClickListener() {
        /**
        * 
        */
        private static final long serialVersionUID = -9193155583440680362L;

        @Override
        public void buttonClick(ClickEvent event) {
            calendar.setEndDate(new Date(100, 1, 7));
        }
    }));

    content.setRowExpandRatio(0, 1);

}

From source file:com.tripoin.util.ui.calendar.HiddenFwdBackButtons.java

License:Apache License

@SuppressWarnings("deprecation")
@Override//w w w .j a v a2s.c  om
protected void init(VaadinRequest request) {
    GridLayout content = new GridLayout(1, 2);
    content.setSizeFull();
    setContent(content);

    final Calendar calendar = new Calendar();
    calendar.setLocale(new Locale("fi", "FI"));

    calendar.setSizeFull();
    calendar.setStartDate(new Date(100, 1, 1));
    calendar.setEndDate(new Date(100, 1, 7));
    content.addComponent(calendar);
    Button button = new Button("Hide forward and back buttons");
    button.addClickListener(new ClickListener() {
        /**
        * 
        */
        private static final long serialVersionUID = 7195717021662140155L;

        @Override
        public void buttonClick(ClickEvent event) {
            // This should hide the forward and back navigation buttons
            calendar.setHandler((BackwardHandler) null);
            calendar.setHandler((ForwardHandler) null);
        }
    });
    content.addComponent(button);

    content.setRowExpandRatio(0, 1);

}

From source file:com.tripoin.util.ui.calendar.NotificationTestUI.java

License:Apache License

@Override
protected void init(com.vaadin.server.VaadinRequest request) {
    GridLayout content = new GridLayout(1, 2);
    content.setSizeFull();//from   w  ww  .  ja v  a  2  s.c om
    content.setRowExpandRatio(1, 1.0f);
    setContent(content);
    final Button btn = new Button("Show working notification", new Button.ClickListener() {
        /**
        * 
        */
        private static final long serialVersionUID = -165570248584063787L;

        @Override
        public void buttonClick(ClickEvent event) {
            Notification.show("This will disappear when you move your mouse!");
        }
    });
    content.addComponent(btn);

    provider = new DummyEventProvider();
    final Calendar cal = new Calendar(provider);
    cal.setLocale(Locale.US);
    cal.setSizeFull();
    cal.setHandler(new DateClickHandler() {
        /**
        * 
        */
        private static final long serialVersionUID = 1903111449161995776L;

        @Override
        public void dateClick(DateClickEvent event) {
            provider.addEvent(event.getDate());
            Notification.show("This should disappear, but if wont unless clicked.");

            // this requestRepaint call interferes with the notification
            cal.markAsDirty();
        }
    });
    content.addComponent(cal);

    java.util.Calendar javaCal = java.util.Calendar.getInstance();
    javaCal.set(java.util.Calendar.YEAR, 2000);
    javaCal.set(java.util.Calendar.MONTH, 0);
    javaCal.set(java.util.Calendar.DAY_OF_MONTH, 1);
    Date start = javaCal.getTime();
    javaCal.set(java.util.Calendar.DAY_OF_MONTH, 31);
    Date end = javaCal.getTime();

    cal.setStartDate(start);
    cal.setEndDate(end);
}

From source file:edu.kit.dama.ui.admin.schedule.SchedulerBasePropertiesLayout.java

License:Apache License

/**
 * Default constructor.//from   ww w.  j ava 2 s .c  o m
 */
public SchedulerBasePropertiesLayout() {
    super();
    LOGGER.debug("Building " + DEBUG_ID_PREFIX + " ...");

    setId(DEBUG_ID_PREFIX.substring(0, DEBUG_ID_PREFIX.length() - 1));

    setSizeFull();
    setMargin(true);
    setSpacing(true);

    setColumns(3);
    setRows(3);
    //first row
    addComponent(getIdField(), 0, 0);
    addComponent(getGroupField(), 1, 0);
    addComponent(getNameField(), 2, 0);

    //second row
    addComponent(getDescriptionArea(), 0, 1, 2, 1);
    Button addTriggerButton = new Button();
    addTriggerButton.setDescription("Add a new trigger.");
    addTriggerButton.setIcon(new ThemeResource(IconContainer.ADD));
    addTriggerButton.addClickListener((Button.ClickEvent event) -> {
        addTrigger();
    });

    Button removeTriggerButton = new Button();
    removeTriggerButton.setDescription("Remove the selected trigger.");
    removeTriggerButton.setIcon(new ThemeResource(IconContainer.DELETE));
    removeTriggerButton.addClickListener((Button.ClickEvent event) -> {
        removeTrigger();
    });

    Button refreshTriggerButton = new Button();
    refreshTriggerButton.setDescription("Refresh the list of triggers.");
    refreshTriggerButton.setIcon(new ThemeResource(IconContainer.REFRESH));
    refreshTriggerButton.addClickListener((Button.ClickEvent event) -> {
        reloadTriggers();
    });

    VerticalLayout buttonLayout = new VerticalLayout(addTriggerButton, refreshTriggerButton,
            removeTriggerButton);
    buttonLayout.setComponentAlignment(addTriggerButton, Alignment.TOP_RIGHT);
    buttonLayout.setComponentAlignment(removeTriggerButton, Alignment.TOP_RIGHT);
    buttonLayout.setMargin(true);

    GridLayout triggerLayout = new UIUtils7.GridLayoutBuilder(2, 2).addComponent(getTriggerTable(), 0, 0, 1, 2)
            .addComponent(buttonLayout, 1, 0, 1, 2).getLayout();
    triggerLayout.setSizeFull();
    triggerLayout.setMargin(false);
    triggerLayout.setColumnExpandRatio(0, .95f);
    triggerLayout.setColumnExpandRatio(1, .05f);
    triggerLayout.setRowExpandRatio(0, .9f);
    triggerLayout.setRowExpandRatio(1, .05f);
    triggerLayout.setRowExpandRatio(2, .05f);

    //third row
    addComponent(triggerLayout, 0, 2, 2, 2);
    addTriggerComponent = new AddTriggerComponent(this);

    setRowExpandRatio(1, .3f);
    setRowExpandRatio(2, .6f);
}

From source file:edu.kit.dama.ui.components.ConfirmationWindow7.java

License:Apache License

/**
 * Builds a customized subwindow <b>ConfirmationWindow</b> that allows the
 * user to confirm his previously requested action.
 *
 * @param pTitle The title of the window.
 * @param pMessage The message shown in the window.
 * @param pOptionType The type of the window (OK or YES_NO) which defines the
 * visible buttons.// ww w  .  j  a  v a  2 s  .c o  m
 * @param pMessageType The message type (INFORMATION, WARNING, ERROR) which
 * determines the icon. If pMessageType is null, no icon will be shown.
 * @param pListener The listener which receives the result if a button was
 * pressed or the window was closed.
 *
 */
ConfirmationWindow7(String pTitle, String pMessage, OPTION_TYPE pOptionType, MESSAGE_TYPE pMessageType,
        IConfirmationWindowListener7 pListener) {
    this.listener = pListener;
    //basic setup

    //set caption depending on type
    String caption = pTitle;
    if (caption == null) {
        if (pMessageType != null) {
            switch (pMessageType) {
            case QUESTION:
                caption = DEFAULT_TITLE;
                break;
            case INFORMATION:
                caption = "Information";
                break;
            case WARNING:
                caption = "Warning";
                break;
            case ERROR:
                caption = "Error";
                break;
            }
        } else {
            //no type provided...use default title
            caption = DEFAULT_TITLE;
        }
    }

    setCaption(caption);
    setModal(true);
    center();

    // Build line of buttons depending on pOptionType
    HorizontalLayout buttonLine = new HorizontalLayout();
    buttonLine.setSpacing(true);
    buttonLine.setWidth("100%");
    //add spacer
    Label spacer = new Label();
    buttonLine.addComponent(spacer);
    //add buttons
    if (OPTION_TYPE.YES_NO_OPTION.equals(pOptionType)) {
        buttonLine.addComponent(buildYesButton("Yes"));
        buttonLine.addComponent(buildNoButton());
        buttonLine.setComponentAlignment(yesButton, Alignment.MIDDLE_RIGHT);
        buttonLine.setComponentAlignment(noButton, Alignment.MIDDLE_RIGHT);
        //Assign ENTER to the YES button
        yesButton.setClickShortcut(ShortcutAction.KeyCode.ENTER, null);
        //Assign ESC to the NO button
        noButton.setClickShortcut(ShortcutAction.KeyCode.ESCAPE, null);
    } else {
        buttonLine.addComponent(buildYesButton("OK"));
        buttonLine.setComponentAlignment(yesButton, Alignment.MIDDLE_RIGHT);
        //Assign ENTER to the OK button
        yesButton.setClickShortcut(ShortcutAction.KeyCode.ENTER, null);
        //Assign ESC to close the dialog
        setCloseShortcut(ShortcutAction.KeyCode.ESCAPE, null);
    }

    buttonLine.setExpandRatio(spacer, 1.0f);

    //determine the icon depending on pMessageType
    ThemeResource icon = null;

    if (pMessageType != null) {
        switch (pMessageType) {
        case QUESTION:
            icon = new ThemeResource("img/24x24/question.png");
            break;
        case INFORMATION:
            icon = new ThemeResource("img/24x24/information.png");
            break;
        case WARNING:
            icon = new ThemeResource("img/24x24/warning.png");
            break;
        case ERROR:
            icon = new ThemeResource("img/24x24/forbidden.png");
            break;
        }
    }
    Component iconComponent = new Label();
    if (icon != null) {
        //icon was set, overwrite icon component
        iconComponent = new Image(null, icon);
    }

    //build the message label
    Label message = new Label(pMessage, ContentMode.HTML);
    message.setSizeUndefined();
    //build the main layout
    GridLayout mainLayout = new UIUtils7.GridLayoutBuilder(2, 2)
            .addComponent(iconComponent, Alignment.TOP_LEFT, 0, 0, 1, 1).addComponent(message, 1, 0, 1, 1)
            .addComponent(buttonLine, 0, 1, 2, 1).getLayout();
    mainLayout.setMargin(true);
    mainLayout.setSpacing(true);
    mainLayout.setColumnExpandRatio(0, .05f);
    mainLayout.setColumnExpandRatio(1, 1f);
    mainLayout.setRowExpandRatio(0, 1f);
    mainLayout.setRowExpandRatio(1, .05f);
    setContent(mainLayout);

    //add the close listener
    addCloseListener(new CloseListener() {
        @Override
        public void windowClose(CloseEvent e) {
            fireCloseEvents(RESULT.CANCEL);
        }
    });
}

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();//  w  ww  .j a v  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("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   w  w w . j av a  2 s  .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();
        }
    });

}