add method to restore content type
[jSite2.git] / src / net / pterodactylus / jsite / project / AbstractEntry.java
1 /*
2  * jSite2 - AbstractEntry.java -
3  * Copyright © 2008 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 net.pterodactylus.jsite.project;
21
22 import java.io.File;
23
24 import net.pterodactylus.util.beans.AbstractBean;
25
26 /**
27  * Abstract base implementation of a {@link Entry}.
28  * 
29  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
30  */
31 public abstract class AbstractEntry extends AbstractBean implements Entry {
32
33         /** The name of the “name” property. */
34         public static final String PROPERTY_NAME = "name";
35
36         /** The name of the “insert” property. */
37         public static final String PROPERTY_INSERT = "insert";
38
39         /** The name of the “content type” property. */
40         public static final String PROPERTY_CONTENT_TYPE = "contentType";
41
42         /** Whether this entry is virtual. */
43         private final boolean virtual;
44
45         /** The name of the file. */
46         private String name;
47
48         /** The default insert flag for this entry. */
49         private boolean defaultInsert;
50
51         /** Whether to insert the file. */
52         private boolean insert;
53
54         /** The default content type of this entry. */
55         private String defaultContentType;
56
57         /** The content type of the file. */
58         private String contentType;
59
60         /**
61          * Creates a new entry.
62          * 
63          * @param virtual
64          *            <code>true</code> if this entry is virtual,
65          *            <code>false</code> otherwise
66          */
67         protected AbstractEntry(boolean virtual) {
68                 this.virtual = virtual;
69         }
70
71         /**
72          * {@inheritDoc}
73          */
74         public boolean isVirtual() {
75                 return virtual;
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         public boolean isDefault() {
82                 return (insert == defaultInsert) && isDefaultContentType();
83         }
84
85         /**
86          * {@inheritDoc}
87          */
88         public boolean isDefaultContentType() {
89                 return ((defaultContentType != null) ? defaultContentType.equals(contentType) : (contentType == null));
90         }
91
92         /**
93          * Sets the default insert flag for this entry. The default insert flag is
94          * derived from {@link File#isHidden()}.
95          * 
96          * @param defaultInsert
97          *            <code>true</code> if the default for this entry is to insert
98          *            it, <code>false</code> otherwise
99          */
100         void setDefaultInsert(boolean defaultInsert) {
101                 this.defaultInsert = defaultInsert;
102         }
103
104         /**
105          * {@inheritDoc}
106          */
107         public String getName() {
108                 return name;
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         public void setName(String name) {
115                 String oldName = this.name;
116                 this.name = name;
117                 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
118         }
119
120         /**
121          * {@inheritDoc}
122          */
123         public boolean isInsert() {
124                 return insert;
125         }
126
127         /**
128          * {@inheritDoc}
129          */
130         public void setInsert(boolean insert) {
131                 boolean oldInsert = this.insert;
132                 this.insert = insert;
133                 fireIfPropertyChanged(PROPERTY_INSERT, oldInsert, insert);
134         }
135
136         /**
137          * {@inheritDoc}
138          */
139         public String getContentType() {
140                 return contentType;
141         }
142
143         /**
144          * {@inheritDoc}
145          */
146         public void setContentType(String contentType) {
147                 String oldContentType = this.contentType;
148                 this.contentType = contentType;
149                 fireIfPropertyChanged(PROPERTY_CONTENT_TYPE, oldContentType, contentType);
150         }
151
152         /**
153          * Sets the default content type of the entry. The default content type is
154          * derived from its extension.
155          * 
156          * @param defaultContentType
157          *            The default content type
158          */
159         void setDefaultContentType(String defaultContentType) {
160                 this.defaultContentType = defaultContentType;
161         }
162
163         /**
164          * {@inheritDoc}
165          */
166         public void restoreDefaultContentType() {
167                 this.contentType = defaultContentType;
168         }
169
170 }