Include field in file options that allows on-insert renaming.
[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         /** Whether to insert a redirect. */
48         private boolean insertRedirect;
49
50         /** The custom key. */
51         private String customKey;
52
53         /** The changed name. */
54         private String changedName;
55
56         /** The default MIME type. */
57         private final String defaultMimeType;
58
59         /** The current MIME type. */
60         private String mimeType;
61
62         /** The container. */
63         private String container;
64
65         /** The edition range. */
66         private int editionRange;
67
68         /** The replace edition state. */
69         private boolean replaceEdition;
70
71         /**
72          * Creates new file options.
73          *
74          * @param defaultMimeType
75          *            The default MIME type of the file
76          */
77         public FileOption(String defaultMimeType) {
78                 insert = DEFAULT_INSERT;
79                 customKey = DEFAULT_CUSTOM_KEY;
80                 this.defaultMimeType = defaultMimeType;
81                 mimeType = defaultMimeType;
82                 container = DEFAULT_CONTAINER;
83                 editionRange = DEFAULT_EDITION_RANGE;
84                 replaceEdition = DEFAULT_REPLACE_EDITION;
85         }
86
87         /**
88          * Returns the custom key. The custom key is only used when
89          * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
90          * true}.
91          *
92          * @return The custom key
93          */
94         public String getCustomKey() {
95                 return customKey;
96         }
97
98         /**
99          * Sets the custom key. The custom key is only used when {@link #isInsert()}
100          * and {@link #isInsertRedirect()} both return {@code true}.
101          *
102          * @param customKey
103          *            The custom key
104          */
105         public void setCustomKey(String customKey) {
106                 if (customKey == null) {
107                         this.customKey = "";
108                 } else {
109                         this.customKey = customKey;
110                 }
111         }
112
113         /**
114          * Returns whether the file should be inserted. If a file is not inserted
115          * and {@link #isInsertRedirect()} is also {@code false}, the file will not
116          * be inserted at all.
117          *
118          * @see #setCustomKey(String)
119          * @return <code>true</code> if the file should be inserted,
120          *         <code>false</code> otherwise
121          */
122         public boolean isInsert() {
123                 return insert;
124         }
125
126         /**
127          * Sets whether the file should be inserted. If a file is not inserted and
128          * {@link #isInsertRedirect()} is also {@code false}, the file will not be
129          * inserted at all.
130          *
131          * @param insert
132          *            <code>true</code> if the file should be inserted,
133          *            <code>false</code> otherwise
134          */
135         public void setInsert(boolean insert) {
136                 this.insert = insert;
137         }
138
139         /**
140          * Returns whether a redirect to a different key should be inserted. This
141          * will only matter if {@link #isInsert()} returns {@code false}. The key
142          * that should be redirected to still needs to be specified via
143          * {@link #setCustomKey(String)}.
144          *
145          * @return {@code true} if a redirect should be inserted, {@code false}
146          *         otherwise
147          */
148         public boolean isInsertRedirect() {
149                 return insertRedirect;
150         }
151
152         /**
153          * Sets whether a redirect should be inserted. This will only matter if
154          * {@link #isInsert()} returns {@code false}, i.e. it has been
155          * {@link #setInsert(boolean)} to {@code false}. The key that should be
156          * redirected to still needs to be specified via
157          * {@link #setCustomKey(String)}.
158          *
159          * @param insertRedirect
160          *            {@code true} if a redirect should be inserted, {@code false}
161          *            otherwise
162          */
163         public void setInsertRedirect(boolean insertRedirect) {
164                 this.insertRedirect = insertRedirect;
165         }
166
167         /**
168          * Returns whether this file has a changed name. Use
169          * {@link #getChangedName()} is this method returns {@code true}.
170          *
171          * @return {@code true} if this file has a changed name, {@code false}
172          *         otherwise
173          */
174         public boolean hasChangedName() {
175                 return (changedName != null) && (changedName.length() > 0);
176         }
177
178         /**
179          * Returns the changed name for this file. This method will return {@code
180          * null} or an empty {@link String} if this file should not be renamed.
181          *
182          * @return The changed name, or {@code null} if this file should not be
183          *         renamed
184          */
185         public String getChangedName() {
186                 return changedName;
187         }
188
189         /**
190          * Sets the changed name for this file. Setting the changed file to {@code
191          * null} or an empty {@link String} will disable renaming.
192          *
193          * @param changedName
194          *            The new changed name for this file
195          */
196         public void setChangedName(String changedName) {
197                 this.changedName = changedName;
198         }
199
200         /**
201          * Sets the MIME type of the file. Setting the MIME type to
202          * <code>null</code> will set the MIME type to the default MIME type.
203          *
204          * @param mimeType
205          *            The MIME type of the file
206          */
207         public void setMimeType(String mimeType) {
208                 if (mimeType == null) {
209                         this.mimeType = defaultMimeType;
210                 } else {
211                         this.mimeType = mimeType;
212                 }
213         }
214
215         /**
216          * Returns the MIME type of the file. If no custom MIME type has been set,
217          * the default MIME type is returned.
218          *
219          * @return The MIME type of the file
220          */
221         public String getMimeType() {
222                 return mimeType;
223         }
224
225         /**
226          * Returns the name of the container this file should be put in.
227          *
228          * @return The name of the container
229          */
230         public String getContainer() {
231                 return container;
232         }
233
234         /**
235          * Sets the name of the container this file should be put in.
236          *
237          * @param container
238          *            The name of the container
239          */
240         public void setContainer(String container) {
241                 if (container == null) {
242                         this.container = DEFAULT_CONTAINER;
243                 } else {
244                         this.container = container;
245                 }
246         }
247
248         /**
249          * Sets whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
250          *
251          * @param replaceEdition
252          *            <code>true</code> to replace tags, <code>false</code> not to
253          *            replace
254          */
255         public void setReplaceEdition(boolean replaceEdition) {
256                 this.replaceEdition = replaceEdition;
257         }
258
259         /**
260          * Returns whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
261          *
262          * @return <code>true</code> if tags should be replaced, <code>false</code>
263          *         otherwise
264          */
265         public boolean getReplaceEdition() {
266                 return replaceEdition;
267         }
268
269         /**
270          * Sets the range of editions that should be replaced.
271          *
272          * @param editionRange
273          *            The range editions to replace
274          */
275         public void setEditionRange(int editionRange) {
276                 this.editionRange = editionRange;
277         }
278
279         /**
280          * Returns the range of editions that should be replaced.
281          *
282          * @return The range of editions to replace
283          */
284         public int getEditionRange() {
285                 return editionRange;
286         }
287
288         /**
289          * Returns whether the options for this file have been modified, i.e. are
290          * not at their default values.
291          *
292          * @return <code>true</code> if the options have been modified,
293          *         <code>false</code> if they are at default values
294          */
295         public boolean isCustom() {
296                 if (insert != DEFAULT_INSERT) {
297                         return true;
298                 }
299                 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
300                         return true;
301                 }
302                 if (!defaultMimeType.equals(mimeType)) {
303                         return true;
304                 }
305                 if (!DEFAULT_CONTAINER.equals(container)) {
306                         return true;
307                 }
308                 if (replaceEdition != DEFAULT_REPLACE_EDITION) {
309                         return true;
310                 }
311                 if (editionRange != DEFAULT_EDITION_RANGE) {
312                         return true;
313                 }
314                 return false;
315         }
316
317 }