Example usage for org.apache.wicket Component getParent

List of usage examples for org.apache.wicket Component getParent

Introduction

In this page you can find the example usage for org.apache.wicket Component getParent.

Prototype

@Override
public final MarkupContainer getParent() 

Source Link

Document

Gets any parent container, or null if there is none.

Usage

From source file:com.pingunaut.wicket.chartjs.core.ChartBehavior.java

License:Apache License

@Override
public void renderHead(Component component, IHeaderResponse response) {
    super.renderHead(component, response);

    // ok, we need jQuery
    response.render(JavaScriptHeaderItem.forReference(JQueryResourceReference.get()));

    ClientProperties clientProperties = ((WebSession) Session.get()).getClientInfo().getProperties();
    boolean isIE = clientProperties.isBrowserInternetExplorer();
    boolean isLowerThan9 = clientProperties.getBrowserVersionMajor() < 9;
    isCanvasSupported = !(isIE && isLowerThan9);
    // ie lower than 9 doesn't know what to do with canvas and some js... so
    // we'll teach him...
    if (!isCanvasSupported) {
        response.render(JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(
                AbstractChartPanel.class, "modernizr-2.6.2-respond-1.1.0.min.js")));
        response.render(JavaScriptHeaderItem
                .forReference(new JavaScriptResourceReference(AbstractChartPanel.class, "excanvas.js")));
    }//from  www  . j  ava2  s  . co m

    response.render(JavaScriptHeaderItem
            .forReference(new JavaScriptResourceReference(AbstractChartPanel.class, "ChartNew.js")));
    response.render(JavaScriptHeaderItem
            .forReference(new JavaScriptResourceReference(AbstractChartPanel.class, "bridge.js")));

    // chart.js docs describe a problem with initializing canvas context
    // onDomReady in IE < 9. to avoid that, onLoad is used in that case
    // instead
    if (isCanvasSupported) {
        response.render(OnDomReadyHeaderItem.forScript("WicketCharts['" + component.getMarkupId()
                + "']=buildChart('" + component.getMarkupId() + "');"));
    } else {
        response.render(OnLoadHeaderItem.forScript("WicketCharts['" + component.getMarkupId()
                + "']=buildChart('" + component.getMarkupId() + "');"));

    }

    if (component.getParent() instanceof AbstractChartPanel) {
        // another IE crap... animation is deactivated for versions < 9
        // because it's not working anyway
        if (isCanvasSupported) {
            response.render(OnDomReadyHeaderItem
                    .forScript(((AbstractChartPanel) component.getParent()).generateChart()));
        } else {
            ((AbstractChartPanel) component.getParent()).getChart().getOptions().setAnimation(false);
            response.render(
                    OnLoadHeaderItem.forScript(((AbstractChartPanel) component.getParent()).generateChart()));
        }

    }
}

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

License:Open Source License

@Override
protected void onAjaxEvent(AjaxRequestTarget target) {
    // update the container (parent) of the pageable, this assumes that
    // the pageable is a component, and that it is a child of a web
    // markup container.

    Component container = ((Component) getPageable());
    // no need for a nullcheck as there is bound to be a non-repeater
    // somewhere higher in the hierarchy
    while (container instanceof AbstractRepeater) {
        container = container.getParent();
    }/*  ww  w  . j a  v  a 2s. c  o m*/

    Component pageableComp = container;
    while (!(container instanceof WebCellBasedView)) {
        container = container.getParent();
    }
    target.addComponent(container != null ? container : pageableComp);

    // in case the navigator is not contained by the container, we have
    // to add it to the response
    if (((MarkupContainer) container).contains(this, true) == false) {
        target.addComponent(this);
    }
}

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

License:Open Source License

/**
 * @see org.apache.wicket.markup.html.form.IFormSubmittingComponent#getInputName()
 *///from   w  ww  . j ava  2 s  .c o  m
public String getInputName() {

    // TODO: This is a copy & paste from the FormComponent class. 
    String id = getId();
    final PrependingStringBuffer inputName = new PrependingStringBuffer(id.length());
    Component c = this;
    while (true) {
        inputName.prepend(id);
        c = c.getParent();
        if (c == null || (c instanceof Form && ((Form) c).isRootForm()) || c instanceof Page) {
            break;
        }
        inputName.prepend(Component.PATH_SEPARATOR);
        id = c.getId();
    }
    return inputName.toString();
}

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

License:Open Source License

private Form<?> getForm() {
    Component c = this;
    while ((c != null) && !(c instanceof Form))
        c = c.getParent();
    return (Form<?>) c;
}

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

License:Open Source License

protected Form<?> getForm() {
    Component c = this;
    while ((c != null) && !(c instanceof Form))
        c = c.getParent();
    return (Form<?>) c;
}

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

License:Open Source License

static void setParentBGcolor(Component comp, Object compColor) {
    MarkupContainer cellContainer = comp.getParent();
    String compColorStr = compColor.toString();
    if (cellContainer instanceof CellContainer) {
        CellContainer cell = (CellContainer) cellContainer;
        if (compColorStr != null && !"".equals(compColorStr)) //$NON-NLS-1$)
        {/*from   w  w  w  .j  av a2  s  . c o m*/
            //remove background color
            if (cell.getBehaviors().contains(cell.backgroundModifier))
                cell.remove(cell.backgroundModifier);
        } else {
            //remove update backgroundcolor
            if (cell.getBehaviors().contains(cell.backgroundModifier))
                cell.remove(cell.backgroundModifier);
            cell.backgroundModifier = new StyleAppendingModifier(
                    new Model<String>("background-color: " + compColorStr));
            cell.add(cell.backgroundModifier);
        }
    }
}

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

License:Open Source License

/**
 * @param component//  www .  j  a  v a2 s  .  c  o m
 */
@SuppressWarnings("nls")
public static boolean setSelectedIndex(Component component, AjaxRequestTarget target, int modifiers,
        boolean bHandleMultiselect) {
    WebForm parentForm = component.findParent(WebForm.class);
    WebCellBasedView tableView = null;
    if (parentForm != null) {
        int parentFormViewType = parentForm.getController().getForm().getView();
        if (parentFormViewType == FormController.TABLE_VIEW
                || parentFormViewType == FormController.LOCKED_TABLE_VIEW
                || parentFormViewType == IForm.LIST_VIEW
                || parentFormViewType == FormController.LOCKED_LIST_VIEW) {
            tableView = component.findParent(WebCellBasedView.class);
            if (tableView == null) {
                // the component is not part of the table view (it is on other form part), so ignore selection change
                return true;
            } else
                tableView.setSelectionMadeByCellAction();

            if (parentFormViewType == IForm.LIST_VIEW
                    || parentFormViewType == FormController.LOCKED_LIST_VIEW) {
                if (component instanceof WebCellBasedViewListViewItem) {
                    ((WebCellBasedViewListViewItem) component).markSelected(target);
                } else {
                    WebCellBasedViewListViewItem listViewItem = component
                            .findParent(WebCellBasedView.WebCellBasedViewListViewItem.class);
                    if (listViewItem != null) {
                        listViewItem.markSelected(target);
                    }
                }
            }
        }
    }

    //search for recordItem model
    Component recordItemModelComponent = component;
    IModel<?> someModel = recordItemModelComponent.getDefaultModel();
    while (!(someModel instanceof RecordItemModel)) {
        recordItemModelComponent = recordItemModelComponent.getParent();
        if (recordItemModelComponent == null)
            break;
        someModel = recordItemModelComponent.getDefaultModel();
    }

    if (someModel instanceof RecordItemModel) {
        if (!(component instanceof WebCellBasedViewListViewItem)) {
            // update the last rendered value for the events component (if updated)
            ((RecordItemModel) someModel).updateRenderedValue(component);
        }

        IRecordInternal rec = (IRecordInternal) someModel.getObject();
        if (rec != null) {
            int index;
            IFoundSetInternal fs = rec.getParentFoundSet();
            if (someModel instanceof FoundsetRecordItemModel) {
                index = ((FoundsetRecordItemModel) someModel).getRowIndex();
            } else {
                index = fs.getRecordIndex(rec); // this is used only on "else", because a "plugins.rawSQL.flushAllClientsCache" could result in index = -1 although the record has not changed (but record & underlying row instances changed)
            }

            if (fs instanceof FoundSet && ((FoundSet) fs).isMultiSelect()) {
                //set the selected record
                ClientProperties clp = ((WebClientInfo) Session.get().getClientInfo()).getProperties();
                String navPlatform = clp.getNavigatorPlatform();
                int controlMask = (navPlatform != null && navPlatform.toLowerCase().indexOf("mac") != -1)
                        ? Event.META_MASK
                        : Event.CTRL_MASK;

                boolean toggle = (modifiers != MODIFIERS_UNSPECIFIED) && ((modifiers & controlMask) != 0);
                boolean extend = (modifiers != MODIFIERS_UNSPECIFIED) && ((modifiers & Event.SHIFT_MASK) != 0);
                boolean isRightClick = (modifiers != MODIFIERS_UNSPECIFIED)
                        && ((modifiers & Event.ALT_MASK) != 0);

                if (!isRightClick) {
                    if (!toggle && !extend && tableView != null && tableView.getDragNDropController() != null
                            && Arrays.binarySearch(((FoundSet) fs).getSelectedIndexes(), index) > -1) {
                        return true;
                    }

                    if (toggle || extend) {
                        if (bHandleMultiselect) {
                            if (toggle) {
                                int[] selectedIndexes = ((FoundSet) fs).getSelectedIndexes();
                                ArrayList<Integer> selectedIndexesA = new ArrayList<Integer>();
                                Integer selectedIndex = new Integer(index);

                                for (int selected : selectedIndexes)
                                    selectedIndexesA.add(new Integer(selected));
                                if (selectedIndexesA.indexOf(selectedIndex) != -1) {
                                    if (selectedIndexesA.size() > 1)
                                        selectedIndexesA.remove(selectedIndex);
                                } else
                                    selectedIndexesA.add(selectedIndex);
                                selectedIndexes = new int[selectedIndexesA.size()];
                                for (int i = 0; i < selectedIndexesA.size(); i++)
                                    selectedIndexes[i] = selectedIndexesA.get(i).intValue();
                                ((FoundSet) fs).setSelectedIndexes(selectedIndexes);
                            } else if (extend) {
                                int anchor = ((FoundSet) fs).getSelectedIndex();
                                int min = Math.min(anchor, index);
                                int max = Math.max(anchor, index);

                                int[] newSelectedIndexes = new int[max - min + 1];
                                for (int i = min; i <= max; i++)
                                    newSelectedIndexes[i - min] = i;
                                ((FoundSet) fs).setSelectedIndexes(newSelectedIndexes);
                            }
                        }
                    } else if (index != -1 || fs.getSize() == 0) {
                        fs.setSelectedIndex(index);
                    }
                }
            } else if (!isIndexSelected(fs, index))
                fs.setSelectedIndex(index);
            if (!isIndexSelected(fs, index) && !(fs instanceof FoundSet && ((FoundSet) fs).isMultiSelect())) {
                // setSelectedIndex failed, probably due to validation failed, do a blur()
                if (target != null)
                    target.appendJavascript("var toBlur = document.getElementById(\"" + component.getMarkupId()
                            + "\");if (toBlur) toBlur.blur();");
                return false;
            }
        }
    }
    return true;
}

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

License:Open Source License

public WebTabPanel(IApplication application, final RuntimeTabPanel scriptable, String name, int orient,
        boolean oneTab) {
    super(name);/*from   ww w . ja v a2s.com*/
    this.application = application;
    this.orient = orient;

    final boolean useAJAX = Utils.getAsBoolean(application.getRuntimeProperties().get("useAJAX")); //$NON-NLS-1$
    setOutputMarkupPlaceholderTag(true);

    if (orient != TabPanel.SPLIT_HORIZONTAL && orient != TabPanel.SPLIT_VERTICAL)
        add(new Label("webform", new Model<String>("")));//temporary add, in case the tab panel does not contain any tabs //$NON-NLS-1$ //$NON-NLS-2$

    // TODO check ignore orient and oneTab??
    IModel<Integer> tabsModel = new AbstractReadOnlyModel<Integer>() {
        private static final long serialVersionUID = 1L;

        @Override
        public Integer getObject() {
            return Integer.valueOf(allTabs.size());
        }
    };

    if (orient != TabPanel.HIDE && orient != TabPanel.SPLIT_HORIZONTAL && orient != TabPanel.SPLIT_VERTICAL
            && !(orient == TabPanel.DEFAULT_ORIENTATION && oneTab)) {
        add(new Loop("tablinks", tabsModel) //$NON-NLS-1$
        {
            private static final long serialVersionUID = 1L;

            private String focusedItem;

            @Override
            protected void populateItem(final LoopItem item) {
                final WebTabHolder holder = allTabs.get(item.getIteration());
                MarkupContainer link = null;
                link = new ServoySubmitLink("tablink", useAJAX) //$NON-NLS-1$
                {
                    private static final long serialVersionUID = 1L;

                    /**
                     * @see wicket.ajax.markup.html.AjaxFallbackLink#onClick(wicket.ajax.AjaxRequestTarget)
                     */
                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        Page page = findPage();
                        if (page != null) {
                            setActiveTabPanel(holder.getPanel());
                            if (target != null) {
                                relinkAtTabPanel(WebTabPanel.this);
                                focusedItem = item.getId();
                                WebEventExecutor.generateResponse(target, page);
                            }
                        }
                    }

                    private void relinkAtForm(WebForm form) {
                        form.visitChildren(WebTabPanel.class, new IVisitor<WebTabPanel>() {
                            public Object component(WebTabPanel wtp) {
                                relinkAtTabPanel(wtp);
                                return IVisitor.CONTINUE_TRAVERSAL;
                            }
                        });
                    }

                    private void relinkAtTabPanel(WebTabPanel wtp) {
                        wtp.relinkFormIfNeeded();
                        wtp.visitChildren(WebForm.class, new IVisitor<WebForm>() {
                            public Object component(WebForm form) {
                                relinkAtForm(form);
                                return IVisitor.CONTINUE_TRAVERSAL;
                            }
                        });
                    }

                    @Override
                    protected void disableLink(final ComponentTag tag) {
                        // if the tag is an anchor proper
                        if (tag.getName().equalsIgnoreCase("a") || tag.getName().equalsIgnoreCase("link") //$NON-NLS-1$//$NON-NLS-2$
                                || tag.getName().equalsIgnoreCase("area")) //$NON-NLS-1$
                        {
                            // Remove any href from the old link
                            tag.remove("href"); //$NON-NLS-1$
                            tag.remove("onclick"); //$NON-NLS-1$
                        }
                    }

                };

                if (item.getId().equals(focusedItem)) {
                    IRequestTarget currentRequestTarget = RequestCycle.get().getRequestTarget();
                    if (currentRequestTarget instanceof AjaxRequestTarget) {
                        ((AjaxRequestTarget) currentRequestTarget).focusComponent(link);
                    }
                    focusedItem = null;
                }

                if (holder.getTooltip() != null) {
                    link.setMetaData(TooltipAttributeModifier.TOOLTIP_METADATA, holder.getTooltip());
                }

                TabIndexHelper.setUpTabIndexAttributeModifier(link, tabSequenceIndex);
                link.add(TooltipAttributeModifier.INSTANCE);

                if (item.getIteration() == 0)
                    link.add(new AttributeModifier("firsttab", true, new Model<Boolean>(Boolean.TRUE))); //$NON-NLS-1$
                link.setEnabled(holder.isEnabled() && WebTabPanel.this.isEnabled());

                String text = holder.getText();
                if (holder.getDisplayedMnemonic() > 0) {
                    final String mnemonic = Character.toString((char) holder.getDisplayedMnemonic());
                    link.add(new SimpleAttributeModifier("accesskey", mnemonic)); //$NON-NLS-1$
                    if (text != null && text.contains(mnemonic) && !HtmlUtils.hasUsefulHtmlContent(text)) {
                        StringBuffer sbBodyText = new StringBuffer(text);
                        int mnemonicIdx = sbBodyText.indexOf(mnemonic);
                        if (mnemonicIdx != -1) {
                            sbBodyText.insert(mnemonicIdx + 1, "</u>"); //$NON-NLS-1$
                            sbBodyText.insert(mnemonicIdx, "<u>"); //$NON-NLS-1$
                            text = sbBodyText.toString();
                        }
                    }
                }
                ServoyTabIcon tabIcon = new ServoyTabIcon("icon", holder, scriptable); //$NON-NLS-1$
                link.add(tabIcon);

                Label label = new Label("linktext", new Model<String>(text)); //$NON-NLS-1$
                label.setEscapeModelStrings(false);
                link.add(label);
                item.add(link);
                IModel<String> selectedOrDisabledClass = new AbstractReadOnlyModel<String>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        if (!holder.isEnabled() || !WebTabPanel.this.isEnabled()) {
                            if (currentForm == holder.getPanel()) {
                                return "disabled_selected_tab"; //$NON-NLS-1$
                            }
                            return "disabled_tab"; //$NON-NLS-1$
                        } else {
                            if (currentForm == holder.getPanel()) {
                                return "selected_tab"; //$NON-NLS-1$
                            }
                            return "deselected_tab"; //$NON-NLS-1$
                        }
                    }
                };
                item.add(new AttributeModifier("class", true, selectedOrDisabledClass)); //$NON-NLS-1$
                label.add(new StyleAppendingModifier(new Model<String>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        String style = "white-space: nowrap;"; //$NON-NLS-1$
                        if (foreground != null) {
                            style += " color:" + PersistHelper.createColorString(foreground); //$NON-NLS-1$
                        }
                        if (holder.getIcon() != null) {
                            style += "; padding-left: 3px"; //$NON-NLS-1$
                        }
                        return style;
                    }
                }));
            }
        });

        // All tab panels get their tabs rearranged after they make it to the browser.
        // On Chrome & Safari the tab rearrangement produces an ugly flicker effect, because
        // initially the tabs are not visible and then they are made visible. By
        // sending the tab as invisible and turning it to visible only after the tabs
        // are arranged, this jumping/flickering effect is gone. However a small delay can now be
        // noticed in Chrome & Safari, which should also be eliminated somehow.
        // The tab panel is set to visible in function "rearrageTabsInTabPanel" from "servoy.js".
        add(new StyleAppendingModifier(new Model<String>() {
            private static final long serialVersionUID = 1L;

            @Override
            public String getObject() {
                return "visibility: hidden;overflow:hidden"; //$NON-NLS-1$
            }
        }));

        add(new AbstractServoyDefaultAjaxBehavior() {

            @Override
            protected void respond(AjaxRequestTarget target) {
            }

            @Override
            public void renderHead(IHeaderResponse response) {
                super.renderHead(response);
                boolean dontRearrangeHere = false;

                if (!(getRequestCycle().getRequestTarget() instanceof AjaxRequestTarget)
                        && Utils.getAsBoolean(((MainPage) getPage()).getController().getApplication()
                                .getRuntimeProperties().get("enableAnchors"))) //$NON-NLS-1$
                {
                    Component parentForm = getParent();
                    while ((parentForm != null) && !(parentForm instanceof WebForm))
                        parentForm = parentForm.getParent();
                    if (parentForm != null) {
                        int anch = ((WebForm) parentForm).getAnchors(WebTabPanel.this.getMarkupId());
                        if (anch != 0 && anch != IAnchorConstants.DEFAULT)
                            dontRearrangeHere = true;
                    }
                }
                if (!dontRearrangeHere) {
                    String jsCall = "rearrageTabsInTabPanel('" + WebTabPanel.this.getMarkupId() + "');"; //$NON-NLS-1$ //$NON-NLS-2$
                    // Safari and Konqueror have some problems with the "domready" event, so for those
                    // browsers we'll use the "load" event. Otherwise use "domready", it reduces the flicker
                    // effect when rearranging the tabs.
                    ClientProperties clp = ((WebClientInfo) Session.get().getClientInfo()).getProperties();
                    if (clp.isBrowserKonqueror() || clp.isBrowserSafari())
                        response.renderOnLoadJavascript(jsCall);
                    else
                        response.renderOnDomReadyJavascript(jsCall);
                }
            }

            @Override
            public boolean isEnabled(Component component) {
                return WebClientSession.get().useAjax();
            }

        });
    }
    add(StyleAttributeModifierModel.INSTANCE);
    add(TooltipAttributeModifier.INSTANCE);
    this.scriptable = scriptable;
    ((ChangesRecorder) scriptable.getChangesRecorder()).setDefaultBorderAndPadding(null,
            TemplateGenerator.DEFAULT_LABEL_PADDING);
}

From source file:com.wiquery.plugins.jqgrid.component.XMLDataRequestTarget.java

License:Apache License

/**
 * Checks if the target contains an ancestor for the given component
 * /*from ww w  . j ava  2 s  .  c  om*/
 * @param component
 * @return <code>true</code> if target contains an ancestor for the given component
 */
private boolean containsAncestorFor(Component component) {
    Component cursor = component.getParent();
    while (cursor != null) {
        if (markupIdToComponent.containsValue(cursor)) {
            return true;
        }
        cursor = cursor.getParent();
    }
    return false;
}

From source file:de.alpharogroup.wicket.base.util.ComponentFinder.java

License:Apache License

/**
 * Finds the first parent of the given childComponent from the given parentClass and a flag if
 * the search shell be continued with the class name if the search with the given parentClass
 * returns null./*from w  w w.  ja  va2  s  .com*/
 *
 * @param childComponent
 *            the child component
 * @param parentClass
 *            the parent class
 * @param byClassname
 *            the flag to search by classname if the search with given parentClass returns null.
 * @return the component
 */
public static Component findParent(final Component childComponent, final Class<? extends Component> parentClass,
        final boolean byClassname) {
    Component parent = childComponent.getParent();
    while (parent != null) {
        if (parent.getClass().equals(parentClass)) {
            break;
        }
        parent = parent.getParent();
    }
    if ((parent == null) && byClassname) {
        return findParentByClassname(childComponent, parentClass);
    }
    return parent;
}