View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.widgets.containers;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.student.common.ui.client.application.Application;
24  import org.kuali.student.common.ui.client.application.ApplicationContext;
25  import org.kuali.student.common.ui.client.application.KSAsyncCallback;
26  import org.kuali.student.common.ui.client.mvc.Callback;
27  import org.kuali.student.common.ui.client.service.ServerPropertiesRpcService;
28  import org.kuali.student.common.ui.client.service.ServerPropertiesRpcServiceAsync;
29  import org.kuali.student.common.ui.client.theme.Theme;
30  import org.kuali.student.common.ui.client.widgets.KSButton;
31  import org.kuali.student.common.ui.client.widgets.KSLabel;
32  import org.kuali.student.common.ui.client.widgets.KSLightBox;
33  import org.kuali.student.common.ui.client.widgets.NavigationHandler;
34  import org.kuali.student.common.ui.client.widgets.StylishDropDown;
35  import org.kuali.student.common.ui.client.widgets.headers.KSHeader;
36  import org.kuali.student.common.ui.client.widgets.menus.KSMenu.MenuImageLocation;
37  import org.kuali.student.common.ui.client.widgets.menus.KSMenuItemData;
38  
39  import com.google.gwt.core.client.GWT;
40  import com.google.gwt.event.dom.client.ClickEvent;
41  import com.google.gwt.event.dom.client.ClickHandler;
42  import com.google.gwt.event.dom.client.MouseOutEvent;
43  import com.google.gwt.event.dom.client.MouseOutHandler;
44  import com.google.gwt.event.dom.client.MouseOverEvent;
45  import com.google.gwt.event.dom.client.MouseOverHandler;
46  import com.google.gwt.user.client.Window;
47  import com.google.gwt.user.client.ui.Anchor;
48  import com.google.gwt.user.client.ui.Composite;
49  import com.google.gwt.user.client.ui.FlowPanel;
50  import com.google.gwt.user.client.ui.FocusPanel;
51  import com.google.gwt.user.client.ui.Frame;
52  import com.google.gwt.user.client.ui.HTML;
53  import com.google.gwt.user.client.ui.PopupPanel;
54  import com.google.gwt.user.client.ui.SimplePanel;
55  import com.google.gwt.user.client.ui.VerticalPanel;
56  import com.google.gwt.user.client.ui.Widget;
57  
58  
59  public class KSWrapper extends Composite{
60  
61      private static final String LUM_APP_URL		= "lum.application.url";
62      private static final String APP_URL			= "application.url";
63      private static final String DOC_SEARCH_URL	= "ks.rice.docSearch.serviceAddress";
64      private static final String RICE_URL		= "ks.rice.url";
65      private static final String RICE_LINK_LABEL	= "ks.rice.label";
66      private static final String APP_VERSION		= "ks.application.version";
67      
68      //This property is used to append code server param to urls so links displayed in get dev mode works as intended
69      private static final String CODE_SERVER		= "ks.gwt.codeServer"; 
70  
71      private ServerPropertiesRpcServiceAsync serverPropertiesRpcService = GWT.create(ServerPropertiesRpcService.class);
72  
73      private FlowPanel layout = new FlowPanel();
74  
75  	private KSHeader ksHeader = new KSHeader();
76  
77  	private StylishDropDown navDropDown = new StylishDropDown(getMessage("wrapperPanelTitleHome"));
78  	private Anchor versionAnchor = new Anchor(" ( Version ) ");
79  	//private Widget headerCustomWidget = Theme.INSTANCE.getCommonWidgets().getHeaderWidget();
80  
81  	private SimplePanel content = new SimplePanel();
82  	private KSLightBox docSearchDialog = new KSLightBox();
83  
84  	private Frame docSearch;
85      private String docSearchUrl = "";
86      private String appUrl = "..";
87      private String lumAppUrl = "..";
88      private String riceURL ="..";
89      private String riceLinkLabel="Rice";
90      private String appVersion = "";
91      private String codeServer = "";
92  
93      private boolean loaded = false;
94  
95      private static class WrapperNavigationHandler extends NavigationHandler{
96  		public WrapperNavigationHandler(String url) {
97  			super(url);
98  		}
99  
100 		@Override
101 		public void beforeNavigate(Callback<Boolean> callback) {
102 			//FIXME notify current controller of the page change so it can perform an action
103 			//FIXME before navigation event
104 			callback.exec(true);
105 		}
106     }
107 	public KSWrapper(){
108 		this.initWidget(layout);
109 	}
110 	protected void onLoad() {
111 		super.onLoad();
112 		if (!loaded){
113 			List<String> serverPropertyList = Arrays.asList(APP_URL, DOC_SEARCH_URL, LUM_APP_URL,RICE_URL,RICE_LINK_LABEL, APP_VERSION, CODE_SERVER);
114 
115 	        serverPropertiesRpcService.get(serverPropertyList, new KSAsyncCallback<Map<String,String>>() {
116 	            public void handleFailure(Throwable caught) {
117 	            	//ignoring, we'll use the default
118 	            	init();
119 	            }
120 
121 	            public void onSuccess(Map<String,String> result) {
122 	                GWT.log("ServerProperties fetched: "+result.toString(), null);
123 	                if(result != null){
124 	                    appUrl 			= result.get(APP_URL);
125 	                    docSearchUrl	= result.get(DOC_SEARCH_URL);
126 	                    lumAppUrl 		= result.get(LUM_APP_URL);
127 	                    riceURL         = result.get(RICE_URL);
128 	                    riceLinkLabel 	= result.get(RICE_LINK_LABEL);
129 	                    appVersion		= result.get(APP_VERSION);
130 	                    Application.getApplicationContext().setVersion(appVersion);
131 	                    if (result.get(CODE_SERVER) != null){
132 	                    	codeServer		= result.get(CODE_SERVER);
133 	                    }
134 	                }
135 	                init();
136 	            }
137 
138 	        });
139 
140 			loaded = false;
141 		}
142 	}
143 	private void init(){
144 		//headerBottomLinks.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
145 		createUserDropDown();
146 		//headerBottomLinks.add(userDropDown);
147 		ksHeader.setHiLabelText("Hi,");
148 		ksHeader.setUserName(Application.getApplicationContext().getUserId());
149 		Anchor logoutLink = new Anchor(getMessage("wrapperPanelLogout"));
150 		logoutLink.addClickHandler(new WrapperNavigationHandler("j_spring_security_logout"));
151 		ksHeader.addLogout(logoutLink);
152 		ksHeader.addLogout(versionAnchor);
153 
154 		//headerBottomLinks.add(logoutLink);
155 		createHelpInfo();
156 		createNavDropDown();
157 		ksHeader.addNavigation(navDropDown);
158 		
159 		
160 		List<KSLabel> topLinks = new ArrayList<KSLabel>();
161 		//FIXME does this do anything?
162 		topLinks.add(buildLink(riceLinkLabel,riceLinkLabel,riceURL+"/portal.do"));
163 		setHeaderCustomLinks(topLinks);
164 
165 		layout.add(ksHeader);
166 		layout.add(content);
167 
168 		navDropDown.addStyleName("KS-Navigation-DropDown");
169 		content.addStyleName("KS-Wrapper-Content");
170 		layout.addStyleName("KS-Wrapper");
171 	}
172 
173 	private void createHelpInfo(){
174 	    versionAnchor.addClickHandler(new ClickHandler(){
175 
176 	           public void onClick(ClickEvent event) {
177 	               final PopupPanel helpPopup = new PopupPanel(true);
178 	               helpPopup.setWidget(new HTML("<br><h3>&nbsp;&nbsp; " + appVersion + "&nbsp;&nbsp;<h3>"));
179 
180 	               helpPopup.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
181 	                   public void setPosition(int offsetWidth, int offsetHeight) {
182 	                     int left = (Window.getClientWidth() - offsetWidth);
183 	                     int top = 0;
184 	                     helpPopup.setPopupPosition(left, top);
185 	                   }
186 	               });
187 	           }
188 	    });
189 	}
190 
191 	private void createUserDropDown() {
192 		List<KSMenuItemData> items = new ArrayList<KSMenuItemData>();
193     	items.add(new KSMenuItemData(getMessage("wrapperPanelLogout"),new WrapperNavigationHandler("j_spring_security_logout")));
194 	}
195 
196 	private void createNavDropDown() {
197 		navDropDown.setImageLocation(MenuImageLocation.LEFT);
198 
199 		List<KSMenuItemData> items = new ArrayList<KSMenuItemData>();
200 
201 		items.add(new KSMenuItemData(getMessage("wrapperPanelTitleHome"),Theme.INSTANCE.getCommonImages().getSpacerIcon(),
202     			new WrapperNavigationHandler(
203     					"#"))
204     	);
205     	items.add(new KSMenuItemData(getMessage("wrapperPanelTitleMyActionList"),Theme.INSTANCE.getCommonImages().getApplicationIcon(),
206     			new WrapperNavigationHandler(
207     					lumAppUrl+"/org.kuali.student.lum.lu.ui.main.LUMMain/LUMMain.jsp"+(codeServer.isEmpty()?"":"?"+codeServer)))
208     	);
209 		items.add(new KSMenuItemData(getMessage("wrapperPanelTitleCurriculumManagement"),Theme.INSTANCE.getCommonImages().getBookIcon(),
210     			new WrapperNavigationHandler(
211     					lumAppUrl+"/org.kuali.student.lum.lu.ui.main.LUMMain/LUMMain.jsp?view=CURRICULUM_MANAGEMENT"+(codeServer.isEmpty()?"":"&"+codeServer)))
212     	);
213     	items.add(new KSMenuItemData(getMessage("wrapperPanelTitleOrg"), Theme.INSTANCE.getCommonImages().getPeopleIcon(),
214     			new WrapperNavigationHandler(
215     					lumAppUrl+"/org.kuali.student.core.organization.ui.OrgEntry/OrgEntry.jsp"))
216     	);
217     	items.add(new KSMenuItemData(getMessage("wrapperPanelTitleWorkflowDocSearch"), Theme.INSTANCE.getCommonImages().getNodeIcon(),
218     			new ClickHandler(){
219 
220 					@Override
221 					public void onClick(ClickEvent event) {
222 						buildDocSearchPanel();
223 						docSearchDialog.show();
224 					}})
225     	);
226     	items.add(new KSMenuItemData(getMessage("wrapperPanelTitleRice"), Theme.INSTANCE.getCommonImages().getRiceIcon(),
227     			new WrapperNavigationHandler(
228     					riceURL+"/portal.do?selectedTab=main"))
229     	);
230 
231 		navDropDown.setShowSelectedItem(true);
232     	navDropDown.setItems(items);
233     	navDropDown.setArrowImage(Theme.INSTANCE.getCommonImages().getDropDownIconWhite());
234 
235 	}
236 
237 	public void setContent(Widget wrappedContent){
238 		content.setWidget(wrappedContent);
239 	}
240 
241 	public void setHeaderCustomLinks(List<KSLabel> links){
242 		for(KSLabel link: links){
243 			FocusPanel panel = new FocusPanel();
244 			panel.setWidget(link);
245 			//headerTopLinks.add(panel);
246 			panel.addStyleName("KS-Wrapper-Header-Custom-Link-Panel");
247 			link.addStyleName("KS-Wrapper-Header-Custom-Link");
248 		}
249 	}
250 
251 	public void setFooterLinks(List<KSLabel> links){
252 		for(KSLabel link: links){
253 			//footer.add(link);
254 			link.addStyleName("KS-Wrapper-Footer-Link");
255 		}
256 	}
257 
258 
259     private KSLabel buildLink(final String text, final String title, final String actionUrl) {
260 
261         //Using KSLabel for now - couldn't change color for Anchor
262         final KSLabel link = new KSLabel(text);
263         link.addStyleName("KS-Header-Link");
264         link.setTitle(title);
265         link.addMouseOverHandler(new MouseOverHandler() {
266 
267             @Override
268             public void onMouseOver(MouseOverEvent event) {
269                 link.addStyleName("KS-Header-Link-Focus");
270             }});
271 
272         link.addMouseOutHandler(new MouseOutHandler() {
273 
274             @Override
275             public void onMouseOut(MouseOutEvent event) {
276                 link.removeStyleName("KS-Header-Link-Focus");
277 
278             }});
279         link.addClickHandler(new ClickHandler() {
280 
281             @Override
282             public void onClick(ClickEvent event) {
283                 Window.Location.assign(actionUrl);
284             }});
285 
286         return link;
287 
288     }
289 
290     //Method to build the light box for the doc search
291     private void buildDocSearchPanel(){
292     	if (docSearch == null){
293 	        docSearch = new Frame();
294 	    	docSearch.setSize("700px", "500px");
295 	        docSearch.setUrl(docSearchUrl);
296 
297 	        VerticalPanel docSearchPanel = new VerticalPanel();
298 	        docSearchPanel.add(docSearch);
299 
300 	        KSButton closeActionButton = new KSButton(getMessage("wrapperPanelClose"));
301 	        closeActionButton.addClickHandler(new ClickHandler(){
302 	            public void onClick(ClickEvent event) {
303 	                docSearchDialog.hide();
304 	            }
305 	        });
306 
307 	        docSearchPanel.add(closeActionButton);
308 	        docSearchDialog.setWidget(docSearchPanel);
309 	        docSearchDialog.setSize(750, 550);
310     	}
311     }
312 
313     private static String getMessage(final String messageId) {
314         return Application.getApplicationContext().getMessage(messageId);
315     }
316 }