Articles/SimplePortalIntegration: DemoPortletApplication.java

File DemoPortletApplication.java, 3.0 kB (added by Marc Englund, 8 months ago)
Line 
1package com.itmill.toolkit.demo;
2
3import java.util.Iterator;
4import java.util.Map;
5
6import javax.servlet.http.HttpSession;
7
8import com.itmill.toolkit.Application;
9import com.itmill.toolkit.service.ApplicationContext.TransactionListener;
10import com.itmill.toolkit.terminal.gwt.server.WebApplicationContext;
11import com.itmill.toolkit.ui.FormLayout;
12import com.itmill.toolkit.ui.Label;
13import com.itmill.toolkit.ui.Window;
14
15public class DemoPortletApplication extends Application {
16
17        private Label portletMode;
18        private Label windowState;
19        private Label userPrincipal;
20        private Label userInfo;
21
22        public void init() {
23                // init main window, add labels
24                Window main = new Window();
25                setMainWindow(main);
26                FormLayout layout = new FormLayout();
27                main.addComponent(layout);
28
29                portletMode = new Label();
30                portletMode.setCaption("PortletMode: ");
31                layout.addComponent(portletMode);
32
33                windowState = new Label();
34                windowState.setCaption("WindowState: ");
35                layout.addComponent(windowState);
36
37                userPrincipal = new Label();
38                userPrincipal.setCaption("UserPrincipal: ");
39                layout.addComponent(userPrincipal);
40
41                userInfo = new Label();
42                userInfo.setCaption("User info: ");
43                layout.addComponent(userInfo);
44                userInfo.setContentMode(Label.CONTENT_PREFORMATTED);
45
46                Label id = new Label("" + this);
47                id.setCaption("Application instance: ");
48                layout.addComponent(id);
49
50                // Add a transaction listener, so we can update the portlet stuff
51                initTransactionListener();
52        }
53
54        private void initTransactionListener() {
55                getContext().addTransactionListener(new TransactionListener() {
56
57                        public void transactionEnd(Application application,
58                                        Object transactionData) {
59                                // not used
60                        }
61
62                        public void transactionStart(Application application,
63                                        Object transactionData) {
64                                if (application == DemoPortletApplication.this) {
65                                        // get the session
66                                        HttpSession ses = ((WebApplicationContext) getContext())
67                                                        .getHttpSession();
68                                        // the portlet has stored it's info there:
69                                        DemoPortlet.PortletInfo p = (DemoPortlet.PortletInfo) ses.getAttribute(ses
70                                                        .getServletContext().getServletContextName()
71                                                        + "/DemoPortletApplication");
72                                        if (p == null) {
73                                                // not found, try w/o application name
74                                                p = (DemoPortlet.PortletInfo) ses.getAttribute(ses
75                                                                .getServletContext().getServletContextName()+"/");
76                                        }
77
78                                        if (p != null) {
79                                                // update labels
80                                                portletMode.setValue(p.getPortletMode());
81                                                windowState.setValue(p.getWindowState());
82                                                userPrincipal.setValue(p.getUserPrincipal());
83                                                Map uinfo = p.getUserInfo();
84                                                if (uinfo != null) {
85                                                        String s = "";
86                                                        for (Iterator it = uinfo.keySet().iterator(); it
87                                                                        .hasNext();) {
88                                                                Object key = it.next();
89                                                                Object val = uinfo.get(key);
90                                                                s += key + ": " + val + "\n";
91                                                        }
92                                                        userInfo.setValue(s);
93                                                } else {
94                                                        userInfo.setValue("-");
95                                                }
96                                        } else {
97                                                // portlet not started
98                                        }
99                                }
100                        }
101
102                });
103
104        }
105}