📄 component.java
字号:
return getMetaData(MARKUP_ID_KEY); } private final int nextAutoIndex() { Page page = findPage(); if (page == null) { throw new WicketRuntimeException( "This component is not (yet) coupled to a page. It has to be able " + "to find the page it is supposed to operate in before you can call " + "this method (Component#getMarkupId)"); } return page.getAutoIndex(); } /** * Retrieves id by which this component is represented within the markup. This is either the id * attribute set explicitly via a call to {@link #setMarkupId(String)}, id attribute defined in * the markup, or an automatically generated id - in that order. * <p> * If no id is set and <code>createIfDoesNotExist</code> is false, this method will return * null. Otherwise it will generate an id value that will be unique in the page. This is the * preferred way as there is no chance of id collision. * <p> * Note: This method should only be called after the component or its parent have been added to * the page. * * @param createIfDoesNotExist * When there is no existing markup id, determines whether it should be generated or * whether <code>null</code> should be returned. * * @return markup id of the component */ public String getMarkupId(boolean createIfDoesNotExist) { Object storedMarkupId = getMarkupIdImpl(); if (storedMarkupId instanceof String) { return (String)storedMarkupId; } if (storedMarkupId == null && createIfDoesNotExist == false) { return null; } final int generatedMarkupId = storedMarkupId instanceof Integer ? ((Integer)storedMarkupId).intValue() : nextAutoIndex(); if (storedMarkupId == null) { setMarkupIdImpl(new Integer(generatedMarkupId)); } // try to read from markup // TODO getting the id from markup doesn't work everywhere yet. // unfortunately, we have to drop this until we have a good solution // for issue http://issues.apache.org/jira/browse/WICKET-694 // markupId = getMarkupAttributes().getString("id"); String markupId = RequestContext.get().encodeMarkupId(getId() + generatedMarkupId); // make sure id is compliant with w3c requirements (starts with a // letter) char c = markupId.charAt(0); if (!Character.isLetter(c)) { markupId = "id" + markupId; } // escape some noncompliant characters markupId = Strings.replaceAll(markupId, "_", "__").toString(); markupId = markupId.replace('.', '_'); markupId = markupId.replace('-', '_'); return markupId; } /** * Retrieves id by which this component is represented within the markup. This is either the id * attribute set explicitly via a call to {@link #setMarkupId(String)}, id attribute defined in * the markup, or an automatically generated id - in that order. * <p> * If no explicit id is set this function will generate an id value that will be unique in the * page. This is the preferred way as there is no chance of id collision. * <p> * Note: This method should only be called after the component or its parent have been added to * the page. * * @return markup id of the component */ public String getMarkupId() { return getMarkupId(true); } /** * Gets metadata for this component using the given key. * * @param key * The key for the data * @return The metadata or null of no metadata was found for the given key * @see MetaDataKey */ public final Serializable getMetaData(final MetaDataKey key) { return key.get(getMetaData()); } private MetaDataEntry[] getMetaData() { MetaDataEntry[] metaData = null; // index where we should expect the entry int index = getFlag(FLAG_MODEL_SET) ? 1 : 0; int length = data_length(); if (index < length) { Object object = data_get(index); if (object instanceof MetaDataEntry[]) { metaData = (MetaDataEntry[])object; } else if (object instanceof MetaDataEntry) { metaData = new MetaDataEntry[] { (MetaDataEntry)object }; } } return metaData; } /** * Gets the model. It returns the object that wraps the backing model. * * @return The model */ public final IModel getModel() { IModel model = getModelImpl(); // If model is null if (model == null) { // give subclass a chance to lazy-init model model = initModel(); setModelImpl(model); } return model; } /** * Gets the backing model object; this is shorthand for getModel().getObject(). * * @return The backing model object */ public final Object getModelObject() { final IModel model = getModel(); if (model != null) { // Get model value for this component. return model.getObject(); } else { return null; } } /** * Gets a model object as a string. Depending on the "escape model strings" flag of the * component, the string is either HTML escaped or not. "HTML escaped" meaning that only HTML * sensitive chars are escaped but not all none-ascii chars. Proper HTML encoding should be used * instead. In case you really need a fully escaped model string you may call * {@link Strings#escapeMarkup(String, boolean, boolean)} on the model string returned. * * @see Strings#escapeMarkup(String, boolean, boolean) * @see #getEscapeModelStrings() * * @return Model object for this component as a string */ public final String getModelObjectAsString() { return getModelObjectAsString(getModelObject()); } /** * Gets a model object as a string. Depending on the "escape model strings" flag of the * component, the string is either HTML escaped or not. "HTML escaped" meaning that only HTML * sensitive chars are escaped but not all none-ascii chars. Proper HTML encoding should be used * instead. In case you really need a fully escaped model string you may call * {@link Strings#escapeMarkup(String, boolean, boolean)} on the model string returned. * * @see Strings#escapeMarkup(String, boolean, boolean) * @see #getEscapeModelStrings() * * @param modelObject * Model object to convert to string * @return The string */ public final String getModelObjectAsString(final Object modelObject) { if (modelObject != null) { // Get converter final IConverter converter = getConverter(modelObject.getClass()); // Model string from property final String modelString = converter.convertToString(modelObject, getLocale()); if (modelString != null) { // If we should escape the markup if (getFlag(FLAG_ESCAPE_MODEL_STRINGS)) { // Escape HTML sensitive characters only. Not all none-ascii chars return Strings.escapeMarkup(modelString, false, false).toString(); } return modelString; } } return ""; } /** * Gets whether or not component will output id attribute into the markup. id attribute will be * set to the value returned from {@link Component#getMarkupId()}. * * @return whether or not component will output id attribute into the markup */ public final boolean getOutputMarkupId() { return getFlag(FLAG_OUTPUT_MARKUP_ID); } /** * Gets whether or not an invisible component will render a placeholder tag. * * @return true if a placeholder tag should be rendered */ public final boolean getOutputMarkupPlaceholderTag() { return getFlag(FLAG_PLACEHOLDER); } /** * Gets the page holding this component. * * @return The page holding this component * @throws IllegalStateException * Thrown if component is not yet attached to a Page. */ public final Page getPage() { // Search for nearest Page final Page page = findPage(); // If no Page was found if (page == null) { // Give up with a nice exception throw new IllegalStateException("No Page found for component " + this); } return page; } /** * @return The page factory for the session that this component is in */ public final IPageFactory getPageFactory() { return getSession().getPageFactory(); } /** * Gets the path to this component relative to the page it is in. * * @return The path to this component relative to the page it is in */ public final String getPageRelativePath() { return Strings.afterFirstPathComponent(getPath(), PATH_SEPARATOR); } /** * Gets any parent container, or null if there is none. * * @return Any parent container, or null if there is none */ public final MarkupContainer getParent() { return parent; } /** * Gets this component's path. * * @return Colon separated path to this component in the component hierarchy */ public final String getPath() { final PrependingStringBuffer buffer = new PrependingStringBuffer(32); for (Component c = this; c != null; c = c.getParent()) { if (buffer.length() > 0) { buffer.prepend(PATH_SEPARATOR); } buffer.prepend(c.getId()); } return buffer.toString(); } /** * If false the component's tag will be printed as well as its body (which is default). If true * only the body will be printed, but not the component's tag. * * @return If true, the component tag will not be printed */ public final boolean getRenderBodyOnly() { return getFlag(FLAG_RENDER_BODY_ONLY); } /** * @return The request for this component's active request cycle */ public final Request getRequest() { RequestCycle requestCycle = getRequestCycle(); if (requestCycle == null) { // Happens often with WicketTester when one forgets to call // createRequestCycle() throw new WicketRuntimeException("No RequestCycle is currently set!"); } return requestCycle.getRequest(); } /** * Gets the active request cycle for this component * * @return The request cycle */ public final RequestCycle getRequestCycle() { return RequestCycle.get(); } /** * @return The response for this component's active request cycle */ public final Response getResponse() { return getRequestCycle().getResponse(); } /** * Gets the current Session object. * * @return The Session that this component is in */ public Session getSession() { return Session.get(); } /** * @return Size of this Component in bytes */ public long getSizeInBytes() { final MarkupContainer originalParent = parent; parent = null; long size = -1; try { size = Objects.sizeof(this); } catch (Exception e) { log.error("Exception getting size for component " + this, e); } parent = originalParent; return size; } /** * @param key * Key of string resource in property file * @return The String * @see Localizer */ public final String getString(final String key) { return getString(key, null); } /** * @param key * The resource key * @param model * The model * @return The formatted string * @see Localizer */ public final String getString(final String key, final IModel model) { return getLocalizer().getString(key, this, model); } /** * @param key * The resource key * @param model * The model * @param defaultValue * A default value if the string cannot be found * @return The formatted string * @see Localizer */ public final String getString(final String key, final IModel model, final String defaultValue) { return getLocalizer().getString(key, this, model, defaultValue); } /** * Gets the style of this component (see {@link org.apache.wicket.Session}). * * @return The style of this component. * * @see org.apache.wicket.Session * @see org.apache.wicket.Session#getStyle() */ public final String getStyle() { String variation = getVariation(); String style = getSession().getStyle(); if (variation != null && !"".equals(variation)) { if (style != null && !"".equals(style)) { style = variation + "_" + style; } else { style = variation; } } return style;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -