KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > Upload


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form;
16
17 import org.apache.tapestry.IForm;
18 import org.apache.tapestry.IMarkupWriter;
19 import org.apache.tapestry.IRequestCycle;
20 import org.apache.tapestry.multipart.MultipartDecoder;
21 import org.apache.tapestry.request.IUploadFile;
22 import org.apache.tapestry.valid.ValidationStrings;
23 import org.apache.tapestry.valid.ValidatorException;
24
25 /**
26  * Form element used to upload files. [ <a
27  * HREF="../../../../../ComponentReference/Upload.html">Component Reference </a>] As of 4.0, the
28  * Upload field can indicate that it is required.
29  *
30  * @author Howard Lewis Ship
31  * @author Paul Ferraro
32  * @since 1.0.8
33  */

34
35 public abstract class Upload extends AbstractRequirableField
36 {
37     /**
38      * @see org.apache.tapestry.form.validator.AbstractRequirableField#bind(org.apache.tapestry.IRequestCycle)
39      */

40     public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException
41     {
42         setFile(getUploadFile());
43     }
44
45     /**
46      * @see org.apache.tapestry.AbstractComponent#finishLoad()
47      */

48     protected void finishLoad()
49     {
50         setRequiredMessage(ValidationStrings.getMessagePattern(
51                 ValidationStrings.REQUIRED_FILE_FIELD,
52                 getPage().getLocale()));
53     }
54
55     private IUploadFile getUploadFile()
56     {
57         return getDecoder().getFileUpload(getName());
58     }
59
60     public String JavaDoc getSubmittedValue(IRequestCycle cycle)
61     {
62         return getUploadFile().getFilePath();
63     }
64
65     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
66     {
67         super.renderFormComponent(writer, cycle);
68
69         // Force the form to use the correct encoding type for file uploads.
70
IForm form = getForm();
71
72         form.setEncodingType("multipart/form-data");
73
74         renderDelegatePrefix(writer, cycle);
75
76         writer.beginEmpty("input");
77         writer.attribute("type", "file");
78         writer.attribute("name", getName());
79
80         if (isDisabled())
81         {
82             writer.attribute("disabled", "disabled");
83         }
84
85         form.getDelegate().writeAttributes(writer, cycle, this, null);
86
87         renderIdAttribute(writer, cycle);
88
89         renderDelegateAttributes(writer, cycle);
90
91         renderDelegateSuffix(writer, cycle);
92     }
93
94     public abstract void setFile(IUploadFile file);
95
96     public abstract MultipartDecoder getDecoder();
97 }
Popular Tags