Project

General

Profile

Download (19.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.taxeditor.ui.section.reference;
11

    
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.joda.time.DateTime;
20

    
21
import eu.etaxonomy.cdm.model.agent.AgentBase;
22
import eu.etaxonomy.cdm.model.agent.Institution;
23
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
24
import eu.etaxonomy.cdm.model.reference.Reference;
25
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
26
import eu.etaxonomy.cdm.model.reference.ReferenceType;
27
import eu.etaxonomy.taxeditor.store.StoreUtil;
28
import eu.etaxonomy.taxeditor.ui.combo.EnumComboElement;
29
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
30
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
31
import eu.etaxonomy.taxeditor.ui.element.DoiWithLabelElement;
32
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
33
import eu.etaxonomy.taxeditor.ui.element.IErrorIntolerableElement;
34
import eu.etaxonomy.taxeditor.ui.element.IExceptionHandler;
35
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
36
import eu.etaxonomy.taxeditor.ui.element.ToggleableTextElement;
37
import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
38
import eu.etaxonomy.taxeditor.ui.element.VerbatimTimePeriodElement;
39
import eu.etaxonomy.taxeditor.ui.mvc.element.DateElement;
40
import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
41
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
42

    
43
/**
44
 * <p>
45
 * ReferenceBaseDetailElement class.
46
 * </p>
47
 *
48
 * @author n.hoffmann
49
 * @created Mar 2, 2010
50
 */
51
public class ReferenceDetailElement extends AbstractIdentifiableEntityDetailElement<Reference>
52
		implements IErrorIntolerableElement, IExceptionHandler {
53

    
54

    
55

    
56
    private ToggleableTextElement toggleableAbbrevCache;
57

    
58
	private TextWithLabelElement text_edition;
59
	private TextWithLabelElement text_editor;
60
	private TextWithLabelElement text_isbn;
61
	private TextWithLabelElement text_issn;
62
	private TextWithLabelElement text_organisation;
63
	private TextWithLabelElement text_pages;
64
	private TextWithLabelElement text_placePublished;
65
	private TextWithLabelElement text_publisher;
66
	private TextWithLabelElement text_referenceAbstract;
67
	private TextWithLabelElement text_series;
68
//	private TextWithLabelElement text_seriesPart;
69
	private TextWithLabelElement text_volume;
70
	private TextWithLabelElement text_abbrevTitle;
71
	private TextWithLabelElement text_title;
72
	private DoiWithLabelElement text_doi;
73
	private UriWithLabelElement text_uri;
74
	private DateElement text_accessed;
75
	private EntitySelectionElement<AgentBase> selection_authorTeam;
76
	private VerbatimTimePeriodElement element_timePeriod;
77
	private EntitySelectionElement<Reference> selection_inReference;
78
	private EntitySelectionElement<Institution> selection_institution;
79
	private EnumComboElement<ReferenceType> combo_referenceType;
80

    
81
	private EntitySelectionElement<Reference> selection_inSeries;
82
	private boolean isWizard;
83

    
84
	/**
85
	 * <p>
86
	 * Constructor for ReferenceBaseDetailElement.
87
	 * </p>
88
	 *
89
	 * @param cdmFormFactory
90
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
91
	 *            object.
92
	 * @param formElement
93
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
94
	 *            object.
95
	 * @param style
96
	 *            a int.
97
	 */
98
	public ReferenceDetailElement(CdmFormFactory cdmFormFactory,
99
			ICdmFormElement formElement, int style, boolean isWizard) {
100
		super(cdmFormFactory, formElement);
101
		this.isWizard = isWizard;
102
	}
103

    
104

    
105
	/** {@inheritDoc} */
106
	@Override
107
	protected void createControls(ICdmFormElement formElement,
108
			Reference entity, int style) {
109
		setWarnForReferencingObjects(formElement);
110
		toggleable_cache = formFactory.createToggleableTextField(formElement,
111
				"Reference Cache", entity.getTitleCache(),
112
				entity.isProtectedTitleCache(), style);
113

    
114
		toggleableAbbrevCache = formFactory.createToggleableTextField(formElement,
115
		        "Nomencl. Cache", entity.getAbbrevTitleCache(),
116
		        entity.isProtectedAbbrevTitleCache(), style);
117

    
118
		combo_referenceType = formFactory.createEnumComboElement(ReferenceType.class,
119
						formElement, style);
120

    
121
		combo_referenceType.setSelection(entity.getType());
122

    
123
		// title
124
		text_title = formFactory.createTextWithLabelElement(formElement,
125
				"Title", entity.getTitle(), style);
126
		// abbreviated title
127
		text_abbrevTitle = formFactory.createTextWithLabelElement(formElement,
128
		        "Nomencl. Title", entity.getAbbrevTitle(), style);
129

    
130
		// author team
131
		selection_authorTeam = formFactory
132
				.createSelectionElement(AgentBase.class,//getConversationHolder(),
133
				        formElement, "Author",
134
						entity.getAuthorship(),
135
						EntitySelectionElement.ALL, style);
136

    
137
		ReferenceType referenceType = entity.getType();
138

    
139
		if (referenceType != null) {
140

    
141
			createInReferenceByType(this, referenceType, entity, SWT.NULL);
142

    
143
			if (referenceType.equals(ReferenceType.Generic)) {
144
				createGenericControls(this, entity, SWT.NULL);
145
				createPublicationControls(this, referenceType, entity, SWT.NULL);
146

    
147
			} else {
148
				if (referenceType.isPrintedUnit()) {
149
					createPrintedUnitControls(this, referenceType, entity,
150
							SWT.NULL);
151
				}
152
				if (referenceType.isPublication()) {
153
					createPublicationControls(this, referenceType, entity,
154
							SWT.NULL);
155
				}
156
				if (referenceType.isVolumeReference()) {
157
					createVolumeControls(this, entity, SWT.NULL);
158
				}
159
				if (referenceType.isSection()) {
160
					createSectionControls(this, entity, SWT.NULL);
161
				}
162

    
163
			}
164
		}
165

    
166
		// date published
167
		element_timePeriod = formFactory.createVerbatimTimePeriodElement(formElement,
168
				"Date Published", entity.getDatePublished(), style);
169
		text_doi = formFactory.createDoiWithLabelElement(formElement, "DOI", entity.getDoi(), style);
170
		createUriAndAbstract(this, entity, SWT.NULL);
171

    
172
		handleToggleableAbbrevTitleField();
173
		handleToggleableCacheField();
174
	}
175

    
176
	private void createWebPageControls(ReferenceDetailElement referenceDetailElement, Reference reference, int style) {
177
		DateTime accessed = reference.getAccessed();
178

    
179
		 text_accessed = formFactory.createDateElement(referenceDetailElement, "Accessed", accessed, style, false);
180

    
181
	}
182

    
183
	/*
184
	 * (non-Javadoc)
185
	 *
186
	 * @see
187
	 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
188
	 * #updateContent()
189
	 */
190
	/** {@inheritDoc} */
191
	@Override
192
	protected void updateContent() {
193
		if (getEntity() == null) {
194
			setEntity(ReferenceFactory.newGeneric());
195
		}
196

    
197
		super.updateContent();
198

    
199
		// enable elements according to state of cache protection
200
		// setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new
201
		// Object[]{text_cache}));
202
		toggleable_cache.setEnabled(getEntity().isProtectedTitleCache());
203
		toggleableAbbrevCache.setEnabled(getEntity().isProtectedAbbrevTitleCache());
204
		setIrrelevantReferenceDetail(false);
205
		setIrrelevantReferenceDetail(true);
206
	}
207

    
208
	/**
209
	 * Creates an in reference for reference types that do hav this.
210
	 *
211
	 * @param element
212
	 * @param referenceType
213
	 * @param reference
214
	 * @param style
215
	 */
216
	private void createInReferenceByType(ICdmFormElement element,
217
			ReferenceType referenceType, Reference reference, int style) {
218
		boolean createInReferenceWidget = true;
219
		String label = "In Reference";
220

    
221
		switch (referenceType) {
222
		case Generic:
223
			break;
224
		case Section:
225
		    break;
226
		case BookSection:
227
			label = "Book";
228
			break;
229
		case InProceedings:
230
			label = "Print Series";
231
			break;
232
		case Article:
233
			label = "Journal";
234
			break;
235
		case WebPage:
236
			break;
237
		default:
238
			createInReferenceWidget = false;
239
		}
240

    
241
		if (createInReferenceWidget) {
242
			selection_inReference = formFactory
243
					.createSelectionElement(Reference.class,//getConversationHolder(),
244
					        element, label,
245
							reference.getInReference(),
246
							EntitySelectionElement.ALL, style);
247
		}
248
	}
249

    
250
	/**
251
	 * Creates additional controls to edit a generic reference
252
	 *
253
	 * @param element
254
	 * @param reference
255
	 * @param listener
256
	 * @param style
257
	 */
258
	private void createGenericControls(ICdmFormElement element,
259
			Reference reference, int style) {
260

    
261
		// editor
262
		text_editor = formFactory.createTextWithLabelElement(element, "Editor",
263
				reference.getEditor(), style);
264

    
265
		// series
266
		text_series = formFactory.createTextWithLabelElement(element, "Series",
267
				reference.getSeriesPart(), style);
268

    
269
		// volume
270
		text_volume = formFactory.createTextWithLabelElement(element, "Volume",
271
				reference.getVolume(), style);
272

    
273
		// pages
274
		text_pages = formFactory.createTextWithLabelElement(element, "Pages",
275
				reference.getPages(), style);
276

    
277

    
278
	}
279

    
280
	/**
281
	 * Creates additional controls to edit a publication
282
	 *
283
	 * @param element
284
	 * @param reference
285
	 * @param listener
286
	 * @param style
287
	 */
288
	private void createPublicationControls(ICdmFormElement element,
289
			ReferenceType referenceType, Reference reference, int style) {
290

    
291
		// placePublished
292
		text_placePublished = formFactory.createTextWithLabelElement(element,
293
				"Place Published", reference.getPlacePublished(), style);
294

    
295
		// publisher
296
		text_publisher = formFactory.createTextWithLabelElement(element,
297
				"Publisher", reference.getPublisher(), style);
298

    
299
		if (referenceType.equals(ReferenceType.Journal)) {
300
			// issn
301
			text_issn = formFactory.createTextWithLabelElement(element, "ISSN",
302
					reference.getIssn(), style);
303
		}
304

    
305
		if (referenceType.equals(ReferenceType.Report)) {
306
			// institution
307
			selection_institution = formFactory
308
					.createSelectionElement(Institution.class,//getConversationHolder(),
309
					        element, "Institution",
310
							reference.getInstitution(),
311
							EntitySelectionElement.ALL, style);
312
		}
313

    
314
		if (referenceType.equals(ReferenceType.Thesis)) {
315
			// school
316
			selection_institution = formFactory
317
					.createSelectionElement(Institution.class,//getConversationHolder(),
318
					        element, "School",
319
							reference.getSchool(),
320
							EntitySelectionElement.ALL, style);
321
		}
322

    
323
		if (referenceType.equals(ReferenceType.Proceedings)) {
324
			// organization
325
			text_organisation = formFactory.createTextWithLabelElement(element,
326
					"Organisation", reference.getOrganization(), style);
327
		}
328
	}
329

    
330
	/**
331
	 * Creates additional controls to edit a section
332
	 *
333
	 * @param element
334
	 * @param reference
335
	 * @param style
336
	 */
337
	private void createSectionControls(ICdmFormElement element,
338
			Reference reference, int style) {
339
		// pages
340
		text_pages = formFactory.createTextWithLabelElement(element, "Pages",
341
				reference.getPages(), style);
342
	}
343

    
344
	/**
345
	 * Creates additional controls to edit a volume
346
	 *
347
	 * @param element
348
	 * @param reference
349
	 * @param style
350
	 */
351
	private void createVolumeControls(ICdmFormElement element,
352
			Reference reference, int style) {
353

    
354
		text_volume = formFactory.createTextWithLabelElement(element, "Volume",
355
				reference.getVolume(), style);
356

    
357
		text_series = formFactory.createTextWithLabelElement(element, "Series",
358
				reference.getSeriesPart(), style);
359
	}
360

    
361
	/**
362
	 *
363
	 * @param element
364
	 * @param referenceType
365
	 * @param reference
366
	 * @param style
367
	 */
368
	private void createPrintedUnitControls(ICdmFormElement element,
369
			ReferenceType referenceType, Reference reference, int style) {
370
		// in series
371
		selection_inSeries = formFactory
372
				.createSelectionElement(Reference.class,//getConversationHolder(),
373
				        element, "In Series",
374
						(Reference) reference.getInSeries(),
375
						EntitySelectionElement.ALL, style);
376

    
377
		// editor
378
		text_editor = formFactory.createTextWithLabelElement(element, "Editor",
379
				reference.getEditor(), style);
380

    
381
		/* series part
382
		text_seriesPart = formFactory.createTextWithLabelElement(element,
383
				"Series", reference.getSeriesPart(), style);
384
*/
385
		if (referenceType.equals(ReferenceType.Book)) {
386
			// edition
387
			text_edition = formFactory.createTextWithLabelElement(element,
388
					"Edition", reference.getEdition(), style);
389

    
390
			// isbn
391
			text_isbn = formFactory.createTextWithLabelElement(element, "ISBN",
392
					reference.getIsbn(), style);
393
		}
394
	}
395

    
396
	private void createUriAndAbstract(ICdmFormElement element,
397
			Reference reference, int style) {
398
		// uri
399
		text_uri = formFactory.createUriWithLabelElement(element, "URI",
400
				reference.getUri(), style);
401
		if (reference.getType().equals(ReferenceType.WebPage)){
402
			createWebPageControls(this, reference, style);
403
		}
404
		// abstract
405
		text_referenceAbstract = formFactory.createMultiLineTextWithLabel(
406
				element, "Reference Abstract", 100, style);
407
		text_referenceAbstract.setText(reference.getReferenceAbstract());
408
	}
409

    
410
	/** {@inheritDoc} */
411
	@Override
412
	public void handleEvent(Object eventSource) {
413
		if (eventSource == combo_referenceType) {
414
			getEntity().setType(combo_referenceType.getSelection());
415
			updateContent();
416
	        //this is needed for the "new" wizards in order to redraw correctly
417
			StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
418
		} else if (eventSource == element_timePeriod) {
419
			getEntity().setDatePublished(element_timePeriod.getTimePeriod());
420
			clearException();
421
		}
422
		// selections
423
		else if (eventSource == selection_authorTeam) {
424
			getEntity().setAuthorship(
425
					(TeamOrPersonBase) selection_authorTeam.getEntity());
426
		} else if (eventSource == selection_inReference) {
427
			getEntity().setInReference(selection_inReference.getSelection());
428
		} else if (eventSource == selection_inSeries) {
429
			getEntity().setInReference(selection_inSeries.getSelection());
430
		} else if (eventSource == selection_institution) {
431
			getEntity().setInstitution(selection_institution.getSelection());
432
		}
433

    
434
		// textfields
435
		else if (eventSource == toggleable_cache) {
436
			handleToggleableCacheField();
437
		}
438
		else if (eventSource == toggleableAbbrevCache) {
439
		    handleToggleableAbbrevTitleField();
440
		} else if (eventSource == text_edition) {
441
			getEntity().setEdition(text_edition.getText());
442
		} else if (eventSource == text_editor) {
443
			getEntity().setEditor(text_editor.getText());
444
		} else if (eventSource == text_isbn) {
445
			getEntity().setIsbn(text_isbn.getText());
446
		} else if (eventSource == text_issn) {
447
			getEntity().setIssn(text_issn.getText());
448
		} else if (eventSource == text_organisation) {
449
			getEntity().setOrganization(text_organisation.getText());
450
		} else if (eventSource == text_pages) {
451
			getEntity().setPages(text_pages.getText());
452
		} else if (eventSource == text_placePublished) {
453
			getEntity().setPlacePublished(text_placePublished.getText());
454
		} else if (eventSource == text_publisher) {
455
			getEntity().setPublisher(text_publisher.getText());
456
		} else if (eventSource == text_referenceAbstract) {
457
			getEntity().setReferenceAbstract(text_referenceAbstract.getText());
458
		} else if (eventSource == text_series) {
459
			getEntity().setSeriesPart(text_series.getText());
460
		} else if (eventSource == text_title ) {
461
			getEntity().setTitle(text_title.getText());
462
			if (!toggleable_cache.isEnabled()){
463
			    toggleable_cache.setText(getEntity().generateTitle());
464
			}
465
		} else if (eventSource == text_abbrevTitle ) {
466
		    getEntity().setAbbrevTitle(text_abbrevTitle.getText());
467
		    if (!toggleableAbbrevCache.isEnabled()){
468
		        toggleableAbbrevCache.setText(getEntity().generateAbbrevTitle());
469
		    }
470
		} else if (eventSource == text_uri) {
471
			getEntity().setUri(text_uri.parseText());
472
		} else if (eventSource == text_volume) {
473
			getEntity().setVolume(text_volume.getText());
474
		}else if (eventSource == text_doi) {
475
            getEntity().setDoi(text_doi.parseText());
476
        }else if (eventSource == text_accessed.getController()) {
477
            getEntity().setAccessed(text_accessed.getController().getDateTime());
478
        }
479

    
480
		if (eventSource != text_title && eventSource != text_abbrevTitle) {
481
		    // set abbrev title cache to null to force new generation
482
		    // for the standard title cache this is done by a PropertyChangeListener registered
483
		    // in IdentifiableEntity.initListeners()
484
		    if(!getEntity().isProtectedAbbrevTitleCache()){
485
		        getEntity().setAbbrevTitleCache(null, getEntity().isProtectedAbbrevTitleCache());
486
		    }
487
		    if(!getEntity().isProtectedTitleCache()){
488
                getEntity().setTitleCache(null, getEntity().isProtectedTitleCache());
489
            }
490

    
491
			toggleable_cache.setText(getEntity().getTitleCache());
492
			toggleableAbbrevCache.setText(getEntity().getAbbrevTitleCache());
493
		}
494

    
495
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
496
	}
497

    
498

    
499
    @Override
500
    public void updateToggleableCacheField() {
501
        if (!getEntity().isProtectedTitleCache()) {
502
            toggleable_cache.setText(getEntity().generateTitle());
503
        }
504
        if (!getEntity().isProtectedAbbrevTitleCache()) {
505
            toggleableAbbrevCache.setText(getEntity().generateAbbrevTitle());
506
        }
507
    }
508

    
509
    protected void handleToggleableAbbrevTitleField() {
510
        boolean pushedAbbrevState = toggleableAbbrevCache.getState();
511
        getEntity().setAbbrevTitleCache(toggleableAbbrevCache.getText(), pushedAbbrevState);
512
        setIrrelevantReferenceDetail(true);
513
        updateToggleableCacheField();
514
    }
515

    
516
	/*
517
	 * (non-Javadoc)
518
	 *
519
	 * @see
520
	 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException
521
	 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
522
	 */
523
	/** {@inheritDoc} */
524
	@Override
525
	public void handleException(CdmPropertyChangeEvent event) {
526
		exceptionOccurred(event);
527
	}
528

    
529
	/*
530
	 * (non-Javadoc)
531
	 *
532
	 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
533
	 */
534
	/** {@inheritDoc} */
535
	@Override
536
	public void clearException() {
537
		exceptionOccurred(null);
538
	}
539

    
540
	private final List<IExceptionHandler> exceptionHandlers = new ArrayList<IExceptionHandler>();
541

    
542
	/*
543
	 * (non-Javadoc)
544
	 *
545
	 * @see
546
	 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler
547
	 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
548
	 */
549
	/** {@inheritDoc} */
550
	@Override
551
	public void addExceptionHandler(IExceptionHandler handler) {
552
		exceptionHandlers.add(handler);
553
	}
554

    
555
	/*
556
	 * (non-Javadoc)
557
	 *
558
	 * @see
559
	 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler
560
	 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
561
	 */
562
	/** {@inheritDoc} */
563
	@Override
564
	public void removeExceptionHandler(IExceptionHandler handler) {
565
		exceptionHandlers.remove(handler);
566
	}
567

    
568
	/*
569
	 * (non-Javadoc)
570
	 *
571
	 * @see
572
	 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred
573
	 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
574
	 */
575
	/** {@inheritDoc} */
576
	@Override
577
	public void exceptionOccurred(CdmPropertyChangeEvent event) {
578
		for (IExceptionHandler handler : exceptionHandlers) {
579
			if (event == null) {
580
				handler.clearException();
581
			} else {
582
				handler.handleException(event);
583
			}
584
		}
585
	}
586

    
587
	@Override
588
	protected void handleToggleableCacheField() {
589

    
590
		boolean pushedState = toggleable_cache.getState();
591
        getEntity().setTitleCache(toggleable_cache.getText(), pushedState);
592
        setIrrelevantReferenceDetail(false);
593
        updateToggleableCacheField();
594
    }
595

    
596
	protected void setIrrelevantReferenceDetail(boolean abbrev){
597

    
598
		List<Object> except = new ArrayList();
599

    
600
	    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}));
601
	    boolean pushedState;
602
	    if (abbrev){
603
	      except.add(text_title);
604
	    } else{
605
	      except.add(text_abbrevTitle);
606
	    }
607
	    pushedState = toggleableAbbrevCache.getState() || toggleable_cache.getState();
608

    
609
	    switch( getEntity().getType()){
610
	    case Journal:
611
	    	except.add(element_timePeriod);
612
	    	break;
613
	    case Book:
614
	    	except.remove(text_series);
615
	    	except.remove(text_edition);
616
	    	break;
617
	    default:
618
	    	break;
619
	    }
620

    
621
	    setIrrelevant(pushedState, except);
622
	    text_title.setIrrelevant(toggleable_cache.getState());
623
	    text_abbrevTitle.setIrrelevant(toggleableAbbrevCache.getState());
624
	}
625

    
626

    
627

    
628

    
629
}
(3-3/6)