Example usage for com.vaadin.ui.declarative Design read

List of usage examples for com.vaadin.ui.declarative Design read

Introduction

In this page you can find the example usage for com.vaadin.ui.declarative Design read.

Prototype

public static DesignContext read(InputStream stream, Component rootComponent) 

Source Link

Document

Loads a design from the given stream using the given root component.

Usage

From source file:com.vaadHL.example.jpa.MainW.java

License:Apache License

public MainW(final IAppContext appContext) {
    super();/* ww w  .  ja v a2 s .c o  m*/

    Design.read("MainDes.html", this);

    this.appContext = appContext;
    em = ENTITY_MANAGER_FACTORY.createEntityManager();
    m = appContext.getI18();

    // ========== list window launch mode =============

    chkReadOnly.addValueChangeListener(new ValueChangeListener() {
        private static final long serialVersionUID = -5304522804592982630L;

        @Override
        public void valueChange(ValueChangeEvent event) {
            listReadOnly = (boolean) event.getProperty().getValue();

        }
    });

    ComponentHelper.populateWIds(cbChoosingMode, m.getArryString("choosingModeS"));
    cbChoosingMode.setNullSelectionAllowed(false);
    cbChoosingMode.addValueChangeListener(new ValueChangeListener() {

        private static final long serialVersionUID = 2649106479371080195L;

        @Override
        public void valueChange(ValueChangeEvent event) {
            if (disableListeners)
                return;
            Object o = cbChoosingMode.getValue();
            if (o == null)
                return;
            int val = (int) o;
            listChoosingMode = (ChoosingMode.values()[val]);
            if (val == 0) {
                laCustNCM.setVisible(true);
                laCustChM.setVisible(false);
            } else {
                laCustNCM.setVisible(false);
                laCustChM.setVisible(true);
            }
        }
    });
    cbChoosingMode.setValue(0);

    String[] lanuageS = { "Default language", "Deutsch", "English", "Polski" };
    final Locale[] locale = { Locale.getDefault(), new Locale("de", "DE"), new Locale("en", "US"),
            new Locale("pl", "PL") };
    ComponentHelper.populateWIds(cbLanguage, lanuageS);
    cbLanguage.setValue(0);
    cbLanguage.setNullSelectionAllowed(false);
    cbLanguage.addValueChangeListener(new ValueChangeListener() {
        @Override
        public void valueChange(ValueChangeEvent event) {
            int val = (int) cbLanguage.getValue();
            setLocale(locale[val]);
        }
    });

    CustomizerFactory customizerFactory = new CustomizerFactory();
    appContext.setWinCustomizerFactory(customizerFactory);
    // ========== list window customization =============

    // no-choose mode
    final LWCustomizeLM customizeLWNoCh = new LWCustomizeLM();

    BeanItem<LWCustomizeLM> customizeLWNoChBI = new BeanItem<LWCustomizeLM>(customizeLWNoCh);

    chkAutoSaveNCM.setPropertyDataSource(customizeLWNoChBI.getItemProperty("autoSaveState"));
    chkAutoRestoreNCM.setPropertyDataSource(customizeLWNoChBI.getItemProperty("autoRestoreState"));
    chkShowMenuNCM.setPropertyDataSource(customizeLWNoChBI.getItemProperty("showMenu"));
    chkDetails.setPropertyDataSource(customizeLWNoChBI.getItemProperty("detailsFunc"));
    chkAddRec.setPropertyDataSource(customizeLWNoChBI.getItemProperty("addFunc"));
    chkDeleteRec.setPropertyDataSource(customizeLWNoChBI.getItemProperty("deleteFunc"));
    chkEditRec.setPropertyDataSource(customizeLWNoChBI.getItemProperty("editFunc"));
    chkViewRec.setPropertyDataSource(customizeLWNoChBI.getItemProperty("viewFunc"));

    ComponentHelper.populateWIdsSkip(cbDoubleClick, m.getArryString("doubleClickModeS"), new Integer[] { 5 });
    cbDoubleClick.addValueChangeListener(new ValueChangeListener() {

        private static final long serialVersionUID = 2649106479371080195L;

        @Override
        public void valueChange(ValueChangeEvent event) {
            if (disableListeners)
                return;
            customizeLWNoCh.setDoubleClickAc(((DoubleClickAc.values()[((int) cbDoubleClick.getValue())])));

        }
    });
    cbDoubleClick.setNullSelectionAllowed(false);
    cbDoubleClick.setValue(0);

    // choose mode
    final LWCustomizeLM customizeLWChMo = new LWCustomizeLM();
    BeanItem<LWCustomizeLM> customizeLWBIChMo = new BeanItem<LWCustomizeLM>(customizeLWChMo);

    chkAutoSaveChM.setPropertyDataSource(customizeLWBIChMo.getItemProperty("autoSaveState"));
    chkAutoRestoreChM.setPropertyDataSource(customizeLWBIChMo.getItemProperty("autoRestoreState"));
    chkShowMenuChM.setPropertyDataSource(customizeLWBIChMo.getItemProperty("showMenu"));
    chkDetailsChM.setPropertyDataSource(customizeLWBIChMo.getItemProperty("detailsFunc"));
    chkAddRecChM.setPropertyDataSource(customizeLWBIChMo.getItemProperty("addFunc"));
    chkDeleteRecChM.setPropertyDataSource(customizeLWBIChMo.getItemProperty("deleteFunc"));
    chkEditRecChM.setPropertyDataSource(customizeLWBIChMo.getItemProperty("editFunc"));
    chkViewRecChM.setPropertyDataSource(customizeLWBIChMo.getItemProperty("viewFunc"));

    cbDoubleClickChM.addValueChangeListener(new ValueChangeListener() {

        private static final long serialVersionUID = -5472665008056617982L;

        @Override
        public void valueChange(ValueChangeEvent event) {
            if (disableListeners)
                return;
            customizeLWChMo.setDoubleClickAc((DoubleClickAc.values()[((int) cbDoubleClickChM.getValue())]));

        }
    });
    cbDoubleClickChM.setNullSelectionAllowed(false);

    final LWCustomize customizeLW = new LWCustomize(customizeLWChMo, customizeLWNoCh);
    customizerFactory.put("L001", customizeLW);

    // ========== form window customization =============
    customizeFWin = new CustomizeFWin();
    final BeanItem<CustomizeFWin> customizeFWinBI = new BeanItem<CustomizeFWin>(customizeFWin);

    chkAutoSave.setPropertyDataSource(customizeFWinBI.getItemProperty("autoSaveState"));
    chkAutoRestore.setPropertyDataSource(customizeFWinBI.getItemProperty("autoRestoreState"));
    chkShowMenu.setPropertyDataSource(customizeFWinBI.getItemProperty("showMenu"));
    chkAskCreate.setPropertyDataSource(customizeFWinBI.getItemProperty("askCreate"));
    chkAskDelete.setPropertyDataSource(customizeFWinBI.getItemProperty("askDelete"));
    chkAskDiscard.setPropertyDataSource(customizeFWinBI.getItemProperty("askDiscard"));
    chkAskSave.setPropertyDataSource(customizeFWinBI.getItemProperty("askSave"));
    chkPrevNext.setPropertyDataSource(customizeFWinBI.getItemProperty("prevNextFunc"));

    ComponentHelper.populateWIds(cbAutoSaveDiscard, m.getArryString("autoSaveDiscard"));
    cbAutoSaveDiscard.setNullSelectionAllowed(false);
    cbAutoSaveDiscard.setValue(0);
    cbAutoSaveDiscard.addValueChangeListener(new ValueChangeListener() {
        private static final long serialVersionUID = -5304522804592982630L;

        @Override
        public void valueChange(ValueChangeEvent event) {
            if (disableListeners)
                return;
            customizeFWin.setAutoSaveDiscard(AutoSaveDiscard.values()[((int) cbAutoSaveDiscard.getValue())]);
        }
    });

    ComponentHelper.populateWIds(cbClosingMethod, m.getArryString("closinMethod"));
    cbClosingMethod.setValue(0);
    cbClosingMethod.setNullSelectionAllowed(false);
    cbClosingMethod.addValueChangeListener(new ValueChangeListener() {
        private static final long serialVersionUID = -8412527417505085156L;

        @Override
        public void valueChange(ValueChangeEvent event) {
            if (disableListeners)
                return;
            customizeFWin.setShowOKCancel(((int) cbClosingMethod.getValue()) == 0 ? true : false);
        }
    });

    customizerFactory.put("M001", customizeFWin);

    // ========== mock permissions =============
    permissions = new TestPermCheckerB(appContext);
    appContext.setWinPermFactory(permissions);

    permissions.put("L001", MyActionsIds.MOCK_ID, false);

    BeanContainer<String, PermItem> permContainer = new BeanContainer<String, PermItem>(PermItem.class);
    permContainer.setBeanIdResolver(new BeanIdResolver<String, TestPermCheckerB.PermItem>() {
        private static final long serialVersionUID = -2858748109465649052L;

        @Override
        public String getIdForBean(PermItem bean) {
            return bean.getWinId() + Integer.toString(bean.getPermId());
        }
    });
    permContainer.addAll(permissions.getContainer().values());

    tPerm.setContainerDataSource(permContainer);
    tPerm.setPageLength(15);

    // immediately
    tPerm.setEditable(true);
    tPerm.setSelectable(false);
    tPerm.setColumnReorderingAllowed(true);
    tPerm.setColumnCollapsingAllowed(true);

    /*
     * tPerm.addGeneratedColumn("permission", new ColumnGenerator() {
     * private static final long serialVersionUID = 8174249667866723293L;
     * 
     * @Override public Object generateCell(Table source, Object itemId,
     * Object columnId) { Item itm = source.getItem(itemId); int acId =
     * (int) itm.getItemProperty("permId").getValue(); return
     * MyActionsIds.getName(appContext.getI18(), acId);
     * 
     * } });
     */

    /*
     * tPerm.addGeneratedColumn("winIdShow", new ColumnGenerator() { private
     * static final long serialVersionUID = 7789570007862946545L;
     * 
     * @Override public Object generateCell(Table source, Object itemId,
     * Object columnId) { Item itm = source.getItem(itemId); String v =
     * (String) itm.getItemProperty("winId").getValue();
     * 
     * return v;
     * 
     * } });
     */

    tPerm.setTableFieldFactory(new TableFieldFactory() {
        private static final long serialVersionUID = 7459048926545235356L;

        public Field<?> createField(Container container, Object itemId, Object propertyId,
                Component uiContext) {
            Field<?> f = DefaultFieldFactory.get().createField(container, itemId, propertyId, uiContext);
            if ("enabled".equals(propertyId))
                f.setCaption("");
            else if ("permission".equals(propertyId))
                f.setWidth("170px");
            else if ("winIdShow".equals(propertyId))
                f.setWidth("70px");

            return f;
        }
    });

    tPerm.setVisibleColumns("winIdShow", "permission", "enabled");
    tPerm.setColumnWidth("winIdShow", 80);
    tPerm.setColumnWidth("permission", -1);
    tPerm.setColumnWidth("enabled", 60);

    // ==========List selection results =============

    // ========== run the test ============================
    btRunTest.addClickListener(new ClickListener() {
        private static final long serialVersionUID = -5384467222048646753L;

        @Override
        public void buttonClick(ClickEvent event) {

            ListTst sub = new ListTst(listChoosingMode, listReadOnly, em, appContext);

            sub.addCloseListener(new CloseListener() {

                /**
                 * 
                 */
                private static final long serialVersionUID = -5625061490326702522L;

                @Override
                public void windowClose(CloseEvent e) {

                    CloseCause clcs = ((BaseListWindow) e.getSource()).getCloseCause();
                    if (clcs == null)
                        return;
                    Object items = clcs.addInfo;

                    switch (clcs.cause) {
                    case CHOOSE:
                        StringBuffer sb = new StringBuffer();
                        sb.append(m.getStringNE("ChoosenS"));
                        if (items == null)
                            sb.append(m.getStringNE("nothing"));
                        else {
                            Set<Object> se;
                            if (items instanceof Set<?>)
                                se = (Set<Object>) items;
                            else {
                                se = new HashSet<Object>();
                                se.add(items);
                            }

                            for (Object it : se) {
                                ItemHelper item = new ItemHelper((Item) it);
                                sb.append('\n');
                                sb.append(item.getString("firstName"));
                                sb.append(" ");
                                sb.append(item.getString("lastName"));
                                sb.append(" ");
                                sb.append(item.getString("yearOfBirth"));
                            }

                        }
                        taChoosen.setReadOnly(false);
                        taChoosen.setValue(sb.toString());
                        taChoosen.setReadOnly(true);
                        break;
                    case CANCEL:
                        taChoosen.setReadOnly(false);
                        taChoosen.setValue(m.getStringNE("canceledN"));
                        taChoosen.setReadOnly(true);
                        break;
                    case NOCHOOSE:
                        taChoosen.setReadOnly(false);
                        taChoosen.setValue(m.getStringNE("clNoSel"));
                        taChoosen.setReadOnly(true);
                    default:
                        break;
                    }
                }
            });
            UI.getCurrent().addWindow(sub);
        }
    });

    // exit
    btExit.addClickListener(new ClickListener() {
        private static final long serialVersionUID = 3384397496569551149L;

        @Override
        public void buttonClick(ClickEvent event) {
            for (final UI ui : VaadinSession.getCurrent().getUIs())
                ui.access(new Runnable() {

                    @Override
                    public void run() {
                        ui.getPage().setLocation("http://vaadhl.mromaniuk.me");
                    }
                });
            getSession().close();
        }
    });

    // versions info
    lbDemoVer.setValue(Version.getFullVersion());
    lbHLVer.setValue(com.vaadHL.Version.getFullVersion());
    lbVaadVer.setValue(com.vaadin.shared.Version.getFullVersion());
}

From source file:org.opencms.ui.CmsVaadinUtils.java

License:Open Source License

/**
 * Reads the given design and resolves the given macros and localizations.<p>
        //from  ww w  .jav  a 2 s.co  m
 * @param component the component whose design to read
 * @param designStream stream to read the design from
 * @param messages the message bundle to use for localization in the design (may be null)
 * @param macros other macros to substitute in the macro design (may be null)
 */
protected static void readAndLocalizeDesign(Component component, InputStream designStream, CmsMessages messages,
        Map<String, String> macros) {

    try {
        byte[] designBytes = CmsFileUtil.readFully(designStream, true);
        final String encoding = "UTF-8";
        String design = new String(designBytes, encoding);
        CmsMacroResolver resolver = new CmsMacroResolver() {

            @Override
            public String getMacroValue(String macro) {

                String result = super.getMacroValue(macro);
                // The macro may contain quotes or angle brackets, so we need to escape the values for insertion into the design file
                return CmsEncoder.escapeXml(result);

            }
        };

        if (macros != null) {
            for (Map.Entry<String, String> entry : macros.entrySet()) {
                resolver.addMacro(entry.getKey(), entry.getValue());
            }
        }
        if (messages != null) {
            resolver.setMessages(messages);
        }
        String resolvedDesign = resolver.resolveMacros(design);
        Design.read(new ByteArrayInputStream(resolvedDesign.getBytes(encoding)), component);
    } catch (IOException e) {
        throw new RuntimeException("Could not read design", e);
    } finally {
        try {
            designStream.close();
        } catch (IOException e) {
            LOG.warn(e.getLocalizedMessage(), e);
        }
    }
}

From source file:org.opencms.ui.components.CmsAppViewLayout.java

License:Open Source License

/**
 * Constructor.<p>/* w  w w  .j  a va 2  s  .  c om*/
 *
 * @param appId the app id
 */
public CmsAppViewLayout(String appId) {

    m_appId = appId;
    Design.read("CmsAppView.html", this);
    Responsive.makeResponsive(this);
    // setting the width to 100% within the java code is required by the responsive resize listeners
    setWidth("100%");
    m_toolbar.init(m_appId);
}

From source file:org.opencms.ui.components.CmsToolBar.java

License:Open Source License

/**
 * Constructor.<p>//from ww w  .  j  a  v  a 2  s  .  com
 */
public CmsToolBar() {
    m_quickLaunchDropDown = createQuickLaunchDropDown();
    m_userDropDown = createUserInfoDropDown();
    m_leftButtons = new VerticalLayout();
    m_rightButtons = new VerticalLayout();
    VerticalLayout layout = new VerticalLayout();
    layout.addComponent(m_leftButtons);
    layout.addComponent(m_rightButtons);
    m_foldedButtonsMenu = new PopupView(getDropDownButtonHtml(FontOpenCms.CONTEXT_MENU_DOTS), layout);
    m_foldedButtonsMenu.addStyleName(OpenCmsTheme.NAVIGATOR_DROPDOWN);
    m_foldedButtonsMenu.setHideOnMouseOut(false);
    Design.read("CmsToolBar.html", this);
}

From source file:org.vaadin.artur.declarative.DeclarativeEditor.java

License:Apache License

protected void updateTree(String string) {
    if (disableEvents) {
        return;/*from   w ww.  j av  a 2s  .com*/
    }

    dc = Design.read(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)), null);
    treeHolder.removeAllComponents();
    if (dc.getRootComponent() != null) {
        treeHolder.addComponent(dc.getRootComponent());
        addValueChangeListeners(dc.getRootComponent());
    }
}