001 package org.tynamo.pages;
002
003 import org.apache.tapestry5.*;
004 import org.apache.tapestry5.annotations.Component;
005 import org.apache.tapestry5.annotations.Environmental;
006 import org.apache.tapestry5.annotations.Property;
007 import org.apache.tapestry5.beaneditor.BeanModel;
008 import org.apache.tapestry5.corelib.components.TextField;
009 import org.apache.tapestry5.ioc.Messages;
010 import org.apache.tapestry5.ioc.annotations.Inject;
011 import org.apache.tapestry5.services.BeanEditContext;
012 import org.apache.tapestry5.services.BeanModelSource;
013 import org.apache.tapestry5.services.PropertyEditContext;
014 import org.apache.tapestry5.services.ValueEncoderSource;
015 import org.chenillekit.tapestry.core.components.DateTimeField;
016 import org.chenillekit.tapestry.core.components.Editor;
017 import org.tynamo.components.*;
018 import org.tynamo.descriptor.CollectionDescriptor;
019 import org.tynamo.descriptor.IdentifierDescriptor;
020 import org.tynamo.descriptor.TynamoPropertyDescriptor;
021 import org.tynamo.services.DescriptorService;
022 import org.tynamo.services.PersistenceService;
023 import org.tynamo.services.TynamoBeanContext;
024 import org.tynamo.util.GenericSelectionModel;
025
026 import java.util.ArrayList;
027 import java.util.Collection;
028 import java.util.List;
029
030
031 public class Editors
032 {
033
034 @Environmental
035 @Property(write = false)
036 private BeanEditContext beanEditContext;
037
038 @Environmental
039 @Property(write = false)
040 private PropertyEditContext propertyEditContext;
041
042 @Environmental
043 @Property(write = false)
044 private TynamoBeanContext tynamoBeanContext;
045
046 @Inject
047 private PersistenceService persitenceService;
048
049 @Inject
050 private DescriptorService descriptorService;
051
052 @Inject
053 private ValueEncoderSource valueEncoderSource;
054
055 @Inject
056 private ComponentResources resources;
057
058 @Inject
059 private BeanModelSource beanModelSource;
060
061 @Component(parameters = {"value=propertyEditContext.propertyValue", "label=prop:propertyEditContext.label",
062 "translate=prop:textFieldTranslator", "validate=prop:textFieldValidator",
063 "clientId=prop:propertyEditContext.propertyId", "annotationProvider=propertyEditContext"})
064 private TextField textField;
065
066 @Component(parameters = {"value=propertyEditContext.propertyValue", "label=prop:propertyEditContext.label",
067 "clientId=propertyEditContext.propertyid", "validate=prop:dateFieldValidator"})
068 private DateTimeField dateField;
069
070 @Component(parameters = {"value=propertyEditContext.propertyValue", "label=prop:propertyEditContext.label",
071 "translate=prop:fckTranslator", "validate=prop:fckValidator",
072 "clientId=prop:propertyEditContext.propertyId", "annotationProvider=propertyEditContext"})
073 private Editor fckEditor;
074
075 @Component(parameters = {"collection=propertyEditContext.propertyValue", "label=prop:propertyEditContext.label",
076 "clientId=prop:propertyEditContext.propertyId", "collectionDescriptor=propertyDescriptor",
077 "owner=tynamoBeanContext.bean"})
078 private EditComposition editComposition;
079
080 @Component(parameters = {"model=tynamoBeanContext.bean", "propertyDescriptor=propertyDescriptor"})
081 private org.tynamo.components.Blob blob;
082
083 public TynamoPropertyDescriptor getPropertyDescriptor()
084 {
085 return descriptorService.getClassDescriptor(beanEditContext.getBeanClass()).getPropertyDescriptor(propertyEditContext.getPropertyId());
086 }
087
088 public SelectModel getSelectModel()
089 {
090 TynamoPropertyDescriptor propertyDescriptor = getPropertyDescriptor();
091
092 if (propertyDescriptor.isCollection())
093 {
094 CollectionDescriptor collectionDescriptor = (CollectionDescriptor) propertyDescriptor;
095 return new GenericSelectionModel(persitenceService.getInstances(collectionDescriptor.getElementType()));
096 } else
097 {
098 return new GenericSelectionModel(persitenceService.getInstances(propertyEditContext.getPropertyType()));
099 }
100 }
101
102 /**
103 * Provide a value encoder for a type.
104 *
105 * @return
106 */
107 public ValueEncoder getValueEncoderForProperty()
108 {
109 TynamoPropertyDescriptor propertyDescriptor = getPropertyDescriptor();
110 if (propertyDescriptor.isCollection())
111 {
112 CollectionDescriptor collectionDescriptor = (CollectionDescriptor) propertyDescriptor;
113 return valueEncoderSource.getValueEncoder(collectionDescriptor.getElementType());
114 } else
115 {
116 return valueEncoderSource.getValueEncoder(propertyEditContext.getPropertyType());
117 }
118 }
119
120 public FieldValidator getDateFieldValidator()
121 {
122 return propertyEditContext.getValidator(dateField);
123 }
124
125 public FieldValidator getFckValidator()
126 {
127 return propertyEditContext.getValidator(fckEditor);
128 }
129
130 public FieldTranslator getFckTranslator()
131 {
132 return propertyEditContext.getTranslator(fckEditor);
133 }
134
135 public FieldTranslator getTextFieldTranslator()
136 {
137 return propertyEditContext.getTranslator(textField);
138 }
139
140 public FieldValidator getTextFieldValidator()
141 {
142 return propertyEditContext.getValidator(textField);
143 }
144
145
146
147 /* public List<Boolean> buildSelectedList()
148 {
149 ArrayList<Boolean> selected = new ArrayList<Boolean>();
150 if (collection != null)
151 {
152 selected = new ArrayList<Boolean>(collection.size());
153 for (Object o : getCollection())
154 {
155 selected.add(false);
156 }
157 }
158 return selected;
159 }
160 */
161
162 public boolean isPropertyValueInstanceOfList()
163 {
164 return propertyEditContext.getPropertyValue() instanceof List;
165 }
166
167 /**
168 *
169 * Palette's parameter "selected" only accepts java.util.List
170 * If the collection is a java.util.Set it needs to get converted
171 *
172 */
173 public List getSelected()
174 {
175 ArrayList selectedList = new ArrayList();
176 selectedList.addAll((Collection) propertyEditContext.getPropertyValue());
177 return selectedList;
178 }
179
180 public void setSelected(List selected)
181 {
182 Collection collection = (Collection) propertyEditContext.getPropertyValue();
183 if (selected != null)
184 {
185 collection.clear();
186 collection.addAll(selected);
187 }
188 }
189
190 public boolean isNotEditable()
191 {
192 IdentifierDescriptor descriptor = (IdentifierDescriptor) getPropertyDescriptor();
193 return descriptor.isGenerated() || propertyEditContext.getPropertyValue() != null;
194 }
195
196 public BeanModel getEmbeddedModel()
197 {
198 return beanModelSource.createEditModel(propertyEditContext.getPropertyType(), getMessages());
199 }
200
201 private Messages getMessages()
202 {
203 return resources.getContainerMessages() != null ? resources.getContainerMessages() : resources.getMessages();
204 }
205
206 /**
207 * Looks for a help message within the messages based on the id.
208 */
209 public String getHelpMessage()
210 {
211 Messages messages = getMessages();
212 String key = propertyEditContext.getPropertyId() + "-help";
213 if (messages.contains(key)) return messages.get(key);
214 return null;
215 }
216
217 }