| 1 |  |  package org.kuali.student.common.ui.client.configurable.mvc.sections; | 
  | 2 |  |   | 
  | 3 |  |  import java.util.ArrayList; | 
  | 4 |  |  import java.util.HashMap; | 
  | 5 |  |  import java.util.Iterator; | 
  | 6 |  |  import java.util.List; | 
  | 7 |  |   | 
  | 8 |  |  import org.kuali.student.common.ui.client.application.Application; | 
  | 9 |  |  import org.kuali.student.common.ui.client.configurable.mvc.LayoutController; | 
  | 10 |  |  import org.kuali.student.common.ui.client.event.SectionUpdateEvent; | 
  | 11 |  |  import org.kuali.student.common.ui.client.widgets.dialog.ConfirmationDialog; | 
  | 12 |  |  import org.kuali.student.common.ui.client.widgets.field.layout.layouts.VerticalFieldLayout; | 
  | 13 |  |  import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; | 
  | 14 |  |  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent; | 
  | 15 |  |  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler; | 
  | 16 |  |   | 
  | 17 |  |  import com.google.gwt.event.dom.client.ClickEvent; | 
  | 18 |  |  import com.google.gwt.event.dom.client.ClickHandler; | 
  | 19 |  |   | 
  | 20 |  |   | 
  | 21 |  |   | 
  | 22 |  |   | 
  | 23 |  |   | 
  | 24 |  |   | 
  | 25 |  |   | 
  | 26 |  |   | 
  | 27 | 0 |  public class SwapSection extends BaseSection implements HasSectionDeletion{ | 
  | 28 |  |           | 
  | 29 | 0 |          private HashMap<String, Section> swapSectionMap = new HashMap<String, Section>(); | 
  | 30 |  |          private KSSelectItemWidgetAbstract selectableWidget; | 
  | 31 | 0 |          private List<Section> deleted = new ArrayList<Section>(); | 
  | 32 |  |          private ConfirmationDialog dialog;  | 
  | 33 | 0 |          private boolean showConfirmation = true; | 
  | 34 | 0 |          private List<String> lastSelection = new ArrayList<String>(); | 
  | 35 |  |          private List<String> deletionParentKeys; | 
  | 36 |  |           | 
  | 37 |  |           | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 | 0 |          public SwapSection(KSSelectItemWidgetAbstract selectableWidget){ | 
  | 44 | 0 |                  this.init(selectableWidget); | 
  | 45 | 0 |          } | 
  | 46 |  |           | 
  | 47 | 0 |          public SwapSection(KSSelectItemWidgetAbstract selectableWidget, ConfirmationDialog dialog){ | 
  | 48 | 0 |          this.dialog = dialog; | 
  | 49 | 0 |                  this.init(selectableWidget); | 
  | 50 | 0 |          } | 
  | 51 |  |           | 
  | 52 |  |          private void init(KSSelectItemWidgetAbstract selectableWidget){ | 
  | 53 | 0 |                  this.selectableWidget = selectableWidget; | 
  | 54 |  |                   | 
  | 55 | 0 |                  if(dialog == null){ | 
  | 56 | 0 |                          dialog =  | 
  | 57 |  |                                  new ConfirmationDialog(Application.getApplicationContext().getMessage("fieldDeletionTitle"),   | 
  | 58 |  |                                                  Application.getApplicationContext().getMessage("fieldDeletionConfirmMessage")); | 
  | 59 |  |                  } | 
  | 60 | 0 |                  dialog.getConfirmButton().addClickHandler(new ClickHandler(){ | 
  | 61 |  |   | 
  | 62 |  |                          @Override | 
  | 63 |  |                          public void onClick(ClickEvent event) { | 
  | 64 | 0 |                                  handleUserSelection(); | 
  | 65 | 0 |                                  dialog.hide(); | 
  | 66 | 0 |                          } | 
  | 67 |  |                  }); | 
  | 68 |  |                   | 
  | 69 | 0 |                  selectableWidget.addSelectionChangeHandler(new SelectionChangeHandler(){ | 
  | 70 |  |   | 
  | 71 |  |                          @Override | 
  | 72 |  |                          public void onSelectionChange(SelectionChangeEvent event) { | 
  | 73 | 0 |                                  if(event.isUserInitiated() && showConfirmation){ | 
  | 74 | 0 |                                          if(SwapSection.this.selectableWidget.getSelectedItems().size() < lastSelection.size()){ | 
  | 75 | 0 |                                                  dialog.show(); | 
  | 76 |  |                                          } | 
  | 77 | 0 |                                          else if(!SwapSection.this.selectableWidget.getSelectedItems().containsAll(lastSelection)){ | 
  | 78 |  |   | 
  | 79 |  |   | 
  | 80 |  |   | 
  | 81 |  |   | 
  | 82 |  |   | 
  | 83 |  |   | 
  | 84 |  |   | 
  | 85 |  |   | 
  | 86 |  |   | 
  | 87 |  |   | 
  | 88 | 0 |                                                  dialog.show(); | 
  | 89 |  |                                          } | 
  | 90 |  |                                          else{ | 
  | 91 | 0 |                                                  handleUserSelection(); | 
  | 92 |  |                                          } | 
  | 93 |  |                                  } | 
  | 94 | 0 |                                  else if(event.isUserInitiated()){ | 
  | 95 | 0 |                                          handleUserSelection(); | 
  | 96 |  |                                  } | 
  | 97 |  |                                  else{ | 
  | 98 | 0 |                                          handleSelection(); | 
  | 99 |  |                                  } | 
  | 100 | 0 |                                  lastSelection.clear(); | 
  | 101 | 0 |                                  lastSelection.addAll(SwapSection.this.selectableWidget.getSelectedItems()); | 
  | 102 | 0 |                          } | 
  | 103 |  |                  }); | 
  | 104 | 0 |                  layout = new VerticalFieldLayout(); | 
  | 105 | 0 |                  this.add(layout); | 
  | 106 | 0 |          } | 
  | 107 |  |           | 
  | 108 |  |          private void handleUserSelection(){ | 
  | 109 | 0 |                  List<String> selected  = SwapSection.this.selectableWidget.getSelectedItems(); | 
  | 110 | 0 |                  for(int i = 0; i < selected.size(); i++){ | 
  | 111 | 0 |                          String key = selected.get(i); | 
  | 112 | 0 |                          showSwappableSection(key); | 
  | 113 |  |                  } | 
  | 114 |  |                   | 
  | 115 | 0 |                  Iterator<String> it = swapSectionMap.keySet().iterator(); | 
  | 116 | 0 |                  while(it.hasNext()){ | 
  | 117 | 0 |                          String key = it.next(); | 
  | 118 | 0 |                          if(!selected.contains(key)){ | 
  | 119 | 0 |                                  removeSwappableSection(key); | 
  | 120 |  |                          } | 
  | 121 | 0 |                  } | 
  | 122 |  |                   | 
  | 123 |  |                   | 
  | 124 |  |                   | 
  | 125 | 0 |          } | 
  | 126 |  |           | 
  | 127 |  |           | 
  | 128 |  |   | 
  | 129 |  |   | 
  | 130 |  |   | 
  | 131 |  |   | 
  | 132 |  |          private void handleSelection(){ | 
  | 133 | 0 |                  List<String> selected  = SwapSection.this.selectableWidget.getSelectedItems(); | 
  | 134 | 0 |                  for(int i = 0; i < selected.size(); i++){ | 
  | 135 | 0 |                          String key = selected.get(i); | 
  | 136 | 0 |                          showSwappableSection(key); | 
  | 137 |  |                  } | 
  | 138 |  |                   | 
  | 139 | 0 |                  Iterator<String> it = swapSectionMap.keySet().iterator(); | 
  | 140 | 0 |                  while(it.hasNext()){ | 
  | 141 | 0 |                          String key = it.next(); | 
  | 142 | 0 |                          if(!selected.contains(key)){ | 
  | 143 | 0 |                                  removeSwappableSection(key); | 
  | 144 |  |                          } | 
  | 145 | 0 |                  } | 
  | 146 | 0 |          } | 
  | 147 |  |           | 
  | 148 |  |          private void showSwappableSection(String key){ | 
  | 149 | 0 |                  Section section = swapSectionMap.get(key); | 
  | 150 | 0 |                  if(section != null){ | 
  | 151 | 0 |                          if(deleted.contains(section)){ | 
  | 152 | 0 |                                  deleted.remove(section); | 
  | 153 |  |                          } | 
  | 154 | 0 |                          if(!section.getLayout().isVisible()){ | 
  | 155 | 0 |                                  section.enableValidation(true); | 
  | 156 | 0 |                                  section.getLayout().setVisible(true); | 
  | 157 |  |                          } | 
  | 158 |  |                  } | 
  | 159 | 0 |          } | 
  | 160 |  |           | 
  | 161 |  |          private void removeSwappableSection(String key){ | 
  | 162 | 0 |                  Section section = swapSectionMap.get(key); | 
  | 163 | 0 |                  if(section != null){ | 
  | 164 | 0 |                          if(!deleted.contains(section)){ | 
  | 165 | 0 |                                  deleted.add(section); | 
  | 166 |  |                          } | 
  | 167 | 0 |                          if(section.getLayout().isVisible()){ | 
  | 168 | 0 |                                  section.enableValidation(false); | 
  | 169 | 0 |                                  section.getLayout().setVisible(false); | 
  | 170 |  |                          } | 
  | 171 |  |   | 
  | 172 |  |                  } | 
  | 173 | 0 |          } | 
  | 174 |  |           | 
  | 175 |  |          public void enableConfirmation(boolean enable){ | 
  | 176 | 0 |                  showConfirmation = enable; | 
  | 177 | 0 |          } | 
  | 178 |  |           | 
  | 179 |  |          public String addSection(Section section, String swapKey){ | 
  | 180 | 0 |                  swapSectionMap.put(swapKey, section); | 
  | 181 | 0 |                  String key = layout.addLayout(section.getLayout()); | 
  | 182 | 0 |                  section.getLayout().setVisible(false); | 
  | 183 | 0 |                  if(selectableWidget.getSelectedItems().contains(swapKey)){ | 
  | 184 | 0 |                          handleSelection(); | 
  | 185 |  |                  } | 
  | 186 | 0 |                  sections.add(section); | 
  | 187 | 0 |                  return key; | 
  | 188 |  |          } | 
  | 189 |  |           | 
  | 190 |  |          public String addSection(String key, Section section, String swapKey){ | 
  | 191 | 0 |                  swapSectionMap.put(swapKey, section); | 
  | 192 | 0 |                  section.getLayout().setKey(key); | 
  | 193 | 0 |                  String rkey = layout.addLayout(section.getLayout()); | 
  | 194 | 0 |                  section.getLayout().setVisible(false); | 
  | 195 | 0 |                  if(selectableWidget.getSelectedItems().contains(swapKey)){ | 
  | 196 | 0 |                          handleSelection(); | 
  | 197 |  |                  } | 
  | 198 | 0 |                  sections.add(section); | 
  | 199 | 0 |                  return rkey; | 
  | 200 |  |          } | 
  | 201 |  |           | 
  | 202 |  |           | 
  | 203 |  |          @Override | 
  | 204 |  |          public String addSection(Section section) { | 
  | 205 | 0 |                  throw new UnsupportedOperationException("Sections can be added to swappable section only through " + | 
  | 206 |  |                                  "the addSection(Section section, String swapKey) method"); | 
  | 207 |  |                   | 
  | 208 |  |          } | 
  | 209 |  |   | 
  | 210 |  |          @Override | 
  | 211 |  |          public String addSection(String key, Section section) { | 
  | 212 | 0 |                  throw new UnsupportedOperationException("Sections can be added to swappable section only through " + | 
  | 213 |  |                                  "the addSection(Section section, String swapKey) method"); | 
  | 214 |  |          } | 
  | 215 |  |   | 
  | 216 |  |          @Override | 
  | 217 |  |          public List<Section> getDeletedSections() { | 
  | 218 | 0 |                  return deleted; | 
  | 219 |  |          } | 
  | 220 |  |   | 
  | 221 |  |      @Override | 
  | 222 |  |      public List<String> getDeletionParentKeys() { | 
  | 223 | 0 |          return deletionParentKeys; | 
  | 224 |  |      } | 
  | 225 |  |   | 
  | 226 |  |       | 
  | 227 |  |   | 
  | 228 |  |   | 
  | 229 |  |   | 
  | 230 |  |   | 
  | 231 |  |   | 
  | 232 |  |      @Override | 
  | 233 |  |      public void setDeletionParentKey(List<String> deletionParentKeys) { | 
  | 234 | 0 |          this.deletionParentKeys = deletionParentKeys; | 
  | 235 | 0 |      } | 
  | 236 |  |  } |