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.pageclicklistener;
20  
21  import java.util.Set;
22  
23  import com.vaadin.event.SelectionEvent;
24  import com.vaadin.event.SelectionEvent.SelectionListener;
25  import com.vaadin.ui.UI;
26  import com.vaadin.ui.renderers.ClickableRenderer.RendererClickEvent;
27  import com.vaadin.ui.renderers.ClickableRenderer.RendererClickListener;
28  
29  /**
30   * The class AbstractPageItemRendererClickListener.
31   *
32   * @param <T>
33   *            the generic type
34   */
35  public abstract class AbstractPageItemRendererClickListener<T> implements RendererClickListener ,SelectionListener{
36  
37  	/** The Constant PAGE_SEPARATOR. */
38  	private static final Character PAGE_SEPARATOR = '/';
39  
40  	/** The Constant serialVersionUID. */
41  	private static final long serialVersionUID = 1L;
42  
43  	/** The page. */
44  	private final String page;
45  
46  	/**
47  	 * Instantiates a new abstract page item renderer click listener.
48  	 *
49  	 * @param page
50  	 *            the page
51  	 */
52  	public AbstractPageItemRendererClickListener(final String page) {
53  		this.page = page;
54  	}
55  
56  	@Override
57  	public final void click(final RendererClickEvent event) {
58  		UI.getCurrent().getNavigator().navigateTo(page + PAGE_SEPARATOR + getPageId((T)event.getItemId()));
59  	}
60  
61  
62  	@Override
63  	public final void select(final SelectionEvent event) {
64  		final Set<T> added =(Set<T>) event.getAdded();
65  
66  		if (!added.isEmpty()) {
67  			UI.getCurrent().getNavigator().navigateTo(page + PAGE_SEPARATOR + getPageId(added.iterator().next()));
68  
69  		}
70  
71  	}
72  
73  	/**
74  	 * Gets the page id.
75  	 *
76  	 * @param t
77  	 *            the t
78  	 * @return the page id
79  	 */
80  	protected abstract String getPageId(T t);
81  
82  }