add javadoc
[jSite.git] / src / de / todesbaum / jsite / application / FileOption.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.application;
21
22 /**
23  * Container for various file options.
24  * 
25  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
26  */
27 public class FileOption {
28
29         /** The default for the insert state. */
30         private static final boolean DEFAULT_INSERT = true;
31
32         /** The default for the custom key. */
33         private static final String DEFAULT_CUSTOM_KEY = "CHK@";
34
35         /** The default container. */
36         private static final String DEFAULT_CONTAINER = "";
37
38         /** The default edition range. */
39         private static final int DEFAULT_EDITION_RANGE = 3;
40
41         /** The default for the replace edition state. */
42         private static final boolean DEFAULT_REPLACE_EDITION = false;
43
44         /** The insert state. */
45         private boolean insert;
46
47         /** The custom key. */
48         private String customKey;
49
50         /** The default MIME type. */
51         private final String defaultMimeType;
52
53         /** The current MIME type. */
54         private String mimeType;
55
56         /** The container. */
57         private String container;
58
59         /** The edition range. */
60         private int editionRange;
61
62         /** The replace edition state. */
63         private boolean replaceEdition;
64
65         /**
66          * Creates new file options.
67          * 
68          * @param defaultMimeType
69          *            The default MIME type of the file
70          */
71         public FileOption(String defaultMimeType) {
72                 insert = DEFAULT_INSERT;
73                 customKey = DEFAULT_CUSTOM_KEY;
74                 this.defaultMimeType = defaultMimeType;
75                 mimeType = defaultMimeType;
76                 container = DEFAULT_CONTAINER;
77                 editionRange = DEFAULT_EDITION_RANGE;
78                 replaceEdition = DEFAULT_REPLACE_EDITION;
79         }
80
81         /**
82          * Returns the custom key. The custom key is only used when
83          * {@link #isInsert()} returns <code>true</code>.
84          * 
85          * @return The custom key
86          */
87         public String getCustomKey() {
88                 return customKey;
89         }
90
91         /**
92          * Sets the custom key. The custom key is only used when {@link #isInsert()}
93          * returns <code>true</code>.
94          * 
95          * @param customKey
96          *            The custom key
97          */
98         public void setCustomKey(String customKey) {
99                 if (customKey == null) {
100                         customKey = "";
101                 }
102                 this.customKey = customKey;
103         }
104
105         /**
106          * Returns whether the file should be inserted. If a file is not inserted, a
107          * custom key has to be specified for it.
108          * 
109          * @see #setCustomKey(String)
110          * @return <code>true</code> if the file should be inserted,
111          *         <code>false</code> otherwise
112          */
113         public boolean isInsert() {
114                 return insert;
115         }
116
117         /**
118          * Sets whether the file should be inserted. If a file is not inserted, a
119          * custom key has to be specified for it.
120          * 
121          * @param insert
122          *            <code>true</code> if the file should be inserted,
123          *            <code>false</code> otherwise
124          */
125         public void setInsert(boolean insert) {
126                 this.insert = insert;
127         }
128
129         /**
130          * Sets the MIME type of the file. Setting the MIME type to
131          * <code>null</code> will set the MIME type to the default MIME type.
132          * 
133          * @param mimeType
134          *            The MIME type of the file
135          */
136         public void setMimeType(String mimeType) {
137                 if (mimeType == null) {
138                         mimeType = defaultMimeType;
139                 }
140                 this.mimeType = mimeType;
141         }
142
143         /**
144          * Returns the MIME type of the file. If no custom MIME type has been set,
145          * the default MIME type is returned.
146          * 
147          * @return The MIME type of the file
148          */
149         public String getMimeType() {
150                 return mimeType;
151         }
152
153         /**
154          * Returns the name of the container this file should be put in.
155          * 
156          * @return The name of the container
157          */
158         public String getContainer() {
159                 return container;
160         }
161
162         /**
163          * Sets the name of the container this file should be put in.
164          * 
165          * @param container
166          *            The name of the container
167          */
168         public void setContainer(String container) {
169                 if (container == null) {
170                         container = DEFAULT_CONTAINER;
171                 }
172                 this.container = container;
173         }
174
175         /**
176          * Sets whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
177          * 
178          * @param replaceEdition
179          *            <code>true</code> to replace tags, <code>false</code> not
180          *            to replace
181          */
182         public void setReplaceEdition(boolean replaceEdition) {
183                 this.replaceEdition = replaceEdition;
184         }
185
186         /**
187          * Returns whether the file should have “$[EDITION+<i>n</i>]” tags
188          * replaced.
189          * 
190          * @return <code>true</code> if tags should be replaced,
191          *         <code>false</code> otherwise
192          */
193         public boolean getReplaceEdition() {
194                 return replaceEdition;
195         }
196
197         /**
198          * Sets the range of editions that should be replaced.
199          * 
200          * @param editionRange
201          *            The range editions to replace
202          */
203         public void setEditionRange(int editionRange) {
204                 this.editionRange = editionRange;
205         }
206
207         /**
208          * Returns the range of editions that should be replaced.
209          * 
210          * @return The range of editions to replace
211          */
212         public int getEditionRange() {
213                 return editionRange;
214         }
215
216         /**
217          * Returns whether the options for this file have been modified, i.e. are
218          * not at their default values.
219          * 
220          * @return <code>true</code> if the options have been modified,
221          *         <code>false</code> if they are at default values
222          */
223         public boolean isCustom() {
224                 if (insert != DEFAULT_INSERT) {
225                         return true;
226                 }
227                 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
228                         return true;
229                 }
230                 if (!defaultMimeType.equals(mimeType)) {
231                         return true;
232                 }
233                 if (!DEFAULT_CONTAINER.equals(container)) {
234                         return true;
235                 }
236                 if (replaceEdition != DEFAULT_REPLACE_EDITION) {
237                         return true;
238                 }
239                 if (editionRange != DEFAULT_EDITION_RANGE) {
240                         return true;
241                 }
242                 return false;
243         }
244
245 }