import java.net.MalformedURLException;
import java.net.URL;
+import java.util.List;
import javax.servlet.http.HttpServletRequest;
import eu.svjatoslav.commons.network.Locale;
import eu.svjatoslav.commons.network.LocaleConfiguration;
-public class Navigation {
+public class Navigation<NI extends NavigationItem> {
- private NavigationItem rootNavigationItem;
+ private NI rootNavigationItem;
- private NavigationItem defaultNavigationItem;
+ private NI defaultNavigationItem;
protected final LocaleConfiguration localeConfiguration;
this.localeConfiguration = localeConfiguration;
}
- public NavigationItem getDefaultNavigationItem() {
+ public NI getDefaultNavigationItem() {
return defaultNavigationItem;
}
return localeConfiguration;
}
- public NavigationItem getRootNavigationItem() {
+ public NI getRootNavigationItem() {
return rootNavigationItem;
}
- public NavigationItem getSelectedItem(final HttpServletRequest request) {
+ public NI getSelectedItem(final HttpServletRequest request) {
final String requestUrl = request.getRequestURL().toString();
try {
final String requestPath = new URL(requestUrl).getPath();
- final NavigationItem match = rootNavigationItem
+ final NI match = (NI) rootNavigationItem
.getMatchingNavigationItem(requestPath);
if (match != null)
final Locale currentLocale = localeConfiguration
.detectCurrentLocale(request);
- final NavigationItem selectedItem = getSelectedItem(request);
+ final NI selectedItem = getSelectedItem(request);
final StringBuffer result = new StringBuffer();
result.append("<div class=\""
+ getGetNotNullString(rootNavigationItem.getCssPrefix())
+ "navigationMenu\">");
- for (final NavigationItem item : getRootNavigationItem()
+ for (final NI item : (List<NI>) getRootNavigationItem()
.getSubElements()) {
if (item == selectedItem)
return result.toString();
}
- public NavigationItem setDefaultNavigationItem(
- final NavigationItem defaultNavigationItem) {
+ public NI setDefaultNavigationItem(final NI defaultNavigationItem) {
this.defaultNavigationItem = defaultNavigationItem;
return defaultNavigationItem;
}
- public NavigationItem setRootNavigationItem(
- final NavigationItem rootNavigationItem) {
+ public NI setRootNavigationItem(final NI rootNavigationItem) {
this.rootNavigationItem = rootNavigationItem;
return rootNavigationItem;
}
import eu.svjatoslav.commons.network.Locale;
import eu.svjatoslav.commons.string.WildCardMatcher;
-public class NavigationItem {
+public class NavigationItem<NI extends NavigationItem, N extends Navigation> {
private String matchingPattern;
+
private final Map<Locale, String> localeToTitle = new HashMap<Locale, String>();
- final ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
- final private NavigationItem parent;
- final private Navigation navigation;
+ private final ArrayList<NI> subElements = new ArrayList<NI>();
+ private final NI parent;
+ private final N navigation;
private final String linkUrl;
-
/**
* CSS prefix is inherited to child menu items.
*/
private String cssPrefix;
- public NavigationItem(final Navigation navigation) {
+ public NavigationItem(final N navigation) {
this.navigation = navigation;
parent = null;
matchingPattern = null;
linkUrl = null;
}
- private NavigationItem(final Navigation navigation,
- final NavigationItem parent, final String linkUrl,
+ public NavigationItem(final NI parent, final String linkUrl,
final String... titles) {
- this.navigation = navigation;
+ this.navigation = (N) parent.getNavigation();
this.parent = parent;
this.linkUrl = linkUrl;
matchingPattern = linkUrl;
initializeLocalizedTitles(titles);
+
+ parent.addNavigationItem(this);
}
- public NavigationItem addChild(final String linkUrl, final String... titles) {
- final NavigationItem item = new NavigationItem(navigation, this,
- linkUrl, titles);
- subElements.add(item);
- return item;
+ public void addNavigationItem(final NI navigationItem) {
+ subElements.add(navigationItem);
}
protected String getCssPrefix() {
return linkUrl;
}
- NavigationItem getMatchingNavigationItem(final String requestPath) {
+ NI getMatchingNavigationItem(final String requestPath) {
if (matchesUrl(requestPath))
- return this;
+ return (NI) this;
- for (final NavigationItem childNavigationItem : subElements) {
- final NavigationItem match = childNavigationItem
+ for (final NI childNavigationItem : subElements) {
+ final NI match = (NI) childNavigationItem
.getMatchingNavigationItem(requestPath);
if (match != null)
return null;
}
- public List<NavigationItem> getSubElements() {
+ public N getNavigation() {
+ return navigation;
+ }
+
+ public List<NI> getSubElements() {
return subElements;
}
}
private void initializeLocalizedTitles(final String... titles) {
- final List<Locale> locales = navigation.getLocaleConfiguration()
+ final List<Locale> locales = getNavigation().getLocaleConfiguration()
.getAllowedLocales();
if (locales.size() != titles.length)
throw new RuntimeException("There should be exactly "
return this;
}
+ @Override
+ public String toString() {
+ return "NavigationItem [matchingPattern=" + matchingPattern
+ + ", localeToTitle=" + localeToTitle + ", subElements count="
+ + subElements.size() + ", linkUrl=" + linkUrl + ", cssPrefix="
+ + cssPrefix + "]";
+ }
+
}