View Javadoc

1   package org.kuali.student.lum.program.client.widgets;
2   
3   import com.google.gwt.event.dom.client.ClickEvent;
4   import com.google.gwt.event.dom.client.ClickHandler;
5   import com.google.gwt.event.shared.HandlerManager;
6   import com.google.gwt.user.client.ui.*;
7   import org.kuali.student.common.ui.client.configurable.mvc.sections.Section;
8   import org.kuali.student.common.ui.client.mvc.DataModel;
9   import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
10  import org.kuali.student.common.ui.client.widgets.KSButton;
11  import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle;
12  import org.kuali.student.common.ui.client.widgets.KSLightBox;
13  import org.kuali.student.lum.common.client.widgets.AppLocations;
14  import org.kuali.student.lum.program.client.ProgramConstants;
15  import org.kuali.student.lum.program.client.ProgramStatus;
16  import org.kuali.student.lum.program.client.ProgramUtils;
17  import org.kuali.student.lum.program.client.events.AfterSaveEvent;
18  import org.kuali.student.lum.program.client.events.ModelLoadedEvent;
19  import org.kuali.student.lum.program.client.events.StateChangeEvent;
20  import org.kuali.student.lum.program.client.properties.ProgramProperties;
21  
22  /**
23   * @author Igor
24   */
25  public class SummaryActionPanel extends Composite {
26  
27      private final HorizontalPanel content = new HorizontalPanel();
28  
29      private final KSButton approveButton = new KSButton(ProgramProperties.get().button_approve());
30  
31      private final KSButton activateButton = new KSButton(ProgramProperties.get().button_activate(), ButtonStyle.SECONDARY);
32  
33      private final Anchor exitAnchor = new Anchor(ProgramProperties.get().link_backCurriculum());
34  
35      private final KSLightBox activateDialog = new KSLightBox();
36      private Section activateSection;
37  
38      private DataModel dataModel;
39  
40      private HandlerManager eventBus;
41  
42      public SummaryActionPanel(Section activateSection, HandlerManager eventBus) {
43          initWidget(content);
44          this.activateSection = activateSection;
45          this.eventBus = eventBus;
46          buildLayout();
47          setStyles();
48          bind();
49      }
50  
51      private void bind() {
52          eventBus.addHandler(AfterSaveEvent.TYPE, new AfterSaveEvent.Handler() {
53              @Override
54              public void onEvent(AfterSaveEvent event) {
55                  dataModel = event.getModel();
56                  processStatus();
57              }
58          });
59          eventBus.addHandler(ModelLoadedEvent.TYPE, new ModelLoadedEvent.Handler() {
60              @Override
61              public void onEvent(ModelLoadedEvent event) {
62                  dataModel = event.getModel();
63                  processStatus();
64              }
65          });
66          approveButton.addClickHandler(new ClickHandler() {
67              @Override
68              public void onClick(ClickEvent event) {
69                  processButtonClick(ProgramStatus.APPROVED);
70              }
71          });
72          activateButton.addClickHandler(new ClickHandler() {
73              @Override
74              public void onClick(ClickEvent event) {
75                  processActivateClick();
76              }
77          });
78          exitAnchor.addClickHandler(new ClickHandler() {
79              @Override
80              public void onClick(ClickEvent event) {
81                  HistoryManager.navigate(AppLocations.Locations.CURRICULUM_MANAGEMENT.getLocation());
82              }
83          });
84      }
85  
86      private void processStatus() {
87          processStatus(ProgramStatus.of(dataModel));
88      }
89  
90      private void processButtonClick(ProgramStatus status) {
91          eventBus.fireEvent(new StateChangeEvent(status));
92      }
93  
94      private void processActivateClick() {
95          String versionFromId = dataModel.get(ProgramConstants.VERSION_FROM_ID);
96          if (versionFromId != null) {
97              activateSection.updateWidgetData(dataModel);
98              activateDialog.show();
99          } else {
100             processButtonClick(ProgramStatus.ACTIVE);
101         }
102     }
103 
104     private void processStatus(ProgramStatus programStatus) {
105         if (programStatus == ProgramStatus.DRAFT) {
106             enableButtons(true, false);
107         } else if (programStatus == ProgramStatus.APPROVED) {
108             enableButtons(false, true);
109         } else if (programStatus == ProgramStatus.ACTIVE) {
110             enableButtons(false, false);
111         } else if (programStatus == ProgramStatus.SUPERSEDED) {
112             content.setVisible(false);
113         }
114     }
115 
116     private void setStyles() {
117         content.addStyleName("programActionPanel");
118         content.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
119     }
120 
121     private void buildLayout() {
122         content.add(approveButton);
123         content.add(activateButton);
124         content.add(exitAnchor);
125 
126         buildActivateDialog();
127     }
128 
129     private void buildActivateDialog() {
130         FlowPanel panel = new FlowPanel();
131 
132         panel.add((Widget) activateSection);
133 
134         KSButton activate = new KSButton(ProgramProperties.get().button_activate(), new ClickHandler() {
135             public void onClick(ClickEvent event) {
136                 activateSection.updateModel(dataModel);
137                 ProgramUtils.setPreviousStatus(dataModel, ProgramStatus.SUPERSEDED.getValue());
138                 processButtonClick(ProgramStatus.ACTIVE);
139                 activateDialog.hide();
140             }
141         });
142         activateDialog.addButton(activate);
143 
144         KSButton cancel = new KSButton(ProgramProperties.get().common_cancel(), ButtonStyle.ANCHOR_LARGE_CENTERED, new ClickHandler() {
145             public void onClick(ClickEvent event) {
146                 activateDialog.hide();
147             }
148         });
149         activateDialog.addButton(cancel);
150 
151         activateDialog.setWidget(panel);
152     }
153 
154     private void enableButtons(boolean enableApprove, boolean enableActivate) {
155         approveButton.setEnabled(enableApprove);
156         activateButton.setEnabled(enableActivate);
157     }
158 }