Search in sources :

Example 1 with AbstractStringValidator

use of com.vaadin.data.validator.AbstractStringValidator in project opennms by OpenNMS.

the class WallboardConfigView method addNewTabComponent.

/**
     * This method is used to add a new {@link TabSheet.Tab} component. It creates a new window querying the user for the name of the new {@link Wallboard}.
     */
protected void addNewTabComponent() {
    final Window window = new Window("New Ops Board");
    window.setModal(true);
    window.setClosable(false);
    window.setResizable(false);
    window.addCloseListener(new Window.CloseListener() {

        @Override
        public void windowClose(Window.CloseEvent e) {
            m_dashboardOverview.refreshTable();
        }
    });
    getUI().addWindow(window);
    window.setContent(new VerticalLayout() {

        TextField name = new TextField("Ops Board Name");

        {
            addComponent(new FormLayout() {

                {
                    setSizeUndefined();
                    setMargin(true);
                    String newName = "Untitled";
                    int i = 1;
                    if (WallboardProvider.getInstance().containsWallboard(newName)) {
                        do {
                            i++;
                            newName = "Untitled #" + i;
                        } while (WallboardProvider.getInstance().containsWallboard(newName));
                    }
                    name.setValue(newName);
                    addComponent(name);
                    name.focus();
                    name.selectAll();
                    name.addValidator(new AbstractStringValidator("Title must be unique") {

                        @Override
                        protected boolean isValidValue(String s) {
                            return (!WallboardProvider.getInstance().containsWallboard(s) && !"".equals(s));
                        }
                    });
                }
            });
            addComponent(new HorizontalLayout() {

                {
                    setMargin(true);
                    setSpacing(true);
                    setWidth("100%");
                    Button cancel = new Button("Cancel");
                    cancel.setDescription("Cancel editing");
                    cancel.addClickListener(new Button.ClickListener() {

                        @Override
                        public void buttonClick(Button.ClickEvent event) {
                            // NMS-7560: Toggle the tab in order to allow us to click it again
                            m_tabSheet.togglePlusTab();
                            window.close();
                        }
                    });
                    cancel.setClickShortcut(ShortcutAction.KeyCode.ESCAPE, null);
                    addComponent(cancel);
                    setExpandRatio(cancel, 1);
                    setComponentAlignment(cancel, Alignment.TOP_RIGHT);
                    Button ok = new Button("Save");
                    ok.setDescription("Save configuration");
                    ok.addClickListener(new Button.ClickListener() {

                        @Override
                        public void buttonClick(Button.ClickEvent event) {
                            if (name.isValid()) {
                                Wallboard wallboard = new Wallboard();
                                wallboard.setTitle(name.getValue());
                                WallboardProvider.getInstance().addWallboard(wallboard);
                                WallboardProvider.getInstance().save();
                                WallboardEditor wallboardEditor = new WallboardEditor(m_dashletSelector, wallboard);
                                TabSheet.Tab tab = m_tabSheet.addTab(wallboardEditor, wallboard.getTitle());
                                wallboardEditor.setTab(tab);
                                m_wallboardEditorMap.put(wallboard, tab);
                                tab.setClosable(true);
                                m_tabSheet.setSelectedTab(tab);
                                window.close();
                            }
                        }
                    });
                    ok.setClickShortcut(ShortcutAction.KeyCode.ENTER, null);
                    addComponent(ok);
                }
            });
        }
    });
}
Also used : Window(com.vaadin.ui.Window) FormLayout(com.vaadin.ui.FormLayout) AbstractStringValidator(com.vaadin.data.validator.AbstractStringValidator) Wallboard(org.opennms.features.vaadin.dashboard.model.Wallboard) HorizontalLayout(com.vaadin.ui.HorizontalLayout) Tab(com.vaadin.ui.TabSheet.Tab) Button(com.vaadin.ui.Button) VerticalLayout(com.vaadin.ui.VerticalLayout) TextField(com.vaadin.ui.TextField)

Aggregations

AbstractStringValidator (com.vaadin.data.validator.AbstractStringValidator)1 Button (com.vaadin.ui.Button)1 FormLayout (com.vaadin.ui.FormLayout)1 HorizontalLayout (com.vaadin.ui.HorizontalLayout)1 Tab (com.vaadin.ui.TabSheet.Tab)1 TextField (com.vaadin.ui.TextField)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1 Window (com.vaadin.ui.Window)1 Wallboard (org.opennms.features.vaadin.dashboard.model.Wallboard)1