View Javadoc

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  public class SwitchSection extends BaseSection {
23      private HashMap<String, Section> swapSectionMap = new HashMap<String, Section>();
24      private KSSelectItemWidgetAbstract selectableWidget;
25      private List<Section> deleted = new ArrayList<Section>();
26      private ConfirmationDialog dialog; 
27      private boolean showConfirmation = true;
28      private List<String> lastSelection = new ArrayList<String>();
29      private List<String> deletionParentKeys;
30      
31      /**
32       * Constructor for SwapSection, note that the SelectableWidget passed in is not added to the
33       * UI but is instead used as reference to show different sections.  The widget must still be added
34       * to a layout as a field or a widget to show in the UI.  A default confirmation dialog will be used.
35       * @param selectableWidget
36       */
37      public SwitchSection(KSSelectItemWidgetAbstract selectableWidget){
38          this.init(selectableWidget);
39      }
40      
41      public SwitchSection(KSSelectItemWidgetAbstract selectableWidget, ConfirmationDialog dialog){
42          this.dialog = dialog;
43          this.init(selectableWidget);
44      }
45      
46      private void init(KSSelectItemWidgetAbstract selectableWidget){
47          this.selectableWidget = selectableWidget;
48          
49          if(dialog == null){
50              dialog = 
51                  new ConfirmationDialog(Application.getApplicationContext().getMessage("fieldDeletionTitle"),  
52                          Application.getApplicationContext().getMessage("fieldDeletionConfirmMessage"));
53          }
54          dialog.getConfirmButton().addClickHandler(new ClickHandler(){
55  
56              @Override
57              public void onClick(ClickEvent event) {
58                  handleUserSelection();
59                  dialog.hide();
60              }
61          });
62          
63          selectableWidget.addSelectionChangeHandler(new SelectionChangeHandler(){
64  
65              @Override
66              public void onSelectionChange(SelectionChangeEvent event) {
67                  if(event.isUserInitiated() && showConfirmation){
68                      if(SwitchSection.this.selectableWidget.getSelectedItems().size() < lastSelection.size()){
69                          dialog.show();
70                      }
71                      else if(!SwitchSection.this.selectableWidget.getSelectedItems().containsAll(lastSelection)){
72  //                      List<String> selected  = SwapSection.this.selectableWidget.getSelectedItems();
73  //                      for(int i = 0; i < selected.size(); i++){
74  //                          String key = selected.get(i);
75  //                          Section section = swapSectionMap.get(key);
76  //                          if(section!=null){
77  //                              if(section.getLayout().isVisible()){
78  //                                  dialog.show();
79  //                              }
80  //                          }
81  //                      }
82                          dialog.show();
83                      }
84                      else{
85                          handleUserSelection();
86                      }
87                  }
88                  else if(event.isUserInitiated()){
89                      handleUserSelection();
90                  }
91                  else{
92                      handleSelection();
93                  }
94                  lastSelection.clear();
95                  lastSelection.addAll(SwitchSection.this.selectableWidget.getSelectedItems());
96              }
97          });
98          layout = new VerticalFieldLayout();
99          this.add(layout);
100     }
101     
102     private void handleUserSelection(){
103         List<String> selected  = SwitchSection.this.selectableWidget.getSelectedItems();
104         for(int i = 0; i < selected.size(); i++){
105             String key = selected.get(i);
106             showSwappableSection(key);
107         }
108         
109         Iterator<String> it = swapSectionMap.keySet().iterator();
110         while(it.hasNext()){
111             String key = it.next();
112             if(!selected.contains(key)){
113                 removeSwappableSection(key);
114             }
115         }
116     }
117     
118     /**
119      * This is handled differently than handleUserSelection because it is assumed that the atpService
120      * is setting the correct values into the widgets, therefore no need to delete sections
121      * (also reduces chance of actually deleting data before it is even shown)
122      */
123     private void handleSelection(){
124         List<String> selected  = SwitchSection.this.selectableWidget.getSelectedItems();
125         for(int i = 0; i < selected.size(); i++){
126             String key = selected.get(i);
127             showSwappableSection(key);
128         }
129         
130         Iterator<String> it = swapSectionMap.keySet().iterator();
131         while(it.hasNext()){
132             String key = it.next();
133             if(!selected.contains(key)){
134                 removeSwappableSection(key);
135             }
136         }
137     }
138     
139     private void showSwappableSection(String key){
140         Section section = swapSectionMap.get(key);
141         if(section != null){
142             if(deleted.contains(section)){
143                 deleted.remove(section);
144             }
145             if(!section.getLayout().isVisible()){
146                 section.enableValidation(true);
147                 section.getLayout().setVisible(true);
148             }
149         }
150     }
151     
152     private void removeSwappableSection(String key){
153         Section section = swapSectionMap.get(key);
154         if(section != null){
155             if(!deleted.contains(section)){
156                 deleted.add(section);
157             }
158             if(section.getLayout().isVisible()){
159                 section.enableValidation(false);
160                 section.getLayout().setVisible(false);
161             }
162 
163         }
164     }
165     
166     public void enableConfirmation(boolean enable){
167         showConfirmation = enable;
168     }
169     
170     public String addSection(Section section, String swapKey){
171         swapSectionMap.put(swapKey, section);
172         String key = layout.addLayout(section.getLayout());
173         section.getLayout().setVisible(false);
174         if(selectableWidget.getSelectedItems().contains(swapKey)){
175             handleSelection();
176         }
177         sections.add(section);
178         return key;
179     }
180     
181     public String addSection(String key, Section section, String swapKey){
182         swapSectionMap.put(swapKey, section);
183         section.getLayout().setKey(key);
184         String rkey = layout.addLayout(section.getLayout());
185         section.getLayout().setVisible(false);
186         if(selectableWidget.getSelectedItems().contains(swapKey)){
187             handleSelection();
188         }
189         sections.add(section);
190         return rkey;
191     }
192     
193     
194     @Override
195     public String addSection(Section section) {
196         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         throw new UnsupportedOperationException("Sections can be added to swappable section only through " +
204                 "the addSection(Section section, String swapKey) method");
205     }
206 }