| 1 |  |  package org.kuali.student.common.ui.client.widgets; | 
  | 2 |  |   | 
  | 3 |  |  import java.util.ArrayList; | 
  | 4 |  |  import java.util.List; | 
  | 5 |  |   | 
  | 6 |  |  import org.kuali.student.common.ui.client.mvc.Callback; | 
  | 7 |  |  import org.kuali.student.common.ui.client.mvc.HasDataValue; | 
  | 8 |  |  import org.kuali.student.common.ui.client.util.Elements; | 
  | 9 |  |  import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton; | 
  | 10 |  |  import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType; | 
  | 11 |  |  import org.kuali.student.core.assembly.data.Data; | 
  | 12 |  |  import org.kuali.student.core.assembly.data.Data.DataValue; | 
  | 13 |  |  import org.kuali.student.core.assembly.data.Data.Value; | 
  | 14 |  |   | 
  | 15 |  |  import com.google.gwt.event.dom.client.ClickEvent; | 
  | 16 |  |  import com.google.gwt.event.dom.client.ClickHandler; | 
  | 17 |  |  import com.google.gwt.event.logical.shared.CloseEvent; | 
  | 18 |  |  import com.google.gwt.event.logical.shared.CloseHandler; | 
  | 19 |  |  import com.google.gwt.event.logical.shared.HasCloseHandlers; | 
  | 20 |  |  import com.google.gwt.event.shared.HandlerRegistration; | 
  | 21 |  |  import com.google.gwt.user.client.ui.Anchor; | 
  | 22 |  |  import com.google.gwt.user.client.ui.Composite; | 
  | 23 |  |  import com.google.gwt.user.client.ui.FlowPanel; | 
  | 24 |  |  import com.google.gwt.user.client.ui.HTMLPanel; | 
  | 25 |  |  import com.google.gwt.user.client.ui.Panel; | 
  | 26 |  |   | 
  | 27 | 0 |  public class KSItemLabel extends Composite implements HasCloseHandlers<KSItemLabel>, HasDataValue { | 
  | 28 |  |   | 
  | 29 | 0 |      private final String id = HTMLPanel.createUniqueId(); | 
  | 30 | 0 |      private final String contentId = HTMLPanel.createUniqueId(); | 
  | 31 |  |   | 
  | 32 | 0 |      private final String removeLinkId = HTMLPanel.createUniqueId(); | 
  | 33 | 0 |      private final String backgroundId = HTMLPanel.createUniqueId(); | 
  | 34 | 0 |      private final String PANEL_CONTENT_OPEN = "<span id='" + contentId + "'></span>"; | 
  | 35 | 0 |      private final String PANEL_CONTENT_REMOVE_LINK = "<span class='ks-selected-list-value-remove' id='" + removeLinkId + "'></span>";  | 
  | 36 |  |   | 
  | 37 | 0 |      private final String PANEL_CONTENT_BACKGROUND = "<div id='" + backgroundId + "' class='ks-selected-list-value-container'></div>"; | 
  | 38 |  |      private Panel mainPanel; | 
  | 39 |  |      private HTMLPanel panel; | 
  | 40 | 0 |      private AbbrButton delete = new AbbrButton(AbbrButtonType.DELETE); | 
  | 41 | 0 |      private Anchor viewDetails = new Anchor("View"); | 
  | 42 |  |      private DataHelper dataHelper; | 
  | 43 |  |      private Data data; | 
  | 44 | 0 |      private List<Callback<Value>> valueChangeCallbacks = | 
  | 45 |  |          new ArrayList<Callback<Value>>(); | 
  | 46 |  |      private String deletedKey; | 
  | 47 |  |       | 
  | 48 | 0 |      private static int classInstanceId = -1; | 
  | 49 |  |      public int instanceId; | 
  | 50 |  |       | 
  | 51 | 0 |      public KSItemLabel(boolean canEdit, DataHelper dataParser) { | 
  | 52 | 0 |          init(canEdit, false, dataParser); | 
  | 53 | 0 |      } | 
  | 54 |  |       | 
  | 55 | 0 |      public KSItemLabel(boolean canEdit, boolean hasDetails, DataHelper dataParser) { | 
  | 56 | 0 |          init(canEdit, hasDetails, dataParser); | 
  | 57 | 0 |      } | 
  | 58 |  |   | 
  | 59 |  |      private void init(boolean canEdit, boolean hasDetails, DataHelper dataParser) { | 
  | 60 | 0 |          classInstanceId++; | 
  | 61 | 0 |          instanceId = classInstanceId; | 
  | 62 | 0 |          mainPanel = new FlowPanel(); | 
  | 63 | 0 |          mainPanel.setStyleName("ks-selected-list-value"); | 
  | 64 | 0 |          panel = new HTMLPanel(PANEL_CONTENT_OPEN + PANEL_CONTENT_BACKGROUND); | 
  | 65 | 0 |          panel.getElement().setId(id); | 
  | 66 | 0 |          this.dataHelper = dataParser; | 
  | 67 | 0 |          mainPanel.add(panel); | 
  | 68 | 0 |          if (hasDetails) { | 
  | 69 | 0 |              viewDetails.getElement().getStyle().setProperty("position", "absolute"); | 
  | 70 | 0 |              viewDetails.getElement().getStyle().setProperty("right", "25px"); | 
  | 71 | 0 |              viewDetails.getElement().getStyle().setProperty("top", "1px"); | 
  | 72 | 0 |              mainPanel.add(viewDetails); | 
  | 73 |  |          } | 
  | 74 | 0 |          if(canEdit) { | 
  | 75 | 0 |              delete.getElement().getStyle().setProperty("position", "absolute"); | 
  | 76 | 0 |              delete.getElement().getStyle().setProperty("right", "3px"); | 
  | 77 | 0 |              delete.getElement().getStyle().setProperty("top", "1px"); | 
  | 78 | 0 |              mainPanel.add(delete); | 
  | 79 | 0 |              initDeleteHandlers(); | 
  | 80 |  |          } | 
  | 81 | 0 |          String labelText = ""; | 
  | 82 | 0 |          panel.getElementById(contentId).setInnerText(labelText); | 
  | 83 | 0 |          panel.setVisible(false); | 
  | 84 | 0 |          mainPanel.setVisible(false); | 
  | 85 | 0 |          super.initWidget(mainPanel); | 
  | 86 | 0 |      } | 
  | 87 |  |       | 
  | 88 |  |      @Override | 
  | 89 |  |      public HandlerRegistration addCloseHandler(CloseHandler<KSItemLabel> handler) { | 
  | 90 | 0 |          return addHandler(handler, CloseEvent.getType()); | 
  | 91 |  |      } | 
  | 92 |  |   | 
  | 93 |  |      @Override | 
  | 94 |  |      public void addValueChangeCallback(Callback<Value> callback) { | 
  | 95 | 0 |          valueChangeCallbacks.add(callback); | 
  | 96 | 0 |      } | 
  | 97 |  |   | 
  | 98 |  |      private void callHandlers() { | 
  | 99 | 0 |          if (valueChangeCallbacks != null) { | 
  | 100 |  |   | 
  | 101 | 0 |              for (Callback<Data.Value> handler : valueChangeCallbacks) { | 
  | 102 | 0 |                  handler.exec(getValue()); | 
  | 103 |  |              } | 
  | 104 |  |          } | 
  | 105 | 0 |      } | 
  | 106 |  |   | 
  | 107 |  |      @Override | 
  | 108 |  |      public Value getValue() { | 
  | 109 | 0 |          DataValue result = new DataValue(data); | 
  | 110 | 0 |          return result; | 
  | 111 |  |      } | 
  | 112 |  |   | 
  | 113 |  |      @Override | 
  | 114 |  |      public void setValue(Value value) { | 
  | 115 | 0 |          deletedKey = null; | 
  | 116 | 0 |          if (value == null) { | 
  | 117 | 0 |              this.data = null; | 
  | 118 |  |          } else { | 
  | 119 | 0 |              this.data = value.get(); | 
  | 120 |  |          } | 
  | 121 | 0 |          callHandlers(); | 
  | 122 | 0 |          redraw(); | 
  | 123 | 0 |      } | 
  | 124 |  |       | 
  | 125 |  |      public String getKey() { | 
  | 126 | 0 |          return dataHelper.getKey(data); | 
  | 127 |  |      } | 
  | 128 |  |       | 
  | 129 |  |      public String getDisplayText() { | 
  | 130 | 0 |          if (data == null) { | 
  | 131 | 0 |              return null; | 
  | 132 |  |          } | 
  | 133 | 0 |          return dataHelper.parse(data); | 
  | 134 |  |      } | 
  | 135 |  |       | 
  | 136 |  |      private String nvl(String inString) { | 
  | 137 | 0 |          return (inString == null)? "" : inString; | 
  | 138 |  |      } | 
  | 139 |  |       | 
  | 140 |  |      private void redraw() { | 
  | 141 | 0 |          String labelText = null; | 
  | 142 | 0 |          labelText = dataHelper.parse(data); | 
  | 143 | 0 |          panel.getElementById(contentId).setInnerHTML(nvl(labelText)); | 
  | 144 | 0 |          panel.setVisible(true); | 
  | 145 | 0 |          mainPanel.setVisible(true); | 
  | 146 | 0 |      } | 
  | 147 |  |   | 
  | 148 |  |      private void initDeleteHandlers() { | 
  | 149 |  |           | 
  | 150 | 0 |          delete.addClickHandler(new ClickHandler() { | 
  | 151 |  |              @Override | 
  | 152 |  |              public void onClick(ClickEvent event) { | 
  | 153 | 0 |                      doRemove(); | 
  | 154 | 0 |              } | 
  | 155 |  |          }); | 
  | 156 | 0 |      } | 
  | 157 |  |   | 
  | 158 |  |      public HandlerRegistration addShowDetailsHandler(ClickHandler clickHandler) { | 
  | 159 | 0 |          return viewDetails.addClickHandler(clickHandler); | 
  | 160 |  |      } | 
  | 161 |  |   | 
  | 162 |  |      private void doRemove() { | 
  | 163 |  |   | 
  | 164 |  |   | 
  | 165 |  |   | 
  | 166 |  |   | 
  | 167 | 0 |          deletedKey = dataHelper.getKey(data); | 
  | 168 | 0 |          data = null; | 
  | 169 | 0 |          redraw(); | 
  | 170 | 0 |          callHandlers(); | 
  | 171 | 0 |          CloseEvent.fire(this, this); | 
  | 172 | 0 |      } | 
  | 173 |  |       | 
  | 174 |  |      public String getDeletedKey() { | 
  | 175 | 0 |          return deletedKey; | 
  | 176 |  |      } | 
  | 177 |  |   | 
  | 178 |  |      public void setHighlighted(boolean highlighted) { | 
  | 179 | 0 |          if (highlighted) { | 
  | 180 | 0 |              Elements.fadeIn(panel.getElementById(backgroundId), 250, 100, new Elements.EmptyFadeCallback()); | 
  | 181 |  |   | 
  | 182 |  |          } else { | 
  | 183 | 0 |              Elements.fadeOut(panel.getElementById(backgroundId), 1000, 0, new Elements.EmptyFadeCallback());    | 
  | 184 |  |   | 
  | 185 |  |          } | 
  | 186 | 0 |      } | 
  | 187 |  |       | 
  | 188 |  |      public void removeHighlight(){ | 
  | 189 | 0 |              Elements.setOpacity(panel.getElementById(backgroundId), 0); | 
  | 190 | 0 |      } | 
  | 191 |  |   | 
  | 192 |  |      @Override | 
  | 193 |  |      public boolean equals(Object obj) { | 
  | 194 | 0 |          KSItemLabel item2 = null; | 
  | 195 | 0 |          boolean result = false; | 
  | 196 |  |          try { | 
  | 197 | 0 |              item2 = (KSItemLabel) obj; | 
  | 198 | 0 |          } catch (Exception e) { | 
  | 199 | 0 |              return false; | 
  | 200 | 0 |          } | 
  | 201 | 0 |          if (item2 != null && nullSafeEquals(getKey(), item2.getKey()) && | 
  | 202 |  |                  nullSafeEquals(getDisplayText(), item2.getDisplayText())) { | 
  | 203 | 0 |              result = true; | 
  | 204 |  |          } | 
  | 205 | 0 |          return result; | 
  | 206 |  |      } | 
  | 207 |  |       | 
  | 208 |  |       | 
  | 209 |  |       | 
  | 210 |  |      private boolean nullSafeEquals(String str1, String str2) { | 
  | 211 | 0 |          boolean result = false; | 
  | 212 | 0 |          String tempStr1 = (str1 == null)? "" : str1; | 
  | 213 | 0 |          String tempStr2 = (str2 == null)? "" : str2; | 
  | 214 | 0 |          result = tempStr1.equals(tempStr2); | 
  | 215 | 0 |          return result; | 
  | 216 |  |      } | 
  | 217 |  |   | 
  | 218 |  |      @Override | 
  | 219 |  |      public int hashCode() { | 
  | 220 | 0 |          String key = getKey(); | 
  | 221 | 0 |          String displayText = getDisplayText(); | 
  | 222 | 0 |          key = (key == null)? "" : key; | 
  | 223 | 0 |          displayText = (displayText == null)? "" : displayText; | 
  | 224 | 0 |          return key.hashCode() + displayText.hashCode(); | 
  | 225 |  |      } | 
  | 226 |  |       | 
  | 227 |  |  } |