GXT之旅:第七章:MVC——MVC重构项目(4)

使用MVC重构RSSReader项目——Item区域

之前,我们已经创建了两个非常类似的Controller和View,分别服务于NavPanel和FeedPanel。现在我们继续按照同样的思路来构建ItemPanel——ItemPanelReady(EventType),ItemController和ItemView。

我们的目的是生成如下样式的应用效果:


  • AppEvents class
package com.danielvaughan.rssreader.client.mvc.events;

import com.extjs.gxt.ui.client.event.EventType;

public class AppEvents {
	public static final EventType Init = new EventType();
	public static final EventType Error = new EventType();
	public static final EventType UIReady = new EventType();
	public static final EventType NavPanelReady = new EventType();
	public static final EventType FeedPanelReady = new EventType();
	public static final EventType ItemPanelReady = new EventType();
}
  • AppController class

package com.danielvaughan.rssreader.client.mvc.controllers;

import com.danielvaughan.rssreader.client.mvc.events.AppEvents;
import com.danielvaughan.rssreader.client.mvc.views.AppView;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;

public class AppController extends Controller {

	private View appView;

	public AppController() {
		registerEventTypes(AppEvents.Init);
		registerEventTypes(AppEvents.Error);
		registerEventTypes(AppEvents.UIReady);
		registerEventTypes(AppEvents.NavPanelReady);
		registerEventTypes(AppEvents.FeedPanelReady);
		registerEventTypes(AppEvents.ItemPanelReady);
	}

	@Override
	public void handleEvent(AppEvent event) {

		forwardToView(appView, event);
	}

	@Override
	public void initialize() {
		super.initialize();
		appView = new AppView(this);
	}
}

  • RSSReader class

package com.danielvaughan.rssreader.client;

import com.danielvaughan.rssreader.client.mvc.controllers.AppController;
import com.danielvaughan.rssreader.client.mvc.controllers.FeedController;
import com.danielvaughan.rssreader.client.mvc.controllers.NavController;
import com.danielvaughan.rssreader.client.mvc.events.AppEvents;
import com.danielvaughan.rssreader.client.services.FeedService;
import com.danielvaughan.rssreader.client.services.FeedServiceAsync;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class RSSReader implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {
		final FeedServiceAsync feedService = GWT.create(FeedService.class);
		Registry.register(RSSReaderConstants.FEED_SERVICE, feedService);
		Dispatcher dispatcher = Dispatcher.get();

		dispatcher.addController(new AppController());
		dispatcher.addController(new NavController());
		dispatcher.addController(new FeedController());
		dispatcher.addController(new ItemController());

		dispatcher.dispatch(AppEvents.Init);
		dispatcher.dispatch(AppEvents.UIReady);
	}
}

  • ItemController class

package com.danielvaughan.rssreader.client.mvc.controllers;

import com.danielvaughan.rssreader.client.mvc.events.AppEvents;
import com.danielvaughan.rssreader.client.mvc.views.ItemView;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;

public class ItemController extends Controller {

	private ItemView itemView;

	public ItemController() {
		registerEventTypes(AppEvents.Init);
	}

	@Override
	public void handleEvent(AppEvent event) {
		forwardToView(itemView, event);
	}

	@Override
	public void initialize() {
		super.initialize();
		itemView = new ItemView(this);
	}
}

  • ItemView class:

package com.danielvaughan.rssreader.client.mvc.views;

import com.danielvaughan.rssreader.client.components.ItemPanel;
import com.danielvaughan.rssreader.client.mvc.events.AppEvents;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;

public class ItemView extends View {
	private final ItemPanel itemPanel = new ItemPanel();

	public ItemView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		EventType eventType = event.getType();
		if (eventType.equals(AppEvents.Init)) {
			Dispatcher.forwardEvent(new AppEvent(AppEvents.ItemPanelReady,
					itemPanel));
		}
	}

}

  • AppView class:
package com.danielvaughan.rssreader.client.mvc.views;

import com.danielvaughan.rssreader.client.mvc.events.AppEvents;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

public class AppView extends View {
	private final ContentPanel mainPanel = new ContentPanel();
	private final Viewport viewport = new Viewport();

	public AppView(Controller controller) {
		super(controller);

	}

	@Override
	protected void handleEvent(AppEvent event) {
		EventType eventType = event.getType();
		if (eventType.equals(AppEvents.Init)) {
			onInit(event);
		} else if (eventType.equals(AppEvents.Error)) {
			onError(event);
		} else if (eventType.equals(AppEvents.UIReady)) {
			onUIReady(event);
		} else if (eventType.equals(AppEvents.NavPanelReady)) {
			onNavPanelReady(event);
		} else if (eventType.equals(AppEvents.FeedPanelReady)) {
			onFeedPanelReady(event);
		} else if (eventType.equals(AppEvents.ItemPanelReady)) {
			onItemPanelReady(event);
		}
	}

	private void onInit(AppEvent event) {
		final BorderLayout borderLayout = new BorderLayout();
		viewport.setLayout(borderLayout);
		HTML headerHtml = new HTML();
		headerHtml.setHTML("<h1>RSS Reader</h1>");
		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH,
				20);
		northData.setCollapsible(false);
		northData.setSplit(false);
		viewport.add(headerHtml, northData);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setCollapsible(false);
		RowLayout rowLayout = new RowLayout(Orientation.VERTICAL);
		mainPanel.setHeaderVisible(false);
		mainPanel.setLayout(rowLayout);
		viewport.add(mainPanel, centerData);
	}

	private void onNavPanelReady(AppEvent event) {
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST,
				200, 150, 300);
		westData.setCollapsible(true);
		westData.setSplit(true);
		Component component = event.getData();
		viewport.add(component, westData);
	}

	private void onFeedPanelReady(AppEvent event) {
		RowData rowData = new RowData();
		rowData.setHeight(.5);
		Component component = event.getData();
		mainPanel.add(component, rowData);
	}

	private void onItemPanelReady(AppEvent event) {
		RowData rowData = new RowData();
		rowData.setHeight(.5);
		Component component = event.getData();
		mainPanel.add(component, rowData);
	}

	private void onUIReady(AppEvent event) {
		RootPanel.get().add(viewport);
	}

	private void onError(AppEvent event) {
	}
}


你可能感兴趣的:(html,mvc,rss,service,Class)