View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package com.tapas.evidence.fe.menu;
20  
21  import javax.inject.Inject;
22  
23  import org.springframework.context.annotation.Scope;
24  import org.springframework.stereotype.Component;
25  import org.vaadin.mvp.eventbus.EventBus;
26  import org.vaadin.mvp.presenter.BasePresenter;
27  import org.vaadin.mvp.presenter.FactoryPresenter;
28  import org.vaadin.mvp.presenter.annotation.Presenter;
29  
30  import com.tapas.evidence.fe.child.ChildPresenter;
31  import com.tapas.evidence.fe.kindergarten.KindergartenPresenter;
32  import com.tapas.evidence.fe.main.MainEventBus;
33  import com.tapas.evidence.fe.responsible.ResponsiblePersonPresenter;
34  import com.tapas.evidence.fe.teacher.TeacherPresenter;
35  import com.tapas.evidence.service.KindergartenService;
36  import com.vaadin.ui.Field.ValueChangeEvent;
37  import com.vaadin.ui.Tree;
38  
39  /**
40   * Presenter for left menu.
41   * @author Michal Bocek
42   * @since 1.0.0
43   */
44  @Component("menuPresenter")
45  @Scope("prototype")
46  @Presenter(view = MenuView.class)
47  public class MenuPresenter extends FactoryPresenter<IMenuView, MainEventBus> {
48  
49  	@Inject
50  	private KindergartenService kindergartenService;
51  	
52  	@Override
53  	public void bind() {
54  		final Tree tree = this.view.getTree();
55  		addEntry(tree, null, Boolean.FALSE, this.getMessage("menu.kindergarten", this.getLocale()), KindergartenPresenter.class);
56  		addEntry(tree, null, Boolean.FALSE, this.getMessage("menu.teacher", this.getLocale()), TeacherPresenter.class);
57  		addEntry(tree, null, Boolean.FALSE, this.getMessage("menu.responsible", this.getLocale()), ResponsiblePersonPresenter.class);
58  		addEntry(tree, null, Boolean.FALSE, this.getMessage("menu.child", this.getLocale()), ChildPresenter.class);
59  	}
60  	
61  	private MenuEntry addEntry(final Tree tree, final MenuEntry parent, final Boolean hasChildren, final String caption,
62  			final Class<? extends BasePresenter<?, ? extends EventBus>> presenterType) {
63  		final MenuEntry entry = new MenuEntry(caption, presenterType);
64  		tree.addItem(entry);
65  		tree.setParent(entry, parent);
66  		tree.setChildrenAllowed(entry, hasChildren);
67  		return entry;
68  	}
69  
70  	/**
71  	 * Event handler method which react on menu sellection. 
72  	 * @param event
73  	 */
74  	public void onSelectMenu(final ValueChangeEvent event) {
75  		// get the selected menu entry and initiate another event
76  		final MenuEntry menuEntry = (MenuEntry) this.view.getTree().getValue();
77  		if (menuEntry != null) {
78  			if (menuEntry.getPresenterType().equals(ChildPresenter.class)) {
79  				this.eventBus.openModule(ResponsiblePersonPresenter.class);
80  			}
81  			this.eventBus.openModule(menuEntry.getPresenterType());
82  		}
83  	}
84  }