| 1 |  |  package org.kuali.student.common.ui.client.configurable.mvc.layouts; | 
  | 2 |  |   | 
  | 3 |  |  import java.util.ArrayList; | 
  | 4 |  |  import java.util.HashMap; | 
  | 5 |  |  import java.util.List; | 
  | 6 |  |  import java.util.Map; | 
  | 7 |  |   | 
  | 8 |  |  import org.kuali.student.common.ui.client.application.Application; | 
  | 9 |  |  import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle; | 
  | 10 |  |  import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView; | 
  | 11 |  |  import org.kuali.student.common.ui.client.mvc.Callback; | 
  | 12 |  |  import org.kuali.student.common.ui.client.mvc.View; | 
  | 13 |  |  import org.kuali.student.common.ui.client.widgets.KSButton; | 
  | 14 |  |  import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle; | 
  | 15 |  |  import org.kuali.student.common.ui.client.widgets.menus.KSMenuItemData; | 
  | 16 |  |   | 
  | 17 |  |  import com.google.gwt.event.dom.client.ClickEvent; | 
  | 18 |  |  import com.google.gwt.event.dom.client.ClickHandler; | 
  | 19 |  |  import com.google.gwt.user.client.ui.FlowPanel; | 
  | 20 |  |   | 
  | 21 | 0 |  public class MenuEditableSectionController extends MenuSectionController{ | 
  | 22 |  |           | 
  | 23 | 0 |          private boolean editMode = false; | 
  | 24 | 0 |          private boolean isEditable = true; | 
  | 25 |  |           | 
  | 26 |  |           | 
  | 27 | 0 |          private Map<Enum<?>, Enum<?>> readOnlyToEditMap = new HashMap<Enum<?>, Enum<?>>(); | 
  | 28 | 0 |          private Map<Enum<?>, Enum<?>> editToReadOnlyMap = new HashMap<Enum<?>, Enum<?>>(); | 
  | 29 |  |           | 
  | 30 | 0 |          private List<View> readOnlyViews = new ArrayList<View>(); | 
  | 31 | 0 |          private List<KSButton> editLinks = new ArrayList<KSButton>(); | 
  | 32 |  |           | 
  | 33 | 0 |          private Callback<Boolean> editLinkCallback = new Callback<Boolean>(){ | 
  | 34 |  |   | 
  | 35 |  |                  @Override | 
  | 36 |  |                  public void exec(Boolean result) { | 
  | 37 | 0 |                          if(result == true){ | 
  | 38 | 0 |                                  KSMenuItemData item = viewMenuItemMap.get(getCurrentView().getViewEnum()); | 
  | 39 | 0 |                                  if(item == null){ | 
  | 40 | 0 |                                          Enum<?> readOnlyEnum = editToReadOnlyMap.get(getCurrentView().getViewEnum()); | 
  | 41 | 0 |                                          if(readOnlyEnum != null){ | 
  | 42 | 0 |                                                  item = viewMenuItemMap.get(readOnlyEnum); | 
  | 43 |  |                                          } | 
  | 44 |  |                                  } | 
  | 45 | 0 |                                  if(item != null && !item.isSelected()){ | 
  | 46 | 0 |                                          item.setSelected(true, false); | 
  | 47 |  |                                  } | 
  | 48 |  |                          } | 
  | 49 | 0 |                  } | 
  | 50 |  |          }; | 
  | 51 |  |   | 
  | 52 |  |          public MenuEditableSectionController(String controllerId) { | 
  | 53 | 0 |                  super(controllerId); | 
  | 54 | 0 |          } | 
  | 55 |  |           | 
  | 56 |  |          public void addMenuItem(String parentMenu, final View readOnlyView, final View editView){ | 
  | 57 | 0 |                  super.addMenuItem(parentMenu, readOnlyView); | 
  | 58 | 0 |                  this.addView(editView); | 
  | 59 | 0 |                  readOnlyToEditMap.put(readOnlyView.getViewEnum(), editView.getViewEnum()); | 
  | 60 | 0 |                  editToReadOnlyMap.put(editView.getViewEnum(), readOnlyView.getViewEnum()); | 
  | 61 | 0 |                  attachEditLink(readOnlyView); | 
  | 62 | 0 |          } | 
  | 63 |  |           | 
  | 64 |  |          public void attachEditLink(final View v){ | 
  | 65 | 0 |                  readOnlyViews.add(v); | 
  | 66 | 0 |                  createEditLink(v); | 
  | 67 | 0 |          } | 
  | 68 |  |           | 
  | 69 |  |          public KSButton generateEditLink(final View v){ | 
  | 70 | 0 |                  KSButton editLink = new KSButton(Application.getApplicationContext().getMessage("edit"), ButtonStyle.DEFAULT_ANCHOR, new ClickHandler(){ | 
  | 71 |  |                           | 
  | 72 |  |                          @Override | 
  | 73 |  |                          public void onClick(ClickEvent event) { | 
  | 74 | 0 |                                  editMode = true; | 
  | 75 | 0 |                                  showView(v.getViewEnum(), editLinkCallback); | 
  | 76 | 0 |                          } | 
  | 77 |  |                  }); | 
  | 78 | 0 |                  editLinks.add(editLink); | 
  | 79 |  |                   | 
  | 80 | 0 |                  editLink.addStyleName("ks-header-edit-link"); | 
  | 81 |  |                   | 
  | 82 | 0 |                  return editLink; | 
  | 83 |  |          } | 
  | 84 |  |           | 
  | 85 |  |          private void createEditLink(final View v){ | 
  | 86 | 0 |                  if(isEditable){ | 
  | 87 | 0 |                          KSButton editLink = generateEditLink(v); | 
  | 88 |  |                           | 
  | 89 | 0 |                          if(v instanceof SectionView){ | 
  | 90 |  |                                   | 
  | 91 | 0 |                                  SectionTitle title = ((SectionView) v).getLayout().getLayoutTitle(); | 
  | 92 | 0 |                                  if(title != null){ | 
  | 93 | 0 |                                          title.add(editLink); | 
  | 94 |  |                                  } | 
  | 95 |  |                                  else{ | 
  | 96 | 0 |                                          ((SectionView) v).getLayout().insert(editLink, 0); | 
  | 97 |  |                                  } | 
  | 98 | 0 |                          } | 
  | 99 | 0 |                          else if(v.asWidget() instanceof FlowPanel){ | 
  | 100 | 0 |                                  ((FlowPanel)v.asWidget()).insert(editLink, 0); | 
  | 101 |  |                          } | 
  | 102 |  |                  } | 
  | 103 | 0 |          } | 
  | 104 |  |           | 
  | 105 |  |          private void detachEditLinks(){ | 
  | 106 | 0 |                  for(KSButton b: editLinks){ | 
  | 107 | 0 |                          b.removeFromParent(); | 
  | 108 |  |                  } | 
  | 109 | 0 |          } | 
  | 110 |  |           | 
  | 111 |  |          @Override | 
  | 112 |  |          public <V extends Enum<?>> void showView(V viewType, Callback<Boolean> onReadyCallback){ | 
  | 113 |  |                   | 
  | 114 | 0 |                  if(editMode && isEditable){ | 
  | 115 | 0 |                          Enum<?> editViewEnum = readOnlyToEditMap.get(viewType); | 
  | 116 | 0 |                          if(editViewEnum != null){ | 
  | 117 | 0 |                                  super.showView(editViewEnum, onReadyCallback); | 
  | 118 |  |                          } | 
  | 119 |  |                          else{ | 
  | 120 | 0 |                                  super.showView(viewType, onReadyCallback); | 
  | 121 |  |                          } | 
  | 122 | 0 |                  } | 
  | 123 |  |                   | 
  | 124 |  |                  else{ | 
  | 125 | 0 |                          Enum<?> readOnlyEnum = editToReadOnlyMap.get(viewType); | 
  | 126 | 0 |                          if(readOnlyEnum != null){ | 
  | 127 | 0 |                                  super.showView(readOnlyEnum, onReadyCallback); | 
  | 128 |  |                          } | 
  | 129 |  |                          else{ | 
  | 130 | 0 |                                  super.showView(viewType, onReadyCallback); | 
  | 131 |  |                          } | 
  | 132 |  |                  } | 
  | 133 | 0 |          } | 
  | 134 |  |           | 
  | 135 |  |          public void setEditable(boolean editable){ | 
  | 136 | 0 |                  if(editable && this.isEditable != true){ | 
  | 137 | 0 |                          this.isEditable = editable; | 
  | 138 | 0 |                          for(View v: readOnlyViews){ | 
  | 139 | 0 |                                  createEditLink(v); | 
  | 140 |  |                          } | 
  | 141 |  |                  } | 
  | 142 | 0 |                  else if(!editable && this.isEditable == true){ | 
  | 143 | 0 |                          this.isEditable = editable; | 
  | 144 | 0 |                          detachEditLinks(); | 
  | 145 |  |                  } | 
  | 146 | 0 |          } | 
  | 147 |  |           | 
  | 148 |  |          public void setEditMode(final boolean editMode){ | 
  | 149 | 0 |                  if(this.editMode != editMode){ | 
  | 150 | 0 |                          this.editMode = editMode; | 
  | 151 | 0 |                          this.showView(this.getCurrentViewEnum(), new Callback<Boolean>(){ | 
  | 152 |  |   | 
  | 153 |  |                                  @Override | 
  | 154 |  |                                  public void exec(Boolean result) { | 
  | 155 |  |                                          if(false){ | 
  | 156 |  |                                                  MenuEditableSectionController.this.editMode = !editMode; | 
  | 157 |  |                                          } | 
  | 158 | 0 |                                  } | 
  | 159 |  |                          }); | 
  | 160 |  |                  } | 
  | 161 | 0 |          } | 
  | 162 |  |           | 
  | 163 |  |          public void addCommonEditButton(String parentMenu, KSButton button){ | 
  | 164 | 0 |                  if(parentMenu != null){ | 
  | 165 | 0 |                          List<View> views = menuViewMap.get(parentMenu); | 
  | 166 | 0 |                          if(views != null){ | 
  | 167 | 0 |                                  for(int i=0; i < views.size(); i++){ | 
  | 168 | 0 |                                          Enum<?> editEnum = readOnlyToEditMap.get(views.get(i).getViewEnum()); | 
  | 169 | 0 |                                          if(editEnum != null){ | 
  | 170 | 0 |                                                  addButtonForView(editEnum, button); | 
  | 171 |  |                                          } | 
  | 172 |  |                                  } | 
  | 173 |  |                          } | 
  | 174 |  |                  } | 
  | 175 | 0 |          } | 
  | 176 |  |           | 
  | 177 |  |  } |