Java tutorial
/* * Better places framework for Gwt. * * Copyright (c) 2014, Rony George * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.cloud4gl.client; import java.util.List; import java.util.HashMap; import java.util.Map; import com.google.gwt.core.client.GWT; import com.google.gwt.event.logical.shared.ValueChangeEvent; import com.google.gwt.event.logical.shared.ValueChangeHandler; import com.google.gwt.user.client.History; import com.google.gwt.user.client.ui.AcceptsOneWidget; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.TabBar; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.place.shared.PlaceHistoryHandler; import com.google.gwt.place.shared.PlaceTokenizer; import com.google.gwt.event.shared.EventBus; import com.google.gwt.place.shared.PlaceController; import org.cloud4gl.client.activity.*; import org.cloud4gl.client.event.*; /** * The AppController is the central class in the "Better places framework for Gwt" * * It implements ValueChangeHandler which listens for URL changes. * * Also implements PlaceTokenizer and gets Place instance from token and * token from place by use of a pre-populated map of Place objects. * This is the common tokeniser used by all sub classes of CommonPlace. * * This also implements ICommonPresenter with gotoPace method for going to any Place. * * There are also getter methods to all app wide resources and the places framework. * * @author georon * */ public class AppController implements ValueChangeHandler<String>, PlaceTokenizer<CommonPlace>, ICommonPresenter { private EventBus eventBus; //??HandlerManager private GreetingServiceAsync rpcService; private PlaceController placeController; private final AppConf appConf; private final ClientFactory clientFactory = GWT.create(ClientFactory.class); private final AppPlaceHistoryMapper appPlaceHistoryMapper; private PlaceHistoryHandler placeHistoryHandler; private AcceptsOneWidget container; //main contents widget private Map<String, CommonPlace> placeMap; private Map<Integer, CommonPlace> placeMapByIndex; private CommonPlace defaultPlace; private CommonPlace currentPlace; private TabBar tabMenu; private int indexOfDefPlace; /** * Constructor takes the AppConf object map * A Map with token for keys, and CommonPlace subclass instances as values is populated * from the places found in AppConf * * Also these objects required for the Places framework and history management * are initialized. * EventBus * PlaceController * AppPlaceHistoryMapper * PlaceHistoryHandler * * registers the default place with PlaceHistoryHandler * * registers itself/this with the History ValueChangeHandler to listen to URL changes. * * @param appConf */ public AppController(AppConf appConf) { this.rpcService = clientFactory.getGreetingService(); this.appConf = appConf; this.placeMap = new HashMap<String, CommonPlace>(); this.placeMapByIndex = new HashMap<Integer, CommonPlace>(); this.indexOfDefPlace = this.appConf.getDefaultPlace() - 1; List<PlaceConf> places = this.appConf.getPlaces(); for (int i = 0, n = places.size(); i < n; i++) { PlaceConf placeConf = places.get(i); CommonPlace cPlace = new CommonPlace(placeConf); cPlace.setMenuIndex(i); placeMap.put(placeConf.getToken(), cPlace); placeMapByIndex.put(new Integer(i), cPlace); if (indexOfDefPlace == i) { defaultPlace = cPlace; } } this.eventBus = clientFactory.getEventBus(); this.placeController = new PlaceController(eventBus); appPlaceHistoryMapper = new AppPlaceHistoryMapper(this); placeHistoryHandler = new PlaceHistoryHandler(appPlaceHistoryMapper); placeHistoryHandler.register(placeController, eventBus, defaultPlace); //fix this this.tabMenu = makeTabBar(appConf); History.addValueChangeHandler(this); } public ClientFactory getClientFactory() { return clientFactory; } public PlaceHistoryHandler getHistoryHandler() { return placeHistoryHandler; } @Override public void goPlace(CommonPlace place) { BaseActivity activity; activity = getActivity(place); activity.init(this, place); activity.start(this.container, this.eventBus); //create view based on activity GWT.log("goPlace:" + place.getToken()); } public void onValueChange(ValueChangeEvent<String> event) { String token = event.getValue(); CommonPlace place; if ((token == null) || ("".equals(token))) { place = getDefaultPlace(); } else { place = getPlace(token); } this.getTabMenu().selectTab(place.getMenuIndex(), false); goPlace(place); } @Override public CommonPlace getPlace(String token) { return placeMap.get(token); } @Override public String getToken(CommonPlace place) { return place.getToken(); } public CommonPlace getPlaceByIndex(Integer index) { return placeMapByIndex.get(index); } public GreetingServiceAsync getRpcService() { return rpcService; } public CommonPlace getDefaultPlace() { return defaultPlace; } public AcceptsOneWidget getContainer() { return container; } public void setContainer(AcceptsOneWidget container) { this.container = container; } public static VerticalPanel makeMenuPanel(CommonPlace commPlace) { final VerticalPanel menuPanel = new VerticalPanel(); LeftNavClickHandler lnClickHandler = new LeftNavClickHandler(); //fix this, right logic is defaultPlace not first place PlaceConf placeConf = commPlace.getPlaceConf(); Map<String, String> activities = placeConf.getActivities(); boolean onFirstItem = true; for (String activityKey : activities.keySet()) { Button item1Button = new Button(activityKey); item1Button.addClickHandler(lnClickHandler); menuPanel.add(item1Button); if (onFirstItem) { menuPanel.setCellVerticalAlignment(item1Button, VerticalPanel.ALIGN_TOP); onFirstItem = false; } } return menuPanel; } public CommonPlace getCurrentPlace() { return currentPlace; } private BaseActivity getActivity(CommonPlace place) { BaseActivity act = place.getNextActivity(); if (act == null) { String defActkey = place.getPlaceConf().getDefaultActivity(); String actClass = place.getPlaceConf().getActivities().get(defActkey); try { act = BaseActivity.getActivity(actClass); place.setNextActivity(act); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return act; } private static TabBar makeTabBar(AppConf appConf) { TabBar tabbar = new TabBar(); List<PlaceConf> places = appConf.getPlaces(); for (PlaceConf placeConf : places) { tabbar.addTab(placeConf.getName()); } return tabbar; } public TabBar getTabMenu() { return tabMenu; } public PlaceController getPlaceController() { return this.placeController; } }