Merge branch 'release/3.7.0'
[taxeditor.git] / eu.etaxonomy.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.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16
17 import org.eclipse.swt.SWT;
18
19 import eu.etaxonomy.cdm.model.agent.AgentBase;
20 import eu.etaxonomy.cdm.model.agent.Institution;
21 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
22 import eu.etaxonomy.cdm.model.reference.Reference;
23 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
24 import eu.etaxonomy.cdm.model.reference.ReferenceType;
25 import eu.etaxonomy.taxeditor.model.AuthorHelper;
26 import eu.etaxonomy.taxeditor.ui.combo.EnumComboElement;
27 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
28 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
29 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
30 import eu.etaxonomy.taxeditor.ui.element.IErrorIntolerableElement;
31 import eu.etaxonomy.taxeditor.ui.element.IExceptionHandler;
32 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
33 import eu.etaxonomy.taxeditor.ui.element.TimePeriodElement;
34 import eu.etaxonomy.taxeditor.ui.element.ToggleableTextElement;
35 import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
36 import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
37 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
38
39 /**
40 * <p>
41 * ReferenceBaseDetailElement class.
42 * </p>
43 *
44 * @author n.hoffmann
45 * @created Mar 2, 2010
46 * @version 1.0
47 */
48 public class ReferenceDetailElement extends AbstractIdentifiableEntityDetailElement<Reference>
49 implements IErrorIntolerableElement, IExceptionHandler {
50
51 private ToggleableTextElement toggleableAbbrevCache;
52
53 private TextWithLabelElement text_edition;
54 private TextWithLabelElement text_editor;
55 private TextWithLabelElement text_isbn;
56 private TextWithLabelElement text_issn;
57 private TextWithLabelElement text_organisation;
58 private TextWithLabelElement text_pages;
59 private TextWithLabelElement text_placePublished;
60 private TextWithLabelElement text_publisher;
61 private TextWithLabelElement text_referenceAbstract;
62 private TextWithLabelElement text_series;
63 private TextWithLabelElement text_seriesPart;
64 private TextWithLabelElement text_volume;
65 private TextWithLabelElement text_abbrevTitle;
66 private TextWithLabelElement text_title;
67 private UriWithLabelElement text_uri;
68 private EntitySelectionElement<AgentBase> selection_authorTeam;
69 private TimePeriodElement element_timePeriod;
70 private EntitySelectionElement<Reference> selection_inReference;
71 private EntitySelectionElement<Institution> selection_institution;
72 private EnumComboElement<ReferenceType> combo_referenceType;
73
74 private EntitySelectionElement<Reference> selection_inSeries;
75
76 /**
77 * <p>
78 * Constructor for ReferenceBaseDetailElement.
79 * </p>
80 *
81 * @param cdmFormFactory
82 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
83 * object.
84 * @param formElement
85 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
86 * object.
87 * @param style
88 * a int.
89 */
90 public ReferenceDetailElement(CdmFormFactory cdmFormFactory,
91 ICdmFormElement formElement, int style) {
92 super(cdmFormFactory, formElement);
93 }
94
95 /*
96 * (non-Javadoc)
97 *
98 * @see
99 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
100 * #createControl(org.eclipse.swt.widgets.Composite, int)
101 */
102 /** {@inheritDoc} */
103 @Override
104 protected void createControls(ICdmFormElement formElement,
105 Reference entity, int style) {
106 toggleable_cache = formFactory.createToggleableTextField(formElement,
107 "Reference Cache", entity.getTitleCache(),
108 entity.isProtectedTitleCache(), style);
109
110 toggleableAbbrevCache = formFactory.createToggleableTextField(formElement,
111 "Abbrev. Cache", entity.getAbbrevTitleCache(),
112 entity.isProtectedAbbrevTitleCache(), style);
113
114 combo_referenceType = formFactory.createEnumComboElement(ReferenceType.class,
115 formElement, style);
116
117 combo_referenceType.setSelection(entity.getType());
118
119 // title
120 text_title = formFactory.createTextWithLabelElement(formElement,
121 "Title", entity.getTitle(), style);
122 // abbreviated title
123 text_abbrevTitle = formFactory.createTextWithLabelElement(formElement,
124 "Abbrev. Title", entity.getAbbrevTitle(), style);
125
126 // author team
127 selection_authorTeam = formFactory
128 .createSelectionElement(AgentBase.class,
129 getConversationHolder(), formElement, "Author",
130 AuthorHelper.getAuthor(entity.getAuthorship()),
131 EntitySelectionElement.ALL, style);
132
133 ReferenceType referenceType = entity.getType();
134
135 if (referenceType != null) {
136
137 createInReferenceByType(this, referenceType, entity, SWT.NULL);
138
139 if (referenceType.equals(ReferenceType.Generic)) {
140 createGenericControls(this, entity, SWT.NULL);
141 createPublicationControls(this, referenceType, entity, SWT.NULL);
142 } else {
143 if (referenceType.isPrintedUnit()) {
144 createPrintedUnitControls(this, referenceType, entity,
145 SWT.NULL);
146 }
147 if (referenceType.isPublication()) {
148 createPublicationControls(this, referenceType, entity,
149 SWT.NULL);
150 }
151 if (referenceType.isVolumeReference()) {
152 createVolumeControls(this, entity, SWT.NULL);
153 }
154 if (referenceType.isSection()) {
155 createSectionControls(this, entity, SWT.NULL);
156 }
157 }
158 }
159
160 // date published
161 element_timePeriod = formFactory.createTimePeriodElement(formElement,
162 "Date Published", entity.getDatePublished(), style);
163
164 createUriAndAbstract(this, entity, SWT.NULL);
165 }
166
167 /*
168 * (non-Javadoc)
169 *
170 * @see
171 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
172 * #updateContent()
173 */
174 /** {@inheritDoc} */
175 @Override
176 protected void updateContent() {
177 if (getEntity() == null) {
178 setEntity(ReferenceFactory.newGeneric());
179 }
180
181 super.updateContent();
182
183 // enable elements according to state of cache protection
184 // setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new
185 // Object[]{text_cache}));
186 toggleable_cache.setEnabled(getEntity().isProtectedTitleCache());
187 toggleableAbbrevCache.setEnabled(getEntity().isProtectedAbbrevTitleCache());
188 setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[] { toggleable_cache }));
189 setIrrelevant(toggleableAbbrevCache.getState(), Arrays.asList(new Object[] { toggleableAbbrevCache}));
190 }
191
192 /**
193 * Creates an in reference for reference types that do hav this.
194 *
195 * @param element
196 * @param referenceType
197 * @param reference
198 * @param style
199 */
200 private void createInReferenceByType(ICdmFormElement element,
201 ReferenceType referenceType, Reference reference, int style) {
202 boolean createInReferenceWidget = true;
203 String label = "In Reference";
204
205 switch (referenceType) {
206 case Generic:
207 break;
208 case Section:
209 break;
210 case BookSection:
211 label = "Book";
212 break;
213 case InProceedings:
214 label = "Print Series";
215 break;
216 case Article:
217 label = "Journal";
218 break;
219 case WebPage:
220 break;
221 default:
222 createInReferenceWidget = false;
223 }
224
225 if (createInReferenceWidget) {
226 selection_inReference = formFactory
227 .createSelectionElement(Reference.class,
228 getConversationHolder(), element, label,
229 reference.getInReference(),
230 EntitySelectionElement.ALL, style);
231 }
232 }
233
234 /**
235 * Creates additional controls to edit a generic reference
236 *
237 * @param element
238 * @param reference
239 * @param listener
240 * @param style
241 */
242 private void createGenericControls(ICdmFormElement element,
243 Reference reference, int style) {
244
245 // editor
246 text_editor = formFactory.createTextWithLabelElement(element, "Editor",
247 reference.getEditor(), style);
248
249 // series
250 text_series = formFactory.createTextWithLabelElement(element, "Series",
251 reference.getSeriesPart(), style);
252
253 // volume
254 text_volume = formFactory.createTextWithLabelElement(element, "Volume",
255 reference.getVolume(), style);
256
257 // pages
258 text_pages = formFactory.createTextWithLabelElement(element, "Pages",
259 reference.getPages(), style);
260 }
261
262 /**
263 * Creates additional controls to edit a publication
264 *
265 * @param element
266 * @param reference
267 * @param listener
268 * @param style
269 */
270 private void createPublicationControls(ICdmFormElement element,
271 ReferenceType referenceType, Reference reference, int style) {
272
273 // placePublished
274 text_placePublished = formFactory.createTextWithLabelElement(element,
275 "Place Published", reference.getPlacePublished(), style);
276
277 // publisher
278 text_publisher = formFactory.createTextWithLabelElement(element,
279 "Publisher", reference.getPublisher(), style);
280
281 if (referenceType.equals(ReferenceType.Journal)) {
282 // issn
283 text_issn = formFactory.createTextWithLabelElement(element, "ISSN",
284 reference.getIssn(), style);
285 }
286
287 if (referenceType.equals(ReferenceType.Report)) {
288 // institution
289 selection_institution = formFactory
290 .createSelectionElement(Institution.class,
291 getConversationHolder(), element, "Institution",
292 reference.getInstitution(),
293 EntitySelectionElement.ALL, style);
294 }
295
296 if (referenceType.equals(ReferenceType.Thesis)) {
297 // school
298 selection_institution = formFactory
299 .createSelectionElement(Institution.class,
300 getConversationHolder(), element, "School",
301 reference.getSchool(),
302 EntitySelectionElement.ALL, style);
303 }
304
305 if (referenceType.equals(ReferenceType.Proceedings)) {
306 // organization
307 text_organisation = formFactory.createTextWithLabelElement(element,
308 "Organisation", reference.getOrganization(), style);
309 }
310 }
311
312 /**
313 * Creates additional controls to edit a section
314 *
315 * @param element
316 * @param reference
317 * @param style
318 */
319 private void createSectionControls(ICdmFormElement element,
320 Reference reference, int style) {
321 // pages
322 text_pages = formFactory.createTextWithLabelElement(element, "Pages",
323 reference.getPages(), style);
324 }
325
326 /**
327 * Creates additional controls to edit a volume
328 *
329 * @param element
330 * @param reference
331 * @param style
332 */
333 private void createVolumeControls(ICdmFormElement element,
334 Reference reference, int style) {
335
336 text_volume = formFactory.createTextWithLabelElement(element, "Volume",
337 reference.getVolume(), style);
338
339 text_series = formFactory.createTextWithLabelElement(element, "Series",
340 reference.getSeriesPart(), style);
341 }
342
343 /**
344 *
345 * @param element
346 * @param referenceType
347 * @param reference
348 * @param style
349 */
350 private void createPrintedUnitControls(ICdmFormElement element,
351 ReferenceType referenceType, Reference reference, int style) {
352 // in series
353 selection_inSeries = formFactory
354 .createSelectionElement(Reference.class,
355 getConversationHolder(), element, "In Series",
356 (Reference) reference.getInSeries(),
357 EntitySelectionElement.ALL, style);
358
359 // editor
360 text_editor = formFactory.createTextWithLabelElement(element, "Editor",
361 reference.getEditor(), style);
362
363 // series part
364 text_seriesPart = formFactory.createTextWithLabelElement(element,
365 "Series", reference.getSeriesPart(), style);
366
367 if (referenceType.equals(ReferenceType.Book)) {
368 // edition
369 text_edition = formFactory.createTextWithLabelElement(element,
370 "Edition", reference.getEdition(), style);
371
372 // isbn
373 text_isbn = formFactory.createTextWithLabelElement(element, "ISBN",
374 reference.getIsbn(), style);
375 }
376 }
377
378 private void createUriAndAbstract(ICdmFormElement element,
379 Reference reference, int style) {
380 // uri
381 text_uri = formFactory.createUriWithLabelElement(element, "URI",
382 reference.getUri(), style);
383 // abstract
384 text_referenceAbstract = formFactory.createMultiLineTextWithLabel(
385 element, "Reference Abstract", 100, style);
386 text_referenceAbstract.setText(reference.getReferenceAbstract());
387 }
388
389 /** {@inheritDoc} */
390 @Override
391 public void handleEvent(Object eventSource) {
392 if (eventSource == combo_referenceType) {
393 getEntity().setType(combo_referenceType.getSelection());
394 updateContent();
395 } else if (eventSource == element_timePeriod) {
396 getEntity().setDatePublished(element_timePeriod.getTimePeriod());
397 clearException();
398 }
399 // selections
400 else if (eventSource == selection_authorTeam) {
401 getEntity().setAuthorship(
402 (TeamOrPersonBase) selection_authorTeam.getSelection());
403 } else if (eventSource == selection_inReference) {
404 getEntity().setInReference(selection_inReference.getSelection());
405 } else if (eventSource == selection_inSeries) {
406 getEntity().setInReference(selection_inSeries.getSelection());
407 } else if (eventSource == selection_institution) {
408 getEntity().setInstitution(selection_institution.getSelection());
409 }
410
411 // textfields
412 else if (eventSource == toggleable_cache) {
413 handleToggleableCacheField();
414 }
415 else if (eventSource == toggleableAbbrevCache) {
416 handleToggleableAbbrevTitleField();
417 } else if (eventSource == text_edition) {
418 getEntity().setEdition(text_edition.getText());
419 } else if (eventSource == text_editor) {
420 getEntity().setEditor(text_editor.getText());
421 } else if (eventSource == text_isbn) {
422 getEntity().setIsbn(text_isbn.getText());
423 } else if (eventSource == text_issn) {
424 getEntity().setIssn(text_issn.getText());
425 } else if (eventSource == text_organisation) {
426 getEntity().setOrganization(text_organisation.getText());
427 } else if (eventSource == text_pages) {
428 getEntity().setPages(text_pages.getText());
429 } else if (eventSource == text_placePublished) {
430 getEntity().setPlacePublished(text_placePublished.getText());
431 } else if (eventSource == text_publisher) {
432 getEntity().setPublisher(text_publisher.getText());
433 } else if (eventSource == text_referenceAbstract) {
434 getEntity().setReferenceAbstract(text_referenceAbstract.getText());
435 } else if (eventSource == text_seriesPart) {
436 getEntity().setSeriesPart(text_seriesPart.getText());
437 } else if (eventSource == text_title) {
438 getEntity().setTitle(text_title.getText());
439 toggleable_cache.setText(text_title.getText());
440 } else if (eventSource == text_abbrevTitle) {
441 getEntity().setAbbrevTitle(text_abbrevTitle.getText());
442 toggleableAbbrevCache.setText(text_abbrevTitle.getText());
443 } else if (eventSource == text_uri) {
444 getEntity().setUri(text_uri.getUri());
445 } else if (eventSource == text_volume) {
446 getEntity().setVolume(text_volume.getText());
447 }
448
449 if (eventSource != text_title && eventSource != text_abbrevTitle) {
450 // set abbrev title cache to null to force new generation
451 // for the standard title cache this is done by a PropertyChangeListener registered
452 // in IdentifiableEntity.initListeners()
453 if(!getEntity().isProtectedAbbrevTitleCache()){
454 getEntity().setAbbrevTitleCache(null, getEntity().isProtectedAbbrevTitleCache());
455 }
456 toggleable_cache.setText(getEntity().getTitleCache());
457 toggleableAbbrevCache.setText(getEntity().getAbbrevTitleCache());
458 }
459
460 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
461 }
462
463
464 @Override
465 public void updateToggleableCacheField() {
466 if (!getEntity().isProtectedTitleCache()) {
467 toggleable_cache.setText(getEntity().generateTitle());
468 }
469 if (!getEntity().isProtectedAbbrevTitleCache()) {
470 toggleableAbbrevCache.setText(getEntity().generateAbbrevTitle());
471 }
472 }
473
474 protected void handleToggleableAbbrevTitleField() {
475 boolean pushedAbbrevState = toggleableAbbrevCache.getState();
476 getEntity().setAbbrevTitleCache(toggleableAbbrevCache.getText(), pushedAbbrevState);
477 setIrrelevant(pushedAbbrevState, Arrays.asList(new Object[] { toggleableAbbrevCache, toggleable_cache, text_title }));
478 updateToggleableCacheField();
479 }
480
481 /*
482 * (non-Javadoc)
483 *
484 * @see
485 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException
486 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
487 */
488 /** {@inheritDoc} */
489 @Override
490 public void handleException(CdmPropertyChangeEvent event) {
491 exceptionOccurred(event);
492 }
493
494 /*
495 * (non-Javadoc)
496 *
497 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
498 */
499 /** {@inheritDoc} */
500 @Override
501 public void clearException() {
502 exceptionOccurred(null);
503 }
504
505 private final List<IExceptionHandler> exceptionHandlers = new ArrayList<IExceptionHandler>();
506
507 /*
508 * (non-Javadoc)
509 *
510 * @see
511 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler
512 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
513 */
514 /** {@inheritDoc} */
515 @Override
516 public void addExceptionHandler(IExceptionHandler handler) {
517 exceptionHandlers.add(handler);
518 }
519
520 /*
521 * (non-Javadoc)
522 *
523 * @see
524 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler
525 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
526 */
527 /** {@inheritDoc} */
528 @Override
529 public void removeExceptionHandler(IExceptionHandler handler) {
530 exceptionHandlers.remove(handler);
531 }
532
533 /*
534 * (non-Javadoc)
535 *
536 * @see
537 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred
538 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
539 */
540 /** {@inheritDoc} */
541 @Override
542 public void exceptionOccurred(CdmPropertyChangeEvent event) {
543 for (IExceptionHandler handler : exceptionHandlers) {
544 if (event == null) {
545 handler.clearException();
546 } else {
547 handler.handleException(event);
548 }
549 }
550 }
551 }