| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractLink |
|
| 0.0;0 |
| 1 | // Copyright 2007, 2008, 2009 The Apache Software Foundation | |
| 2 | // | |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | // you may not use this file except in compliance with the License. | |
| 5 | // You may obtain a copy of the License at | |
| 6 | // | |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | // | |
| 9 | // Unless required by applicable law or agreed to in writing, software | |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 | // See the License for the specific language governing permissions and | |
| 13 | // limitations under the License. | |
| 14 | ||
| 15 | package org.apache.tapestry5.corelib.base; | |
| 16 | ||
| 17 | import org.apache.tapestry5.*; | |
| 18 | import org.apache.tapestry5.annotations.Parameter; | |
| 19 | import org.apache.tapestry5.annotations.SetupRender; | |
| 20 | import org.apache.tapestry5.annotations.SupportsInformalParameters; | |
| 21 | import org.apache.tapestry5.dom.Element; | |
| 22 | import org.apache.tapestry5.ioc.annotations.Inject; | |
| 23 | ||
| 24 | /** | |
| 25 | * Provides base utilities for classes that generate clickable links. | |
| 26 | */ | |
| 27 | @SupportsInformalParameters | |
| 28 | 649 | public abstract class AbstractLink implements ClientElement |
| 29 | { | |
| 30 | /** | |
| 31 | * An anchor value to append to the generated URL (the hash separator will be added automatically). | |
| 32 | */ | |
| 33 | @Parameter(defaultPrefix = BindingConstants.LITERAL) | |
| 34 | private String anchor; | |
| 35 | ||
| 36 | /** | |
| 37 | * If true, then then no link element is rendered (and no informal parameters as well). The body is, however, still | |
| 38 | * rendered. | |
| 39 | */ | |
| 40 | @Parameter("false") | |
| 41 | private boolean disabled; | |
| 42 | ||
| 43 | @Inject | |
| 44 | private ComponentResources resources; | |
| 45 | ||
| 46 | @Inject | |
| 47 | private RenderSupport renderSupport; | |
| 48 | ||
| 49 | private Link link; | |
| 50 | ||
| 51 | private Element element; | |
| 52 | ||
| 53 | private String clientId; | |
| 54 | ||
| 55 | private String buildHref(Link link) | |
| 56 | { | |
| 57 | 41050 | String href = link.toURI(); |
| 58 | ||
| 59 | 41050 | if (anchor == null) return href; |
| 60 | ||
| 61 | 2 | return href + "#" + anchor; |
| 62 | } | |
| 63 | ||
| 64 | ||
| 65 | @SetupRender | |
| 66 | void resetElementAndClientId() | |
| 67 | { | |
| 68 | 41082 | element = null; |
| 69 | 41082 | clientId = null; |
| 70 | 41082 | } |
| 71 | ||
| 72 | /** | |
| 73 | * Writes an <a> element with the provided link as the href attribute. A call to {@link | |
| 74 | * org.apache.tapestry5.MarkupWriter#end()} is <em>not</em> provided. Automatically appends an anchor if | |
| 75 | * the component's anchor parameter is non-null. Informal parameters are rendered as well. | |
| 76 | * | |
| 77 | * @param writer to write markup to | |
| 78 | * @param link the link that will form the href | |
| 79 | * @param namesAndValues additional attributes to write | |
| 80 | */ | |
| 81 | protected final void writeLink(MarkupWriter writer, Link link, Object... namesAndValues) | |
| 82 | { | |
| 83 | 41050 | element = writer.element("a", "href", buildHref(link)); |
| 84 | ||
| 85 | 41050 | writer.attributes(namesAndValues); |
| 86 | ||
| 87 | 41050 | resources.renderInformalParameters(writer); |
| 88 | ||
| 89 | 41050 | this.link = link; |
| 90 | 41050 | } |
| 91 | ||
| 92 | /** | |
| 93 | * Returns the most recently rendered {@link org.apache.tapestry5.Link} for this component. Subclasses calculate | |
| 94 | * their link value as they render, and the value is valid until the end of the request, or the next time the same | |
| 95 | * component renders itself (if inside a loop). | |
| 96 | * | |
| 97 | * @return the most recent link, or null | |
| 98 | */ | |
| 99 | public Link getLink() | |
| 100 | { | |
| 101 | 0 | return link; |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * Returns the unique client id for this element. This is valid only after the component has rendered (its start | |
| 106 | * tag). A client id is generated the first time this method is invoked, after the link renders its start tag. | |
| 107 | */ | |
| 108 | public final String getClientId() | |
| 109 | { | |
| 110 | 176 | if (clientId == null) |
| 111 | { | |
| 112 | 176 | if (element == null) |
| 113 | 0 | throw new IllegalStateException( |
| 114 | String.format("Client id for %s is not available as it did not render yet (or was disabled).", | |
| 115 | resources.getCompleteId())); | |
| 116 | ||
| 117 | 176 | clientId = renderSupport.allocateClientId(resources); |
| 118 | ||
| 119 | 176 | element.forceAttributes("id", clientId); |
| 120 | } | |
| 121 | ||
| 122 | 176 | return clientId; |
| 123 | } | |
| 124 | ||
| 125 | /** | |
| 126 | * Returns true if the component is disabled (as per its disabled parameter). Disabled link components should not | |
| 127 | * render a tag, but should still render their body. | |
| 128 | */ | |
| 129 | public boolean isDisabled() | |
| 130 | { | |
| 131 | 82164 | return disabled; |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * Used for testing. | |
| 136 | */ | |
| 137 | final void inject(String anchor, ComponentResources resources) | |
| 138 | { | |
| 139 | 4 | this.anchor = anchor; |
| 140 | 4 | this.resources = resources; |
| 141 | 4 | } |
| 142 | } |