View Javadoc
1   /*
2    * Copyright 2014 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  
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  /**
29   * The Class PageModeMenuCommand.
30   */
31  public final class PageModeMenuCommand implements Command, ClickListener {
32  
33  	/** The Constant PAGE_SEPARATOR. */
34  	private static final Character PAGE_SEPARATOR = '/';
35  
36  	/** The Constant serialVersionUID. */
37  	private static final long serialVersionUID = 1L;
38  
39  	/** The page reference. */
40  	private final String pageReference;
41  
42  	/** The page. */
43  	private final String page;
44  
45  
46  	/**
47  	 * Instantiates a new page mode menu command.
48  	 *
49  	 * @param page
50  	 *            the page
51  	 * @param pageMode
52  	 *            the page mode
53  	 */
54  	public PageModeMenuCommand(final String page, final PageMode pageMode) {
55  		super();
56  		this.page = page;
57  		pageReference = pageMode.toString();
58  	}
59  
60  	/**
61  	 * Instantiates a new page mode menu command.
62  	 *
63  	 * @param page
64  	 *            the page
65  	 * @param part
66  	 *            the part
67  	 */
68  	public PageModeMenuCommand(final String page, final String part) {
69  		super();
70  		this.page = page;
71  		pageReference = part;
72  	}
73  
74  
75  	/**
76  	 * Instantiates a new page mode menu command.
77  	 *
78  	 * @param page
79  	 *            the page
80  	 * @param pageMode
81  	 *            the page mode
82  	 * @param part
83  	 *            the part
84  	 */
85  	public PageModeMenuCommand(final String page, final PageMode pageMode,final String part) {
86  		super();
87  		this.page = page;
88  		pageReference = new StringBuilder().append(pageMode).append(PAGE_SEPARATOR).append(part).toString();
89  	}
90  
91  
92  
93  	/**
94  	 * Instantiates a new page mode menu command.
95  	 *
96  	 * @param page
97  	 *            the page
98  	 * @param pageMode
99  	 *            the page mode
100 	 * @param part
101 	 *            the part
102 	 */
103 	public PageModeMenuCommand(final String page, final String pageMode, final String part) {
104 		this.page = page;
105 		pageReference = pageMode + PAGE_SEPARATOR + part;
106 	}
107 
108 	/**
109 	 * Gets the page path.
110 	 *
111 	 * @return the page path
112 	 */
113 	public String getPagePath() {
114 		if (pageReference != null && !pageReference.isEmpty()) {
115 			return page + PAGE_SEPARATOR + pageReference;
116 		} else {
117 			return page;
118 		}
119 	}
120 
121 	@Override
122 	public void menuSelected(final MenuItem selectedItem) {
123 		UI.getCurrent().getNavigator().navigateTo(page + PAGE_SEPARATOR + pageReference);
124 	}
125 
126 	@Override
127 	public void buttonClick(final ClickEvent event) {
128 		UI.getCurrent().getNavigator().navigateTo(page + PAGE_SEPARATOR + pageReference);
129 	}
130 
131 }