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