Project

General

Profile

Download (17.5 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.net.URISyntaxException;
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.List;
17

    
18
import org.eclipse.swt.SWT;
19

    
20
import eu.etaxonomy.cdm.model.agent.AgentBase;
21
import eu.etaxonomy.cdm.model.agent.Institution;
22
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
23
import eu.etaxonomy.cdm.model.reference.Reference;
24
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
25
import eu.etaxonomy.cdm.model.reference.ReferenceType;
26
import eu.etaxonomy.taxeditor.model.AbstractUtility;
27
import eu.etaxonomy.taxeditor.model.AuthorHelper;
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.ICdmFormElement;
32
import eu.etaxonomy.taxeditor.ui.element.IErrorIntolerableElement;
33
import eu.etaxonomy.taxeditor.ui.element.IExceptionHandler;
34
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
35
import eu.etaxonomy.taxeditor.ui.element.TimePeriodElement;
36
import eu.etaxonomy.taxeditor.ui.element.ToggleableTextElement;
37
import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
38
import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
39
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
40

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

    
53
    private ToggleableTextElement toggleableAbbrevCache;
54

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

    
76
	private EntitySelectionElement<Reference> selection_inSeries;
77

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

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

    
112
		toggleableAbbrevCache = formFactory.createToggleableTextField(formElement,
113
		        "Abbrev. Cache", entity.getAbbrevTitleCache(),
114
		        entity.isProtectedAbbrevTitleCache(), style);
115

    
116
		combo_referenceType = formFactory.createEnumComboElement(ReferenceType.class,
117
						formElement, style);
118

    
119
		combo_referenceType.setSelection(entity.getType());
120

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

    
128
		// author team
129
		selection_authorTeam = formFactory
130
				.createSelectionElement(AgentBase.class,
131
						getConversationHolder(), formElement, "Author",
132
						AuthorHelper.getAuthor(entity.getAuthorTeam()),
133
						EntitySelectionElement.ALL, style);
134

    
135
		ReferenceType referenceType = entity.getType();
136

    
137
		if (referenceType != null) {
138

    
139
			createInReferenceByType(this, referenceType, entity, SWT.NULL);
140

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

    
162
		// date published
163
		element_timePeriod = formFactory.createTimePeriodElement(formElement,
164
				"Date Published", entity.getDatePublished(), style);
165

    
166
		createUriAndAbstract(this, entity, SWT.NULL);
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
        setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[] { toggleable_cache }));
191
        setIrrelevant(toggleableAbbrevCache.getState(), Arrays.asList(new Object[] { toggleableAbbrevCache}));
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 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.getSeries(), 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.getSeries(), 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 Part", 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().setAuthorTeam(
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_series) {
436
			getEntity().setSeries(text_series.getText());
437
		} else if (eventSource == text_seriesPart) {
438
			getEntity().setSeriesPart(text_seriesPart.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
			try {
447
				getEntity().setUri(text_uri.getUri());
448
			} catch (URISyntaxException e) {
449
				AbstractUtility.warningDialog("Invalid URI", getClass(),
450
						e.getLocalizedMessage());
451
			}
452
		} else if (eventSource == text_volume) {
453
			getEntity().setVolume(text_volume.getText());
454
		}
455

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

    
467
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
468
	}
469

    
470

    
471
    @Override
472
    public void updateToggleableCacheField() {
473
        if (!getEntity().isProtectedTitleCache()) {
474
            toggleable_cache.setText(getEntity().generateTitle());
475
        }
476
        if (!getEntity().isProtectedAbbrevTitleCache()) {
477
            toggleableAbbrevCache.setText(getEntity().generateAbbrevTitle());
478
        }
479
    }
480

    
481
    protected void handleToggleableAbbrevTitleField() {
482
        boolean pushedAbbrevState = toggleableAbbrevCache.getState();
483
        getEntity().setAbbrevTitleCache(toggleableAbbrevCache.getText(), pushedAbbrevState);
484
        setIrrelevant(pushedAbbrevState, Arrays.asList(new Object[] { toggleableAbbrevCache, toggleable_cache, text_title }));
485
        updateToggleableCacheField();
486
    }
487

    
488
	/*
489
	 * (non-Javadoc)
490
	 *
491
	 * @see
492
	 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException
493
	 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
494
	 */
495
	/** {@inheritDoc} */
496
	@Override
497
	public void handleException(CdmPropertyChangeEvent event) {
498
		exceptionOccurred(event);
499
	}
500

    
501
	/*
502
	 * (non-Javadoc)
503
	 *
504
	 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
505
	 */
506
	/** {@inheritDoc} */
507
	@Override
508
	public void clearException() {
509
		exceptionOccurred(null);
510
	}
511

    
512
	private final List<IExceptionHandler> exceptionHandlers = new ArrayList<IExceptionHandler>();
513

    
514
	/*
515
	 * (non-Javadoc)
516
	 *
517
	 * @see
518
	 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler
519
	 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
520
	 */
521
	/** {@inheritDoc} */
522
	@Override
523
	public void addExceptionHandler(IExceptionHandler handler) {
524
		exceptionHandlers.add(handler);
525
	}
526

    
527
	/*
528
	 * (non-Javadoc)
529
	 *
530
	 * @see
531
	 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler
532
	 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
533
	 */
534
	/** {@inheritDoc} */
535
	@Override
536
	public void removeExceptionHandler(IExceptionHandler handler) {
537
		exceptionHandlers.remove(handler);
538
	}
539

    
540
	/*
541
	 * (non-Javadoc)
542
	 *
543
	 * @see
544
	 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred
545
	 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
546
	 */
547
	/** {@inheritDoc} */
548
	@Override
549
	public void exceptionOccurred(CdmPropertyChangeEvent event) {
550
		for (IExceptionHandler handler : exceptionHandlers) {
551
			if (event == null) {
552
				handler.clearException();
553
			} else {
554
				handler.handleException(event);
555
			}
556
		}
557
	}
558
}
(3-3/5)