1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.form; |
16 | |
|
17 | |
import org.apache.hivemind.HiveMind; |
18 | |
import org.apache.tapestry.IForm; |
19 | |
import org.apache.tapestry.IMarkupWriter; |
20 | |
import org.apache.tapestry.IRequestCycle; |
21 | |
import org.apache.tapestry.multipart.MultipartDecoder; |
22 | |
import org.apache.tapestry.request.IUploadFile; |
23 | |
import org.apache.tapestry.valid.ValidatorException; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public abstract class Upload extends AbstractFormComponent implements ValidatableField |
38 | |
{ |
39 | |
public abstract void setFile(IUploadFile file); |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
46 | |
{ |
47 | |
|
48 | 0 | IForm form = getForm(); |
49 | |
|
50 | 0 | form.setEncodingType("multipart/form-data"); |
51 | |
|
52 | 0 | renderDelegatePrefix(writer, cycle); |
53 | |
|
54 | 0 | writer.beginEmpty("input"); |
55 | 0 | writer.attribute("type", "file"); |
56 | 0 | writer.attribute("name", getName()); |
57 | |
|
58 | 0 | if (isDisabled()) |
59 | |
{ |
60 | 0 | writer.attribute("disabled", "disabled"); |
61 | |
} |
62 | |
|
63 | 0 | renderIdAttribute(writer, cycle); |
64 | |
|
65 | 0 | renderDelegateAttributes(writer, cycle); |
66 | |
|
67 | 0 | getValidatableFieldSupport().renderContributions(this, writer, cycle); |
68 | |
|
69 | 0 | renderInformalParameters(writer, cycle); |
70 | |
|
71 | 0 | writer.closeTag(); |
72 | |
|
73 | 0 | renderDelegateSuffix(writer, cycle); |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
81 | |
{ |
82 | 0 | IUploadFile file = getDecoder().getFileUpload(getName()); |
83 | |
|
84 | 0 | if (file != null && HiveMind.isBlank(file.getFileName())) |
85 | |
{ |
86 | 0 | file = null; |
87 | |
} |
88 | |
|
89 | |
try |
90 | |
{ |
91 | 0 | getValidatableFieldSupport().validate(this, writer, cycle, file); |
92 | |
|
93 | 0 | setFile(file); |
94 | |
} |
95 | 0 | catch (ValidatorException e) |
96 | |
{ |
97 | 0 | getForm().getDelegate().record(e); |
98 | 0 | } |
99 | 0 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public abstract ValidatableFieldSupport getValidatableFieldSupport(); |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public abstract MultipartDecoder getDecoder(); |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public boolean isRequired() |
115 | |
{ |
116 | 0 | return getValidatableFieldSupport().isRequired(this); |
117 | |
} |
118 | |
} |