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