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