| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.ui.client.configurable.mvc.multiplicity; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.ArrayList; | 
  | 19 |  |  import java.util.List; | 
  | 20 |  |  import java.util.Set; | 
  | 21 |  |   | 
  | 22 |  |  import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle; | 
  | 23 |  |  import org.kuali.student.common.ui.client.mvc.Callback; | 
  | 24 |  |   | 
  | 25 |  |  import com.google.gwt.user.client.ui.Composite; | 
  | 26 |  |  import com.google.gwt.user.client.ui.FlowPanel; | 
  | 27 |  |  import com.google.gwt.user.client.ui.Widget; | 
  | 28 |  |   | 
  | 29 |  |   | 
  | 30 |  |   | 
  | 31 |  |   | 
  | 32 |  |   | 
  | 33 |  |   | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |  public abstract class MultiplicityComposite extends Composite { | 
  | 43 |  |   | 
  | 44 | 0 |      protected FlowPanel mainPanel = new FlowPanel(); | 
  | 45 | 0 |      protected FlowPanel itemsPanel = new FlowPanel(); | 
  | 46 | 0 |      protected boolean loaded = false; | 
  | 47 | 0 |      protected int itemCount = 0; | 
  | 48 | 0 |      protected int visualItemCount = 0; | 
  | 49 | 0 |      protected int minEmptyItems = 0; | 
  | 50 | 0 |      protected List<MultiplicityItem> items = new ArrayList<MultiplicityItem>(); | 
  | 51 | 0 |      protected List<MultiplicityItem> removed = new ArrayList<MultiplicityItem>(); | 
  | 52 |  |      protected Set<Integer> itemKeys; | 
  | 53 | 0 |      public static enum StyleType{TOP_LEVEL, SUB_LEVEL}; | 
  | 54 |  |      protected StyleType style; | 
  | 55 | 0 |      protected SectionTitle titleWidget = SectionTitle.generateEmptyTitle(); | 
  | 56 |  |       | 
  | 57 | 0 |      public MultiplicityComposite(StyleType style){ | 
  | 58 | 0 |              this.style = style; | 
  | 59 | 0 |          initWidget(mainPanel); | 
  | 60 | 0 |      } | 
  | 61 |  |           | 
  | 62 | 0 |      protected Callback<MultiplicityItem> removeCallback = new Callback<MultiplicityItem>(){ | 
  | 63 |  |   | 
  | 64 |  |          public void exec(MultiplicityItem itemToRemove) { | 
  | 65 |  |               | 
  | 66 | 0 |                  visualItemCount--; | 
  | 67 | 0 |              itemToRemove.setDeleted(true); | 
  | 68 | 0 |              removed.add(itemToRemove); | 
  | 69 | 0 |              itemsPanel.remove(itemToRemove); | 
  | 70 | 0 |          } | 
  | 71 |  |      }; | 
  | 72 |  |           | 
  | 73 |  |       | 
  | 74 |  |       | 
  | 75 |  |   | 
  | 76 |  |   | 
  | 77 |  |   | 
  | 78 |  |   | 
  | 79 |  |          public MultiplicityItem addItem(){ | 
  | 80 | 0 |                  itemCount++; | 
  | 81 | 0 |                  visualItemCount++; | 
  | 82 |  |               | 
  | 83 | 0 |              MultiplicityItem item = getItemDecorator(style); | 
  | 84 | 0 |              Widget itemWidget = createItem(); | 
  | 85 |  |               | 
  | 86 | 0 |              if (item != null){ | 
  | 87 | 0 |                      item.setItemKey(new Integer(itemCount -1)); | 
  | 88 | 0 |                      item.setItemWidget(itemWidget); | 
  | 89 | 0 |                      item.setRemoveCallback(removeCallback); | 
  | 90 | 0 |              } else if (itemWidget instanceof MultiplicityItem){ | 
  | 91 | 0 |                      item = (MultiplicityItem)itemWidget; | 
  | 92 | 0 |                      item.setItemKey(new Integer(itemCount -1)); | 
  | 93 |  |              } | 
  | 94 | 0 |              items.add(item); | 
  | 95 | 0 |              item.redraw(); | 
  | 96 | 0 |              itemsPanel.add(item); | 
  | 97 |  |               | 
  | 98 | 0 |              return item; | 
  | 99 |  |          } | 
  | 100 |  |           | 
  | 101 |  |           | 
  | 102 |  |   | 
  | 103 |  |   | 
  | 104 |  |   | 
  | 105 |  |   | 
  | 106 |  |          public int getAddItemKey(){ | 
  | 107 | 0 |                  return itemCount-1; | 
  | 108 |  |          } | 
  | 109 |  |           | 
  | 110 |  |          public void incrementItemKey(){ | 
  | 111 | 0 |                  itemCount++; | 
  | 112 | 0 |          } | 
  | 113 |  |          | 
  | 114 |  |      public void onLoad() { | 
  | 115 | 0 |          if (!loaded) {             | 
  | 116 |  |               | 
  | 117 |  |                   | 
  | 118 | 0 |              mainPanel.add(itemsPanel); | 
  | 119 |  |              | 
  | 120 | 0 |              Widget addWidget = generateAddWidget(); | 
  | 121 | 0 |              if (addWidget != null){ | 
  | 122 | 0 |                  mainPanel.add(addWidget); | 
  | 123 |  |              } | 
  | 124 |  |               | 
  | 125 | 0 |              loaded = true; | 
  | 126 |  |          } | 
  | 127 |  |           | 
  | 128 | 0 |          if (!loaded || itemCount == 0){ | 
  | 129 | 0 |              for (int i=0; i < minEmptyItems; i++){ | 
  | 130 | 0 |                      addItem(); | 
  | 131 |  |              }                 | 
  | 132 |  |          } | 
  | 133 | 0 |      } | 
  | 134 |  |   | 
  | 135 |  |      public List<MultiplicityItem> getItems() { | 
  | 136 | 0 |          return items; | 
  | 137 |  |      } | 
  | 138 |  |   | 
  | 139 |  |      public List<MultiplicityItem> getRemovedItems() { | 
  | 140 | 0 |          return removed; | 
  | 141 |  |      } | 
  | 142 |  |   | 
  | 143 |  |      public void clear(){ | 
  | 144 | 0 |          itemsPanel.clear(); | 
  | 145 | 0 |          items.clear(); | 
  | 146 | 0 |          removed.clear(); | 
  | 147 | 0 |          itemCount = 0; | 
  | 148 | 0 |      } | 
  | 149 |  |              | 
  | 150 |  |      public void redraw(){ | 
  | 151 | 0 |          for (MultiplicityItem item:items){ | 
  | 152 | 0 |              item.redraw(); | 
  | 153 |  |          } | 
  | 154 | 0 |      } | 
  | 155 |  |       | 
  | 156 |  |       | 
  | 157 |  |   | 
  | 158 |  |   | 
  | 159 |  |      public void setMinEmptyItems(int minCount){ | 
  | 160 | 0 |              this.minEmptyItems = minCount; | 
  | 161 | 0 |      } | 
  | 162 |  |   | 
  | 163 |  |       | 
  | 164 |  |   | 
  | 165 |  |   | 
  | 166 |  |      public abstract MultiplicityItem getItemDecorator(StyleType style); | 
  | 167 |  |   | 
  | 168 |  |       | 
  | 169 |  |       | 
  | 170 |  |   | 
  | 171 |  |   | 
  | 172 |  |      public abstract Widget createItem(); | 
  | 173 |  |   | 
  | 174 |  |       | 
  | 175 |  |   | 
  | 176 |  |   | 
  | 177 |  |   | 
  | 178 |  |   | 
  | 179 |  |      public abstract Widget generateAddWidget(); | 
  | 180 |  |   | 
  | 181 |  |  } |