Merged refactoring from development branch.
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / ui / section / reference / ReferenceDetailElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.section.reference;
12
13 import java.net.URISyntaxException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.eclipse.swt.SWT;
19
20 import eu.etaxonomy.cdm.model.agent.AgentBase;
21 import eu.etaxonomy.cdm.model.agent.Institution;
22 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
23 import eu.etaxonomy.cdm.model.reference.Reference;
24 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
25 import eu.etaxonomy.cdm.model.reference.ReferenceType;
26 import eu.etaxonomy.taxeditor.model.AuthorHelper;
27 import eu.etaxonomy.taxeditor.store.StoreUtil;
28 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
29 import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
30 import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
31 import eu.etaxonomy.taxeditor.ui.forms.IErrorIntolerableElement;
32 import eu.etaxonomy.taxeditor.ui.forms.IExceptionHandler;
33 import eu.etaxonomy.taxeditor.ui.forms.TextWithLabelElement;
34 import eu.etaxonomy.taxeditor.ui.forms.TimePeriodElement;
35 import eu.etaxonomy.taxeditor.ui.forms.ToggleableTextElement;
36 import eu.etaxonomy.taxeditor.ui.forms.UriWithLabelElement;
37 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.EnumComboType;
38 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.SelectionType;
39 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
40 import eu.etaxonomy.taxeditor.ui.selection.AbstractSelectionElement;
41 import eu.etaxonomy.taxeditor.ui.selection.ReferenceSelectionElement;
42 import eu.etaxonomy.taxeditor.ui.term.ReferenceTypeComboElement;
43
44 /**
45 * <p>ReferenceBaseDetailElement class.</p>
46 *
47 * @author n.hoffmann
48 * @created Mar 2, 2010
49 * @version 1.0
50 */
51 public class ReferenceDetailElement extends AbstractCdmDetailElement<Reference> implements IErrorIntolerableElement, IExceptionHandler {
52
53 // property holder
54 private ToggleableTextElement toggleable_cache;
55 private TextWithLabelElement text_edition;
56 private TextWithLabelElement text_editor;
57 private TextWithLabelElement text_isbn;
58 private TextWithLabelElement text_issn;
59 private TextWithLabelElement text_organisation;
60 private TextWithLabelElement text_pages;
61 private TextWithLabelElement text_placePublished;
62 private TextWithLabelElement text_publisher;
63 private TextWithLabelElement text_referenceAbstract;
64 private TextWithLabelElement text_series;
65 private TextWithLabelElement text_seriesPart;
66 private TextWithLabelElement text_title;
67 private TextWithLabelElement text_volume;
68 private UriWithLabelElement text_uri;
69 private AbstractSelectionElement<AgentBase> selection_authorTeam;
70 private TimePeriodElement element_timePeriod;
71 private AbstractSelectionElement<Reference> selection_inReference;
72 private AbstractSelectionElement<Institution> selection_institution;
73 private ReferenceTypeComboElement combo_referenceType;
74
75 private ReferenceSelectionElement selection_inSeries;
76
77
78 /**
79 * <p>Constructor for ReferenceBaseDetailElement.</p>
80 *
81 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
82 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
83 * @param style a int.
84 */
85 public ReferenceDetailElement(CdmFormFactory cdmFormFactory,
86 ICdmFormElement formElement, int style) {
87 super(cdmFormFactory, formElement);
88 }
89
90 /*
91 * (non-Javadoc)
92 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite#createControl(org.eclipse.swt.widgets.Composite, int)
93 */
94 /** {@inheritDoc} */
95 @Override
96 protected void createControls(ICdmFormElement formElement, Reference entity, int style) {
97 toggleable_cache = formFactory.createToggleableTextField(formElement, "Reference Cache", entity.getTitleCache(), entity.isProtectedTitleCache(), style);
98
99 combo_referenceType = (ReferenceTypeComboElement) formFactory.createEnumComboElement(EnumComboType.REFERENCETYPE, formElement, style);
100
101 combo_referenceType.setSelection(entity.getType());
102
103 // title
104 text_title = formFactory.createTextWithLabelElement(formElement, "Title", entity.getTitle(), style);
105
106 // author team
107 selection_authorTeam = formFactory.createSelectionElement(SelectionType.TEAM, getConversationHolder(), formElement, "Author", AuthorHelper.getAuthor(entity.getAuthorTeam()), style);
108
109 ReferenceType referenceType = entity.getType();
110
111 if(referenceType != null){
112
113 createInReferenceByType(this, referenceType, entity, SWT.NULL);
114
115 if(referenceType.equals(ReferenceType.Generic)){
116 createGenericControls(this, entity, SWT.NULL);
117 }else{
118 if(referenceType.isPrintedUnit()){
119 createPrintedUnitControls(this, referenceType, entity, SWT.NULL);
120 }
121 if(referenceType.isPublication()){
122 createPublicationControls(this, referenceType, entity, SWT.NULL);
123 }
124 if(referenceType.isVolumeReference()){
125 createVolumeControls(this, entity, SWT.NULL);
126 }
127 if(referenceType.isSection()){
128 createSectionControls(this, entity, SWT.NULL);
129 }
130 }
131 }
132
133 // date published
134 element_timePeriod = formFactory.createTimePeriodElement(formElement, "Date Published", entity.getDatePublished(), style);
135
136 createUriAndAbstract(this, entity, SWT.NULL);
137 }
138
139 /*
140 * (non-Javadoc)
141 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite#updateContent()
142 */
143 /** {@inheritDoc} */
144 @Override
145 protected void updateContent() {
146 if(getEntity() == null){
147 setEntity(ReferenceFactory.newGeneric());
148 }
149
150 super.updateContent();
151
152 // enable elements according to state of cache protection
153 // setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new Object[]{text_cache}));
154 toggleable_cache.setEnabled(getEntity().isProtectedTitleCache());
155 setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
156 }
157
158 /**
159 * Creates an in reference for reference types that do hav this.
160 *
161 * @param element
162 * @param referenceType
163 * @param reference
164 * @param style
165 */
166 private void createInReferenceByType(
167 ICdmFormElement element,
168 ReferenceType referenceType, Reference reference, int style) {
169 boolean createInReferenceWidget = true;
170 String label = "In Reference";
171
172 switch(referenceType){
173 case Generic:
174 break;
175 case BookSection:
176 label = "Book";
177 break;
178 case InProceedings:
179 label = "Print Series";
180 break;
181 case Article:
182 label = "Journal";
183 break;
184 case WebPage:
185 break;
186 default:
187 createInReferenceWidget = false;
188 }
189
190 if(createInReferenceWidget){
191 selection_inReference = formFactory.createSelectionElement(SelectionType.REFERENCE, getConversationHolder(),
192 element, label, reference.getInReference(), style);
193 }
194 }
195
196 /**
197 * Creates additional controls to edit a generic reference
198 *
199 * @param element
200 * @param reference
201 * @param listener
202 * @param style
203 */
204 private void createGenericControls(ICdmFormElement element, Reference reference, int style){
205
206 //editor
207 text_editor = formFactory.createTextWithLabelElement(element, "Editor", reference.getEditor(), style);
208
209 // series
210 text_series = formFactory.createTextWithLabelElement(element, "Series", reference.getSeries(), style);
211
212 // volume
213 text_volume = formFactory.createTextWithLabelElement(element, "Volume", reference.getVolume(), style);
214
215 // pages
216 text_pages = formFactory.createTextWithLabelElement(element, "Pages", reference.getPages(), style);
217 }
218
219
220 /**
221 * Creates additional controls to edit a publication
222 *
223 * @param element
224 * @param reference
225 * @param listener
226 * @param style
227 */
228 private void createPublicationControls(ICdmFormElement element, ReferenceType referenceType, Reference reference, int style){
229
230 // placePublished
231 text_placePublished = formFactory.createTextWithLabelElement(element, "Place Published", reference.getPlacePublished(), style);
232
233 // publisher
234 text_publisher = formFactory.createTextWithLabelElement(element, "Publisher", reference.getPublisher(), style);
235
236 if(referenceType.equals(ReferenceType.Journal)){
237 //issn
238 text_issn = formFactory.createTextWithLabelElement(element, "ISSN", reference.getIssn(), style);
239 }
240
241 if(referenceType.equals(ReferenceType.Report)){
242 // institution
243 selection_institution = formFactory.createSelectionElement(SelectionType.INSTITUTION, getConversationHolder(), element, "Institution", reference.getInstitution(), style);
244 }
245
246 if(referenceType.equals(ReferenceType.Thesis)){
247 // school
248 selection_institution = formFactory.createSelectionElement(SelectionType.INSTITUTION, getConversationHolder(), element, "School", reference.getSchool(), style);
249 }
250
251 if(referenceType.equals(ReferenceType.Proceedings)){
252 //organization
253 text_organisation = formFactory.createTextWithLabelElement(element, "Organisation", reference.getOrganization(), style);
254 }
255 }
256
257
258 /**
259 * Creates additional controls to edit a section
260 *
261 * @param element
262 * @param reference
263 * @param style
264 */
265 private void createSectionControls(ICdmFormElement element, Reference reference, int style){
266 // pages
267 text_pages = formFactory.createTextWithLabelElement(element, "Pages", reference.getPages(), style);
268 }
269
270 /**
271 * Creates additional controls to edit a volume
272 *
273 * @param element
274 * @param reference
275 * @param style
276 */
277 private void createVolumeControls(ICdmFormElement element, Reference reference, int style){
278
279 text_volume = formFactory.createTextWithLabelElement(element, "Volume", reference.getVolume(), style);
280
281 text_series = formFactory.createTextWithLabelElement(element, "Series", reference.getSeries(), style);
282 }
283
284
285 /**
286 *
287 * @param element
288 * @param referenceType
289 * @param reference
290 * @param style
291 */
292 private void createPrintedUnitControls(ICdmFormElement element, ReferenceType referenceType, Reference reference, int style){
293 // in series
294 selection_inSeries = (ReferenceSelectionElement) formFactory.createSelectionElement(SelectionType.REFERENCE, getConversationHolder(), element, "In Series", (Reference) reference.getInSeries(), style);
295
296 //editor
297 text_editor = formFactory.createTextWithLabelElement(element, "Editor", reference.getEditor(), style);
298
299 // series part
300 text_seriesPart = formFactory.createTextWithLabelElement(element, "Series Part", reference.getSeriesPart(), style);
301
302 if(referenceType.equals(ReferenceType.Book)){
303 // edition
304 text_edition = formFactory.createTextWithLabelElement(element, "Edition", reference.getEdition(), style);
305
306 //isbn
307 text_isbn = formFactory.createTextWithLabelElement(element, "ISBN", reference.getIsbn(), style);
308 }
309 }
310
311 private void createUriAndAbstract(ICdmFormElement element,
312 Reference reference, int style) {
313 // uri
314 text_uri = formFactory.createUriWithLabelElement(element, "URI", reference.getUri(), style);
315 // abstract
316 text_referenceAbstract = formFactory.createMultilineTextWithLabel(element, "Reference Abstract", 100, style);
317 text_referenceAbstract.setText(reference.getReferenceAbstract());
318 }
319
320
321 /** {@inheritDoc} */
322 @Override
323 public void handleEvent(Object eventSource) {
324 if(eventSource == combo_referenceType){
325 getEntity().setType(combo_referenceType.getSelection());
326 getParentElement().refresh();
327 }
328 else if(eventSource == element_timePeriod){
329 getEntity().setDatePublished(element_timePeriod.getTimePeriod());
330 clearException();
331 }
332 // selections
333 else if(eventSource == selection_authorTeam){
334 getEntity().setAuthorTeam((TeamOrPersonBase)selection_authorTeam.getSelection());
335 }
336 else if(eventSource == selection_inReference){
337 getEntity().setInReference(selection_inReference.getSelection());
338 }
339 else if(eventSource == selection_inSeries){
340 getEntity().setInReference(selection_inSeries.getSelection());
341 }
342 else if(eventSource == selection_institution){
343 getEntity().setInstitution(selection_institution.getSelection());
344 }
345
346 // textfields
347 else if(eventSource == toggleable_cache){
348 getEntity().setTitleCache(toggleable_cache.getText(), toggleable_cache.getState());
349 // setEnabled(! text_cache.getState(), Arrays.asList(new Object[]{text_cache}));
350 setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
351 }
352 else if(eventSource == text_edition){
353 getEntity().setEdition(text_edition.getText());
354 }
355 else if(eventSource == text_editor){
356 getEntity().setEditor(text_editor.getText());
357 }
358 else if(eventSource == text_isbn){
359 getEntity().setIsbn(text_isbn.getText());
360 }
361 else if(eventSource == text_issn){
362 getEntity().setIssn(text_issn.getText());
363 }
364 else if(eventSource == text_organisation){
365 getEntity().setOrganization(text_organisation.getText());
366 }
367 else if(eventSource == text_pages){
368 getEntity().setPages(text_pages.getText());
369 }
370 else if(eventSource == text_placePublished){
371 getEntity().setPlacePublished(text_placePublished.getText());
372 }
373 else if(eventSource == text_publisher){
374 getEntity().setPublisher(text_publisher.getText());
375 }
376 else if(eventSource == text_referenceAbstract){
377 getEntity().setReferenceAbstract(text_referenceAbstract.getText());
378 }
379 else if(eventSource == text_series){
380 getEntity().setSeries(text_series.getText());
381 }
382 else if(eventSource == text_seriesPart){
383 getEntity().setSeriesPart(text_seriesPart.getText());
384 }
385 else if(eventSource == text_title){
386 getEntity().setTitle(text_title.getText());
387 }
388 else if(eventSource == text_uri){
389 try {
390 getEntity().setUri(text_uri.getUri());
391 } catch (URISyntaxException e) {
392 StoreUtil.warningDialog("Invlid URI", getClass(), e.getLocalizedMessage());
393 }
394 }
395 else if(eventSource == text_volume){
396 getEntity().setVolume(text_volume.getText());
397 }
398
399 else if(eventSource != toggleable_cache){
400 toggleable_cache.setText(getEntity().getTitleCache());
401 }
402 }
403
404 /*
405 * (non-Javadoc)
406 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException(eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
407 */
408 /** {@inheritDoc} */
409 @Override
410 public void handleException(CdmPropertyChangeEvent event) {
411 exceptionOccurred(event);
412 }
413
414 /* (non-Javadoc)
415 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
416 */
417 /** {@inheritDoc} */
418 @Override
419 public void clearException() {
420 exceptionOccurred(null);
421 }
422
423 private List<IExceptionHandler> exceptionHandlers = new ArrayList<IExceptionHandler>();
424
425 /* (non-Javadoc)
426 * @see eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler(eu.etaxonomy.taxeditor.forms.IExceptionHandler)
427 */
428 /** {@inheritDoc} */
429 @Override
430 public void addExceptionHandler(IExceptionHandler handler) {
431 exceptionHandlers.add(handler);
432 }
433
434 /* (non-Javadoc)
435 * @see eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler(eu.etaxonomy.taxeditor.forms.IExceptionHandler)
436 */
437 /** {@inheritDoc} */
438 @Override
439 public void removeExceptionHandler(IExceptionHandler handler) {
440 exceptionHandlers.remove(handler);
441 }
442
443 /* (non-Javadoc)
444 * @see eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred(eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
445 */
446 /** {@inheritDoc} */
447 @Override
448 public void exceptionOccurred(CdmPropertyChangeEvent event) {
449 for(IExceptionHandler handler : exceptionHandlers){
450 if(event == null) {
451 clearException();
452 }else{
453 handler.handleException(event);
454 }
455 }
456 }
457 }