| 1 |  |  package org.kuali.student.lum.program.client.variation; | 
  | 2 |  |   | 
  | 3 |  |  import com.google.gwt.core.client.GWT; | 
  | 4 |  |  import com.google.gwt.event.dom.client.ClickEvent; | 
  | 5 |  |  import com.google.gwt.event.dom.client.ClickHandler; | 
  | 6 |  |  import com.google.gwt.user.client.Window; | 
  | 7 |  |  import com.google.gwt.user.client.ui.Anchor; | 
  | 8 |  |  import com.google.gwt.user.client.ui.FlexTable; | 
  | 9 |  |  import org.kuali.student.common.ui.client.application.ViewContext; | 
  | 10 |  |  import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport; | 
  | 11 |  |  import org.kuali.student.common.ui.client.mvc.DataModel; | 
  | 12 |  |  import org.kuali.student.common.ui.client.mvc.history.HistoryManager; | 
  | 13 |  |  import org.kuali.student.common.ui.client.widgets.KSButton; | 
  | 14 |  |  import org.kuali.student.common.ui.shared.IdAttributes.IdType; | 
  | 15 |  |  import org.kuali.student.core.assembly.data.Data; | 
  | 16 |  |  import org.kuali.student.lum.common.client.configuration.Configuration; | 
  | 17 |  |  import org.kuali.student.lum.program.client.ProgramConstants; | 
  | 18 |  |  import org.kuali.student.lum.program.client.ProgramRegistry; | 
  | 19 |  |  import org.kuali.student.lum.program.client.ProgramUtils; | 
  | 20 |  |  import org.kuali.student.lum.program.client.events.UpdateEvent; | 
  | 21 |  |  import org.kuali.student.lum.program.client.major.MajorManager; | 
  | 22 |  |  import org.kuali.student.lum.program.client.properties.ProgramProperties; | 
  | 23 |  |   | 
  | 24 |  |   | 
  | 25 |  |   | 
  | 26 |  |   | 
  | 27 | 0 |  public class VariationsBinding extends ModelWidgetBindingSupport<FlexTable> { | 
  | 28 |  |   | 
  | 29 |  |      private String url; | 
  | 30 |  |   | 
  | 31 |  |      private boolean editable; | 
  | 32 |  |   | 
  | 33 |  |      private Configuration configuration; | 
  | 34 |  |   | 
  | 35 | 0 |      public VariationsBinding(String url, boolean editable) { | 
  | 36 | 0 |          this.url = url; | 
  | 37 | 0 |          this.editable = editable; | 
  | 38 | 0 |      } | 
  | 39 |  |   | 
  | 40 |  |      public VariationsBinding(String url, boolean editable, Configuration configuration) { | 
  | 41 | 0 |          this(url, editable); | 
  | 42 | 0 |          this.configuration = configuration; | 
  | 43 | 0 |      } | 
  | 44 |  |   | 
  | 45 |  |      @Override | 
  | 46 |  |      public void setModelValue(FlexTable table, DataModel model, String path) { | 
  | 47 | 0 |          GWT.log("VariationsBinding.setModelValue...", null); | 
  | 48 | 0 |      } | 
  | 49 |  |   | 
  | 50 |  |      @Override | 
  | 51 |  |      public void setWidgetValue(final FlexTable table, final DataModel model, String path) { | 
  | 52 | 0 |          table.clear(); | 
  | 53 | 0 |          final Data variationMap = model.get(path); | 
  | 54 | 0 |          if (variationMap != null) { | 
  | 55 | 0 |              int row = 0; | 
  | 56 | 0 |              for (final Data.Property property : variationMap) { | 
  | 57 | 0 |                  final Data variationData = property.getValue(); | 
  | 58 | 0 |                  final int currentRow = row; | 
  | 59 | 0 |                  Anchor anchor = new Anchor(getVariationName(variationData)); | 
  | 60 | 0 |                  anchor.addClickHandler(new ClickHandler() { | 
  | 61 |  |                      @Override | 
  | 62 |  |                      public void onClick(ClickEvent event) { | 
  | 63 | 0 |                          ProgramRegistry.setData(variationData); | 
  | 64 | 0 |                          ProgramRegistry.setRow(currentRow); | 
  | 65 | 0 |                          ProgramUtils.addCredentialProgramDataToVariation(variationData, model); | 
  | 66 | 0 |                          String id = (String) model.get("id"); | 
  | 67 | 0 |                          ViewContext viewContext = new ViewContext(); | 
  | 68 | 0 |                          viewContext.setId(id); | 
  | 69 | 0 |                          viewContext.setIdType(IdType.OBJECT_ID); | 
  | 70 | 0 |                          HistoryManager.navigate(url, viewContext); | 
  | 71 | 0 |                      } | 
  | 72 |  |                  }); | 
  | 73 | 0 |                  table.setWidget(row, 0, anchor); | 
  | 74 | 0 |                  if (editable) { | 
  | 75 | 0 |                      KSButton removeButton = new KSButton(ProgramProperties.get().common_remove()); | 
  | 76 | 0 |                      table.setWidget(row, 1, removeButton);                                              | 
  | 77 | 0 |                      removeButton.addClickHandler(new ClickHandler() { | 
  | 78 |  |   | 
  | 79 |  |                          @Override | 
  | 80 |  |                          public void onClick(ClickEvent event) { | 
  | 81 | 0 |                              if (Window.confirm("Are you sure you want to delete specialization?")) { | 
  | 82 | 0 |                                  variationMap.remove(new Data.IntegerKey(currentRow)); | 
  | 83 | 0 |                                  MajorManager.getEventBus().fireEvent(new UpdateEvent()); | 
  | 84 |  |                              } | 
  | 85 | 0 |                          } | 
  | 86 |  |                      }); | 
  | 87 | 0 |                      if (configuration != null && configuration.checkPermission(model)) { | 
  | 88 | 0 |                          removeButton.setEnabled(false); | 
  | 89 |  |                      } | 
  | 90 |  |   | 
  | 91 |  |                  } | 
  | 92 | 0 |                  row++; | 
  | 93 | 0 |              } | 
  | 94 |  |          } | 
  | 95 | 0 |      } | 
  | 96 |  |   | 
  | 97 |  |      private String getVariationName(Data data) { | 
  | 98 | 0 |          StringBuilder nameBuilder = new StringBuilder(); | 
  | 99 | 0 |          nameBuilder.append(data.<Object>get(ProgramConstants.LONG_TITLE)); | 
  | 100 | 0 |          nameBuilder.append(" (").append(data.<Object>get(ProgramConstants.CODE)).append(")"); | 
  | 101 | 0 |          return nameBuilder.toString(); | 
  | 102 |  |      } | 
  | 103 |  |  } |