View Javadoc

1   package org.kuali.student.lum.lu.ui.dependency.client.widgets;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   
7   import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
8   import org.kuali.student.common.ui.client.reporting.ReportExportWidget;
9   import org.kuali.student.common.ui.client.util.ExportElement;
10  import org.kuali.student.common.ui.client.util.ExportUtils;
11  import org.kuali.student.common.ui.client.widgets.KSButton;
12  import org.kuali.student.common.ui.client.widgets.KSLabel;
13  import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle;
14  import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel;
15  import org.kuali.student.common.ui.client.widgets.field.layout.layouts.VerticalFieldLayout;
16  import org.kuali.student.common.ui.client.widgets.search.CollapsablePanel;
17  
18  import com.google.gwt.event.dom.client.ClickEvent;
19  import com.google.gwt.event.dom.client.ClickHandler;
20  import com.google.gwt.user.client.ui.Composite;
21  import com.google.gwt.user.client.ui.FlowPanel;
22  import com.google.gwt.user.client.ui.Widget;
23  
24  public class DependencyResultPanel extends Composite implements ReportExportWidget {
25  
26      protected KSLabel headerLabel = new KSLabel();
27  
28      protected VerticalFieldLayout dependencySectionContainer = new VerticalFieldLayout();
29  
30      protected HashMap<String, DependencySection> dependencySections = new HashMap<String, DependencySection>();
31  
32      // Map of all dependency types in all sections, keyed by "Dependency Section Name"."Dependency Type Name"
33      protected HashMap<String, DependencyTypeSection> dependencyTypeSections = new HashMap<String, DependencyTypeSection>();
34  
35      public DependencyResultPanel() {
36          this.initWidget(dependencySectionContainer);
37          dependencySectionContainer.addWidget(headerLabel);
38          dependencySectionContainer.addStyleName("ks-dependency-results");
39      }
40  
41      public void addSection(String sectionName, String sectionTitle) {
42          DependencySection section = new DependencySection(sectionTitle);
43          dependencySectionContainer.add(section);
44          dependencySections.put(sectionName, section);
45      }
46  
47      /**
48       * @param section
49       * @param collapsableSectionName
50       */
51      public DependencyTypeSection addDependencyTypeSection(String sectionName, String sectionTypeName, Widget sectionHeader) {
52          String typeSectionKey = sectionName + "." + sectionTypeName;
53          DependencyTypeSection typeSection = new DependencyTypeSection(sectionHeader);
54          dependencyTypeSections.put(typeSectionKey, typeSection);
55          DependencySection section = dependencySections.get(sectionName);
56          section.addWidget(typeSection);
57          return typeSection;
58      }
59  
60      public DependencyTypeSection getDependencyTypeSection(String sectionName, String sectionTypeName) {
61          String typeSectionKey = sectionName + "." + sectionTypeName;
62          return dependencyTypeSections.get(typeSectionKey);
63      }
64  
65      public void setHeaderTitle(String title) {
66          // TODO: This may need to be replaced with document to surface handle print/export
67          SectionTitle sectionTitle = SectionTitle.generateH2Title(title);
68          dependencySectionContainer.setLayoutTitle(sectionTitle);
69          dependencySectionContainer.underlineTitle(true);
70  
71          KSButton expandLabel = new KSButton("Expand All", ButtonStyle.DEFAULT_ANCHOR);
72          KSButton collapseLabel = new KSButton("Collapse All", ButtonStyle.DEFAULT_ANCHOR);
73  
74          FlowPanel expandArea = new FlowPanel();
75          expandArea.add(expandLabel);
76          SpanPanel spanPanel = new SpanPanel(" | ");
77          spanPanel.setExportElement(false);
78          expandArea.add(spanPanel);
79          expandArea.add(collapseLabel);
80          dependencySectionContainer.addWidget(expandArea);
81  
82          expandLabel.addClickHandler(new ClickHandler() {
83              public void onClick(ClickEvent event) {
84                  for (DependencySection section : dependencySections.values()) {
85                      section.expandAll();
86                  }
87              }
88          });
89  
90          collapseLabel.addClickHandler(new ClickHandler() {
91              public void onClick(ClickEvent event) {
92                  for (DependencySection section : dependencySections.values()) {
93                      section.collapseAll();
94                  }
95              }
96          });
97  
98      }
99  
100     /**
101      * Call this to finalize the dependency widgets once all data has been loaded.
102      */
103     public void finishLoad() {
104         // This is to update the counts for dependencies
105         for (DependencyTypeSection typeSection : dependencyTypeSections.values()) {
106             typeSection.finishLoad();
107         }
108 
109         // Don't show any sections that don't have dependencies
110         for (DependencySection section : dependencySections.values()) {
111             section.setVisible(section.dependencyTypeSections.size() > 0);
112         }
113     }
114 
115     public static class DependencySection extends VerticalFieldLayout {
116         protected FlowPanel header = new FlowPanel();
117 
118         // List of dependency types within this section
119         List<DependencyTypeSection> dependencyTypeSections = new ArrayList<DependencyTypeSection>();
120 
121         public String getTitleTextFromTitleWidget() {
122             if (this.layoutTitle != null) {
123                 // return this.layoutTitle.getTitleText();
124                 return this.layoutTitle.getElement().getInnerText();
125             }
126             return null;
127         }
128 
129         public DependencySection(String title) {
130             SectionTitle sectionTitle = SectionTitle.generateH4Title(title);
131             sectionTitle.addStyleName("ks-dependency-section-title");
132             header.add(sectionTitle);
133             header.addStyleName("header-underline");
134             header.addStyleName("ks-dependency-section-header");
135             this.setTitleWidget(header);
136             this.addStyleName("ks-dependency-section");
137 
138             KSButton expandLabel = new KSButton("Expand All", ButtonStyle.DEFAULT_ANCHOR);
139             KSButton collapseLabel = new KSButton("Collapse All", ButtonStyle.DEFAULT_ANCHOR);
140 
141             expandLabel.addClickHandler(new ClickHandler() {
142                 public void onClick(ClickEvent event) {
143                     DependencySection.this.expandAll();
144                 }
145             });
146 
147             collapseLabel.addClickHandler(new ClickHandler() {
148                 public void onClick(ClickEvent event) {
149                     DependencySection.this.collapseAll();
150                 }
151             });
152 
153             header.add(expandLabel);
154             SpanPanel spanPanel = new SpanPanel(" | ");
155             spanPanel.setExportElement(false);
156             header.add(spanPanel);
157             header.add(collapseLabel);
158 
159         }
160 
161         @Override
162         public String addWidget(Widget widget) {
163             dependencyTypeSections.add((DependencyTypeSection) widget);
164             return super.addWidget(widget);
165         }
166 
167         public void expandAll() {
168             for (DependencyTypeSection typeSection : dependencyTypeSections) {
169                 if (!typeSection.isOpen()) {
170                     typeSection.open();
171                 }
172             }
173         }
174 
175         public void collapseAll() {
176             for (DependencyTypeSection typeSection : dependencyTypeSections) {
177                 typeSection.close();
178             }
179         }
180 
181         protected void setVisibility() {
182             // If all children elements (dependency types) in this section is not visible, the entire
183             // section should not be showing.
184             int visibleCount = 0;
185             for (DependencyTypeSection typeSection : dependencyTypeSections) {
186                 if (typeSection.isVisible()) {
187                     visibleCount++;
188                 }
189             }
190             this.setVisible(visibleCount > 0);
191         }
192     }
193 
194     public static class DependencyTypeSection extends CollapsablePanel {
195 
196         protected FlowPanel content = new FlowPanel();
197         KSLabel countLabel = new KSLabel();
198 
199         List<CollapsablePanel> dependencyItems = new ArrayList<CollapsablePanel>();
200 
201         public DependencyTypeSection(Widget label) {
202             this.init(label, content, isOpen, true, ImagePosition.ALIGN_LEFT);
203             content.addStyleName("ks-dependency-type-section");
204             super.linkPanel.add(countLabel);
205         }
206 
207         public void finishLoad() {
208             int numItems = dependencyItems.size();
209             countLabel.setText("(" + numItems + "):");
210             if (numItems <= 5) {
211                 super.content.setVisible(true);
212                 super.isOpen = true;
213                 super.setImageState();
214             }
215         }
216 
217         public void addDependencyItem(Widget label, Widget details) {
218             CollapsablePanel depItem = null;
219             if (details != null) {
220                 depItem = new CollapsablePanel(label, details, false, true, ImagePosition.ALIGN_LEFT);
221             } else {
222                 depItem = new CollapsablePanel(label, new SpanPanel(), false, false, ImagePosition.ALIGN_LEFT);
223             }
224             content.add(depItem);
225             dependencyItems.add(depItem);
226         }
227 
228         public void expandAll() {
229             for (CollapsablePanel depItem : dependencyItems) {
230                 if (!depItem.isOpen()) {
231                     depItem.open();
232                 }
233             }
234         }
235 
236         public void collapseAll() {
237             for (CollapsablePanel depItem : dependencyItems) {
238                 depItem.close();
239             }
240         }
241         
242         @Override
243         public List<ExportElement> getExportElementSubset(ExportElement parent) {
244             
245             ArrayList<ExportElement> linkElementSubItems = new ArrayList<ExportElement>();
246             parent.setPrintType(ExportElement.LIST_SUBREPORT);
247         	if (this.isVisible()) {  // If the panel is not open, then don't look further...
248 			
249         	    for (CollapsablePanel depItem : dependencyItems) {
250         	        if (depItem.isVisible()) {
251 					
252         	            ExportElement linkElement = new ExportElement();
253         	            linkElement.setSectionName(parent.getSectionName());
254         	            linkElement.setViewName(parent.getViewName());
255         	            linkElement.setFieldValue(depItem.getExportFieldValue());
256                 
257         	            List<ExportElement> subset = depItem.getExportElementSubset(parent);
258         	            linkElement.setSubset(setCurrOverToItalic(subset));
259                 
260         	            String fieldValue = linkElement.getFieldValue();
261         	            if (fieldValue.indexOf("View Course Set") > 0) {
262         	                fieldValue = fieldValue.replaceAll("View Course Set", "");
263         	            } else if (fieldValue.indexOf("View Course") > 0) {
264         	                fieldValue = fieldValue.replaceAll("View Course", "");
265         	            } else if (fieldValue.indexOf("View Program") > 0) {
266         	                fieldValue = fieldValue.replaceAll("View Program", "");
267         	            }
268                 
269         	            if (fieldValue.indexOf("Different Org") > 0) 
270         	                fieldValue = fieldValue.replaceAll("Different Org", "   * Different Org");
271                 
272         	            linkElement.setFieldValue(fieldValue);
273         	            if (linkElement.getValue() != ""){
274         	                if (linkElement.getSubset() == null){
275         	                    linkElement.setPrintType(ExportElement.LIST);
276         	                } else {
277         	                    linkElement.setPrintType(ExportElement.LIST_SUBREPORT);
278         	                }
279         	            }
280                 
281         	            linkElementSubItems.add(linkElement);
282         	        }
283                 }
284         	}
285             return linkElementSubItems;
286         }
287         
288         private List<ExportElement> setCurrOverToItalic(List<ExportElement> subset){
289             if (subset != null){
290                 for (ExportElement element : subset){
291                     if ((element.getValue() != null)&&(element.getValue().contains("Curriculum Oversight"))){
292                         element.setPrintType(ExportElement.ITALIC);
293                     }
294                     element.setSubset(setCurrOverToItalic(element.getSubset()));
295                 }
296             }
297             return subset;
298         }
299 
300         @Override
301         public String getExportFieldValue() {
302             String text = null;
303             for (int i = 0; i < this.linkPanel.getWidgetCount(); i++) {
304                 Widget child = this.linkPanel.getWidget(i);
305                 if (child instanceof SpanPanel) {
306                     SpanPanel header = (SpanPanel) child;
307                     text = header.getExportFieldValue();
308                 }
309                 if (child instanceof KSLabel && (text != null) && (text != "")) {
310                 	KSLabel countLabel = (KSLabel) child;
311                 	text = text + countLabel.getText();
312                 }
313             }
314             return text;
315         }
316     }
317 
318     public void hide(String dependencySection, String dependencyType) {
319         DependencyTypeSection typeSection = dependencyTypeSections.get(dependencySection + "." + dependencyType);
320         if (typeSection != null) {
321             typeSection.setVisible(false);
322             DependencySection section = dependencySections.get(dependencySection);
323             section.setVisibility();
324         }
325     }
326 
327     public void show(String dependencySection, String dependencyType) {
328         DependencyTypeSection typeSection = dependencyTypeSections.get(dependencySection + "." + dependencyType);
329         if (typeSection != null) {
330             typeSection.setVisible(true);
331             DependencySection section = dependencySections.get(dependencySection);
332             section.setVisibility();
333         }
334     }
335 
336     public void hideAll() {
337         for (DependencyTypeSection typeSection : dependencyTypeSections.values()) {
338             typeSection.setVisible(false);
339         }
340         for (DependencySection section : dependencySections.values()) {
341             section.setVisible(false);
342         }
343     }
344 
345     public void showAll() {
346         for (DependencyTypeSection typeSection : dependencyTypeSections.values()) {
347             typeSection.setVisible(true);
348         }
349         for (DependencySection section : dependencySections.values()) {
350             section.setVisible(section.dependencyTypeSections.size() > 0);
351         }
352     }
353 
354     public VerticalFieldLayout getDependencySectionContainer() {
355         return dependencySectionContainer;
356     }
357 
358     @Override
359     public boolean isExportElement() {
360         return true;
361     }
362 
363     @Override
364     public List<ExportElement> getExportElementSubset(ExportElement parent) {
365         ArrayList<ExportElement> returnItems = new ArrayList<ExportElement>();
366         //
367         if (this.getWidget() instanceof VerticalFieldLayout) {
368             VerticalFieldLayout verticalFieldLayoutWidget = (VerticalFieldLayout) this.getWidget();
369             returnItems.addAll(ExportUtils.getDetailsForWidget(verticalFieldLayoutWidget.getVerticalLayout(), parent.getViewName(), parent.getSectionName()));
370         }
371         //
372         for (DependencySection section : this.dependencySections.values()) {
373         	
374             returnItems.addAll(ExportUtils.getDetailsForWidget(section, parent.getViewName(), parent.getSectionName()));
375         	}
376         return returnItems;
377     }
378 
379     @Override
380     public String getExportFieldValue() {
381         return null;
382     }
383 
384 }