1 | |
package org.kuali.student.common.ui.client.widgets.documenttool; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
7 | |
import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle; |
8 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
9 | |
import org.kuali.student.common.ui.client.service.DocumentRpcService; |
10 | |
import org.kuali.student.common.ui.client.service.DocumentRpcServiceAsync; |
11 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton; |
12 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType; |
13 | |
import org.kuali.student.core.document.dto.RefDocRelationInfo; |
14 | |
import org.kuali.student.core.dto.StatusInfo; |
15 | |
|
16 | |
import com.google.gwt.core.client.GWT; |
17 | |
import com.google.gwt.dom.client.Style; |
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.FlexTable; |
22 | |
import com.google.gwt.user.client.ui.HTML; |
23 | |
|
24 | 0 | public class DocumentList extends Composite{ |
25 | 0 | private DocumentRpcServiceAsync documentServiceAsync = GWT.create(DocumentRpcService.class); |
26 | 0 | private FlexTable tableLayout = new FlexTable(); |
27 | |
private List<RefDocRelationInfo> docInfos; |
28 | |
private Callback<String> deleteCallback; |
29 | 0 | private boolean canDelete = false; |
30 | 0 | private boolean showDesc = true; |
31 | 0 | private boolean showTitle = true; |
32 | |
private String refObjectType; |
33 | |
|
34 | 0 | public DocumentList(String refObjectType, boolean showTitle, boolean showDesc) { |
35 | 0 | this.showTitle = showTitle; |
36 | 0 | this.showDesc = showDesc; |
37 | 0 | this.refObjectType = refObjectType; |
38 | 0 | this.initWidget(tableLayout); |
39 | 0 | } |
40 | |
|
41 | 0 | public DocumentList(String refObjectType, List<RefDocRelationInfo> docInfos, boolean showTitle, boolean showDesc) { |
42 | 0 | this.showTitle = showTitle; |
43 | 0 | this.showDesc = showDesc; |
44 | 0 | this.refObjectType = refObjectType; |
45 | 0 | setDocInfos(docInfos); |
46 | 0 | this.initWidget(tableLayout); |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
public DocumentList(String refObjectType, List<RefDocRelationInfo> docInfos, |
51 | 0 | Callback<String> deleteCallback) { |
52 | 0 | canDelete = true; |
53 | 0 | this.refObjectType = refObjectType; |
54 | 0 | this.deleteCallback = deleteCallback; |
55 | 0 | setDocInfos(docInfos); |
56 | 0 | this.initWidget(tableLayout); |
57 | 0 | } |
58 | |
|
59 | |
public void getAndSetDocInfos(String id){ |
60 | 0 | documentServiceAsync.getRefDocIdsForRef(refObjectType, id, new KSAsyncCallback<List<RefDocRelationInfo>>(){ |
61 | |
|
62 | |
@Override |
63 | |
public void handleFailure(Throwable caught) { |
64 | 0 | GWT.log("getRefDocIdsForRef failed", caught); |
65 | 0 | setDocInfos(null); |
66 | 0 | } |
67 | |
|
68 | |
@Override |
69 | |
public void onSuccess(List<RefDocRelationInfo> result) { |
70 | 0 | setDocInfos(result); |
71 | 0 | } |
72 | |
}); |
73 | 0 | } |
74 | |
|
75 | |
public void setDocInfos(List<RefDocRelationInfo> docInfos) { |
76 | 0 | this.docInfos = docInfos; |
77 | 0 | redraw(); |
78 | 0 | } |
79 | |
|
80 | |
public void add(RefDocRelationInfo docInfo) { |
81 | 0 | docInfos = (docInfos == null)? new ArrayList<RefDocRelationInfo>() : |
82 | |
docInfos; |
83 | 0 | docInfos.add(docInfo); |
84 | 0 | redraw(); |
85 | 0 | } |
86 | |
|
87 | |
private void redraw() { |
88 | 0 | tableLayout.clear(); |
89 | 0 | if (docInfos != null) { |
90 | 0 | int rowIndex = 0; |
91 | |
|
92 | 0 | for (final RefDocRelationInfo docInfo : docInfos) { |
93 | 0 | HTML name = new HTML("No file exists"); |
94 | 0 | HTML documentText = new HTML(); |
95 | |
|
96 | 0 | int columnIndex = 0; |
97 | |
|
98 | |
|
99 | 0 | if (rowIndex == 0 && showTitle) { |
100 | 0 | StringBuilder titleTextSb = new StringBuilder(); |
101 | 0 | titleTextSb.append("Attached Documents (").append(docInfos.size()).append(")"); |
102 | 0 | SectionTitle uploadedFileSectionHeader = SectionTitle.generateH3Title(titleTextSb.toString()); |
103 | 0 | uploadedFileSectionHeader.getElement().getStyle().setProperty("borderBottom", "1px solid #D8D8D8"); |
104 | 0 | tableLayout.setWidget(rowIndex, columnIndex, uploadedFileSectionHeader); |
105 | 0 | tableLayout.getFlexCellFormatter().setColSpan(rowIndex, columnIndex, 3); |
106 | 0 | rowIndex++; |
107 | |
} |
108 | |
|
109 | 0 | tableLayout.setWidget(rowIndex, columnIndex, name); |
110 | 0 | name.setHTML("<a href=\"" + GWT.getModuleBaseURL()+"rpcservices/DocumentUpload?docId=" + docInfo.getDocumentId() + "\" target=\"_blank\"><b>" + docInfo.getTitle() + "</b></a>"); |
111 | 0 | name.getElement().getStyle().setPaddingRight(20d, Style.Unit.PX); |
112 | 0 | tableLayout.setWidget(rowIndex, columnIndex, name); |
113 | 0 | columnIndex++; |
114 | 0 | if(showDesc){ |
115 | 0 | documentText.setHTML(docInfo.getDesc().getPlain()); |
116 | 0 | documentText.getElement().getStyle().setPaddingRight(20d, Style.Unit.PX); |
117 | 0 | tableLayout.setWidget(rowIndex, columnIndex, documentText); |
118 | 0 | columnIndex++; |
119 | |
} |
120 | 0 | if(canDelete){ |
121 | 0 | AbbrButton delete = new AbbrButton(AbbrButtonType.DELETE); |
122 | |
|
123 | 0 | tableLayout.setWidget(rowIndex, columnIndex, delete); |
124 | 0 | delete.addClickHandler(new ClickHandler(){ |
125 | |
@Override |
126 | |
public void onClick(ClickEvent event) { |
127 | |
try { |
128 | |
|
129 | |
|
130 | 0 | documentServiceAsync.deleteRefDocRelationAndOrphanedDoc(docInfo.getId(), docInfo.getDocumentId(), new KSAsyncCallback<StatusInfo>(){ |
131 | |
@Override |
132 | |
public void onSuccess(StatusInfo result) { |
133 | 0 | deleteCallback.exec(result.toString()); |
134 | 0 | } |
135 | |
}); |
136 | 0 | } catch (Exception e) { |
137 | 0 | GWT.log("deleteRefDocRelationAndOrphanedDoc failed", e); |
138 | 0 | } |
139 | |
|
140 | |
|
141 | 0 | } |
142 | |
}); |
143 | |
} |
144 | 0 | rowIndex++; |
145 | 0 | } |
146 | |
} |
147 | 0 | } |
148 | |
} |