PageModeMenuCommand.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  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.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.views.common.pagelinks.api;

  20. import org.apache.commons.lang3.StringUtils;

  21. import com.hack23.cia.web.impl.ui.application.views.common.viewnames.PageMode;
  22. import com.vaadin.ui.Button.ClickEvent;
  23. import com.vaadin.ui.Button.ClickListener;
  24. import com.vaadin.ui.MenuBar.Command;
  25. import com.vaadin.ui.MenuBar.MenuItem;
  26. import com.vaadin.ui.UI;

  27. /**
  28.  * The Class PageModeMenuCommand.
  29.  */
  30. public final class PageModeMenuCommand implements Command, ClickListener {

  31.     /** The Constant PAGE_SEPARATOR. */
  32.     private static final Character PAGE_SEPARATOR = '/';

  33.     /** The Constant serialVersionUID. */
  34.     private static final long serialVersionUID = 1L;

  35.     /** The page. */
  36.     private final String page;

  37.     /** The page reference. */
  38.     private final String pageReference;


  39.     /**
  40.      * Instantiates a new page mode menu command.
  41.      *
  42.      * @param page
  43.      *            the page
  44.      * @param pageMode
  45.      *            the page mode
  46.      */
  47.     public PageModeMenuCommand(final String page, final PageMode pageMode) {
  48.         super();
  49.         this.page = page;
  50.         pageReference = pageMode.toString();
  51.     }

  52.     /**
  53.      * Instantiates a new page mode menu command.
  54.      *
  55.      * @param page
  56.      *            the page
  57.      * @param pageMode
  58.      *            the page mode
  59.      * @param part
  60.      *            the part
  61.      */
  62.     public PageModeMenuCommand(final String page, final PageMode pageMode,final String part) {
  63.         super();
  64.         this.page = page;
  65.         pageReference = new StringBuilder().append(pageMode).append(PAGE_SEPARATOR).append(part).toString();
  66.     }


  67.     /**
  68.      * Instantiates a new page mode menu command.
  69.      *
  70.      * @param page
  71.      *            the page
  72.      * @param part
  73.      *            the part
  74.      */
  75.     public PageModeMenuCommand(final String page, final String part) {
  76.         super();
  77.         this.page = page;
  78.         pageReference = part;
  79.     }



  80.     /**
  81.      * Instantiates a new page mode menu command.
  82.      *
  83.      * @param page
  84.      *            the page
  85.      * @param pageMode
  86.      *            the page mode
  87.      * @param part
  88.      *            the part
  89.      */
  90.     public PageModeMenuCommand(final String page, final String pageMode, final String part) {
  91.         this.page = page;
  92.         pageReference = pageMode + PAGE_SEPARATOR + part;
  93.     }

  94.     @Override
  95.     public void buttonClick(final ClickEvent event) {
  96.         UI.getCurrent().getNavigator().navigateTo(page + PAGE_SEPARATOR + pageReference);
  97.     }

  98.     /**
  99.      * Gets the page path.
  100.      *
  101.      * @return the page path
  102.      */
  103.     public String getPagePath() {
  104.         if (pageReference != null && !pageReference.isEmpty()) {
  105.             return page + PAGE_SEPARATOR + pageReference;
  106.         } else {
  107.             return page;
  108.         }
  109.     }

  110.     @Override
  111.     public void menuSelected(final MenuItem selectedItem) {
  112.         UI.getCurrent().getNavigator().navigateTo(page + PAGE_SEPARATOR + pageReference);
  113.     }


  114.     /**
  115.      * Matches.
  116.      *
  117.      * @param page the page
  118.      * @param parameters the parameters
  119.      * @return true, if successful
  120.      */
  121.     public boolean matches(final String page, final String parameters) {
  122.         return this.page.equals(page) && StringUtils.contains(parameters, pageReference);
  123.     }

  124.     /**
  125.      * Gets the page id.
  126.      *
  127.      * @param parameters the parameters
  128.      * @return the page id
  129.      */
  130.     public static String getPageId(final String parameters) {
  131.         if (parameters != null) {
  132.             String cleanedString = parameters;
  133.             if (parameters.contains("[")) {
  134.                 cleanedString = cleanedString.replace(cleanedString.substring(cleanedString.indexOf('[') , cleanedString.lastIndexOf(']')+1), "");
  135.             }

  136.             return cleanedString.substring(cleanedString.lastIndexOf('/') + "/".length()).replace(PageMode.OVERVIEW.toString(),"");
  137.         } else {
  138.             return "";
  139.         }
  140.     }

  141.     /**
  142.      * Creates the item page command.
  143.      *
  144.      * @param part the part
  145.      * @return the page mode menu command
  146.      */
  147.     public PageModeMenuCommand createItemPageCommand(final String part) {
  148.         return new PageModeMenuCommand(page, pageReference, part);
  149.     }

  150. }