001 /*
002 * Created on Jan 28, 2005
003 *
004 * Copyright 2004 Chris Nelson
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS" BASIS,
011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 * See the License for the specific language governing permissions and limitations under the License.
013 */
014 package org.trails.descriptor;
015
016 import java.io.Serializable;
017 import java.lang.reflect.InvocationTargetException;
018 import java.util.Hashtable;
019 import java.util.Map;
020
021 import org.apache.commons.beanutils.BeanUtils;
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024
025 /**
026 * @author fus8882
027 * <p/>
028 * TODO To change the template for this generated type comment go to Window -
029 * Preferences - Java - Code Style - Code Templates
030 */
031 public class TrailsDescriptor implements IDescriptor, Serializable
032 {
033 protected static final Log LOG = LogFactory.getLog(TrailsDescriptor.class);
034
035 private String displayName;
036
037 private String shortDescription;
038
039 protected Class type;
040
041 private boolean hidden;
042
043 Map<String, IDescriptorExtension> extensions = new Hashtable<String, IDescriptorExtension>();
044
045 /**
046 * @param dto
047 */
048 public TrailsDescriptor(TrailsDescriptor dto)
049 {
050 try
051 {
052 BeanUtils.copyProperties(this, dto);
053 } catch (IllegalAccessException e)
054 {
055 LOG.error(e.getMessage());
056 e.printStackTrace();
057 } catch (InvocationTargetException e)
058 {
059 LOG.error(e.getMessage());
060 e.printStackTrace();
061 } catch (Exception e)
062 {
063 LOG.error(e.toString());
064 e.printStackTrace();
065 }
066 }
067
068 public TrailsDescriptor(IDescriptor descriptor)
069 {
070 try
071 {
072 BeanUtils.copyProperties(this, descriptor);
073 copyExtensionsFrom(descriptor);
074 } catch (IllegalAccessException e)
075 {
076 LOG.error(e.getMessage());
077 e.printStackTrace();
078 } catch (InvocationTargetException e)
079 {
080 LOG.error(e.getMessage());
081 e.printStackTrace();
082 } catch (Exception e)
083 {
084 LOG.error(e.toString());
085 e.printStackTrace();
086 }
087 }
088
089 public TrailsDescriptor(Class type)
090 {
091 this.type = type;
092 }
093
094 public String getDisplayName()
095 {
096 return displayName;
097 }
098
099 public void setDisplayName(String displayName)
100 {
101 this.displayName = displayName;
102 }
103
104 public String getShortDescription()
105 {
106 return shortDescription;
107 }
108
109 public void setShortDescription(String shortDescription)
110 {
111 this.shortDescription = shortDescription;
112 }
113
114 @Override
115 public Object clone()
116 {
117 return new TrailsDescriptor(this);
118 }
119
120 public void copyFrom(IDescriptor descriptor)
121 {
122 try
123 {
124 BeanUtils.copyProperties(this, descriptor);
125 copyExtensionsFrom(descriptor);
126 } catch (IllegalAccessException e)
127 {
128 LOG.error(e.getMessage());
129 e.printStackTrace();
130 } catch (InvocationTargetException e)
131 {
132 LOG.error(e.getMessage());
133 e.printStackTrace();
134 } catch (Exception e)
135 {
136 LOG.error(e.toString());
137 e.printStackTrace();
138 }
139 }
140
141 public void copyExtensionsFrom(IDescriptor descriptor)
142 {
143 Map<String, IDescriptorExtension> exts = descriptor.getExtensions();
144
145 for (Map.Entry<String, IDescriptorExtension> entry : exts.entrySet())
146 {
147 String keye = entry.getKey();
148 IDescriptorExtension value = entry.getValue();
149 try
150 {
151 this.addExtension(keye, (IDescriptorExtension) BeanUtils.cloneBean(value));
152 } catch (Exception e)
153 {
154 //@todo fix clone methods.
155 }
156 }
157 }
158
159 public boolean isHidden()
160 {
161 return hidden;
162 }
163
164 public void setHidden(boolean hidden)
165 {
166 this.hidden = hidden;
167 }
168
169 public Class getType()
170 {
171 return type;
172 }
173
174 public void setType(Class type)
175 {
176 this.type = type;
177 }
178
179 /**
180 * Keye is property name preceded by package name
181 */
182 public boolean supportsExtension(String keye)
183 {
184 return getExtension(keye) != null;
185 }
186
187 public boolean supportsExtension(Class extensionType)
188 {
189 return supportsExtension(extensionType.getName());
190 }
191
192 /**
193 * Keye is property name preceded by package name
194 */
195 public IDescriptorExtension getExtension(String keye)
196 {
197 return extensions.get(keye);
198 }
199
200 /**
201 * Keye is property name preceded by package name
202 */
203 public void addExtension(String keye, IDescriptorExtension extension)
204 {
205 extensions.put(keye, extension);
206 }
207
208 public void addExtension(Class extensionType, IDescriptorExtension extension)
209 {
210 addExtension(extensionType.getName(), extension);
211 }
212
213 /**
214 * Keye is property name preceded by package name
215 */
216 public void removeExtension(String keye)
217 {
218 extensions.remove(keye);
219 }
220
221 public void removeExtension(Class extensionType)
222 {
223 removeExtension(extensionType.getName());
224 }
225
226 public <E extends IDescriptorExtension> E getExtension(Class<E> extensionType)
227 {
228 return (E) extensions.get(extensionType.getName());
229 }
230
231 /**
232 * This getter method is here just to allow clone(), copyFrom() and
233 * BeanUtils.copyProperties(this, descriptor); to work correctly
234 */
235 public Map<String, IDescriptorExtension> getExtensions()
236 {
237 return extensions;
238 }
239
240 /**
241 * This setter method is here just to allow clone(), copyFrom() and
242 * BeanUtils.copyProperties(this, descriptor); to work correctly
243 */
244 public void setExtensions(Map<String, IDescriptorExtension> extensions)
245 {
246 this.extensions = extensions;
247 }
248 }