01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.extensions.breadcrumb;
18:
19: import org.apache.wicket.extensions.breadcrumb.panel.BreadCrumbPanel;
20: import org.apache.wicket.markup.html.link.Link;
21: import org.apache.wicket.version.undo.Change;
22:
23: /**
24: * A link that when clicked will set the the active
25: * {@link IBreadCrumbParticipant bread crumb participant} to the one that is
26: * returned by {@link #getParticipant(String)}. It is used internally by
27: * {@link BreadCrumbBar the the bread crumb bar component}, and you can use it
28: * for rendering links e.g. with
29: * {@link BreadCrumbPanel bread crumb panel components}.
30: *
31: * <p>
32: * When clicked, it registers a change for backbutton support.
33: * </p>
34: *
35: * @author Eelco Hillenius
36: */
37: public abstract class BreadCrumbLink extends Link {
38: private static final long serialVersionUID = 1L;
39:
40: /** The bread crumb model. */
41: private final IBreadCrumbModel breadCrumbModel;
42:
43: /**
44: * Construct.
45: *
46: * @param id
47: * The link id
48: * @param breadCrumbModel
49: * The bread crumb model
50: */
51: public BreadCrumbLink(String id, IBreadCrumbModel breadCrumbModel) {
52: super (id);
53: this .breadCrumbModel = breadCrumbModel;
54: }
55:
56: /**
57: * @see org.apache.wicket.markup.html.link.Link#onClick()
58: */
59: public void onClick() {
60: // get the currently active particpant
61: final IBreadCrumbParticipant active = breadCrumbModel
62: .getActive();
63: if (active == null) {
64: throw new IllegalStateException(
65: "The model has no active bread crumb. Before using "
66: + this
67: + ", you have to have at least one bread crumb in the model");
68: }
69:
70: // get the participant to set as active
71: final IBreadCrumbParticipant participant = getParticipant(active
72: .getComponent().getId());
73:
74: // add back button support
75: addStateChange(new Change() {
76: private static final long serialVersionUID = 1L;
77:
78: public void undo() {
79: breadCrumbModel.setActive(active);
80: }
81: });
82:
83: // set the next participant as the active one
84: breadCrumbModel.setActive(participant);
85: }
86:
87: /**
88: * Gets the {@link IBreadCrumbParticipant bread crumb participant} to be set
89: * active when the link is clicked.
90: *
91: * @param componentId
92: * When the participant creates it's own view, it typically
93: * should use this component id for the component that is
94: * returned by {@link IBreadCrumbParticipant#getComponent()}.
95: * @return The bread crumb participant
96: */
97: protected abstract IBreadCrumbParticipant getParticipant(
98: String componentId);
99: }
|