From 0d0484517a2d49c5de609d39868528d605e23ec9 Mon Sep 17 00:00:00 2001 From: tamasmak Date: Fri, 1 Mar 2024 13:09:22 +0100 Subject: [PATCH 1/4] Update vcf-nav version --- .../src/main/java/com/vaadin/kubernetes/demo/Application.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/Application.java b/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/Application.java index fed8149..cf6232c 100644 --- a/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/Application.java +++ b/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/Application.java @@ -20,7 +20,7 @@ @Theme("kuberneteskitdemo") @PWA(name = "Kubernetes Kit Demo", shortName = "Kubernetes Kit Demo") @NpmPackage(value = "line-awesome", version = "1.3.0") -@NpmPackage(value = "@vaadin-component-factory/vcf-nav", version = "1.0.6") +@NpmPackage(value = "@vaadin-component-factory/vcf-nav", version = "1.1.3") @Push public class Application implements AppShellConfigurator { From 15cda398b9fa3e918f83cc61ec679e0b8d1d5874 Mon Sep 17 00:00:00 2001 From: tamasmak Date: Fri, 1 Mar 2024 13:09:37 +0100 Subject: [PATCH 2/4] Format code --- .../starter/KubernetesKitConfiguration.java | 3 ++- .../starter/KubernetesKitProperties.java | 6 +++--- .../starter/SerializationProperties.java | 16 ++++++++-------- .../sessiontracker/SessionTrackerFilter.java | 3 ++- .../debug/DebugBackendConnector.java | 14 +++++++------- .../sessiontracker/serialization/debug/Job.java | 16 ++++++++-------- .../debug/SerializationDebugRequestHandler.java | 17 +++++++++++------ .../SessionTrackerCookieTest.java | 12 ++++++++---- .../SessionTrackerFilterTest.java | 10 ++++++---- .../SerializationDebugRequestHandlerTest.java | 10 +++++----- 10 files changed, 60 insertions(+), 47 deletions(-) diff --git a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/KubernetesKitConfiguration.java b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/KubernetesKitConfiguration.java index 9dcce0f..5a8008d 100644 --- a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/KubernetesKitConfiguration.java +++ b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/KubernetesKitConfiguration.java @@ -174,7 +174,8 @@ public static class VaadinReplicatedSessionDevModeConfiguration { @ConditionalOnMissingBean SerializationDebugRequestHandler.InitListener sessionSerializationDebugToolInstaller( SerializationProperties serializationProperties) { - return new SerializationDebugRequestHandler.InitListener(serializationProperties); + return new SerializationDebugRequestHandler.InitListener( + serializationProperties); } @Bean diff --git a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/KubernetesKitProperties.java b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/KubernetesKitProperties.java index 312243b..37922d5 100644 --- a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/KubernetesKitProperties.java +++ b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/KubernetesKitProperties.java @@ -69,7 +69,7 @@ public void setAutoConfigure(boolean autoConfigure) { * value. * * @return the distributed storage session key cookie's SameSite attribute - * value + * value */ public SameSite getClusterKeyCookieSameSite() { return clusterKeyCookieSameSite; @@ -79,8 +79,8 @@ public SameSite getClusterKeyCookieSameSite() { * Sets the distributed storage session key cookie's SameSite attribute. * * @param sameSite - * value of the distributed storage session key cookie's - * SameSite attribute + * value of the distributed storage session key cookie's SameSite + * attribute */ public void setClusterKeyCookieSameSite(SameSite sameSite) { this.clusterKeyCookieSameSite = sameSite; diff --git a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/SerializationProperties.java b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/SerializationProperties.java index 1fe2929..0b27fd7 100644 --- a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/SerializationProperties.java +++ b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/SerializationProperties.java @@ -37,23 +37,23 @@ public class SerializationProperties { private final TransientsProperties transients = new TransientsProperties(); /** - * Gets the timeout in milliseconds to wait for the serialization - * to be completed. + * Gets the timeout in milliseconds to wait for the serialization to be + * completed. * - * @return the timeout in milliseconds to wait for the serialization - * to be completed, defaults to 30000 ms + * @return the timeout in milliseconds to wait for the serialization to be + * completed, defaults to 30000 ms */ public int getTimeout() { return timeout; } /** - * Sets the timeout in milliseconds to wait for the serialization - * to be completed. + * Sets the timeout in milliseconds to wait for the serialization to be + * completed. * * @param timeout - * the timeout in milliseconds to wait for the serialization - * to be completed, defaults to 30000 ms + * the timeout in milliseconds to wait for the serialization to + * be completed, defaults to 30000 ms */ public void setTimeout(int timeout) { this.timeout = timeout; diff --git a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilter.java b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilter.java index 11a51a7..f921e6e 100644 --- a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilter.java +++ b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilter.java @@ -93,7 +93,8 @@ private Consumer cookieConsumer(HttpServletRequest request) { return (Cookie cookie) -> { cookie.setHttpOnly(true); - String path = request.getContextPath().isEmpty() ? "/" : request.getContextPath(); + String path = request.getContextPath().isEmpty() ? "/" + : request.getContextPath(); cookie.setPath(path); SameSite sameSite = properties.getClusterKeyCookieSameSite(); diff --git a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/DebugBackendConnector.java b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/DebugBackendConnector.java index adb928f..26dc094 100644 --- a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/DebugBackendConnector.java +++ b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/DebugBackendConnector.java @@ -63,12 +63,12 @@ public void deleteSession(String clusterKey) { } /** - * Blocks the thread for up to the defined timeout in milliseconds, - * waiting for serialization to be completed. + * Blocks the thread for up to the defined timeout in milliseconds, waiting + * for serialization to be completed. * * @param timeout - * the timeout in milliseconds to wait for the serialization - * to be completed. + * the timeout in milliseconds to wait for the serialization to + * be completed. * @param logger * the logger to add potential error information. * @return the serialized session holder. @@ -78,9 +78,9 @@ SessionInfo waitForCompletion(int timeout, Logger logger) { if (!latch.await(timeout, TimeUnit.MILLISECONDS)) { job.timeout(); logger.error( - "Session serialization timed out because did not complete in {} ms. " + - "Increase the serialization timeout (in milliseconds) by the " + - "'vaadin.serialization.timeout' application or system property.", + "Session serialization timed out because did not complete in {} ms. " + + "Increase the serialization timeout (in milliseconds) by the " + + "'vaadin.serialization.timeout' application or system property.", timeout); } } catch (Exception e) { // NOSONAR diff --git a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/Job.java b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/Job.java index dd518e7..dafa3bd 100644 --- a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/Job.java +++ b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/Job.java @@ -244,14 +244,14 @@ Result complete() { long duration = TimeUnit.NANOSECONDS .toMillis(System.nanoTime() - this.startTimeNanos); if (!outcome.contains(Outcome.SERIALIZATION_TIMEOUT)) { - messages.computeIfPresent(Outcome.NOT_SERIALIZABLE_CLASSES.name(), - (unused, info) -> info.stream() - .flatMap(className -> Stream.concat( - Stream.of(className), - unserializableDetails - .getOrDefault(className, - Collections.emptyList()) - .stream().map(entry -> "\t" + entry))) + messages.computeIfPresent(Outcome.NOT_SERIALIZABLE_CLASSES.name(), ( + unused, + info) -> info.stream().flatMap(className -> Stream.concat( + Stream.of(className), + unserializableDetails + .getOrDefault(className, + Collections.emptyList()) + .stream().map(entry -> "\t" + entry))) .collect(Collectors.toList())); } return new Result(sessionId, storageKey, outcome, duration, messages); diff --git a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/SerializationDebugRequestHandler.java b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/SerializationDebugRequestHandler.java index 54d920f..cc942f6 100644 --- a/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/SerializationDebugRequestHandler.java +++ b/kubernetes-kit-starter/src/main/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/SerializationDebugRequestHandler.java @@ -89,7 +89,8 @@ public class SerializationDebugRequestHandler implements RequestHandler { private final SerializationProperties serializationProperties; - public SerializationDebugRequestHandler(SerializationProperties serializationProperties) { + public SerializationDebugRequestHandler( + SerializationProperties serializationProperties) { this.serializationProperties = serializationProperties; } @@ -133,7 +134,8 @@ public boolean handleRequest(VaadinSession vaadinSession, }); } } - int serializationTimeout = getSerializationTimeout(serializationProperties); + int serializationTimeout = getSerializationTimeout( + serializationProperties); vaadinRequest.setAttribute( SERIALIZATION_TEST_REQUEST_ATTRIBUTE_KEY, new Runner(onSuccess, serializationTimeout)); @@ -205,7 +207,8 @@ public static void serializeAndDeserialize(WrappedSession session, new DebugTransientHandler(job)); try { trySerialize(serializer, debugHttpSession, job); - SessionInfo info = connector.waitForCompletion(serializationTimeout, LOGGER); + SessionInfo info = connector.waitForCompletion(serializationTimeout, + LOGGER); if (info != null) { debugHttpSession = new DebugHttpSession( "DEBUG-DESERIALIZE-" + session.getId()); @@ -252,7 +255,8 @@ private int getSerializationTimeout(SerializationProperties properties) { if (properties != null && properties.getTimeout() > 0) { timeout = properties.getTimeout(); } else { - String timeoutStr = System.getProperty(SERIALIZATION_TIMEOUT_PROPERTY); + String timeoutStr = System + .getProperty(SERIALIZATION_TIMEOUT_PROPERTY); if (timeoutStr != null) { timeout = Integer.parseInt(timeoutStr); } @@ -326,8 +330,9 @@ public void serviceInit(ServiceInitEvent serviceInitEvent) { } else { logger.info( "Installing SerializationDebugRequestHandler for session serialization debug"); - serviceInitEvent.addRequestHandler( - new SerializationDebugRequestHandler(serializationProperties)); + serviceInitEvent + .addRequestHandler(new SerializationDebugRequestHandler( + serializationProperties)); } } } diff --git a/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookieTest.java b/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookieTest.java index 5e2c281..d5752b1 100644 --- a/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookieTest.java +++ b/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerCookieTest.java @@ -29,7 +29,8 @@ void setIfNeeded_nullCookies_attributeIsSetAndCookieIsConfigured() { when(request.getCookies()).thenReturn(null); HttpServletResponse response = mock(HttpServletResponse.class); @SuppressWarnings("unchecked") - Consumer cookieConsumer = (Consumer) mock(Consumer.class); + Consumer cookieConsumer = (Consumer) mock( + Consumer.class); SessionTrackerCookie.setIfNeeded(session, request, response, cookieConsumer); @@ -46,7 +47,8 @@ void setIfNeeded_emptyCookies_attributeIsSetAndCookieIsConfigured() { when(request.getCookies()).thenReturn(new Cookie[0]); HttpServletResponse response = mock(HttpServletResponse.class); @SuppressWarnings("unchecked") - Consumer cookieConsumer = (Consumer) mock(Consumer.class); + Consumer cookieConsumer = (Consumer) mock( + Consumer.class); SessionTrackerCookie.setIfNeeded(session, request, response, cookieConsumer); @@ -66,7 +68,8 @@ void setIfNeeded_nullSessionAttribute_attributeIsSet() { when(request.getCookies()).thenReturn(new Cookie[] { new Cookie(CurrentKey.COOKIE_NAME, clusterKey) }); HttpServletResponse response = mock(HttpServletResponse.class); - Consumer cookieConsumer = (Cookie cookie) -> {}; + Consumer cookieConsumer = (Cookie cookie) -> { + }; SessionTrackerCookie.setIfNeeded(session, request, response, cookieConsumer); @@ -86,7 +89,8 @@ void setIfNeeded_nonNullSessionAttribute_attributeIsNotSet() { when(request.getCookies()).thenReturn(new Cookie[] { new Cookie(CurrentKey.COOKIE_NAME, clusterKey) }); HttpServletResponse response = mock(HttpServletResponse.class); - Consumer cookieConsumer = (Cookie cookie) -> {}; + Consumer cookieConsumer = (Cookie cookie) -> { + }; SessionTrackerCookie.setIfNeeded(session, request, response, cookieConsumer); diff --git a/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilterTest.java b/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilterTest.java index d322469..9d4bbff 100644 --- a/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilterTest.java +++ b/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/SessionTrackerFilterTest.java @@ -139,11 +139,13 @@ void validHttpSession_cookieConsumer_configuresCookie() throws Exception { setupHttpSession(); when(request.getContextPath()).thenReturn("contextpath"); - try (MockedStatic mockedStatic = mockStatic(SessionTrackerCookie.class)) { + try (MockedStatic mockedStatic = mockStatic( + SessionTrackerCookie.class)) { filter.doFilter(request, response, filterChain); - mockedStatic.verify(() -> SessionTrackerCookie.setIfNeeded(any(), any(), any(), - cookieConsumerArgumentCaptor.capture())); - Consumer cookieConsumer = cookieConsumerArgumentCaptor.getValue(); + mockedStatic.verify(() -> SessionTrackerCookie.setIfNeeded(any(), + any(), any(), cookieConsumerArgumentCaptor.capture())); + Consumer cookieConsumer = cookieConsumerArgumentCaptor + .getValue(); cookieConsumer.accept(cookie); } diff --git a/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/SerializationDebugRequestHandlerTest.java b/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/SerializationDebugRequestHandlerTest.java index a71a901..5c36d78 100644 --- a/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/SerializationDebugRequestHandlerTest.java +++ b/kubernetes-kit-starter/src/test/java/com/vaadin/kubernetes/starter/sessiontracker/serialization/debug/SerializationDebugRequestHandlerTest.java @@ -49,8 +49,7 @@ import static org.mockito.Mockito.when; @EnableOnJavaIOReflection -@EnabledIfSystemProperty(named = "sun.io.serialization.extendedDebugInfo", matches = "true", - disabledReason = "Tests need system property sun.io.serialization.extendedDebugInfo to be enabled") +@EnabledIfSystemProperty(named = "sun.io.serialization.extendedDebugInfo", matches = "true", disabledReason = "Tests need system property sun.io.serialization.extendedDebugInfo to be enabled") class SerializationDebugRequestHandlerTest { SerializationDebugRequestHandler handler; @@ -83,8 +82,8 @@ public void publishResults(Result result) { resultHolder.set(result); } }); - doAnswer(i -> (SerializableConsumer) resultHolder::set) - .when(ui).accessLater(any(SerializableConsumer.class), + doAnswer(i -> (SerializableConsumer) resultHolder::set).when(ui) + .accessLater(any(SerializableConsumer.class), any(SerializableRunnable.class)); when(ui.getPushConfiguration()).thenReturn(pushConfiguration); when(vaadinService.findUI(any())).thenReturn(ui); @@ -308,7 +307,8 @@ void handleRequest_serializationTimeout_timeoutReported() { Result result = resultHolder.get(); assertThat(result.getSessionId()).isEqualTo(httpSession.getId()); assertThat(result.getOutcomes()).containsExactlyInAnyOrder( - Outcome.NOT_SERIALIZABLE_CLASSES, Outcome.SERIALIZATION_TIMEOUT); + Outcome.NOT_SERIALIZABLE_CLASSES, + Outcome.SERIALIZATION_TIMEOUT); } @Test From 47fc9fe4de4e8577e4ffb37c5c79fe199f39e9c5 Mon Sep 17 00:00:00 2001 From: tamasmak Date: Mon, 4 Mar 2024 15:58:47 +0100 Subject: [PATCH 3/4] Revert vcf-nav version change --- .../src/main/java/com/vaadin/kubernetes/demo/Application.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/Application.java b/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/Application.java index cf6232c..fed8149 100644 --- a/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/Application.java +++ b/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/Application.java @@ -20,7 +20,7 @@ @Theme("kuberneteskitdemo") @PWA(name = "Kubernetes Kit Demo", shortName = "Kubernetes Kit Demo") @NpmPackage(value = "line-awesome", version = "1.3.0") -@NpmPackage(value = "@vaadin-component-factory/vcf-nav", version = "1.1.3") +@NpmPackage(value = "@vaadin-component-factory/vcf-nav", version = "1.0.6") @Push public class Application implements AppShellConfigurator { From 9594f60a093c0b8912a1b98064091078544a4fd9 Mon Sep 17 00:00:00 2001 From: tamasmak Date: Mon, 4 Mar 2024 16:06:04 +0100 Subject: [PATCH 4/4] Use SideNav and SideNavItem component --- .../demo/components/appnav/AppNav.java | 147 -------- .../demo/components/appnav/AppNavItem.java | 325 ------------------ .../kubernetes/demo/views/MainLayout.java | 37 +- 3 files changed, 18 insertions(+), 491 deletions(-) delete mode 100644 kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/components/appnav/AppNav.java delete mode 100644 kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/components/appnav/AppNavItem.java diff --git a/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/components/appnav/AppNav.java b/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/components/appnav/AppNav.java deleted file mode 100644 index fea0c7d..0000000 --- a/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/components/appnav/AppNav.java +++ /dev/null @@ -1,147 +0,0 @@ -package com.vaadin.kubernetes.demo.components.appnav; - -import com.vaadin.flow.component.Component; -import com.vaadin.flow.component.HasSize; -import com.vaadin.flow.component.HasStyle; -import com.vaadin.flow.component.Tag; -import com.vaadin.flow.component.dependency.JsModule; -import com.vaadin.flow.dom.Element; -import java.util.Optional; - -/** - * A navigation menu with support for hierarchical and flat menus. - *

- * Items can be added using {@link #addItem(AppNavItem)} and hierarchy can be - * created by adding {@link AppNavItem} instances to other {@link AppNavItem} - * instances. - */ -@JsModule("@vaadin-component-factory/vcf-nav") -@Tag("vcf-nav") -public class AppNav extends Component implements HasSize, HasStyle { - - /** - * Creates a new menu without any label. - */ - public AppNav() { - } - - /** - * Creates a new menu with the given label. - * - * @param label - * the label to use - */ - public AppNav(String label) { - setLabel(label); - } - - /** - * Adds menu item(s) to the menu. - * - * @param appNavItems - * the menu item(s) to add - * @return the menu for chaining - */ - public AppNav addItem(AppNavItem... appNavItems) { - for (AppNavItem appNavItem : appNavItems) { - getElement().appendChild(appNavItem.getElement()); - } - - return this; - } - - /** - * Removes the menu item from the menu. - *

- * If the given menu item is not a child of this menu, does nothing. - * - * @param appNavItem - * the menu item to remove - * @return the menu for chaining - */ - public AppNav removeItem(AppNavItem appNavItem) { - Optional parent = appNavItem.getParent(); - if (parent.isPresent() && parent.get() == this) { - getElement().removeChild(appNavItem.getElement()); - } - - return this; - } - - /** - * Removes all menu items from this item. - * - * @return this item for chaining - */ - public AppNav removeAllItems() { - getElement().removeAllChildren(); - return this; - } - - /** - * Gets the textual label for the navigation. - * - * @return the label or null if no label has been set - */ - public String getLabel() { - return getExistingLabelElement().map(e -> e.getText()).orElse(null); - } - - /** - * Set a textual label for the navigation. - *

- * This can help the end user to distinguish groups of navigation items. The - * label is also available for screen reader users. - * - * @param label - * the label to set - * @return this instance for chaining - */ - public AppNav setLabel(String label) { - getLabelElement().setText(label); - return this; - } - - private Optional getExistingLabelElement() { - return getElement().getChildren() - .filter(child -> "label".equals(child.getAttribute("slot"))) - .findFirst(); - } - - private Element getLabelElement() { - return getExistingLabelElement().orElseGet(() -> { - Element element = new Element("span"); - element.setAttribute("slot", "label"); - getElement().appendChild(element); - return element; - }); - } - - /** - * Check if the end user is allowed to collapse/hide and expand/show the - * navigation items. - *

- * NOTE: The navigation has to have a label for it to be collapsible. - * - * @return true if the menu is collapsible, false otherwise - */ - public boolean isCollapsible() { - return getElement().hasAttribute("collapsible"); - } - - /** - * Allow the end user to collapse/hide and expand/show the navigation items. - *

- * NOTE: The navigation has to have a label for it to be collapsible. - * - * @param collapsible - * true to make the whole navigation component collapsible, false - * otherwise - * @return this instance for chaining - */ - public AppNav setCollapsible(boolean collapsible) { - getElement().setAttribute("collapsible", ""); - return this; - } - -} diff --git a/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/components/appnav/AppNavItem.java b/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/components/appnav/AppNavItem.java deleted file mode 100644 index da8736b..0000000 --- a/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/components/appnav/AppNavItem.java +++ /dev/null @@ -1,325 +0,0 @@ -package com.vaadin.kubernetes.demo.components.appnav; - -import com.vaadin.flow.component.Component; -import com.vaadin.flow.component.Tag; -import com.vaadin.flow.component.dependency.JsModule; -import com.vaadin.flow.component.html.Span; -import com.vaadin.flow.component.icon.Icon; -import com.vaadin.flow.dom.Element; -import com.vaadin.flow.internal.StateTree; -import com.vaadin.flow.router.RouteConfiguration; -import com.vaadin.flow.router.Router; -import com.vaadin.flow.server.VaadinService; -import java.util.Optional; - -/** - * A menu item for the {@link AppNav} component. - *

- * Can contain a label and/or an icon and links to a given {@code path}. - */ -@JsModule("@vaadin-component-factory/vcf-nav") -@Tag("vcf-nav-item") -public class AppNavItem extends Component { - - /** - * Creates a menu item which does not link to any view but only shows the - * given label. - * - * @param label - * the label for the item - */ - public AppNavItem(String label) { - setLabel(label); - } - - /** - * Creates a new menu item using the given label that links to the given - * path. - * - * @param label - * the label for the item - * @param path - * the path to link to - */ - public AppNavItem(String label, String path) { - setPath(path); - setLabel(label); - } - - /** - * Creates a new menu item using the given label that links to the given - * view. - * - * @param label - * the label for the item - * @param view - * the view to link to - */ - public AppNavItem(String label, Class view) { - setPath(view); - setLabel(label); - } - - /** - * Creates a new menu item using the given label and icon that links to the - * given path. - * - * @param label - * the label for the item - * @param path - * the path to link to - * @param icon - * the icon for the item - */ - public AppNavItem(String label, String path, Icon icon) { - setPath(path); - setLabel(label); - setIcon(icon); - } - - /** - * Creates a new menu item using the given label that links to the given - * view. - * - * @param label - * the label for the item - * @param view - * the view to link to - * @param icon - * the icon for the item - */ - public AppNavItem(String label, Class view, - Icon icon) { - setPath(view); - setLabel(label); - setIcon(icon); - } - - /** - * Creates a new menu item using the given label and icon that links to the - * given path. - * - * @param label - * the label for the item - * @param path - * the path to link to - * @param iconClass - * the CSS class to use for showing the icon - */ - public AppNavItem(String label, String path, String iconClass) { - setPath(path); - setLabel(label); - - setIconClass(iconClass); - } - - /** - * Creates a new menu item using the given label and icon that links to the - * given path. - * - * @param label - * the label for the item - * @param view - * the view to link to - * @param iconClass - * the CSS class to use for showing the icon - */ - public AppNavItem(String label, Class view, - String iconClass) { - setPath(view); - setLabel(label); - - setIconClass(iconClass); - } - - /** - * Adds menu item(s) inside this item, creating a hierarchy. - * - * @param appNavItems - * the menu item(s) to add - * @return this item for chaining - */ - public AppNavItem addItem(AppNavItem... appNavItems) { - for (AppNavItem appNavItem : appNavItems) { - appNavItem.getElement().setAttribute("slot", "children"); - getElement().appendChild(appNavItem.getElement()); - } - - return this; - } - - /** - * Removes the given menu item from this item. - *

- * If the given menu item is not a child of this menu item, does nothing. - * - * @param appNavItem - * the menu item to remove - * @return this item for chaining - */ - public AppNavItem removeItem(AppNavItem appNavItem) { - Optional parent = appNavItem.getParent(); - if (parent.isPresent() && parent.get() == this) { - getElement().removeChild(appNavItem.getElement()); - } - - return this; - } - - /** - * Removes all menu items from this item. - * - * @return this item for chaining - */ - public AppNavItem removeAllItems() { - getElement().removeAllChildren(); - return this; - } - - /** - * Gets the label for the item. - * - * @return the label or null if no label has been set - */ - public String getLabel() { - return getExistingLabelElement().map(e -> e.getText()).orElse(null); - } - - /** - * Set a textual label for the item. - *

- * The label is also available for screen rader users. - * - * @param label - * the label to set - * @return this instance for chaining - */ - public AppNavItem setLabel(String label) { - getLabelElement().setText(label); - return this; - } - - private Optional getExistingLabelElement() { - return getElement().getChildren() - .filter(child -> !child.hasAttribute("slot")).findFirst(); - } - - private Element getLabelElement() { - return getExistingLabelElement().orElseGet(() -> { - Element element = Element.createText(""); - getElement().appendChild(element); - return element; - }); - } - - /** - * Sets the path this item links to. - * - * @param path - * the path to link to - * @return this instance for chaining - */ - public AppNavItem setPath(String path) { - getElement().setAttribute("path", path); - return this; - } - - /** - * Sets the view this item links to. - * - * @param view - * the view to link to - * @return this instance for chaining - */ - public AppNavItem setPath(Class view) { - String url = RouteConfiguration.forRegistry(getRouter().getRegistry()) - .getUrl(view); - setPath(url); - return this; - } - - private Router getRouter() { - Router router = null; - if (getElement().getNode().isAttached()) { - StateTree tree = (StateTree) getElement().getNode().getOwner(); - router = tree.getUI().getInternals().getRouter(); - } - if (router == null) { - router = VaadinService.getCurrent().getRouter(); - } - if (router == null) { - throw new IllegalStateException( - "Implicit router instance is not available. " - + "Use overloaded method with explicit router parameter."); - } - return router; - } - - public String getPath() { - return getElement().getAttribute("path"); - } - - private int getIconElementIndex() { - for (int i = 0; i < getElement().getChildCount(); i++) { - if ("prefix" - .equals(getElement().getChild(i).getAttribute("slot"))) { - return i; - } - } - return -1; - } - - /** - * Sets the icon for the item. - *

- * Can also be used to set a custom component to be shown in front of the - * label. - * - * @param icon - * the icon to show - * @return this instance for chaining - */ - public AppNavItem setIcon(Component icon) { - icon.getElement().setAttribute("slot", "prefix"); - int iconElementIndex = getIconElementIndex(); - if (iconElementIndex != -1) { - getElement().setChild(iconElementIndex, icon.getElement()); - } else { - getElement().appendChild(icon.getElement()); - } - return this; - } - - /** - * Sets the icon using a CSS class for the item. - *

- * Can also be used to set a custom component to be shown in front of the - * label. - * - * @param iconClass - * the CSS class to use for showing the icon - * @return this instance for chaining - */ - public AppNavItem setIconClass(String iconClass) { - Span icon = new Span(); - icon.setClassName(iconClass); - setIcon(icon); - return this; - } - - /** - * Sets the expanded status of the item. - * - * @param value - * true to expand the item, false to collapse it - */ - public AppNavItem setExpanded(boolean value) { - if (value) { - getElement().setAttribute("expanded", ""); - } else { - getElement().removeAttribute("expanded"); - } - return this; - } - -} diff --git a/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/views/MainLayout.java b/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/views/MainLayout.java index 6d0093b..4feecfe 100644 --- a/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/views/MainLayout.java +++ b/kubernetes-kit-demo/src/main/java/com/vaadin/kubernetes/demo/views/MainLayout.java @@ -1,7 +1,8 @@ package com.vaadin.kubernetes.demo.views; -import com.vaadin.kubernetes.demo.components.appnav.AppNav; -import com.vaadin.kubernetes.demo.components.appnav.AppNavItem; +import com.vaadin.flow.component.html.Span; +import com.vaadin.flow.component.sidenav.SideNav; +import com.vaadin.flow.component.sidenav.SideNavItem; import com.vaadin.kubernetes.demo.views.counter.CounterView; import com.vaadin.kubernetes.demo.views.counter.PushCounterView; import com.vaadin.kubernetes.demo.views.list.ListView; @@ -47,30 +48,22 @@ private void addDrawerContent() { Scroller scroller = new Scroller(createNavigation()); - addToDrawer(header, scroller, createFooter()); + addToDrawer(header, scroller, new Footer()); } - private AppNav createNavigation() { - // AppNav is not yet an official component. - // For documentation, visit https://github.com/vaadin/vcf-nav#readme - AppNav nav = new AppNav(); + private SideNav createNavigation() { + SideNav nav = new SideNav(); - nav.addItem(new AppNavItem("Counter", CounterView.class, - "la la-plus-circle")); - nav.addItem(new AppNavItem("PUSH Counter", PushCounterView.class, - "la la-plus-circle")); - nav.addItem( - new AppNavItem("List", ListView.class, "la la-plus-circle")); + nav.addItem(new SideNavItem("Counter", CounterView.class, + new Icon("la", "la-plus-circle"))); + nav.addItem(new SideNavItem("PUSH Counter", PushCounterView.class, + new Icon("la", "la-plus-circle"))); + nav.addItem(new SideNavItem("List", ListView.class, + new Icon("la", "la-plus-circle"))); return nav; } - private Footer createFooter() { - Footer layout = new Footer(); - - return layout; - } - @Override protected void afterNavigation() { super.afterNavigation(); @@ -82,4 +75,10 @@ private String getCurrentPageTitle() { .getAnnotation(PageTitle.class); return title == null ? "" : title.value(); } + + static class Icon extends Span { + Icon(String... classNames) { + addClassNames(classNames); + } + } }