1 | |
package org.kuali.student.core.document.ui.client.widgets.documenttool; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import org.kuali.student.common.dto.StatusInfo; |
7 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
8 | |
import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle; |
9 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
10 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton; |
11 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType; |
12 | |
import org.kuali.student.core.document.dto.RefDocRelationInfo; |
13 | |
import org.kuali.student.core.document.ui.client.service.DocumentRpcService; |
14 | |
import org.kuali.student.core.document.ui.client.service.DocumentRpcServiceAsync; |
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 | |
|
107 | |
|
108 | |
|
109 | 0 | rowIndex++; |
110 | |
|
111 | |
|
112 | 0 | HTML fileNameHeader = new HTML(); |
113 | 0 | fileNameHeader.setText("File Name"); |
114 | 0 | fileNameHeader.addStyleName("KS-DocumentList-Attachment-Column-Header"); |
115 | 0 | tableLayout.setWidget(rowIndex, 0, fileNameHeader); |
116 | |
|
117 | |
|
118 | 0 | HTML descriptionHeader = new HTML(); |
119 | 0 | descriptionHeader.setText("Description"); |
120 | 0 | descriptionHeader.addStyleName("KS-DocumentList-Attachment-Column-Header"); |
121 | 0 | tableLayout.setWidget(rowIndex, 1, descriptionHeader); |
122 | |
|
123 | |
|
124 | 0 | HTML deleteHeader = new HTML(); |
125 | 0 | deleteHeader.setText("Delete"); |
126 | 0 | deleteHeader.addStyleName("KS-DocumentList-Attachment-Column-Header"); |
127 | 0 | tableLayout.setWidget(rowIndex, 2, deleteHeader); |
128 | |
|
129 | 0 | rowIndex++; |
130 | |
} |
131 | |
|
132 | 0 | tableLayout.setWidget(rowIndex, columnIndex, name); |
133 | 0 | name.setHTML("<a href=\"" + GWT.getModuleBaseURL()+"rpcservices/DocumentUpload?docId=" + docInfo.getDocumentId() + "\" target=\"_blank\"><b>" + docInfo.getTitle() + "</b></a>"); |
134 | 0 | name.getElement().getStyle().setPaddingRight(20d, Style.Unit.PX); |
135 | 0 | tableLayout.setWidget(rowIndex, columnIndex, name); |
136 | 0 | columnIndex++; |
137 | 0 | if(showDesc){ |
138 | 0 | documentText.setHTML(docInfo.getDesc().getPlain()); |
139 | 0 | documentText.getElement().getStyle().setPaddingRight(20d, Style.Unit.PX); |
140 | 0 | tableLayout.setWidget(rowIndex, columnIndex, documentText); |
141 | 0 | columnIndex++; |
142 | |
} |
143 | 0 | if(canDelete){ |
144 | 0 | AbbrButton delete = new AbbrButton(AbbrButtonType.DELETE); |
145 | |
|
146 | 0 | tableLayout.setWidget(rowIndex, columnIndex, delete); |
147 | 0 | delete.addClickHandler(new ClickHandler(){ |
148 | |
@Override |
149 | |
public void onClick(ClickEvent event) { |
150 | |
try { |
151 | |
|
152 | |
|
153 | 0 | documentServiceAsync.deleteRefDocRelationAndOrphanedDoc(docInfo.getId(), docInfo.getDocumentId(), new KSAsyncCallback<StatusInfo>(){ |
154 | |
@Override |
155 | |
public void onSuccess(StatusInfo result) { |
156 | 0 | deleteCallback.exec(result.toString()); |
157 | 0 | } |
158 | |
}); |
159 | 0 | } catch (Exception e) { |
160 | 0 | GWT.log("deleteRefDocRelationAndOrphanedDoc failed", e); |
161 | 0 | } |
162 | |
|
163 | |
|
164 | 0 | } |
165 | |
}); |
166 | |
} |
167 | 0 | rowIndex++; |
168 | 0 | } |
169 | |
} |
170 | 0 | } |
171 | |
} |