Vaadin portlet Mode Changes

时间:2011-10-07 08:11:47

标签: java liferay portlet vaadin

我有一个Liferay Vaadin portlet有两种模式:编辑和查看模式。我在portlet中看到的第一件事是带有标签的viewContent:“未配置,如果未配置portlet。现在,如果我在Editmode中配置portlet,我会看到我在配置中所做的事情,它到目前为止工作但是现在如果我退出或重新启动浏览器(退出并重新开始),我看到未配置的带有标签的viewContent(“未配置”)

代码:

Window window; // Main Window
VerticalLayout viewContent; // View Mode Content 
VerticalLayout editContent; // Edit Mode Content(Configs)
Label viewText;
Button b;
Panel panel;
Embedded PictureA;

public void init() {
    window = new Window("");
    setMainWindow(window);
    viewContent = new VerticalLayout(); 
    editContent = new VerticalLayout();
    PictureA = new Embedded("", new ExternalResource(PictureAURL));
    PictureA.setType(Embedded.TYPE_IMAGE);

    panel = new Panel();
    panel.setStyleName(Reindeer.PANEL_LIGHT);

    // viewContent
    viewText = new Label("Not Configured" , Label.CONTENT_XHTML);
    viewContent.addComponent(viewText);
    window.setContent(viewContent);

    // EditContent
    b = new Button("PictureA");
    b.addListener(this):
    editContent.addComponent(b);
}

public void buttonClick(ClickEvent event) {
    if (event.getSource == b) {
        viewContent.revomeComponent(viewText);
        panel.addComponent(PictureA);
        viewContent.addComponent(panel);
    }
}

@Override
public void handleRenderRequest(RenderRequest request,
        RenderResponse response, Window window) {

}

@Override
public void handleActionRequest(ActionRequest request,
        ActionResponse response, Window window) {

}

@Override
public void handleEventRequest(EventRequest request,
        EventResponse response, Window window) {

}

@Override
public void handleResourceRequest(ResourceRequest request,
        ResourceResponse response, Window window) {

    // Switch the view according to the portlet mode 
    if (request.getPortletMode() == PortletMode.EDIT) 
        window.setContent(editContent); 
    else if (request.getPortletMode() == PortletMode.VIEW) 
        window.setContent(viewContent);         
}

情况:如果我点击按钮“PictureA”,标签“未配置”将被删除,带有嵌入图片的面板将被添加到viewContent。

唯一的问题是它没有被保存:/任何想法?也许我忘记了什么?

1 个答案:

答案 0 :(得分:4)

是的,您没有在任何地方保存配置。当会话结束(关闭浏览器)并重新打开应用程序时,会再次执行init并恢复原始的“未配置”状态。

例如,您可以将其存储到portlet首选项中。在handleResourceRequest方法中,您必须获取PortletPreferences的句柄:

this.prefs = request.getPreferences();

要在按钮单击处理程序中保存状态,请执行以下操作:

this.prefs.setValues("myapp.configured", new String[] {"true"});
this.prefs.store();

您还想恢复handleResourceRequest方法中已有的状态。做类似的事情:

boolean configured = Boolean.parseBoolean(this.prefs.getValues("myapp.configured", new String[] { "false" })[0]);

if (configured && PictureA.getParent() == null) {
    // PictureA is not visible, but it should be.
    viewContent.removeComponent(viewText);
    panel.addComponent(PictureA);
    viewContent.addComponent(panel);
}