Because I needed the functionality I used the pseudo code provided by
Johan to alter Link to accommodate the Anchor attribute.
I called it anchor because basically you want to jump to an anchor tag,
I agree with Eelco (in
http://www.nabble.com/Re%3A-Link-to-Anchor-p4345613.html) that anchor
is to global, but I also haven't got a better idea.
Also I have written a Anchor.java that enables you to dynamically
generate the <a name=""> attribute. I positioned it in the extensions
project but any other (examples/stuff/..) project is fine (if you even
want it in one of the projects, anyway ;-) ). However I'll not be able
to maintain it due to an upcoming 'gap year'.
Hope you guys find it useful.
Attached are Anchor.java and Link.java.patch (both for the 1.2 branch)
Thijs
SourceForge.net wrote:
Feature Requests item #1491239, was opened at 2006-05-18 23:44
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=684978&aid=1491239&group_id=119783
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: core
Group: 1.2
Status: Open
Priority: 5
Submitted By: Eelco Hillenius (eelco12)
Assigned to: Nobody/Anonymous (nobody)
Summary: Add anchor ability to Links and Buttons
Initial Comment:
See thread
http://www.nabble.com/Link-to-Anchor-t1600262.html
Basically, 'Link.setAnchor() so that users can set the
anchor dynamically. Also proposed <a href="#anAnchor"
wicket:id="anchoredLink">an href with # followed by a
word is automatically an anchor</a>'.
We can implement them both, and we can look whether we
can support the same functionality with Buttons.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=684978&aid=1491239&group_id=119783
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Wicket-develop mailing list
Wicket-develop@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-develop
/*
*
==============================================================================
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package wicket.extensions.markup.html.basic;
import wicket.markup.ComponentTag;
import wicket.markup.MarkupStream;
import wicket.markup.html.WebMarkupContainer;
import wicket.model.IModel;
import wicket.model.Model;
import wicket.util.string.Strings;
/**
* An Anchor component replaces its tag attribute 'name' with the String
version of its model
* object returned by getModelObjectAsString().
* <p>
* Exactly what is used for the 'name' attribute, depends on the model. The
simplest
* case is an Anchor with a static String model, which can be constructed like
* this:
*
* <pre>
* add(new Anchor("myAnchor", "the name of the anchor"))
* </pre>
*
* An anchor with a dynamic model can be created like this:
*
* <pre>
* add(new Anchor("myAnchor", new PropertyModel(person,
"name"));
* </pre>
*
* and in you HTML file"
* <pre>
* <a name="#" wicket:id="myAnchor"></a>
* </pre>
*
* @author Thijs Vonk
*/
public class Anchor extends WebMarkupContainer
{
private static final long serialVersionUID = 1L;
/**
* Constructor
*
* @param id
* See Component
*/
public Anchor(final String id)
{
super(id);
}
/**
* Convenience constructor. Same as Anchor(String, new Model(String))
*
* @param id
* See Component
* @param name
* The Anchor text
*
* @see wicket.Component#Component(String, IModel)
*/
public Anchor(final String id, String name)
{
this(id, new Model(name));
}
/**
* @see wicket.Component#Component(String, IModel)
*/
public Anchor(final String id, IModel model)
{
super(id, model);
}
/**
* Handles this anchors tag.
*
* @param tag
* the component tag
* @see wicket.Component#onComponentTag(ComponentTag)
*/
protected final void onComponentTag(final ComponentTag tag)
{
// Default handling for tag
super.onComponentTag(tag);
// if the tag is an anchor proper
if (tag.getName().equalsIgnoreCase("a"))
{
// generate the name attribute
tag.put("name", getModelObjectAsString());
}
}
}
Index: Link.java
===================================================================
--- Link.java (revision 6282)
+++ Link.java (working copy)
@@ -95,6 +95,11 @@
* link </i>.
*/
private String beforeDisabledLink;
+
+ /**
+ * Simple insertion string to allow links to have a named anchor.
+ */
+ private String anchor;
/**
* The popup specification. If not-null, a javascript on-click event
handler
@@ -238,6 +243,40 @@
this.autoEnable = autoEnable;
return this;
}
+
+ /**
+ * Sets the String to add to the Url as an anchor.
+ * The resulting url will look like:
+ * <pre>
+ * http://www.example.com#anchorName
+ * </pre>
+ *
+ * An anchor can also be defined in your HTML file:
+ * <pre>
+ * <a href="#anchorName"
wicket:id="myLink">click here</a>
+ * </pre>
+ *
+ * If the anchor is set with this method,
+ * the anchor set in the markup file will be ignored.
+ *
+ * @param anchor
+ * name of the <a name="anchor"></a> ellement to
jump to
+ * @return This
+ */
+ public void setAnchor(final String anchor)
+ {
+ this.anchor = anchor;
+ }
+
+ /**
+ * Gets the String that represents the anchorName set for this Link.
+ *
+ * @return the String that represents the anchor set for this Link.
+ */
+ public String getAnchor()
+ {
+ return this.anchor;
+ }
/**
* Sets the insertion string to allow disabled links to look like
@@ -344,6 +383,23 @@
// Set href to link to this link's linkClicked method
CharSequence url = getURL();
+
+ // Check if the anchor is set with setAnchor
+ if (anchor == null){
+ // get a possible anchor from the href attribute
+ anchor = tag.getAttributes().getString("href");
+
+ // only append the anchor to the url if it starts with a #
and it is larger then 1 character
+ if(anchor != null && anchor.startsWith("#") &&
anchor.length()>1)
+ {
+ url = url + anchor;
+ }else{
+ anchor = null; // we set the anchor back to null to
prevent it from showing the second or more times onComponentTag is called
+ }
+ }else{ // use the anchor provided by the setAnchor method.
+ url = url + "#" + anchor;
+ }
+
// If we're disabled
if (!isEnabled())
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Wicket-develop mailing list
Wicket-develop@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-develop