Renaming MediaMetaData to MediaInfo
[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.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.TeamOrPersonBase;
21 import eu.etaxonomy.cdm.model.reference.Reference;
22 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
23 import eu.etaxonomy.cdm.model.reference.ReferenceType;
24 import eu.etaxonomy.taxeditor.model.AuthorHelper;
25 import eu.etaxonomy.taxeditor.store.StoreUtil;
26 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.EnumComboType;
28 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.SelectionType;
29 import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
30 import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
31 import eu.etaxonomy.taxeditor.ui.forms.IErrorIntolerableElement;
32 import eu.etaxonomy.taxeditor.ui.forms.IExceptionHandler;
33 import eu.etaxonomy.taxeditor.ui.forms.TextWithLabelElement;
34 import eu.etaxonomy.taxeditor.ui.forms.TimePeriodElement;
35 import eu.etaxonomy.taxeditor.ui.forms.ToggleableTextElement;
36 import eu.etaxonomy.taxeditor.ui.forms.UriWithLabelElement;
37 import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
38 import eu.etaxonomy.taxeditor.ui.selection.AgentSelectionElement;
39 import eu.etaxonomy.taxeditor.ui.selection.InstitutionSelectionElement;
40 import eu.etaxonomy.taxeditor.ui.selection.ReferenceSelectionElement;
41 import eu.etaxonomy.taxeditor.ui.term.ReferenceTypeComboElement;
42
43 /**
44 * <p>
45 * ReferenceBaseDetailElement class.
46 * </p>
47 *
48 * @author n.hoffmann
49 * @created Mar 2, 2010
50 * @version 1.0
51 */
52 public class ReferenceDetailElement extends AbstractIdentifiableEntityDetailElement<Reference>
53 implements IErrorIntolerableElement, IExceptionHandler {
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_title;
67 private TextWithLabelElement text_volume;
68 private UriWithLabelElement text_uri;
69 private AgentSelectionElement selection_authorTeam;
70 private TimePeriodElement element_timePeriod;
71 private ReferenceSelectionElement selection_inReference;
72 private InstitutionSelectionElement selection_institution;
73 private ReferenceTypeComboElement combo_referenceType;
74
75 private ReferenceSelectionElement selection_inSeries;
76
77 /**
78 * <p>
79 * Constructor for ReferenceBaseDetailElement.
80 * </p>
81 *
82 * @param cdmFormFactory
83 * a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory}
84 * object.
85 * @param formElement
86 * a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement}
87 * object.
88 * @param style
89 * a int.
90 */
91 public ReferenceDetailElement(CdmFormFactory cdmFormFactory,
92 ICdmFormElement formElement, int style) {
93 super(cdmFormFactory, formElement);
94 }
95
96 /*
97 * (non-Javadoc)
98 *
99 * @see
100 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
101 * #createControl(org.eclipse.swt.widgets.Composite, int)
102 */
103 /** {@inheritDoc} */
104 @Override
105 protected void createControls(ICdmFormElement formElement,
106 Reference entity, int style) {
107 toggleable_cache = formFactory.createToggleableTextField(formElement,
108 "Reference Cache", entity.getTitleCache(),
109 entity.isProtectedTitleCache(), style);
110
111 combo_referenceType = (ReferenceTypeComboElement) formFactory
112 .createEnumComboElement(EnumComboType.REFERENCETYPE,
113 formElement, style);
114
115 combo_referenceType.setSelection(entity.getType());
116
117 // title
118 text_title = formFactory.createTextWithLabelElement(formElement,
119 "Title", entity.getTitle(), style);
120
121 // author team
122 selection_authorTeam = (AgentSelectionElement) formFactory
123 .createSelectionElement(SelectionType.AGENT,
124 getConversationHolder(), formElement, "Author",
125 AuthorHelper.getAuthor(entity.getAuthorTeam()),
126 AgentSelectionElement.DEFAULT, style);
127
128 ReferenceType referenceType = entity.getType();
129
130 if (referenceType != null) {
131
132 createInReferenceByType(this, referenceType, entity, SWT.NULL);
133
134 if (referenceType.equals(ReferenceType.Generic)) {
135 createGenericControls(this, entity, SWT.NULL);
136 createPublicationControls(this, referenceType, entity, SWT.NULL);
137 } else {
138 if (referenceType.isPrintedUnit()) {
139 createPrintedUnitControls(this, referenceType, entity,
140 SWT.NULL);
141 }
142 if (referenceType.isPublication()) {
143 createPublicationControls(this, referenceType, entity,
144 SWT.NULL);
145 }
146 if (referenceType.isVolumeReference()) {
147 createVolumeControls(this, entity, SWT.NULL);
148 }
149 if (referenceType.isSection()) {
150 createSectionControls(this, entity, SWT.NULL);
151 }
152 }
153 }
154
155 // date published
156 element_timePeriod = formFactory.createTimePeriodElement(formElement,
157 "Date Published", entity.getDatePublished(), style);
158
159 createUriAndAbstract(this, entity, SWT.NULL);
160 }
161
162 /*
163 * (non-Javadoc)
164 *
165 * @see
166 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
167 * #updateContent()
168 */
169 /** {@inheritDoc} */
170 @Override
171 protected void updateContent() {
172 if (getEntity() == null) {
173 setEntity(ReferenceFactory.newGeneric());
174 }
175
176 super.updateContent();
177
178 // enable elements according to state of cache protection
179 // setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new
180 // Object[]{text_cache}));
181 toggleable_cache.setEnabled(getEntity().isProtectedTitleCache());
182 setIrrelevant(toggleable_cache.getState(),
183 Arrays.asList(new Object[] { toggleable_cache }));
184 }
185
186 /**
187 * Creates an in reference for reference types that do hav this.
188 *
189 * @param element
190 * @param referenceType
191 * @param reference
192 * @param style
193 */
194 private void createInReferenceByType(ICdmFormElement element,
195 ReferenceType referenceType, Reference reference, int style) {
196 boolean createInReferenceWidget = true;
197 String label = "In Reference";
198
199 switch (referenceType) {
200 case Generic:
201 break;
202 case BookSection:
203 label = "Book";
204 break;
205 case InProceedings:
206 label = "Print Series";
207 break;
208 case Article:
209 label = "Journal";
210 break;
211 case WebPage:
212 break;
213 default:
214 createInReferenceWidget = false;
215 }
216
217 if (createInReferenceWidget) {
218 selection_inReference = (ReferenceSelectionElement) formFactory
219 .createSelectionElement(SelectionType.REFERENCE,
220 getConversationHolder(), element, label,
221 reference.getInReference(),
222 ReferenceSelectionElement.DEFAULT, style);
223 }
224 }
225
226 /**
227 * Creates additional controls to edit a generic reference
228 *
229 * @param element
230 * @param reference
231 * @param listener
232 * @param style
233 */
234 private void createGenericControls(ICdmFormElement element,
235 Reference reference, int style) {
236
237 // editor
238 text_editor = formFactory.createTextWithLabelElement(element, "Editor",
239 reference.getEditor(), style);
240
241 // series
242 text_series = formFactory.createTextWithLabelElement(element, "Series",
243 reference.getSeries(), style);
244
245 // volume
246 text_volume = formFactory.createTextWithLabelElement(element, "Volume",
247 reference.getVolume(), style);
248
249 // pages
250 text_pages = formFactory.createTextWithLabelElement(element, "Pages",
251 reference.getPages(), style);
252 }
253
254 /**
255 * Creates additional controls to edit a publication
256 *
257 * @param element
258 * @param reference
259 * @param listener
260 * @param style
261 */
262 private void createPublicationControls(ICdmFormElement element,
263 ReferenceType referenceType, Reference reference, int style) {
264
265 // placePublished
266 text_placePublished = formFactory.createTextWithLabelElement(element,
267 "Place Published", reference.getPlacePublished(), style);
268
269 // publisher
270 text_publisher = formFactory.createTextWithLabelElement(element,
271 "Publisher", reference.getPublisher(), style);
272
273 if (referenceType.equals(ReferenceType.Journal)) {
274 // issn
275 text_issn = formFactory.createTextWithLabelElement(element, "ISSN",
276 reference.getIssn(), style);
277 }
278
279 if (referenceType.equals(ReferenceType.Report)) {
280 // institution
281 selection_institution = (InstitutionSelectionElement) formFactory
282 .createSelectionElement(SelectionType.INSTITUTION,
283 getConversationHolder(), element, "Institution",
284 reference.getInstitution(),
285 InstitutionSelectionElement.DEFAULT, style);
286 }
287
288 if (referenceType.equals(ReferenceType.Thesis)) {
289 // school
290 selection_institution = (InstitutionSelectionElement) formFactory
291 .createSelectionElement(SelectionType.INSTITUTION,
292 getConversationHolder(), element, "School",
293 reference.getSchool(),
294 InstitutionSelectionElement.DEFAULT, style);
295 }
296
297 if (referenceType.equals(ReferenceType.Proceedings)) {
298 // organization
299 text_organisation = formFactory.createTextWithLabelElement(element,
300 "Organisation", reference.getOrganization(), style);
301 }
302 }
303
304 /**
305 * Creates additional controls to edit a section
306 *
307 * @param element
308 * @param reference
309 * @param style
310 */
311 private void createSectionControls(ICdmFormElement element,
312 Reference reference, int style) {
313 // pages
314 text_pages = formFactory.createTextWithLabelElement(element, "Pages",
315 reference.getPages(), style);
316 }
317
318 /**
319 * Creates additional controls to edit a volume
320 *
321 * @param element
322 * @param reference
323 * @param style
324 */
325 private void createVolumeControls(ICdmFormElement element,
326 Reference reference, int style) {
327
328 text_volume = formFactory.createTextWithLabelElement(element, "Volume",
329 reference.getVolume(), style);
330
331 text_series = formFactory.createTextWithLabelElement(element, "Series",
332 reference.getSeries(), style);
333 }
334
335 /**
336 *
337 * @param element
338 * @param referenceType
339 * @param reference
340 * @param style
341 */
342 private void createPrintedUnitControls(ICdmFormElement element,
343 ReferenceType referenceType, Reference reference, int style) {
344 // in series
345 selection_inSeries = (ReferenceSelectionElement) formFactory
346 .createSelectionElement(SelectionType.REFERENCE,
347 getConversationHolder(), element, "In Series",
348 reference.getInSeries(),
349 ReferenceSelectionElement.DEFAULT, style);
350
351 // editor
352 text_editor = formFactory.createTextWithLabelElement(element, "Editor",
353 reference.getEditor(), style);
354
355 // series part
356 text_seriesPart = formFactory.createTextWithLabelElement(element,
357 "Series Part", reference.getSeriesPart(), style);
358
359 if (referenceType.equals(ReferenceType.Book)) {
360 // edition
361 text_edition = formFactory.createTextWithLabelElement(element,
362 "Edition", reference.getEdition(), style);
363
364 // isbn
365 text_isbn = formFactory.createTextWithLabelElement(element, "ISBN",
366 reference.getIsbn(), style);
367 }
368 }
369
370 private void createUriAndAbstract(ICdmFormElement element,
371 Reference reference, int style) {
372 // uri
373 text_uri = formFactory.createUriWithLabelElement(element, "URI",
374 reference.getUri(), style);
375 // abstract
376 text_referenceAbstract = formFactory.createMultilineTextWithLabel(
377 element, "Reference Abstract", 100, style);
378 text_referenceAbstract.setText(reference.getReferenceAbstract());
379 }
380
381 /** {@inheritDoc} */
382 @Override
383 public void handleEvent(Object eventSource) {
384 if (eventSource == combo_referenceType) {
385 getEntity().setType(combo_referenceType.getSelection());
386 updateContent();
387 } else if (eventSource == element_timePeriod) {
388 getEntity().setDatePublished(element_timePeriod.getTimePeriod());
389 clearException();
390 }
391 // selections
392 else if (eventSource == selection_authorTeam) {
393 getEntity().setAuthorTeam(
394 (TeamOrPersonBase) selection_authorTeam.getSelection());
395 } else if (eventSource == selection_inReference) {
396 getEntity().setInReference(selection_inReference.getSelection());
397 } else if (eventSource == selection_inSeries) {
398 getEntity().setInReference(selection_inSeries.getSelection());
399 } else if (eventSource == selection_institution) {
400 getEntity().setInstitution(selection_institution.getSelection());
401 }
402
403 // textfields
404 else if (eventSource == toggleable_cache) {
405 handleToggleableCacheField();
406 } else if (eventSource == text_edition) {
407 getEntity().setEdition(text_edition.getText());
408 } else if (eventSource == text_editor) {
409 getEntity().setEditor(text_editor.getText());
410 } else if (eventSource == text_isbn) {
411 getEntity().setIsbn(text_isbn.getText());
412 } else if (eventSource == text_issn) {
413 getEntity().setIssn(text_issn.getText());
414 } else if (eventSource == text_organisation) {
415 getEntity().setOrganization(text_organisation.getText());
416 } else if (eventSource == text_pages) {
417 getEntity().setPages(text_pages.getText());
418 } else if (eventSource == text_placePublished) {
419 getEntity().setPlacePublished(text_placePublished.getText());
420 } else if (eventSource == text_publisher) {
421 getEntity().setPublisher(text_publisher.getText());
422 } else if (eventSource == text_referenceAbstract) {
423 getEntity().setReferenceAbstract(text_referenceAbstract.getText());
424 } else if (eventSource == text_series) {
425 getEntity().setSeries(text_series.getText());
426 } else if (eventSource == text_seriesPart) {
427 getEntity().setSeriesPart(text_seriesPart.getText());
428 } else if (eventSource == text_title) {
429 getEntity().setTitle(text_title.getText());
430 } else if (eventSource == text_uri) {
431 try {
432 getEntity().setUri(text_uri.getUri());
433 } catch (URISyntaxException e) {
434 StoreUtil.warningDialog("Invlid URI", getClass(),
435 e.getLocalizedMessage());
436 }
437 } else if (eventSource == text_volume) {
438 getEntity().setVolume(text_volume.getText());
439 }
440
441 else if (eventSource != toggleable_cache) {
442 toggleable_cache.setText(getEntity().getTitleCache());
443 }
444
445 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
446 }
447
448 /*
449 * (non-Javadoc)
450 *
451 * @see
452 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException
453 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
454 */
455 /** {@inheritDoc} */
456 @Override
457 public void handleException(CdmPropertyChangeEvent event) {
458 exceptionOccurred(event);
459 }
460
461 /*
462 * (non-Javadoc)
463 *
464 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
465 */
466 /** {@inheritDoc} */
467 @Override
468 public void clearException() {
469 exceptionOccurred(null);
470 }
471
472 private final List<IExceptionHandler> exceptionHandlers = new ArrayList<IExceptionHandler>();
473
474 /*
475 * (non-Javadoc)
476 *
477 * @see
478 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler
479 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
480 */
481 /** {@inheritDoc} */
482 @Override
483 public void addExceptionHandler(IExceptionHandler handler) {
484 exceptionHandlers.add(handler);
485 }
486
487 /*
488 * (non-Javadoc)
489 *
490 * @see
491 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler
492 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
493 */
494 /** {@inheritDoc} */
495 @Override
496 public void removeExceptionHandler(IExceptionHandler handler) {
497 exceptionHandlers.remove(handler);
498 }
499
500 /*
501 * (non-Javadoc)
502 *
503 * @see
504 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred
505 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
506 */
507 /** {@inheritDoc} */
508 @Override
509 public void exceptionOccurred(CdmPropertyChangeEvent event) {
510 for (IExceptionHandler handler : exceptionHandlers) {
511 if (event == null) {
512 handler.clearException();
513 } else {
514 handler.handleException(event);
515 }
516 }
517 }
518 }