| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.ui.client.widgets.list.impl; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.ArrayList; | 
  | 19 |  |  import java.util.List; | 
  | 20 |  |   | 
  | 21 |  |  import org.kuali.student.common.dto.Idable; | 
  | 22 |  |  import org.kuali.student.common.ui.client.mvc.Callback; | 
  | 23 |  |  import org.kuali.student.common.ui.client.widgets.KSLabel; | 
  | 24 |  |  import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; | 
  | 25 |  |  import org.kuali.student.common.ui.client.widgets.list.ListItems; | 
  | 26 |  |  import org.kuali.student.common.ui.client.widgets.list.ModelListItems; | 
  | 27 |  |   | 
  | 28 |  |  import com.google.gwt.event.dom.client.BlurHandler; | 
  | 29 |  |  import com.google.gwt.event.dom.client.FocusHandler; | 
  | 30 |  |  import com.google.gwt.event.shared.HandlerRegistration; | 
  | 31 |  |  import com.google.gwt.user.client.ui.FlexTable; | 
  | 32 |  |  import com.google.gwt.user.client.ui.Widget; | 
  | 33 |  |   | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |  public class KSLabelListImpl extends KSSelectItemWidgetAbstract { | 
  | 42 |  |   | 
  | 43 | 0 |      private static final HandlerRegistration NO_OP_REGISTRATION = new HandlerRegistration() { | 
  | 44 |  |                  @Override | 
  | 45 |  |                  public void removeHandler() { | 
  | 46 |  |                           | 
  | 47 | 0 |                  } | 
  | 48 |  |      }; | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 | 0 |          private FlexTable labels = new FlexTable(); | 
  | 52 | 0 |      private List<String> selectedItems = new ArrayList<String>(); | 
  | 53 |  |   | 
  | 54 | 0 |      private int maxCols = 1;  | 
  | 55 | 0 |      private boolean enabled = true; | 
  | 56 |  |   | 
  | 57 | 0 |      public KSLabelListImpl() { | 
  | 58 | 0 |          initWidget(labels); | 
  | 59 | 0 |      } | 
  | 60 |  |   | 
  | 61 |  |       | 
  | 62 |  |   | 
  | 63 |  |   | 
  | 64 |  |      public void deSelectItem(String id) { | 
  | 65 | 0 |         this.selectedItems.remove(id); | 
  | 66 | 0 |      } | 
  | 67 |  |   | 
  | 68 |  |       | 
  | 69 |  |   | 
  | 70 |  |   | 
  | 71 |  |      public List<String> getSelectedItems() { | 
  | 72 | 0 |          return selectedItems; | 
  | 73 |  |      } | 
  | 74 |  |       | 
  | 75 |  |       | 
  | 76 |  |   | 
  | 77 |  |   | 
  | 78 |  |   | 
  | 79 |  |      public List<String> getSelectedItemsForExport() { | 
  | 80 | 0 |              List<String> selectedList = new ArrayList<String>(); | 
  | 81 | 0 |              int rowCnt = this.labels.getRowCount(); | 
  | 82 | 0 |              for (int i = 0; i < rowCnt; i++) { | 
  | 83 | 0 |                          Widget theWidget = this.labels.getWidget(i, 0); | 
  | 84 | 0 |                          if (theWidget instanceof KSLabel) { | 
  | 85 | 0 |                                  KSLabel ksLbl = (KSLabel) theWidget; | 
  | 86 | 0 |                                  String label = ksLbl.getText(); | 
  | 87 | 0 |                                  selectedList.add(label); | 
  | 88 |  |                          } | 
  | 89 |  |                  } | 
  | 90 | 0 |          return selectedList; | 
  | 91 |  |      } | 
  | 92 |  |   | 
  | 93 |  |   | 
  | 94 |  |   | 
  | 95 |  |       | 
  | 96 |  |   | 
  | 97 |  |   | 
  | 98 |  |      public void selectItem(String id) { | 
  | 99 | 0 |          if (!selectedItems.contains(id)){ | 
  | 100 | 0 |               this.selectedItems.add(id); | 
  | 101 |  |          } | 
  | 102 |  |   | 
  | 103 |  |           | 
  | 104 | 0 |          redraw(); | 
  | 105 | 0 |      } | 
  | 106 |  |   | 
  | 107 |  |      public void redraw(){ | 
  | 108 | 0 |          labels.clear(); | 
  | 109 | 0 |          int currCount = 0; | 
  | 110 | 0 |          int row = 0; | 
  | 111 | 0 |          int col = 0; | 
  | 112 |  |   | 
  | 113 | 0 |          int itemCount = 0; | 
  | 114 | 0 |          if (super.getListItems() != null){ | 
  | 115 | 0 |                  itemCount = super.getListItems().getItemCount(); | 
  | 116 |  |          } else { | 
  | 117 | 0 |                  itemCount = selectedItems.size(); | 
  | 118 |  |          } | 
  | 119 |  |   | 
  | 120 | 0 |          if (maxCols <= 2){ | 
  | 121 |  |               | 
  | 122 | 0 |              int maxRows = (itemCount / maxCols) + (itemCount % 2); | 
  | 123 | 0 |              for (String id:selectedItems){ | 
  | 124 | 0 |                  currCount++; | 
  | 125 | 0 |                  row = (currCount % maxRows); | 
  | 126 | 0 |                  row = ((row == 0) ? maxRows:row) - 1; | 
  | 127 |  |   | 
  | 128 | 0 |                  labels.setWidget(row, col, createLabel(id)); | 
  | 129 |  |   | 
  | 130 | 0 |                  col += ((row + 1)/ maxRows) * 1; | 
  | 131 |  |              } | 
  | 132 | 0 |          } else { | 
  | 133 |  |               | 
  | 134 | 0 |              for (String id:selectedItems){ | 
  | 135 | 0 |                  currCount++; | 
  | 136 | 0 |                  col = currCount % maxCols; | 
  | 137 | 0 |                  col = ((col == 0) ? maxCols:col) - 1; | 
  | 138 |  |   | 
  | 139 | 0 |                  labels.setWidget(row, col, createLabel(id)); | 
  | 140 |  |   | 
  | 141 | 0 |                  row += ((col + 1 )/ maxCols) * 1; | 
  | 142 |  |              } | 
  | 143 |  |          } | 
  | 144 |  |   | 
  | 145 | 0 |          super.setInitialized(true); | 
  | 146 | 0 |      } | 
  | 147 |  |   | 
  | 148 |  |      @Override | 
  | 149 |  |      public <T extends Idable> void setListItems(ListItems listItems) { | 
  | 150 | 0 |          if(listItems instanceof ModelListItems){ | 
  | 151 | 0 |              Callback<T> redrawCallback = new Callback<T>(){ | 
  | 152 |  |   | 
  | 153 |  |                  @Override | 
  | 154 |  |                  public void exec(T result){ | 
  | 155 | 0 |                      KSLabelListImpl.this.redraw(); | 
  | 156 | 0 |                  } | 
  | 157 |  |              }; | 
  | 158 | 0 |              ((ModelListItems<T>)listItems).addOnAddCallback(redrawCallback); | 
  | 159 | 0 |              ((ModelListItems<T>)listItems).addOnRemoveCallback(redrawCallback); | 
  | 160 | 0 |              ((ModelListItems<T>)listItems).addOnUpdateCallback(redrawCallback); | 
  | 161 | 0 |              ((ModelListItems<T>)listItems).addOnBulkUpdateCallback(redrawCallback); | 
  | 162 |  |          } | 
  | 163 |  |   | 
  | 164 | 0 |          super.setListItems(listItems); | 
  | 165 |  |   | 
  | 166 | 0 |          redraw(); | 
  | 167 | 0 |      } | 
  | 168 |  |   | 
  | 169 |  |      private KSLabel createLabel(String id){ | 
  | 170 | 0 |              if (super.getListItems() == null || super.getListItems().getItemCount() == 0){ | 
  | 171 | 0 |                      return new KSLabel(id); | 
  | 172 |  |              } else { | 
  | 173 | 0 |                      return new KSLabel(super.getListItems().getItemText(id)); | 
  | 174 |  |              } | 
  | 175 |  |      } | 
  | 176 |  |   | 
  | 177 | 0 |      public void onLoad() {} | 
  | 178 |  |   | 
  | 179 |  |      @Override | 
  | 180 |  |      public void setColumnSize(int col) { | 
  | 181 | 0 |          maxCols = col; | 
  | 182 | 0 |      } | 
  | 183 |  |   | 
  | 184 |  |      @Override | 
  | 185 |  |      public void setEnabled(boolean b) { | 
  | 186 |  |   | 
  | 187 |  |   | 
  | 188 |  |   | 
  | 189 |  |   | 
  | 190 |  |   | 
  | 191 |  |   | 
  | 192 | 0 |      } | 
  | 193 |  |   | 
  | 194 |  |      @Override | 
  | 195 |  |      public boolean isEnabled() { | 
  | 196 | 0 |          return enabled; | 
  | 197 |  |      } | 
  | 198 |  |   | 
  | 199 |  |      @Override | 
  | 200 |  |      public boolean isMultipleSelect(){ | 
  | 201 | 0 |          return true; | 
  | 202 |  |      } | 
  | 203 |  |      @Override | 
  | 204 |  |      public void clear(){ | 
  | 205 | 0 |          selectedItems.clear(); | 
  | 206 | 0 |          redraw(); | 
  | 207 | 0 |      } | 
  | 208 |  |   | 
  | 209 |  |          @Override | 
  | 210 |  |          public HandlerRegistration addFocusHandler(FocusHandler handler) { | 
  | 211 | 0 |                  return NO_OP_REGISTRATION; | 
  | 212 |  |          } | 
  | 213 |  |   | 
  | 214 |  |          @Override | 
  | 215 |  |          public HandlerRegistration addBlurHandler(BlurHandler handler) { | 
  | 216 | 0 |                  return NO_OP_REGISTRATION; | 
  | 217 |  |          } | 
  | 218 |  |   | 
  | 219 |  |  } |