我们现在需要保证,当有新的feed添加的时候,feeds列表要正确的更新出来。同时,我们也需要保证,当用户选择了某个feed的tab的时候,相对应于feeds列表中的feed会同步的被选中。
为了实现如上两个要求,我们要创建两个对应的events——一个feed被添加,一个feed tab被选中
public static final EventType TabSelected = new EventType(); public static final EventType FeedAdded = new EventType();
public NavController() { registerEventTypes(AppEvents.Init); registerEventTypes(AppEvents.FeedAdded); registerEventTypes(AppEvents.TabSelected); }
private final ListField<BeanModel> feedList = new ListField<BeanModel>(); private ListLoader<ListLoadResult<BeanModel>> loader;
public void reloadFeeds() { loader.load(); }
public void selectFeed(Feed feed) { BeanModelFactory beanModelFactory = BeanModelLookup.get().getFactory( feed.getClass()); feedList.setSelection(Arrays.asList(beanModelFactory.createModel(feed))); }
private FeedList feedList = new FeedList(); public NavPanel() { setHeading("Navigation"); setLayout(new FitLayout()); initToolbar(); add(feedList); }
public void reloadFeeds() { feedList.reloadFeeds(); } public void selectFeed(Feed feed) { feedList.selectFeed(feed); }
private void onTabSelected(AppEvent event) { Feed feed = (Feed) event.getData(); navPanel.selectFeed(feed); }
private void onFeedAdded(AppEvent event) { navPanel.reloadFeeds(); }
@Override protected void handleEvent(AppEvent event) { EventType eventType = event.getType(); if (eventType.equals(AppEvents.Init)) { Dispatcher.forwardEvent(new AppEvent(AppEvents.NavPanelReady, navPanel)); } else if (eventType.equals(AppEvents.TabSelected)) { onTabSelected(event); } else if (eventType.equals(AppEvents.FeedAdded)) { onFeedAdded(event); } }
public void addFeed(final String feedUrl) { final FeedServiceAsync feedService = Registry .get(RSSReaderConstants.FEED_SERVICE); feedService.addExistingFeed(feedUrl, new AsyncCallback<Void>() { @Override public void onFailure(Throwable caught) { Info.display("RSS Reader", "Failed to add feed at: " + feedUrl); } @Override public void onSuccess(Void result) { tfUrl.clear(); Info.display("RSS Reader", "Feed at " + feedUrl + " added successfully"); Dispatcher.forwardEvent(AppEvents.FeedAdded); hide(); } }); }
public void save(final Feed feed) { feed.setTitle(tfTitle.getValue()); feed.setDescription(taDescription.getValue()); feed.setLink(tfLink.getValue()); final FeedServiceAsync feedService = Registry .get(RSSReaderConstants.FEED_SERVICE); feedService.saveFeed(feed, new AsyncCallback<Void>() { @Override public void onFailure(Throwable caught) { Info.display("RSS Reader", "Failed to save feed: " + feed.getTitle()); } @Override public void onSuccess(Void result) { Info.display("RSS Reader", "Feed " + feed.getTitle() + " saved sucessfully"); Dispatcher.forwardEvent(AppEvents.FeedAdded); } }); }
private void onFeedSelected(AppEvent event) { final Feed feed = event.getData(); final ItemGrid itemGrid = new ItemGrid(feed); TabItem tabItem = new TabItem(feed.getTitle()); tabItem.setId(feed.getUuid()); tabItem.setData("feed", feed); tabItem.add(itemGrid); tabItem.addListener(Events.Select, new Listener<TabPanelEvent>() {// 选中之后的tab清空item // selection @Override public void handleEvent(TabPanelEvent be) { itemGrid.resetSelection(); Dispatcher.forwardEvent(new AppEvent( AppEvents.TabSelected, feed)); } }); tabItem.setClosable(true); feedPanel.addTab(tabItem); }