commit | author | age
|
9e5bee
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
|
16 |
package com.gitblit.wicket;
|
|
17 |
|
|
18 |
import java.io.Serializable;
|
31bcbe
|
19 |
import java.util.ArrayList;
|
JM |
20 |
import java.util.List;
|
9e5bee
|
21 |
|
JM |
22 |
import org.apache.wicket.PageParameters;
|
31bcbe
|
23 |
import org.apache.wicket.markup.html.WebPage;
|
9e5bee
|
24 |
|
31bcbe
|
25 |
import com.gitblit.utils.StringUtils;
|
9e5bee
|
26 |
|
JM |
27 |
/**
|
|
28 |
* Represents a page link registration for the topbar.
|
|
29 |
*
|
|
30 |
* @author James Moger
|
|
31 |
*
|
|
32 |
*/
|
|
33 |
public class PageRegistration implements Serializable {
|
|
34 |
private static final long serialVersionUID = 1L;
|
|
35 |
|
|
36 |
public final String translationKey;
|
31bcbe
|
37 |
public final Class<? extends WebPage> pageClass;
|
9e5bee
|
38 |
public final PageParameters params;
|
JM |
39 |
|
31bcbe
|
40 |
public PageRegistration(String translationKey, Class<? extends WebPage> pageClass) {
|
9e5bee
|
41 |
this(translationKey, pageClass, null);
|
JM |
42 |
}
|
|
43 |
|
31bcbe
|
44 |
public PageRegistration(String translationKey, Class<? extends WebPage> pageClass,
|
9e5bee
|
45 |
PageParameters params) {
|
JM |
46 |
this.translationKey = translationKey;
|
|
47 |
this.pageClass = pageClass;
|
|
48 |
this.params = params;
|
|
49 |
}
|
31bcbe
|
50 |
|
JM |
51 |
/**
|
11924d
|
52 |
* Represents a page link to a non-Wicket page. Might be external.
|
JM |
53 |
*
|
|
54 |
* @author James Moger
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
public static class OtherPageLink extends PageRegistration {
|
|
58 |
|
|
59 |
private static final long serialVersionUID = 1L;
|
|
60 |
|
|
61 |
public final String url;
|
|
62 |
|
|
63 |
public OtherPageLink(String translationKey, String url) {
|
|
64 |
super(translationKey, null);
|
|
65 |
this.url = url;
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
|
69 |
/**
|
31bcbe
|
70 |
* Represents a DropDownMenu for the topbar
|
JM |
71 |
*
|
|
72 |
* @author James Moger
|
|
73 |
*
|
|
74 |
*/
|
|
75 |
public static class DropDownMenuRegistration extends PageRegistration {
|
|
76 |
|
|
77 |
private static final long serialVersionUID = 1L;
|
|
78 |
|
|
79 |
public final List<DropDownMenuItem> menuItems;
|
|
80 |
|
|
81 |
public DropDownMenuRegistration(String translationKey, Class<? extends WebPage> pageClass) {
|
|
82 |
super(translationKey, pageClass);
|
|
83 |
menuItems = new ArrayList<DropDownMenuItem>();
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* A MenuItem for the DropDownMenu.
|
|
89 |
*
|
|
90 |
* @author James Moger
|
|
91 |
*
|
|
92 |
*/
|
|
93 |
public static class DropDownMenuItem implements Serializable {
|
|
94 |
|
|
95 |
private static final long serialVersionUID = 1L;
|
|
96 |
|
8f86e2
|
97 |
final PageParameters parameters;
|
31bcbe
|
98 |
final String displayText;
|
JM |
99 |
final String parameter;
|
|
100 |
final String value;
|
8f86e2
|
101 |
final boolean isSelected;
|
31bcbe
|
102 |
|
JM |
103 |
/**
|
|
104 |
* Divider constructor.
|
|
105 |
*/
|
|
106 |
public DropDownMenuItem() {
|
8f86e2
|
107 |
this(null, null, null, null);
|
31bcbe
|
108 |
}
|
JM |
109 |
|
|
110 |
/**
|
|
111 |
* Standard Menu Item constructor.
|
|
112 |
*
|
|
113 |
* @param displayText
|
|
114 |
* @param parameter
|
|
115 |
* @param value
|
|
116 |
*/
|
|
117 |
public DropDownMenuItem(String displayText, String parameter, String value) {
|
bc57cd
|
118 |
this(displayText, parameter, value, null);
|
JM |
119 |
}
|
|
120 |
|
|
121 |
/**
|
|
122 |
* Standard Menu Item constructor that preserves aggregate parameters.
|
|
123 |
*
|
|
124 |
* @param displayText
|
|
125 |
* @param parameter
|
|
126 |
* @param value
|
|
127 |
*/
|
|
128 |
public DropDownMenuItem(String displayText, String parameter, String value,
|
|
129 |
PageParameters params) {
|
31bcbe
|
130 |
this.displayText = displayText;
|
JM |
131 |
this.parameter = parameter;
|
|
132 |
this.value = value;
|
8f86e2
|
133 |
|
bc57cd
|
134 |
if (params == null) {
|
8f86e2
|
135 |
// no parameters specified
|
JM |
136 |
parameters = new PageParameters();
|
|
137 |
setParameter(parameter, value);
|
|
138 |
isSelected = false;
|
bc57cd
|
139 |
} else {
|
8f86e2
|
140 |
parameters = new PageParameters(params);
|
JM |
141 |
if (parameters.containsKey(parameter)) {
|
|
142 |
isSelected = params.getString(parameter).equals(value);
|
|
143 |
if (isSelected) {
|
|
144 |
// already selected, so remove this enables toggling
|
|
145 |
parameters.remove(parameter);
|
|
146 |
} else {
|
|
147 |
// set the new selection value
|
|
148 |
setParameter(parameter, value);
|
|
149 |
}
|
|
150 |
} else {
|
|
151 |
// not currently selected
|
|
152 |
isSelected = false;
|
|
153 |
setParameter(parameter, value);
|
|
154 |
}
|
bc57cd
|
155 |
}
|
8f86e2
|
156 |
}
|
JM |
157 |
|
|
158 |
private void setParameter(String parameter, String value) {
|
bc57cd
|
159 |
if (!StringUtils.isEmpty(parameter)) {
|
JM |
160 |
if (StringUtils.isEmpty(value)) {
|
8f86e2
|
161 |
this.parameters.remove(parameter);
|
bc57cd
|
162 |
} else {
|
8f86e2
|
163 |
this.parameters.put(parameter, value);
|
bc57cd
|
164 |
}
|
JM |
165 |
}
|
31bcbe
|
166 |
}
|
JM |
167 |
|
|
168 |
public String formatParameter() {
|
|
169 |
if (StringUtils.isEmpty(parameter) || StringUtils.isEmpty(value)) {
|
|
170 |
return "";
|
|
171 |
}
|
|
172 |
return parameter + "=" + value;
|
|
173 |
}
|
|
174 |
|
bc57cd
|
175 |
public PageParameters getPageParameters() {
|
8f86e2
|
176 |
return parameters;
|
bc57cd
|
177 |
}
|
JM |
178 |
|
31bcbe
|
179 |
public boolean isDivider() {
|
JM |
180 |
return displayText == null && value == null && parameter == null;
|
|
181 |
}
|
|
182 |
|
8f86e2
|
183 |
public boolean isSelected() {
|
JM |
184 |
return isSelected;
|
|
185 |
}
|
|
186 |
|
31bcbe
|
187 |
@Override
|
JM |
188 |
public int hashCode() {
|
|
189 |
if (isDivider()) {
|
|
190 |
// divider menu item
|
|
191 |
return super.hashCode();
|
|
192 |
}
|
|
193 |
if (StringUtils.isEmpty(displayText)) {
|
|
194 |
return value.hashCode() + parameter.hashCode();
|
|
195 |
}
|
|
196 |
return displayText.hashCode();
|
|
197 |
}
|
|
198 |
|
|
199 |
@Override
|
|
200 |
public boolean equals(Object o) {
|
|
201 |
if (o instanceof DropDownMenuItem) {
|
|
202 |
return hashCode() == o.hashCode();
|
|
203 |
}
|
|
204 |
return false;
|
|
205 |
}
|
|
206 |
|
|
207 |
@Override
|
|
208 |
public String toString() {
|
|
209 |
if (StringUtils.isEmpty(displayText)) {
|
|
210 |
return formatParameter();
|
|
211 |
}
|
|
212 |
return displayText;
|
|
213 |
}
|
|
214 |
}
|
9e5bee
|
215 |
} |