View Javadoc

1   package org.kuali.student.lum.program.client.widgets;
2   
3   import java.util.Date;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.kuali.student.common.assembly.data.Metadata;
8   import org.kuali.student.common.assembly.data.QueryPath;
9   import org.kuali.student.common.ui.client.application.Application;
10  import org.kuali.student.common.ui.client.application.ViewContext;
11  import org.kuali.student.common.ui.client.configurable.mvc.DefaultWidgetFactory;
12  import org.kuali.student.common.ui.client.configurable.mvc.binding.HasDataValueBinding;
13  import org.kuali.student.common.ui.client.mvc.DataModel;
14  import org.kuali.student.common.ui.client.mvc.HasDataValue;
15  import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
16  import org.kuali.student.lum.common.client.widgets.AppLocations;
17  import org.kuali.student.lum.program.client.ProgramConstants;
18  import org.kuali.student.lum.program.client.ProgramMsgConstants;
19  import org.kuali.student.lum.program.client.ProgramUtils;
20  import org.kuali.student.lum.program.client.events.AfterSaveEvent;
21  import org.kuali.student.lum.program.client.events.ModelLoadedEvent;
22  import org.kuali.student.lum.program.client.major.MajorController;
23  
24  import com.google.gwt.event.dom.client.ClickEvent;
25  import com.google.gwt.event.dom.client.ClickHandler;
26  import com.google.gwt.event.shared.HandlerManager;
27  import com.google.gwt.user.client.ui.Anchor;
28  import com.google.gwt.user.client.ui.Composite;
29  import com.google.gwt.user.client.ui.HorizontalPanel;
30  import com.google.gwt.user.client.ui.Label;
31  import com.google.gwt.user.client.ui.SimplePanel;
32  import com.google.gwt.user.client.ui.VerticalPanel;
33  import com.google.gwt.user.client.ui.Widget;
34  
35  /**
36   * @author Igor
37   */
38  public class ProgramSideBar extends Composite {
39  
40      private final VerticalPanel content = new VerticalPanel();
41  
42      private State state = State.VIEW;
43  
44      private Type type;
45  
46      private Label versionLabel;
47      private Anchor viewVersion;
48      private Label historyLabel = new Label(getLabel(ProgramMsgConstants.SIDEBAR_HISTORY));
49      private Label lastUpdatedDate = new Label();
50      private SimplePanel scheduledReviewDate = new SimplePanel();
51      private Label lastReviewDate = new Label();
52      private final HandlerManager eventBus;
53  
54      private ViewContext viewContext;
55  
56      private final SideBarDialogManager dialogManager;
57  
58      public ProgramSideBar(HandlerManager eventBus, Type type) {
59          this.eventBus = eventBus;
60          this.type = type;
61          dialogManager = new SideBarDialogManager(eventBus);
62          initWidget(content);
63          setStyles();
64          buildLayout();
65          bind();
66      }
67  
68      public void initialize(MajorController controller) {
69          DataModel model = controller.getProgramModel();
70          dialogManager.configureView(model.getDefinition(), controller);
71          viewContext = controller.getViewContext();
72          updateFields(model);
73      }
74  
75      private void bind() {
76          eventBus.addHandler(ModelLoadedEvent.TYPE, new ModelLoadedEvent.Handler() {
77              @Override
78              public void onEvent(ModelLoadedEvent event) {
79                  updateFields(event.getModel());
80              }
81          });
82          eventBus.addHandler(AfterSaveEvent.TYPE, new AfterSaveEvent.Handler() {
83              @Override
84              public void onEvent(AfterSaveEvent event) {
85                  DataModel model = event.getModel();
86                  dialogManager.configureView(model.getDefinition(), event.getController());
87                  updateFields(event.getModel());
88              }
89          });
90      }
91  
92      private void updateFields(DataModel model) {
93      	String programType = model.get(ProgramConstants.TYPE);
94  
95      	//The sidebar fields should only be updated if the model that was changed/loaded is associated with the sidebar
96      	boolean doUpdate =	(type == ProgramSideBar.Type.CORE && ProgramConstants.CORE_LU_TYPE_ID.equals(programType)) ||
97      						(type == ProgramSideBar.Type.CREDENTIAL && programType.startsWith(ProgramConstants.CRED_LU_TYPE_PREFIX)) ||
98      						(type == ProgramSideBar.Type.MAJOR && ProgramConstants.MAJOR_LU_TYPE_ID.equals(programType)); 
99      	
100     	if (doUpdate){
101 	    	setVersion((Long)model.get(ProgramConstants.VERSION), versionLabel);
102 	        setDate((Date) model.get(ProgramConstants.LAST_UPDATED_DATE), lastUpdatedDate);
103 	        lastReviewDate.setText((String) model.get(ProgramConstants.LAST_REVIEW_DATE));
104 	        setWidget(ProgramConstants.SCHEDULED_REVIEW_DATE, scheduledReviewDate, model);
105     	}
106     }
107 
108     private void setDate(Date updatedDate, Label lastUpdatedDate) {
109         if (updatedDate != null) {
110             lastUpdatedDate.setText(ProgramUtils.df.format(updatedDate));
111         } else {
112             lastUpdatedDate.setText("");
113         }
114     }
115 
116     private void setVersion(Long version, Label versionaLabel){
117     	if (version != null){
118        		viewVersion.setVisible(version >= 1);
119     		versionLabel.setText(getLabel(ProgramMsgConstants.SIDEBAR_VERSION, String.valueOf(version)));
120     	} else {
121     		versionLabel.setText(getLabel(ProgramMsgConstants.SIDEBAR_VERSION, ""));
122     		viewVersion.setVisible(false);
123     	}
124     }
125 
126     private void setWidget(String path, SimplePanel container, DataModel model) {
127         Metadata mData = model.getMetadata(QueryPath.parse(path));
128 
129         // CoreProgram and CredentialProgram don't have SCHEDULED_REVIEW_DATE
130         if (null == mData && ProgramConstants.SCHEDULED_REVIEW_DATE.equals(path)) {
131             return;
132         }
133         Widget widget = DefaultWidgetFactory.getInstance().getReadOnlyWidget(model.getMetadata(QueryPath.parse(path)));
134         HasDataValueBinding.INSTANCE.setWidgetValue((HasDataValue) widget, model, path);
135         container.setWidget(widget);
136     }
137 
138 
139     private void buildLayout() {
140         content.clear();
141         Label history = new Label(getLabel(ProgramMsgConstants.SIDEBAR_HISTORY));
142         history.addStyleName("KS-Program-History");
143         content.add(history);
144 
145         content.add(createVersionPanel());
146         content.add(createDatePanel(getLabel(ProgramMsgConstants.SIDEBAR_PROGRAMLASTUPDATED), lastUpdatedDate, false));
147         if (type == Type.MAJOR) {
148             content.add(createDatePanel(getLabel(ProgramMsgConstants.SIDEBAR_SCHEDULEDREVIEWDATE), scheduledReviewDate, true));
149             content.add(createDatePanel(getLabel(ProgramMsgConstants.SIDEBAR_LASTREVIEWDATE), lastReviewDate, true));
150         }
151         content.add(createVersionHistoryPanel());
152     }
153 
154     private Widget createDatePanel(String title, Widget widget, boolean showEdit) {
155         VerticalPanel verticalPanel = new VerticalPanel();
156         HorizontalPanel datePanel = new HorizontalPanel();
157         datePanel.addStyleName("datePanel");
158         datePanel.add(widget);
159         if (state == State.EDIT && showEdit) {
160             Anchor edit = new Anchor(getLabel(ProgramMsgConstants.COMMON_EDIT));
161             edit.addClickHandler(new ClickHandler() {
162 
163                 @Override
164                 public void onClick(ClickEvent event) {
165                     dialogManager.show();
166                 }
167             });
168             datePanel.add(edit);
169         }
170         verticalPanel.add(new Label(title));
171         verticalPanel.add(datePanel);
172         return verticalPanel;
173     }
174 
175     private Widget createHistoryPanel(){
176     	VerticalPanel  verticalPanel = new VerticalPanel();
177     	versionLabel = new Label(getLabel(ProgramMsgConstants.SIDEBAR_VERSION, ""));
178     	verticalPanel.add(versionLabel);
179     	return verticalPanel;
180     }
181 
182     private Widget createVersionPanel(){
183     	VerticalPanel  verticalPanel = new VerticalPanel();
184     	versionLabel = new Label(getLabel(ProgramMsgConstants.SIDEBAR_VERSION, ""));
185     	verticalPanel.add(versionLabel);
186     	return verticalPanel;
187     }
188 
189     private Widget createVersionHistoryPanel(){
190     	VerticalPanel  verticalPanel = new VerticalPanel();
191     	viewVersion = new Anchor(getLabel(ProgramMsgConstants.SIDEBAR_VIEWHISTORY));
192     	viewVersion.addClickHandler(new ClickHandler(){
193 			@Override
194 			public void onClick(ClickEvent event) {
195 				switch (type){
196 					case MAJOR: HistoryManager.navigate(AppLocations.Locations.VIEW_PROGRAM_VERSIONS.getLocation()); break;
197 					case CORE: HistoryManager.navigate(AppLocations.Locations.VIEW_CORE_VERSIONS.getLocation()); break;
198 					case CREDENTIAL: HistoryManager.navigate(AppLocations.Locations.VIEW_BACC_VERSIONS.getLocation()); break;
199 				}
200 
201 
202 			}
203 
204     	});
205     	//viewVersion.setVisible(false);
206 
207     	verticalPanel.add(viewVersion);
208 
209     	return verticalPanel;
210     }
211 
212     public void setState(State state) {
213         this.state = state;
214         buildLayout();
215     }
216 
217     private void setStyles() {
218         content.addStyleName("sideBar");
219         content.addStyleName("KS-Program-History-Sidebar");
220         historyLabel.addStyleName("history");
221     }
222 
223     public static enum State {
224         EDIT,
225         VIEW
226     }
227 
228     public static enum Type {
229         CREDENTIAL,
230         CORE,
231         MAJOR
232     }
233     
234     private String getLabel(String messageKey) {
235         return Application.getApplicationContext().getUILabel(ProgramMsgConstants.PROGRAM_MSG_GROUP, messageKey);
236     }
237     
238     private String getLabel(String messageKey, String parameter) {
239         Map<String, Object> parameters = new HashMap<String, Object>();
240         parameters.put("0", parameter);
241         return Application.getApplicationContext().getUILabel(ProgramMsgConstants.PROGRAM_MSG_GROUP, messageKey, parameters);
242     } 
243 }