001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.markup.html.link;
018:
019: import org.apache.wicket.Application;
020: import org.apache.wicket.markup.ComponentTag;
021: import org.apache.wicket.markup.MarkupStream;
022: import org.apache.wicket.markup.html.WebMarkupContainer;
023: import org.apache.wicket.model.IModel;
024:
025: /**
026: * Base class that that contains functionality for rendering disabled links.
027: *
028: * @author Matej Knopp
029: */
030: public abstract class AbstractLink extends WebMarkupContainer {
031:
032: /**
033: * Construct.
034: *
035: * @param id
036: * @param model
037: */
038: public AbstractLink(String id, IModel model) {
039: super (id, model);
040: }
041:
042: /**
043: * Construct.
044: *
045: * @param id
046: */
047: public AbstractLink(String id) {
048: super (id);
049: }
050:
051: /**
052: * Simple insertion string to allow disabled links to look like <i>Disabled
053: * link </i>.
054: */
055: private String beforeDisabledLink;
056:
057: /**
058: * Simple insertion string to allow disabled links to look like <i>Disabled
059: * link </i>.
060: */
061: private String afterDisabledLink;
062:
063: /**
064: * Sets the insertion string to allow disabled links to look like
065: * <i>Disabled link </i>.
066: *
067: * @param afterDisabledLink
068: * The insertion string
069: */
070: public void setAfterDisabledLink(final String afterDisabledLink) {
071: if (afterDisabledLink == null) {
072: throw new IllegalArgumentException(
073: "Value cannot be null. For no text, specify an empty String instead.");
074: }
075: this .afterDisabledLink = afterDisabledLink;
076: }
077:
078: /**
079: * Gets the insertion string to allow disabled links to look like
080: * <i>Disabled link </i>.
081: *
082: * @return The insertion string
083: */
084: public String getAfterDisabledLink() {
085: return afterDisabledLink;
086: }
087:
088: /**
089: * Sets the insertion string to allow disabled links to look like
090: * <i>Disabled link </i>.
091: *
092: * @param beforeDisabledLink
093: * The insertion string
094: */
095: public void setBeforeDisabledLink(final String beforeDisabledLink) {
096: if (beforeDisabledLink == null) {
097: throw new IllegalArgumentException(
098: "Value cannot be null. For no text, specify an empty String instead.");
099: }
100: this .beforeDisabledLink = beforeDisabledLink;
101: }
102:
103: protected void onBeforeRender() {
104: super .onBeforeRender();
105:
106: // Set default for before/after link text
107: if (beforeDisabledLink == null) {
108: final Application app = getApplication();
109: beforeDisabledLink = app.getMarkupSettings()
110: .getDefaultBeforeDisabledLink();
111: afterDisabledLink = app.getMarkupSettings()
112: .getDefaultAfterDisabledLink();
113: }
114: }
115:
116: /**
117: * Gets the insertion string to allow disabled links to look like
118: * <i>Disabled link </i>.
119: *
120: * @return The insertion string
121: */
122: public String getBeforeDisabledLink() {
123: return beforeDisabledLink;
124: }
125:
126: /**
127: * Helper methods that both checks whether the link is enabled and whether
128: * the action ENABLE is allowed.
129: *
130: * @return whether the link should be rendered as enabled
131: */
132: protected final boolean isLinkEnabled() {
133: return isEnabled() && isEnableAllowed();
134: }
135:
136: /**
137: * Renders this link's body.
138: *
139: * @param markupStream
140: * the markup stream
141: * @param openTag
142: * the open part of this tag
143: * @see org.apache.wicket.Component#onComponentTagBody(MarkupStream,
144: * ComponentTag)
145: */
146: protected void onComponentTagBody(final MarkupStream markupStream,
147: final ComponentTag openTag) {
148: // Draw anything before the body?
149: if (!isLinkEnabled() && getBeforeDisabledLink() != null) {
150: getResponse().write(getBeforeDisabledLink());
151: }
152:
153: // Render the body of the link
154: renderComponentTagBody(markupStream, openTag);
155:
156: // Draw anything after the body?
157: if (!isLinkEnabled() && getAfterDisabledLink() != null) {
158: getResponse().write(getAfterDisabledLink());
159: }
160: }
161:
162: /**
163: * Alters the tag so that the link renders as disabled.
164: *
165: * This method is meant to be called from
166: * {@link #onComponentTag(ComponentTag)} method of the derived class.
167: *
168: * @param tag
169: */
170: protected void disableLink(final ComponentTag tag) {
171: // if the tag is an anchor proper
172: if (tag.getName().equalsIgnoreCase("a")
173: || tag.getName().equalsIgnoreCase("link")
174: || tag.getName().equalsIgnoreCase("area")) {
175: // Change anchor link to span tag
176: tag.setName("span");
177:
178: // Remove any href from the old link
179: tag.remove("href");
180:
181: tag.remove("onclick");
182: }
183: // if the tag is a button or input
184: else if ("button".equalsIgnoreCase(tag.getName())
185: || "input".equalsIgnoreCase(tag.getName())) {
186: tag.put("disabled", "disabled");
187: }
188: }
189: }
|