GWT 2.1放置没有活动的示例

时间:2011-02-02 22:43:08

标签: gwt

有没有人有任何关于如何使用Places而不使用历史记录管理活动的示例。我快速敲了一下,可以通过浏览器返回和浏览器向前点击看到网址发生变化,但显示屏不会随处可见。

我正在使用DecoratedTabPanel并且有一个SelectionHandler来触发getPlaceController()。goTo(place)。

任何想法都会有用。

1 个答案:

答案 0 :(得分:14)

这是一段简单的代码,我用它来演示你的期望。它基于GWT和MVP开发文档(GWT and MVP

在此示例中,您可以在两个选项卡之间导航。选择时,将创建一个新的历史记录项(没有任何活动)。只要您使用浏览器按钮返回/转发,页面就会正确更新。

我定义了一个地方,一个活动及其视图。我根据自己的需要调整了AppActivityMapper,AppActivityManager和ClientFactory。代码很轻,不需要理解注释。我只是在需要时给出了一些解释,但如果不清楚则不要犹豫。

<强> ExampleView.java

public interface ExampleView extends IsWidget {
    void selectTab(int index);
}

<强> ExampleViewImpl.java

public class ExampleViewImpl extends Composite implements ExampleView, SelectionHandler<Integer> {

private DecoratedTabPanel panel;

public ExampleViewImpl() {
    panel = new DecoratedTabPanel();

    initComposite();

    initWidget(panel);
}

private void initComposite() {              
    panel.add(new HTML("Content 1"), "Tab 1");
    panel.add(new HTML("Content 2"), "Tab 2");

    panel.selectTab(0);

    panel.addSelectionHandler(this);
}

@Override
public void selectTab(int index) {
    if (index >=0 && index < panel.getWidgetCount()) {
        if (index != panel.getTabBar().getSelectedTab()) {
            panel.selectTab(index);
        }
    }
}

@Override
public void onSelection(SelectionEvent<Integer> event) {
    // Fire an history event corresponding to the tab selected
    switch (event.getSelectedItem()) {
    case 0:
        History.newItem("thetabplace:1");
        break;
    case 1:
        History.newItem("thetabplace:2");
        break;
    }
}
}

<强> ClientFactory.java

public class ClientFactory {
private final EventBus eventBus = new SimpleEventBus();
private final PlaceController placeController = new PlaceController(eventBus);
private final ExampleViewImpl example = new ExampleViewImpl();

public EventBus getEventBus() {
    return this.eventBus;
}

public PlaceController getPlaceController() {
    return this.placeController;
}

public ExampleViewImpl getExampleView() {
    return example;
}
}

<强> ExampleActivity.java

public class ExampleActivity extends AbstractActivity {

private ExampleView view;
private ClientFactory factory;

public ExampleActivity(ExamplePlace place, ClientFactory factory) {
    // Get the factory reference
    this.factory = factory;

    // Get the reference to the view
    view = this.factory.getExampleView();

    // Select the tab corresponding to the token value
    if (place.getToken() != null) {
        // By default the first tab is selected
        if (place.getToken().equals("") || place.getToken().equals("1")) {
            view.selectTab(0);
        } else if (place.getToken().equals("2")) {
            view.selectTab(1);
        }
    }
}

@Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
    // Attach this view to the application container
    panel.setWidget(view);
}
}

<强> ExamplePlace.java

/**
 * Just an very basic place
 */
public class ExamplePlace extends Place {
// The token corresponding to an action
private String token;

// This place should use a token to identify a view behavior
public ExamplePlace(String token) {
    this.token = token;
}

// Return the current token
public String getToken() {
    return token;
}

// Custom prefix to break the default name : ExamplePlace
// So that the history token will be thetabplace:token
// and not any more : ExamplePlace:token
@Prefix(value="thetabplace")
public static class Tokenizer implements PlaceTokenizer<ExamplePlace> {
    @Override
    public String getToken(ExamplePlace place) {
        return place.getToken();
    }

    @Override
    public ExamplePlace getPlace(String token) {
        return new ExamplePlace(token);
    }
}
}

<强> AppActivityMapper.java

public class AppActivityMapper implements ActivityMapper {
private ClientFactory clientFactory;

public AppActivityMapper(ClientFactory clientFactory) {
    super();
    this.clientFactory = clientFactory;
}

@Override
public Activity getActivity(Place place) {
    if (place instanceof ExamplePlace) {
        return new ExampleActivity((ExamplePlace) place, clientFactory);
    }
    return null;
}
}

<强> AppPlaceHistoryMapper.java

@WithTokenizers({ExamplePlace.Tokenizer.class})
public interface AppPlaceHistoryMapper extends PlaceHistoryMapper
{
}

一起

private Place defaultPlace = new ExamplePlace("1");
private SimplePanel appWidget = new SimplePanel();

public void onModuleLoad() {
    ClientFactory clientFactory = new ClientFactory();
    EventBus eventBus = clientFactory.getEventBus();
    PlaceController placeController = clientFactory.getPlaceController();

    // Start ActivityManager for the main widget with our ActivityMapper
    ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
    ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
    activityManager.setDisplay(appWidget);

    // Start PlaceHistoryHandler with our PlaceHistoryMapper
    AppPlaceHistoryMapper historyMapper= GWT.create(AppPlaceHistoryMapper.class);
    PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
    historyHandler.register(placeController, eventBus, defaultPlace);

    RootPanel.get().add(appWidget);
    // Goes to the place represented on URL else default place
    historyHandler.handleCurrentHistory();
}