2011-10-07 42 views
1

我有兩種模式的Liferay Vaadin portlet:編輯和查看模式。我在portlet中看到的第一件事是帶標籤的viewContent:「未配置,如果portlet未配置。現在,如果我在Editmode中配置portlet,我會看到我在配置中做的東西,它工作到目前爲止但現在,如果我註銷或重新啓動瀏覽器(退出並重新開始),我看沒有配置viewContent與標籤(「未配置」)Vaadin portlet模式更改

代碼:

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。

唯一的問題是它沒有被保存:/任何想法?也許我忘了一些東西?

回答

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); 
} 
+0

我一直在試圖理解你剛剛做了很長時間,我仍然沒有得到它,但它的工作原理。例如,如果我想用另一個按鈕刪除PitureA,那麼我該怎麼做? – Kiesa

+0

這只是一個顯示如何將狀態保存到portlet首選項並在handleResourceRequest中恢復狀態的示例。如果您喜歡,可以使用viewContent.addComponent和viewContent.removeComponent方法隨意修改UI結構。 – eeq