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