001    package org.kuali.student.lum.program.client.widgets;
002    
003    import java.util.Date;
004    
005    import org.kuali.student.common.assembly.data.Metadata;
006    import org.kuali.student.common.assembly.data.QueryPath;
007    import org.kuali.student.common.ui.client.application.ViewContext;
008    import org.kuali.student.common.ui.client.configurable.mvc.DefaultWidgetFactory;
009    import org.kuali.student.common.ui.client.configurable.mvc.binding.HasDataValueBinding;
010    import org.kuali.student.common.ui.client.mvc.DataModel;
011    import org.kuali.student.common.ui.client.mvc.HasDataValue;
012    import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
013    import org.kuali.student.lum.common.client.widgets.AppLocations;
014    import org.kuali.student.lum.program.client.ProgramConstants;
015    import org.kuali.student.lum.program.client.ProgramUtils;
016    import org.kuali.student.lum.program.client.events.AfterSaveEvent;
017    import org.kuali.student.lum.program.client.events.ModelLoadedEvent;
018    import org.kuali.student.lum.program.client.major.MajorController;
019    import org.kuali.student.lum.program.client.properties.ProgramProperties;
020    
021    import com.google.gwt.event.dom.client.ClickEvent;
022    import com.google.gwt.event.dom.client.ClickHandler;
023    import com.google.gwt.event.shared.HandlerManager;
024    import com.google.gwt.user.client.ui.Anchor;
025    import com.google.gwt.user.client.ui.Composite;
026    import com.google.gwt.user.client.ui.HorizontalPanel;
027    import com.google.gwt.user.client.ui.Label;
028    import com.google.gwt.user.client.ui.SimplePanel;
029    import com.google.gwt.user.client.ui.VerticalPanel;
030    import com.google.gwt.user.client.ui.Widget;
031    
032    /**
033     * @author Igor
034     */
035    public class ProgramSideBar extends Composite {
036    
037        private final VerticalPanel content = new VerticalPanel();
038    
039        private State state = State.VIEW;
040    
041        private Type type;
042    
043        private Label versionLabel;
044        private Anchor viewVersion;
045        private Label historyLabel = new Label(ProgramProperties.get().sideBar_history());
046        private Label lastUpdatedDate = new Label();
047        private SimplePanel scheduledReviewDate = new SimplePanel();
048        private Label lastReviewDate = new Label();
049        private final HandlerManager eventBus;
050    
051        private ViewContext viewContext;
052    
053        private final SideBarDialogManager dialogManager;
054    
055        public ProgramSideBar(HandlerManager eventBus, Type type) {
056            this.eventBus = eventBus;
057            this.type = type;
058            dialogManager = new SideBarDialogManager(eventBus);
059            initWidget(content);
060            setStyles();
061            buildLayout();
062            bind();
063        }
064    
065        public void initialize(MajorController controller) {
066            DataModel model = controller.getProgramModel();
067            dialogManager.configureView(model.getDefinition(), controller);
068            viewContext = controller.getViewContext();
069            updateFields(model);
070        }
071    
072        private void bind() {
073            eventBus.addHandler(ModelLoadedEvent.TYPE, new ModelLoadedEvent.Handler() {
074                @Override
075                public void onEvent(ModelLoadedEvent event) {
076                    updateFields(event.getModel());
077                }
078            });
079            eventBus.addHandler(AfterSaveEvent.TYPE, new AfterSaveEvent.Handler() {
080                @Override
081                public void onEvent(AfterSaveEvent event) {
082                    DataModel model = event.getModel();
083                    dialogManager.configureView(model.getDefinition(), event.getController());
084                    updateFields(event.getModel());
085                }
086            });
087        }
088    
089        private void updateFields(DataModel model) {
090            String programType = model.get(ProgramConstants.TYPE);
091    
092            //The sidebar fields should only be updated if the model that was changed/loaded is associated with the sidebar
093            boolean doUpdate =      (type == ProgramSideBar.Type.CORE && ProgramConstants.CORE_LU_TYPE_ID.equals(programType)) ||
094                                                    (type == ProgramSideBar.Type.CREDENTIAL && programType.startsWith(ProgramConstants.CRED_LU_TYPE_PREFIX)) ||
095                                                    (type == ProgramSideBar.Type.MAJOR && ProgramConstants.MAJOR_LU_TYPE_ID.equals(programType)); 
096            
097            if (doUpdate){
098                    setVersion((Long)model.get(ProgramConstants.VERSION), versionLabel);
099                    setDate((Date) model.get(ProgramConstants.LAST_UPDATED_DATE), lastUpdatedDate);
100                    lastReviewDate.setText((String) model.get(ProgramConstants.LAST_REVIEW_DATE));
101                    setWidget(ProgramConstants.SCHEDULED_REVIEW_DATE, scheduledReviewDate, model);
102            }
103        }
104    
105        private void setDate(Date updatedDate, Label lastUpdatedDate) {
106            if (updatedDate != null) {
107                lastUpdatedDate.setText(ProgramUtils.df.format(updatedDate));
108            } else {
109                lastUpdatedDate.setText("");
110            }
111        }
112    
113        private void setVersion(Long version, Label versionaLabel){
114            if (version != null){
115                    viewVersion.setVisible(version >= 1);
116                    versionLabel.setText(ProgramProperties.get().sideBar_version(String.valueOf(version)));
117            } else {
118                    versionLabel.setText(ProgramProperties.get().sideBar_version(""));
119                    viewVersion.setVisible(false);
120            }
121        }
122    
123        private void setWidget(String path, SimplePanel container, DataModel model) {
124            Metadata mData = model.getMetadata(QueryPath.parse(path));
125    
126            // CoreProgram and CredentialProgram don't have SCHEDULED_REVIEW_DATE
127            if (null == mData && ProgramConstants.SCHEDULED_REVIEW_DATE.equals(path)) {
128                return;
129            }
130            Widget widget = DefaultWidgetFactory.getInstance().getReadOnlyWidget(model.getMetadata(QueryPath.parse(path)));
131            HasDataValueBinding.INSTANCE.setWidgetValue((HasDataValue) widget, model, path);
132            container.setWidget(widget);
133        }
134    
135    
136        private void buildLayout() {
137            content.clear();
138            Label history = new Label(ProgramProperties.get().sideBar_history());
139            history.addStyleName("KS-Program-History");
140            content.add(history);
141    
142            content.add(createVersionPanel());
143            content.add(createDatePanel(ProgramProperties.get().sideBar_programLastUpdated(), lastUpdatedDate, false));
144            if (type == Type.MAJOR) {
145                content.add(createDatePanel(ProgramProperties.get().sideBar_scheduledReviewDate(), scheduledReviewDate, true));
146                content.add(createDatePanel(ProgramProperties.get().sideBar_lastReviewDate(), lastReviewDate, true));
147            }
148            content.add(createVersionHistoryPanel());
149        }
150    
151        private Widget createDatePanel(String title, Widget widget, boolean showEdit) {
152            VerticalPanel verticalPanel = new VerticalPanel();
153            HorizontalPanel datePanel = new HorizontalPanel();
154            datePanel.addStyleName("datePanel");
155            datePanel.add(widget);
156            if (state == State.EDIT && showEdit) {
157                Anchor edit = new Anchor(ProgramProperties.get().common_edit());
158                edit.addClickHandler(new ClickHandler() {
159    
160                    @Override
161                    public void onClick(ClickEvent event) {
162                        dialogManager.show();
163                    }
164                });
165                datePanel.add(edit);
166            }
167            verticalPanel.add(new Label(title));
168            verticalPanel.add(datePanel);
169            return verticalPanel;
170        }
171    
172        private Widget createHistoryPanel(){
173            VerticalPanel  verticalPanel = new VerticalPanel();
174            versionLabel = new Label(ProgramProperties.get().sideBar_version(""));
175            verticalPanel.add(versionLabel);
176            return verticalPanel;
177        }
178    
179        private Widget createVersionPanel(){
180            VerticalPanel  verticalPanel = new VerticalPanel();
181            versionLabel = new Label(ProgramProperties.get().sideBar_version(""));
182            verticalPanel.add(versionLabel);
183            return verticalPanel;
184        }
185    
186        private Widget createVersionHistoryPanel(){
187            VerticalPanel  verticalPanel = new VerticalPanel();
188            viewVersion = new Anchor(ProgramProperties.get().sideBar_viewHistory());
189            viewVersion.addClickHandler(new ClickHandler(){
190                            @Override
191                            public void onClick(ClickEvent event) {
192                                    switch (type){
193                                            case MAJOR: HistoryManager.navigate(AppLocations.Locations.VIEW_PROGRAM_VERSIONS.getLocation()); break;
194                                            case CORE: HistoryManager.navigate(AppLocations.Locations.VIEW_CORE_VERSIONS.getLocation()); break;
195                                            case CREDENTIAL: HistoryManager.navigate(AppLocations.Locations.VIEW_BACC_VERSIONS.getLocation()); break;
196                                    }
197    
198    
199                            }
200    
201            });
202            //viewVersion.setVisible(false);
203    
204            verticalPanel.add(viewVersion);
205    
206            return verticalPanel;
207        }
208    
209        public void setState(State state) {
210            this.state = state;
211            buildLayout();
212        }
213    
214        private void setStyles() {
215            content.addStyleName("sideBar");
216            content.addStyleName("KS-Program-History-Sidebar");
217            historyLabel.addStyleName("history");
218        }
219    
220        public static enum State {
221            EDIT,
222            VIEW
223        }
224    
225        public static enum Type {
226            CREDENTIAL,
227            CORE,
228            MAJOR
229        }
230    }