Example usage for org.apache.wicket Page visitChildren

List of usage examples for org.apache.wicket Page visitChildren

Introduction

In this page you can find the example usage for org.apache.wicket Page visitChildren.

Prototype

public final <S extends Component, R> R visitChildren(final Class<?> clazz, final IVisitor<S, R> visitor) 

Source Link

Document

Traverses all child components of the given class in this container, calling the visitor's visit method at each one.

Usage

From source file:com.servoy.j2db.server.headlessclient.dataui.WebEventExecutor.java

License:Open Source License

@SuppressWarnings("nls")
public static void generateResponse(final AjaxRequestTarget target, Page page) {
    WebClientSession webClientSession = WebClientSession.get();
    if (target != null && page instanceof MainPage && webClientSession != null
            && webClientSession.getWebClient() != null
            && webClientSession.getWebClient().getSolution() != null) {
        if (target instanceof CloseableAjaxRequestTarget && ((CloseableAjaxRequestTarget) target).isClosed()) {
            return;
        }//from  w w w.j  a  va 2  s .c om
        // do executed the events for before generating the response.
        webClientSession.getWebClient().executeEvents();

        if (webClientSession.getWebClient() == null || webClientSession.getWebClient().getSolution() == null) {
            // how can web client be null here ?
            return;
        }
        final MainPage mainPage = ((MainPage) page);

        if (mainPage.getPageMap() instanceof ModifiedAccessStackPageMap) {
            // at every request mark the pagemap as dirty so lru eviction really works
            ((ModifiedAccessStackPageMap) mainPage.getPageMap()).flagDirty();
        }

        // If the main form is switched then do a normal redirect.
        if (mainPage.isMainFormSwitched()) {
            mainPage.versionPush();
            RequestCycle.get().setResponsePage(page);
        }

        else {
            page.visitChildren(WebTabPanel.class, new Component.IVisitor<WebTabPanel>() {
                public Object component(WebTabPanel component) {
                    component.initalizeFirstTab();
                    return IVisitor.CONTINUE_TRAVERSAL;
                }
            });

            mainPage.addWebAnchoringInfoIfNeeded();

            final Set<WebCellBasedView> tableViewsToRender = new HashSet<WebCellBasedView>();
            final List<String> valueChangedIds = new ArrayList<String>();
            final List<String> invalidValueIds = new ArrayList<String>();
            final Map<WebCellBasedView, List<Integer>> tableViewsWithChangedRowIds = new HashMap<WebCellBasedView, List<Integer>>();

            // first, get all invalidValue & valueChanged components
            page.visitChildren(IProviderStylePropertyChanges.class, new Component.IVisitor<Component>() {
                public Object component(Component component) {
                    if (component instanceof IDisplayData && !((IDisplayData) component).isValueValid()) {
                        invalidValueIds.add(component.getMarkupId());
                    }
                    if (((IProviderStylePropertyChanges) component).getStylePropertyChanges()
                            .isValueChanged()) {
                        if (component.getParent().isVisibleInHierarchy()) {
                            // the component will get added to the target & rendered only if it's parent is visible in hierarchy because changed flag is also set (see the visitor below)
                            // so we will only list these components if they are visible otherwise ajax timer could end up sending hundreds of id's that don't actually render every 5 seconds
                            // because the valueChanged flag is cleared only by onRender
                            valueChangedIds.add(component.getMarkupId());
                            if (component instanceof MarkupContainer) {
                                ((MarkupContainer) component).visitChildren(IDisplayData.class,
                                        new IVisitor<Component>() {
                                            public Object component(Component comp) {
                                                // labels/buttons that don't display data are not changed
                                                if (!(comp instanceof ILabel)) {
                                                    valueChangedIds.add(comp.getMarkupId());
                                                }
                                                return CONTINUE_TRAVERSAL;
                                            }
                                        });
                            }
                        }
                    }
                    return CONTINUE_TRAVERSAL;
                }
            });

            // add changed components to target; if a component is changed, the change check won't go deeper in hierarchy
            page.visitChildren(IProviderStylePropertyChanges.class, new Component.IVisitor<Component>() {
                public Object component(Component component) {
                    if (((IProviderStylePropertyChanges) component).getStylePropertyChanges().isChanged()) {
                        if (component.getParent().isVisibleInHierarchy()) {
                            target.addComponent(component);
                            generateDragAttach(component, target.getHeaderResponse());

                            WebForm parentForm = component.findParent(WebForm.class);
                            boolean isDesignMode = parentForm != null && parentForm.isDesignMode();

                            if (!component.isVisible() || (component instanceof WrapperContainer
                                    && !((WrapperContainer) component).getDelegate().isVisible())) {
                                ((IProviderStylePropertyChanges) component).getStylePropertyChanges()
                                        .setRendered();
                                if (isDesignMode) {
                                    target.appendJavascript("Servoy.ClientDesign.hideSelected('"
                                            + component.getMarkupId() + "')");
                                }
                            } else {
                                if (isDesignMode) {
                                    target.appendJavascript("Servoy.ClientDesign.refreshSelected('"
                                            + component.getMarkupId() + "')");
                                }
                                // some components need to perform js layout tasks when their markup is replaced when using anchored layout
                                mainPage.getPageContributor().markComponentForAnchorLayoutIfNeeded(component);
                            }

                            ListItem<IRecordInternal> row = component.findParent(ListItem.class);
                            if (row != null) {
                                WebCellBasedView wcbv = row.findParent(WebCellBasedView.class);
                                if (wcbv != null) {
                                    if (tableViewsWithChangedRowIds.get(wcbv) == null) {
                                        tableViewsWithChangedRowIds.put(wcbv, new ArrayList<Integer>());
                                    }
                                    List<Integer> ids = tableViewsWithChangedRowIds.get(wcbv);
                                    int changedRowIdx = wcbv.indexOf(row);
                                    if (changedRowIdx >= 0 && !ids.contains(changedRowIdx)) {
                                        ids.add(changedRowIdx);
                                    }
                                }
                            }
                        }
                        return IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
                    } else if (component instanceof WebCellBasedView)
                        tableViewsToRender.add((WebCellBasedView) component);
                    return component.isVisible() ? IVisitor.CONTINUE_TRAVERSAL
                            : IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
                }
            });

            page.visitChildren(IComponentToRequestAttacher.class, new Component.IVisitor<Component>() {
                public Object component(Component component) {
                    ((IComponentToRequestAttacher) component).attachComponents(target);
                    return IVisitor.CONTINUE_TRAVERSAL;
                }
            });

            final List<String> visibleEditors = new ArrayList<String>();
            page.visitChildren(WebDataHtmlArea.class, new Component.IVisitor<Component>() {
                public Object component(Component component) {
                    visibleEditors.add(((WebDataHtmlArea) component).getEditorID());
                    return IVisitor.CONTINUE_TRAVERSAL;
                }
            });
            StringBuffer argument = new StringBuffer();
            for (String id : visibleEditors) {
                argument.append("\"");
                argument.append(id);
                argument.append("\"");
                if (visibleEditors.indexOf(id) != visibleEditors.size() - 1) {
                    argument.append(",");
                }
            }
            target.prependJavascript("Servoy.HTMLEdit.removeInvalidEditors(" + argument + ");");

            String rowSelectionScript, columnResizeScript;
            for (final WebCellBasedView wcbv : tableViewsToRender) {
                if (wcbv.isScrollMode())
                    wcbv.scrollViewPort(target, true);
                wcbv.updateRowSelection(target);
                List<Integer> changedIds = tableViewsWithChangedRowIds.get(wcbv);
                List<Integer> selectedIndexesChanged = wcbv.getIndexToUpdate(false);
                List<Integer> mergedIds = selectedIndexesChanged != null ? selectedIndexesChanged
                        : new ArrayList<Integer>();
                if (changedIds != null) {
                    for (Integer id : changedIds) {
                        if (!mergedIds.contains(id)) {
                            mergedIds.add(id);
                        }
                    }
                }
                rowSelectionScript = wcbv.getRowSelectionScript(mergedIds);
                wcbv.clearSelectionByCellActionFlag();
                if (rowSelectionScript != null)
                    target.appendJavascript(rowSelectionScript);
                columnResizeScript = wcbv.getColumnResizeScript();
                if (columnResizeScript != null)
                    target.appendJavascript(columnResizeScript);
            }

            // double check if the page contributor is changed, because the above IStylePropertyChanges ischanged could have altered it.
            if (mainPage.getPageContributor().getStylePropertyChanges().isChanged()) {
                target.addComponent((Component) mainPage.getPageContributor());
            }
            if (invalidValueIds.size() == 0) {
                target.appendJavascript("setValidationFailed(null);"); //$NON-NLS-1$
            } else {
                target.appendJavascript("setValidationFailed('" + invalidValueIds.get(0) + "');"); //$NON-NLS-1$
            }
            Component comp = mainPage.getAndResetToFocusComponent();
            if (comp != null) {
                if (comp instanceof WebDataHtmlArea) {
                    target.appendJavascript("tinyMCE.activeEditor.focus()");
                } else {
                    target.focusComponent(comp);
                }
            } else if (mainPage.getAndResetMustFocusNull()) {
                // This is needed for example when showing a non-modal dialog in IE7 (or otherwise
                // the new window would be displayed in the background).
                target.focusComponent(null);
            }
            if (valueChangedIds.size() > 0) {
                argument = new StringBuffer();
                for (String id : valueChangedIds) {
                    argument.append("\"");
                    argument.append(id);
                    argument.append("\"");
                    if (valueChangedIds.indexOf(id) != valueChangedIds.size() - 1) {
                        argument.append(",");
                    }
                }
                target.prependJavascript("storeValueAndCursorBeforeUpdate(" + argument + ");");
                target.appendJavascript("restoreValueAndCursorAfterUpdate();");
            }

            //if we have admin info, show it
            String adminInfo = mainPage.getAdminInfo();
            if (adminInfo != null) {
                adminInfo = Utils.stringReplace(adminInfo, "\r", "");
                adminInfo = Utils.stringReplace(adminInfo, "\n", "\\n");
                target.appendJavascript("alert('Servoy admin info : " + adminInfo + "');");
            }

            // If we have a status text, set it.
            String statusText = mainPage.getStatusText();
            if (statusText != null) {
                target.appendJavascript("setStatusText('" + statusText + "');");
            }

            String show = mainPage.getShowUrlScript();
            if (show != null) {
                target.appendJavascript(show);
            }

            mainPage.renderJavascriptChanges(target);

            if (((WebClientInfo) webClientSession.getClientInfo()).getProperties().isBrowserInternetExplorer()
                    && ((WebClientInfo) webClientSession.getClientInfo()).getProperties()
                            .getBrowserVersionMajor() < 9) {
                target.appendJavascript("Servoy.Utils.checkWebFormHeights();");
            }
            try {
                if (isStyleSheetLimitForIE(page.getSession())) {
                    target.appendJavascript("testStyleSheets();");
                }
            } catch (Exception e) {
                Debug.error(e);//cannot retrieve session/clientinfo/properties?
                target.appendJavascript("testStyleSheets();");
            }
        }
    }
}

From source file:com.servoy.j2db.server.headlessclient.PageContributor.java

License:Open Source License

public PageContributor(final IApplication application, String id) {
    super(id, new Model());
    this.application = application;
    setOutputMarkupPlaceholderTag(true);

    add(new AbstractServoyDefaultAjaxBehavior() {
        private static final long serialVersionUID = 1L;

        @Override/*from ww w .j a  va 2s  .  c om*/
        protected void respond(AjaxRequestTarget target) {
            String update = getRequestCycle().getRequest().getParameter("update"); //$NON-NLS-1$
            // get the update parameter and check if that is still the same, else wait for the next.
            if (Long.parseLong(update) == lastTableUpdate) {
                for (int i = 0; i < tablesToRender.size(); i++) {
                    Component comp = tablesToRender.get(i);
                    if (comp.isVisibleInHierarchy()) {
                        target.addComponent(comp);
                    }
                }
                tablesToRender.clear();
                WebEventExecutor.generateResponse(target, findPage());
            } else {
                Debug.log("IGNORED TABLE REQUEST");
            }
        }

        @Override
        public void renderHead(IHeaderResponse response) {
            super.renderHead(response);
            response.renderOnDomReadyJavascript(getCallbackScript().toString());
        }

        @Override
        public CharSequence getCallbackUrl(boolean onlyTargetActivePage) {
            CharSequence url = super.getCallbackUrl(true);
            url = url + "&update=" + lastTableUpdate; //$NON-NLS-1$
            return url;
        }

        @Override
        public boolean isEnabled(Component component) {
            return tablesToRender.size() > 0 && super.isEnabled(component);
        }
    });
    add(eventCallbackBehavior = new EventCallbackBehavior());
    add(new AbstractServoyDefaultAjaxBehavior() {
        @Override
        public void renderHead(IHeaderResponse response) {
            if (isFormWidthZero()) {
                response.renderOnLoadJavascript("Servoy.Resize.onWindowResize();"); //$NON-NLS-1$
            }
        }

        @Override
        protected void respond(AjaxRequestTarget target) {
            // not used
        }

        private boolean isFormWidthZero() {
            final boolean[] returnValue = { false };
            Page page = findPage();
            if (page != null) {
                page.visitChildren(WebForm.class, new Component.IVisitor<WebForm>() {
                    public Object component(WebForm form) {
                        if (form.getFormWidth() == 0 && form.isVisibleInHierarchy()) {
                            IWebFormContainer formContainer = form.findParent(IWebFormContainer.class);
                            if (!(formContainer instanceof WebSplitPane)) {
                                returnValue[0] = true;
                                return IVisitor.STOP_TRAVERSAL;
                            }
                        }
                        return IVisitor.CONTINUE_TRAVERSAL;
                    }
                });
            }
            return returnValue[0];
        }
    });
}

From source file:org.apache.isis.viewer.wicket.ui.panels.FormExecutorDefault.java

License:Apache License

private boolean hasBlobsOrClobs(final Page page) {

    // this is a bit of a hack... currently the blob/clob panel doesn't correctly redraw itself.
    // we therefore force a re-forward (unless is declared as unchanging).
    final Object hasBlobsOrClobs = page.visitChildren(IsisBlobOrClobPanelAbstract.class,
            new IVisitor<IsisBlobOrClobPanelAbstract, Object>() {
                @Override/*from  w w w. ja va2  s . c  o  m*/
                public void component(final IsisBlobOrClobPanelAbstract object, final IVisit<Object> visit) {
                    if (!isUnchanging(object)) {
                        visit.stop(true);
                    }
                }

                private boolean isUnchanging(final IsisBlobOrClobPanelAbstract object) {
                    final ScalarModel scalarModel = (ScalarModel) object.getModel();
                    final UnchangingFacet unchangingFacet = scalarModel.getFacet(UnchangingFacet.class);
                    return unchangingFacet != null && unchangingFacet.value();
                }

            });
    return hasBlobsOrClobs != null;
}

From source file:org.artifactory.common.wicket.util.AjaxUtils.java

License:Open Source License

public static void refreshFeedback(final AjaxRequestTarget target) {
    Page page = WicketUtils.getPage();
    if (page == null || target == null) {
        return;/*  ww w .  j a  va  2 s .c o m*/
    }
    page.visitChildren(IFeedback.class, new IVisitor<Component, Void>() {
        @Override
        public void component(Component component, IVisit<Void> visit) {
            if (component.getOutputMarkupId()) {
                target.add(component);
            }
        }
    });
}

From source file:org.cast.isi.page.ISIStatelessPage.java

License:Open Source License

private void checkIfPageStateless(Page p) {
    if (!p.isPageStateless()) {
        // find out why
        final List<Component> statefulComponents = new ArrayList<Component>();
        p.visitChildren(Component.class, new IVisitor<Component, Void>() {
            public void component(Component component, IVisit<Void> visit) {
                if (!component.isStateless()) {
                    statefulComponents.add(component);
                }/*  ww  w. java2s. co m*/
            }
        });

        String message = "Whoops! this page is no longer stateless";
        if (statefulComponents.size() > 0) {
            message += " - the reason is that it contains the following stateful components: ";
            for (Component c : statefulComponents) {
                message += "\n" + c.getMarkupId() + "[" + c.getClass().getCanonicalName() + "]";
            }
        }
        log.warn(message);
    }

}

From source file:org.opensingular.form.wicket.util.WicketFormProcessing.java

License:Apache License

/**
 * Atualiza todos os componentes vinculados as instancias informadas
 *//*w w w .ja va 2  s  . c om*/
private static void updateBoundComponents(Page page, AjaxRequestTarget target, Set<SInstance> instances) {
    page.visitChildren(Component.class, new SInstanceBoudComponentUpdateVisitor(target, instances));
}