b18ad686f51b9dc9ae63b57ada92ad4aee450ce3
[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 handleToggleableAbbrevTitleField();
166 handleToggleableCacheField();
167 }
168
169 /*
170 * (non-Javadoc)
171 *
172 * @see
173 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
174 * #updateContent()
175 */
176 /** {@inheritDoc} */
177 @Override
178 protected void updateContent() {
179 if (getEntity() == null) {
180 setEntity(ReferenceFactory.newGeneric());
181 }
182
183 super.updateContent();
184
185 // enable elements according to state of cache protection
186 // setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new
187 // Object[]{text_cache}));
188 toggleable_cache.setEnabled(getEntity().isProtectedTitleCache());
189 toggleableAbbrevCache.setEnabled(getEntity().isProtectedAbbrevTitleCache());
190 setIrrelevantReferenceDetail(false);
191 setIrrelevantReferenceDetail(true);
192 }
193
194 /**
195 * Creates an in reference for reference types that do hav this.
196 *
197 * @param element
198 * @param referenceType
199 * @param reference
200 * @param style
201 */
202 private void createInReferenceByType(ICdmFormElement element,
203 ReferenceType referenceType, Reference reference, int style) {
204 boolean createInReferenceWidget = true;
205 String label = "In Reference";
206
207 switch (referenceType) {
208 case Generic:
209 break;
210 case Section:
211 break;
212 case BookSection:
213 label = "Book";
214 break;
215 case InProceedings:
216 label = "Print Series";
217 break;
218 case Article:
219 label = "Journal";
220 break;
221 case WebPage:
222 break;
223 default:
224 createInReferenceWidget = false;
225 }
226
227 if (createInReferenceWidget) {
228 selection_inReference = formFactory
229 .createSelectionElement(Reference.class,
230 getConversationHolder(), element, label,
231 reference.getInReference(),
232 EntitySelectionElement.ALL, style);
233 }
234 }
235
236 /**
237 * Creates additional controls to edit a generic reference
238 *
239 * @param element
240 * @param reference
241 * @param listener
242 * @param style
243 */
244 private void createGenericControls(ICdmFormElement element,
245 Reference reference, int style) {
246
247 // editor
248 text_editor = formFactory.createTextWithLabelElement(element, "Editor",
249 reference.getEditor(), style);
250
251 // series
252 text_series = formFactory.createTextWithLabelElement(element, "Series",
253 reference.getSeriesPart(), style);
254
255 // volume
256 text_volume = formFactory.createTextWithLabelElement(element, "Volume",
257 reference.getVolume(), style);
258
259 // pages
260 text_pages = formFactory.createTextWithLabelElement(element, "Pages",
261 reference.getPages(), style);
262 }
263
264 /**
265 * Creates additional controls to edit a publication
266 *
267 * @param element
268 * @param reference
269 * @param listener
270 * @param style
271 */
272 private void createPublicationControls(ICdmFormElement element,
273 ReferenceType referenceType, Reference reference, int style) {
274
275 // placePublished
276 text_placePublished = formFactory.createTextWithLabelElement(element,
277 "Place Published", reference.getPlacePublished(), style);
278
279 // publisher
280 text_publisher = formFactory.createTextWithLabelElement(element,
281 "Publisher", reference.getPublisher(), style);
282
283 if (referenceType.equals(ReferenceType.Journal)) {
284 // issn
285 text_issn = formFactory.createTextWithLabelElement(element, "ISSN",
286 reference.getIssn(), style);
287 }
288
289 if (referenceType.equals(ReferenceType.Report)) {
290 // institution
291 selection_institution = formFactory
292 .createSelectionElement(Institution.class,
293 getConversationHolder(), element, "Institution",
294 reference.getInstitution(),
295 EntitySelectionElement.ALL, style);
296 }
297
298 if (referenceType.equals(ReferenceType.Thesis)) {
299 // school
300 selection_institution = formFactory
301 .createSelectionElement(Institution.class,
302 getConversationHolder(), element, "School",
303 reference.getSchool(),
304 EntitySelectionElement.ALL, style);
305 }
306
307 if (referenceType.equals(ReferenceType.Proceedings)) {
308 // organization
309 text_organisation = formFactory.createTextWithLabelElement(element,
310 "Organisation", reference.getOrganization(), style);
311 }
312 }
313
314 /**
315 * Creates additional controls to edit a section
316 *
317 * @param element
318 * @param reference
319 * @param style
320 */
321 private void createSectionControls(ICdmFormElement element,
322 Reference reference, int style) {
323 // pages
324 text_pages = formFactory.createTextWithLabelElement(element, "Pages",
325 reference.getPages(), style);
326 }
327
328 /**
329 * Creates additional controls to edit a volume
330 *
331 * @param element
332 * @param reference
333 * @param style
334 */
335 private void createVolumeControls(ICdmFormElement element,
336 Reference reference, int style) {
337
338 text_volume = formFactory.createTextWithLabelElement(element, "Volume",
339 reference.getVolume(), style);
340
341 text_series = formFactory.createTextWithLabelElement(element, "Series",
342 reference.getSeriesPart(), style);
343 }
344
345 /**
346 *
347 * @param element
348 * @param referenceType
349 * @param reference
350 * @param style
351 */
352 private void createPrintedUnitControls(ICdmFormElement element,
353 ReferenceType referenceType, Reference reference, int style) {
354 // in series
355 selection_inSeries = formFactory
356 .createSelectionElement(Reference.class,
357 getConversationHolder(), element, "In Series",
358 (Reference) reference.getInSeries(),
359 EntitySelectionElement.ALL, style);
360
361 // editor
362 text_editor = formFactory.createTextWithLabelElement(element, "Editor",
363 reference.getEditor(), style);
364
365 /* series part
366 text_seriesPart = formFactory.createTextWithLabelElement(element,
367 "Series", reference.getSeriesPart(), style);
368 */
369 if (referenceType.equals(ReferenceType.Book)) {
370 // edition
371 text_edition = formFactory.createTextWithLabelElement(element,
372 "Edition", reference.getEdition(), style);
373
374 // isbn
375 text_isbn = formFactory.createTextWithLabelElement(element, "ISBN",
376 reference.getIsbn(), style);
377 }
378 }
379
380 private void createUriAndAbstract(ICdmFormElement element,
381 Reference reference, int style) {
382 // uri
383 text_uri = formFactory.createUriWithLabelElement(element, "URI",
384 reference.getUri(), style);
385 // abstract
386 text_referenceAbstract = formFactory.createMultiLineTextWithLabel(
387 element, "Reference Abstract", 100, style);
388 text_referenceAbstract.setText(reference.getReferenceAbstract());
389 }
390
391 /** {@inheritDoc} */
392 @Override
393 public void handleEvent(Object eventSource) {
394 if (eventSource == combo_referenceType) {
395 getEntity().setType(combo_referenceType.getSelection());
396 updateContent();
397 } else if (eventSource == element_timePeriod) {
398 getEntity().setDatePublished(element_timePeriod.getTimePeriod());
399 clearException();
400 }
401 // selections
402 else if (eventSource == selection_authorTeam) {
403 getEntity().setAuthorship(
404 (TeamOrPersonBase) selection_authorTeam.getSelection());
405 } else if (eventSource == selection_inReference) {
406 getEntity().setInReference(selection_inReference.getSelection());
407 } else if (eventSource == selection_inSeries) {
408 getEntity().setInReference(selection_inSeries.getSelection());
409 } else if (eventSource == selection_institution) {
410 getEntity().setInstitution(selection_institution.getSelection());
411 }
412
413 // textfields
414 else if (eventSource == toggleable_cache) {
415 handleToggleableCacheField();
416 }
417 else if (eventSource == toggleableAbbrevCache) {
418 handleToggleableAbbrevTitleField();
419 } else if (eventSource == text_edition) {
420 getEntity().setEdition(text_edition.getText());
421 } else if (eventSource == text_editor) {
422 getEntity().setEditor(text_editor.getText());
423 } else if (eventSource == text_isbn) {
424 getEntity().setIsbn(text_isbn.getText());
425 } else if (eventSource == text_issn) {
426 getEntity().setIssn(text_issn.getText());
427 } else if (eventSource == text_organisation) {
428 getEntity().setOrganization(text_organisation.getText());
429 } else if (eventSource == text_pages) {
430 getEntity().setPages(text_pages.getText());
431 } else if (eventSource == text_placePublished) {
432 getEntity().setPlacePublished(text_placePublished.getText());
433 } else if (eventSource == text_publisher) {
434 getEntity().setPublisher(text_publisher.getText());
435 } else if (eventSource == text_referenceAbstract) {
436 getEntity().setReferenceAbstract(text_referenceAbstract.getText());
437 } else if (eventSource == text_series) {
438 getEntity().setSeriesPart(text_series.getText());
439 } else if (eventSource == text_title) {
440 getEntity().setTitle(text_title.getText());
441 toggleable_cache.setText(text_title.getText());
442 } else if (eventSource == text_abbrevTitle) {
443 getEntity().setAbbrevTitle(text_abbrevTitle.getText());
444 toggleableAbbrevCache.setText(text_abbrevTitle.getText());
445 } else if (eventSource == text_uri) {
446 getEntity().setUri(text_uri.parseText());
447 } else if (eventSource == text_volume) {
448 getEntity().setVolume(text_volume.getText());
449 }
450
451 if (eventSource != text_title && eventSource != text_abbrevTitle) {
452 // set abbrev title cache to null to force new generation
453 // for the standard title cache this is done by a PropertyChangeListener registered
454 // in IdentifiableEntity.initListeners()
455 if(!getEntity().isProtectedAbbrevTitleCache()){
456 getEntity().setAbbrevTitleCache(null, getEntity().isProtectedAbbrevTitleCache());
457 }
458 toggleable_cache.setText(getEntity().getTitleCache());
459 toggleableAbbrevCache.setText(getEntity().getAbbrevTitleCache());
460 }
461
462 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
463 }
464
465
466 @Override
467 public void updateToggleableCacheField() {
468 if (!getEntity().isProtectedTitleCache()) {
469 toggleable_cache.setText(getEntity().generateTitle());
470 }
471 if (!getEntity().isProtectedAbbrevTitleCache()) {
472 toggleableAbbrevCache.setText(getEntity().generateAbbrevTitle());
473 }
474 }
475
476 protected void handleToggleableAbbrevTitleField() {
477 boolean pushedAbbrevState = toggleableAbbrevCache.getState();
478 getEntity().setAbbrevTitleCache(toggleableAbbrevCache.getText(), pushedAbbrevState);
479 setIrrelevantReferenceDetail(true);
480 updateToggleableCacheField();
481 }
482
483 /*
484 * (non-Javadoc)
485 *
486 * @see
487 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException
488 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
489 */
490 /** {@inheritDoc} */
491 @Override
492 public void handleException(CdmPropertyChangeEvent event) {
493 exceptionOccurred(event);
494 }
495
496 /*
497 * (non-Javadoc)
498 *
499 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
500 */
501 /** {@inheritDoc} */
502 @Override
503 public void clearException() {
504 exceptionOccurred(null);
505 }
506
507 private final List<IExceptionHandler> exceptionHandlers = new ArrayList<IExceptionHandler>();
508
509 /*
510 * (non-Javadoc)
511 *
512 * @see
513 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler
514 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
515 */
516 /** {@inheritDoc} */
517 @Override
518 public void addExceptionHandler(IExceptionHandler handler) {
519 exceptionHandlers.add(handler);
520 }
521
522 /*
523 * (non-Javadoc)
524 *
525 * @see
526 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler
527 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
528 */
529 /** {@inheritDoc} */
530 @Override
531 public void removeExceptionHandler(IExceptionHandler handler) {
532 exceptionHandlers.remove(handler);
533 }
534
535 /*
536 * (non-Javadoc)
537 *
538 * @see
539 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred
540 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
541 */
542 /** {@inheritDoc} */
543 @Override
544 public void exceptionOccurred(CdmPropertyChangeEvent event) {
545 for (IExceptionHandler handler : exceptionHandlers) {
546 if (event == null) {
547 handler.clearException();
548 } else {
549 handler.handleException(event);
550 }
551 }
552 }
553
554 @Override
555 protected void handleToggleableCacheField() {
556
557 boolean pushedState = toggleable_cache.getState();
558 getEntity().setTitleCache(toggleable_cache.getText(), pushedState);
559 setIrrelevantReferenceDetail(false);
560 updateToggleableCacheField();
561 }
562
563 protected void setIrrelevantReferenceDetail(boolean abbrev){
564
565 List<Object> except = new ArrayList();
566 except.addAll( Arrays.asList(new Object[] { toggleable_cache, toggleableAbbrevCache, text_editor, text_isbn, text_issn, text_organisation, text_pages, text_placePublished, text_publisher, text_referenceAbstract, text_uri, selection_institution}));
567 boolean pushedState;
568 if (abbrev){
569 except.add(text_title);
570 pushedState = toggleableAbbrevCache.getState();
571 } else{
572 except.add(text_abbrevTitle);
573 pushedState = toggleable_cache.getState();
574 }
575 switch( getEntity().getType()){
576 case Journal:
577 except.add(element_timePeriod);
578 break;
579 case Book:
580 except.remove(text_series);
581 except.remove(text_edition);
582 break;
583 }
584 setIrrelevant(pushedState, except);
585 }
586 }