fix parameter assignments
[jSite.git] / src / de / todesbaum / jsite / application / FileOption.java
1 /*
2  * jSite - a tool for uploading websites into Freenet Copyright (C) 2006 David
3  * Roden
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  * 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                         this.customKey = "";
101                 } else {
102                         this.customKey = customKey;
103                 }
104         }
105
106         /**
107          * Returns whether the file should be inserted. If a file is not inserted, a
108          * custom key has to be specified for it.
109          *
110          * @see #setCustomKey(String)
111          * @return <code>true</code> if the file should be inserted,
112          *         <code>false</code> otherwise
113          */
114         public boolean isInsert() {
115                 return insert;
116         }
117
118         /**
119          * Sets whether the file should be inserted. If a file is not inserted, a
120          * custom key has to be specified for it.
121          *
122          * @param insert
123          *            <code>true</code> if the file should be inserted,
124          *            <code>false</code> otherwise
125          */
126         public void setInsert(boolean insert) {
127                 this.insert = insert;
128         }
129
130         /**
131          * Sets the MIME type of the file. Setting the MIME type to
132          * <code>null</code> will set the MIME type to the default MIME type.
133          *
134          * @param mimeType
135          *            The MIME type of the file
136          */
137         public void setMimeType(String mimeType) {
138                 if (mimeType == null) {
139                         this.mimeType = defaultMimeType;
140                 } else {
141                         this.mimeType = mimeType;
142                 }
143         }
144
145         /**
146          * Returns the MIME type of the file. If no custom MIME type has been set,
147          * the default MIME type is returned.
148          *
149          * @return The MIME type of the file
150          */
151         public String getMimeType() {
152                 return mimeType;
153         }
154
155         /**
156          * Returns the name of the container this file should be put in.
157          *
158          * @return The name of the container
159          */
160         public String getContainer() {
161                 return container;
162         }
163
164         /**
165          * Sets the name of the container this file should be put in.
166          *
167          * @param container
168          *            The name of the container
169          */
170         public void setContainer(String container) {
171                 if (container == null) {
172                         this.container = DEFAULT_CONTAINER;
173                 } else {
174                         this.container = container;
175                 }
176         }
177
178         /**
179          * Sets whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
180          *
181          * @param replaceEdition
182          *            <code>true</code> to replace tags, <code>false</code> not to
183          *            replace
184          */
185         public void setReplaceEdition(boolean replaceEdition) {
186                 this.replaceEdition = replaceEdition;
187         }
188
189         /**
190          * Returns whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
191          *
192          * @return <code>true</code> if tags should be replaced, <code>false</code>
193          *         otherwise
194          */
195         public boolean getReplaceEdition() {
196                 return replaceEdition;
197         }
198
199         /**
200          * Sets the range of editions that should be replaced.
201          *
202          * @param editionRange
203          *            The range editions to replace
204          */
205         public void setEditionRange(int editionRange) {
206                 this.editionRange = editionRange;
207         }
208
209         /**
210          * Returns the range of editions that should be replaced.
211          *
212          * @return The range of editions to replace
213          */
214         public int getEditionRange() {
215                 return editionRange;
216         }
217
218         /**
219          * Returns whether the options for this file have been modified, i.e. are
220          * not at their default values.
221          *
222          * @return <code>true</code> if the options have been modified,
223          *         <code>false</code> if they are at default values
224          */
225         public boolean isCustom() {
226                 if (insert != DEFAULT_INSERT) {
227                         return true;
228                 }
229                 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
230                         return true;
231                 }
232                 if (!defaultMimeType.equals(mimeType)) {
233                         return true;
234                 }
235                 if (!DEFAULT_CONTAINER.equals(container)) {
236                         return true;
237                 }
238                 if (replaceEdition != DEFAULT_REPLACE_EDITION) {
239                         return true;
240                 }
241                 if (editionRange != DEFAULT_EDITION_RANGE) {
242                         return true;
243                 }
244                 return false;
245         }
246
247 }