Project

General

Profile

Download (18.1 KB) Statistics
| Branch: | Tag: | Revision:
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.ui.combo.EnumComboElement;
26
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
28
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
29
import eu.etaxonomy.taxeditor.ui.element.IErrorIntolerableElement;
30
import eu.etaxonomy.taxeditor.ui.element.IExceptionHandler;
31
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
32
import eu.etaxonomy.taxeditor.ui.element.TimePeriodElement;
33
import eu.etaxonomy.taxeditor.ui.element.ToggleableTextElement;
34
import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
35
import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
36
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
37

    
38
/**
39
 * <p>
40
 * ReferenceBaseDetailElement class.
41
 * </p>
42
 *
43
 * @author n.hoffmann
44
 * @created Mar 2, 2010
45
 * @version 1.0
46
 */
47
public class ReferenceDetailElement extends AbstractIdentifiableEntityDetailElement<Reference>
48
		implements IErrorIntolerableElement, IExceptionHandler {
49

    
50
    private ToggleableTextElement toggleableAbbrevCache;
51

    
52
	private TextWithLabelElement text_edition;
53
	private TextWithLabelElement text_editor;
54
	private TextWithLabelElement text_isbn;
55
	private TextWithLabelElement text_issn;
56
	private TextWithLabelElement text_organisation;
57
	private TextWithLabelElement text_pages;
58
	private TextWithLabelElement text_placePublished;
59
	private TextWithLabelElement text_publisher;
60
	private TextWithLabelElement text_referenceAbstract;
61
	private TextWithLabelElement text_series;
62
//	private TextWithLabelElement text_seriesPart;
63
	private TextWithLabelElement text_volume;
64
	private TextWithLabelElement text_abbrevTitle;
65
	private TextWithLabelElement text_title;
66
	private UriWithLabelElement text_uri;
67
	private EntitySelectionElement<AgentBase> selection_authorTeam;
68
	private TimePeriodElement element_timePeriod;
69
	private EntitySelectionElement<Reference> selection_inReference;
70
	private EntitySelectionElement<Institution> selection_institution;
71
	private EnumComboElement<ReferenceType> combo_referenceType;
72

    
73
	private EntitySelectionElement<Reference> selection_inSeries;
74

    
75
	/**
76
	 * <p>
77
	 * Constructor for ReferenceBaseDetailElement.
78
	 * </p>
79
	 *
80
	 * @param cdmFormFactory
81
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
82
	 *            object.
83
	 * @param formElement
84
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
85
	 *            object.
86
	 * @param style
87
	 *            a int.
88
	 */
89
	public ReferenceDetailElement(CdmFormFactory cdmFormFactory,
90
			ICdmFormElement formElement, int style) {
91
		super(cdmFormFactory, formElement);
92
	}
93

    
94
	/*
95
	 * (non-Javadoc)
96
	 *
97
	 * @see
98
	 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
99
	 * #createControl(org.eclipse.swt.widgets.Composite, int)
100
	 */
101
	/** {@inheritDoc} */
102
	@Override
103
	protected void createControls(ICdmFormElement formElement,
104
			Reference entity, int style) {
105
		toggleable_cache = formFactory.createToggleableTextField(formElement,
106
				"Reference Cache", entity.getTitleCache(),
107
				entity.isProtectedTitleCache(), style);
108

    
109
		toggleableAbbrevCache = formFactory.createToggleableTextField(formElement,
110
		        "Abbrev. Cache", entity.getAbbrevTitleCache(),
111
		        entity.isProtectedAbbrevTitleCache(), style);
112

    
113
		combo_referenceType = formFactory.createEnumComboElement(ReferenceType.class,
114
						formElement, style);
115

    
116
		combo_referenceType.setSelection(entity.getType());
117

    
118
		// title
119
		text_title = formFactory.createTextWithLabelElement(formElement,
120
				"Title", entity.getTitle(), style);
121
		// abbreviated title
122
		text_abbrevTitle = formFactory.createTextWithLabelElement(formElement,
123
		        "Abbrev. Title", entity.getAbbrevTitle(), style);
124

    
125
		// author team
126
		selection_authorTeam = formFactory
127
				.createSelectionElement(AgentBase.class,
128
						getConversationHolder(), formElement, "Author",
129
						entity.getAuthorship(),
130
						EntitySelectionElement.ALL, style);
131

    
132
		ReferenceType referenceType = entity.getType();
133

    
134
		if (referenceType != null) {
135

    
136
			createInReferenceByType(this, referenceType, entity, SWT.NULL);
137

    
138
			if (referenceType.equals(ReferenceType.Generic)) {
139
				createGenericControls(this, entity, SWT.NULL);
140
				createPublicationControls(this, referenceType, entity, SWT.NULL);
141
			} else {
142
				if (referenceType.isPrintedUnit()) {
143
					createPrintedUnitControls(this, referenceType, entity,
144
							SWT.NULL);
145
				}
146
				if (referenceType.isPublication()) {
147
					createPublicationControls(this, referenceType, entity,
148
							SWT.NULL);
149
				}
150
				if (referenceType.isVolumeReference()) {
151
					createVolumeControls(this, entity, SWT.NULL);
152
				}
153
				if (referenceType.isSection()) {
154
					createSectionControls(this, entity, SWT.NULL);
155
				}
156
			}
157
		}
158

    
159
		// date published
160
		element_timePeriod = formFactory.createTimePeriodElement(formElement,
161
				"Date Published", entity.getDatePublished(), style);
162

    
163
		createUriAndAbstract(this, entity, SWT.NULL);
164
		handleToggleableAbbrevTitleField();
165
		handleToggleableCacheField();
166
	}
167

    
168
	/*
169
	 * (non-Javadoc)
170
	 *
171
	 * @see
172
	 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
173
	 * #updateContent()
174
	 */
175
	/** {@inheritDoc} */
176
	@Override
177
	protected void updateContent() {
178
		if (getEntity() == null) {
179
			setEntity(ReferenceFactory.newGeneric());
180
		}
181

    
182
		super.updateContent();
183

    
184
		// enable elements according to state of cache protection
185
		// setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new
186
		// Object[]{text_cache}));
187
		toggleable_cache.setEnabled(getEntity().isProtectedTitleCache());
188
		toggleableAbbrevCache.setEnabled(getEntity().isProtectedAbbrevTitleCache());
189
		setIrrelevantReferenceDetail(false);
190
		setIrrelevantReferenceDetail(true);
191
	}
192

    
193
	/**
194
	 * Creates an in reference for reference types that do hav this.
195
	 *
196
	 * @param element
197
	 * @param referenceType
198
	 * @param reference
199
	 * @param style
200
	 */
201
	private void createInReferenceByType(ICdmFormElement element,
202
			ReferenceType referenceType, Reference reference, int style) {
203
		boolean createInReferenceWidget = true;
204
		String label = "In Reference";
205

    
206
		switch (referenceType) {
207
		case Generic:
208
			break;
209
		case Section:
210
		    break;
211
		case BookSection:
212
			label = "Book";
213
			break;
214
		case InProceedings:
215
			label = "Print Series";
216
			break;
217
		case Article:
218
			label = "Journal";
219
			break;
220
		case WebPage:
221
			break;
222
		default:
223
			createInReferenceWidget = false;
224
		}
225

    
226
		if (createInReferenceWidget) {
227
			selection_inReference = formFactory
228
					.createSelectionElement(Reference.class,
229
							getConversationHolder(), element, label,
230
							reference.getInReference(),
231
							EntitySelectionElement.ALL, style);
232
		}
233
	}
234

    
235
	/**
236
	 * Creates additional controls to edit a generic reference
237
	 *
238
	 * @param element
239
	 * @param reference
240
	 * @param listener
241
	 * @param style
242
	 */
243
	private void createGenericControls(ICdmFormElement element,
244
			Reference reference, int style) {
245

    
246
		// editor
247
		text_editor = formFactory.createTextWithLabelElement(element, "Editor",
248
				reference.getEditor(), style);
249

    
250
		// series
251
		text_series = formFactory.createTextWithLabelElement(element, "Series",
252
				reference.getSeriesPart(), style);
253

    
254
		// volume
255
		text_volume = formFactory.createTextWithLabelElement(element, "Volume",
256
				reference.getVolume(), style);
257

    
258
		// pages
259
		text_pages = formFactory.createTextWithLabelElement(element, "Pages",
260
				reference.getPages(), style);
261
	}
262

    
263
	/**
264
	 * Creates additional controls to edit a publication
265
	 *
266
	 * @param element
267
	 * @param reference
268
	 * @param listener
269
	 * @param style
270
	 */
271
	private void createPublicationControls(ICdmFormElement element,
272
			ReferenceType referenceType, Reference reference, int style) {
273

    
274
		// placePublished
275
		text_placePublished = formFactory.createTextWithLabelElement(element,
276
				"Place Published", reference.getPlacePublished(), style);
277

    
278
		// publisher
279
		text_publisher = formFactory.createTextWithLabelElement(element,
280
				"Publisher", reference.getPublisher(), style);
281

    
282
		if (referenceType.equals(ReferenceType.Journal)) {
283
			// issn
284
			text_issn = formFactory.createTextWithLabelElement(element, "ISSN",
285
					reference.getIssn(), style);
286
		}
287

    
288
		if (referenceType.equals(ReferenceType.Report)) {
289
			// institution
290
			selection_institution = formFactory
291
					.createSelectionElement(Institution.class,
292
							getConversationHolder(), element, "Institution",
293
							reference.getInstitution(),
294
							EntitySelectionElement.ALL, style);
295
		}
296

    
297
		if (referenceType.equals(ReferenceType.Thesis)) {
298
			// school
299
			selection_institution = formFactory
300
					.createSelectionElement(Institution.class,
301
							getConversationHolder(), element, "School",
302
							reference.getSchool(),
303
							EntitySelectionElement.ALL, style);
304
		}
305

    
306
		if (referenceType.equals(ReferenceType.Proceedings)) {
307
			// organization
308
			text_organisation = formFactory.createTextWithLabelElement(element,
309
					"Organisation", reference.getOrganization(), style);
310
		}
311
	}
312

    
313
	/**
314
	 * Creates additional controls to edit a section
315
	 *
316
	 * @param element
317
	 * @param reference
318
	 * @param style
319
	 */
320
	private void createSectionControls(ICdmFormElement element,
321
			Reference reference, int style) {
322
		// pages
323
		text_pages = formFactory.createTextWithLabelElement(element, "Pages",
324
				reference.getPages(), style);
325
	}
326

    
327
	/**
328
	 * Creates additional controls to edit a volume
329
	 *
330
	 * @param element
331
	 * @param reference
332
	 * @param style
333
	 */
334
	private void createVolumeControls(ICdmFormElement element,
335
			Reference reference, int style) {
336

    
337
		text_volume = formFactory.createTextWithLabelElement(element, "Volume",
338
				reference.getVolume(), style);
339

    
340
		text_series = formFactory.createTextWithLabelElement(element, "Series",
341
				reference.getSeriesPart(), style);
342
	}
343

    
344
	/**
345
	 *
346
	 * @param element
347
	 * @param referenceType
348
	 * @param reference
349
	 * @param style
350
	 */
351
	private void createPrintedUnitControls(ICdmFormElement element,
352
			ReferenceType referenceType, Reference reference, int style) {
353
		// in series
354
		selection_inSeries = formFactory
355
				.createSelectionElement(Reference.class,
356
						getConversationHolder(), element, "In Series",
357
						(Reference) reference.getInSeries(),
358
						EntitySelectionElement.ALL, style);
359

    
360
		// editor
361
		text_editor = formFactory.createTextWithLabelElement(element, "Editor",
362
				reference.getEditor(), style);
363

    
364
		/* series part
365
		text_seriesPart = formFactory.createTextWithLabelElement(element,
366
				"Series", reference.getSeriesPart(), style);
367
*/
368
		if (referenceType.equals(ReferenceType.Book)) {
369
			// edition
370
			text_edition = formFactory.createTextWithLabelElement(element,
371
					"Edition", reference.getEdition(), style);
372

    
373
			// isbn
374
			text_isbn = formFactory.createTextWithLabelElement(element, "ISBN",
375
					reference.getIsbn(), style);
376
		}
377
	}
378

    
379
	private void createUriAndAbstract(ICdmFormElement element,
380
			Reference reference, int style) {
381
		// uri
382
		text_uri = formFactory.createUriWithLabelElement(element, "URI",
383
				reference.getUri(), style);
384
		// abstract
385
		text_referenceAbstract = formFactory.createMultiLineTextWithLabel(
386
				element, "Reference Abstract", 100, style);
387
		text_referenceAbstract.setText(reference.getReferenceAbstract());
388
	}
389

    
390
	/** {@inheritDoc} */
391
	@Override
392
	public void handleEvent(Object eventSource) {
393
		if (eventSource == combo_referenceType) {
394
			getEntity().setType(combo_referenceType.getSelection());
395
			updateContent();
396
		} else if (eventSource == element_timePeriod) {
397
			getEntity().setDatePublished(element_timePeriod.getTimePeriod());
398
			clearException();
399
		}
400
		// selections
401
		else if (eventSource == selection_authorTeam) {
402
			getEntity().setAuthorship(
403
					(TeamOrPersonBase) selection_authorTeam.getSelection());
404
		} else if (eventSource == selection_inReference) {
405
			getEntity().setInReference(selection_inReference.getSelection());
406
		} else if (eventSource == selection_inSeries) {
407
			getEntity().setInReference(selection_inSeries.getSelection());
408
		} else if (eventSource == selection_institution) {
409
			getEntity().setInstitution(selection_institution.getSelection());
410
		}
411

    
412
		// textfields
413
		else if (eventSource == toggleable_cache) {
414
			handleToggleableCacheField();
415
		}
416
		else if (eventSource == toggleableAbbrevCache) {
417
		    handleToggleableAbbrevTitleField();
418
		} else if (eventSource == text_edition) {
419
			getEntity().setEdition(text_edition.getText());
420
		} else if (eventSource == text_editor) {
421
			getEntity().setEditor(text_editor.getText());
422
		} else if (eventSource == text_isbn) {
423
			getEntity().setIsbn(text_isbn.getText());
424
		} else if (eventSource == text_issn) {
425
			getEntity().setIssn(text_issn.getText());
426
		} else if (eventSource == text_organisation) {
427
			getEntity().setOrganization(text_organisation.getText());
428
		} else if (eventSource == text_pages) {
429
			getEntity().setPages(text_pages.getText());
430
		} else if (eventSource == text_placePublished) {
431
			getEntity().setPlacePublished(text_placePublished.getText());
432
		} else if (eventSource == text_publisher) {
433
			getEntity().setPublisher(text_publisher.getText());
434
		} else if (eventSource == text_referenceAbstract) {
435
			getEntity().setReferenceAbstract(text_referenceAbstract.getText());
436
		} else if (eventSource == text_series) {
437
			getEntity().setSeriesPart(text_series.getText());
438
		} else if (eventSource == text_title) {
439
			getEntity().setTitle(text_title.getText());
440
			toggleable_cache.setText(text_title.getText());
441
		} else if (eventSource == text_abbrevTitle) {
442
		    getEntity().setAbbrevTitle(text_abbrevTitle.getText());
443
		    toggleableAbbrevCache.setText(text_abbrevTitle.getText());
444
		} else if (eventSource == text_uri) {
445
			getEntity().setUri(text_uri.parseText());
446
		} else if (eventSource == text_volume) {
447
			getEntity().setVolume(text_volume.getText());
448
		}
449

    
450
		if (eventSource != text_title && eventSource != text_abbrevTitle) {
451
		    // set abbrev title cache to null to force new generation
452
		    // for the standard title cache this is done by a PropertyChangeListener registered
453
		    // in IdentifiableEntity.initListeners()
454
		    if(!getEntity().isProtectedAbbrevTitleCache()){
455
		        getEntity().setAbbrevTitleCache(null, getEntity().isProtectedAbbrevTitleCache());
456
		    }
457
			toggleable_cache.setText(getEntity().getTitleCache());
458
			toggleableAbbrevCache.setText(getEntity().getAbbrevTitleCache());
459
		}
460

    
461
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
462
	}
463

    
464

    
465
    @Override
466
    public void updateToggleableCacheField() {
467
        if (!getEntity().isProtectedTitleCache()) {
468
            toggleable_cache.setText(getEntity().generateTitle());
469
        }
470
        if (!getEntity().isProtectedAbbrevTitleCache()) {
471
            toggleableAbbrevCache.setText(getEntity().generateAbbrevTitle());
472
        }
473
    }
474

    
475
    protected void handleToggleableAbbrevTitleField() {
476
        boolean pushedAbbrevState = toggleableAbbrevCache.getState();
477
        getEntity().setAbbrevTitleCache(toggleableAbbrevCache.getText(), pushedAbbrevState);
478
        setIrrelevantReferenceDetail(true);
479
        updateToggleableCacheField();
480
    }
481

    
482
	/*
483
	 * (non-Javadoc)
484
	 *
485
	 * @see
486
	 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException
487
	 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
488
	 */
489
	/** {@inheritDoc} */
490
	@Override
491
	public void handleException(CdmPropertyChangeEvent event) {
492
		exceptionOccurred(event);
493
	}
494

    
495
	/*
496
	 * (non-Javadoc)
497
	 *
498
	 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
499
	 */
500
	/** {@inheritDoc} */
501
	@Override
502
	public void clearException() {
503
		exceptionOccurred(null);
504
	}
505

    
506
	private final List<IExceptionHandler> exceptionHandlers = new ArrayList<IExceptionHandler>();
507

    
508
	/*
509
	 * (non-Javadoc)
510
	 *
511
	 * @see
512
	 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler
513
	 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
514
	 */
515
	/** {@inheritDoc} */
516
	@Override
517
	public void addExceptionHandler(IExceptionHandler handler) {
518
		exceptionHandlers.add(handler);
519
	}
520

    
521
	/*
522
	 * (non-Javadoc)
523
	 *
524
	 * @see
525
	 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler
526
	 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
527
	 */
528
	/** {@inheritDoc} */
529
	@Override
530
	public void removeExceptionHandler(IExceptionHandler handler) {
531
		exceptionHandlers.remove(handler);
532
	}
533

    
534
	/*
535
	 * (non-Javadoc)
536
	 *
537
	 * @see
538
	 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred
539
	 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
540
	 */
541
	/** {@inheritDoc} */
542
	@Override
543
	public void exceptionOccurred(CdmPropertyChangeEvent event) {
544
		for (IExceptionHandler handler : exceptionHandlers) {
545
			if (event == null) {
546
				handler.clearException();
547
			} else {
548
				handler.handleException(event);
549
			}
550
		}
551
	}
552

    
553
	@Override
554
	protected void handleToggleableCacheField() {
555

    
556
		boolean pushedState = toggleable_cache.getState();
557
        getEntity().setTitleCache(toggleable_cache.getText(), pushedState);
558
        setIrrelevantReferenceDetail(false);
559
        updateToggleableCacheField();
560
    }
561

    
562
	protected void setIrrelevantReferenceDetail(boolean abbrev){
563

    
564
		List<Object> except = new ArrayList();
565
	    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}));
566
	    boolean pushedState;
567
	    if (abbrev){
568
	      except.add(text_title);
569
	      pushedState = toggleableAbbrevCache.getState();
570
	    } else{
571
	    	except.add(text_abbrevTitle);
572
	    	pushedState = toggleable_cache.getState();
573
	    }
574
	    switch( getEntity().getType()){
575
	    case Journal:
576
	    	except.add(element_timePeriod);
577
	    	break;
578
	    case Book:
579
	    	except.remove(text_series);
580
	    	except.remove(text_edition);
581
	    	break;
582
	    default:
583
	    	break;
584
	    }
585
	    setIrrelevant(pushedState, except);
586
	}
587
}
(3-3/5)